/* ============ G_InitLua ============ */ void G_InitLua() { char buf[MAX_STRING_CHARS]; char filename[MAX_QPATH]; G_Printf("------- Lua Initialization -------\n"); g_luaState = lua_open(); // Lua standard lib luaopen_base(g_luaState); luaopen_string(g_luaState); // Quake lib luaopen_entity(g_luaState); luaopen_game(g_luaState); luaopen_qmath(g_luaState); luaopen_vector(g_luaState); // load map specific Lua script as default trap_Cvar_VariableStringBuffer("mapname", buf, sizeof(buf)); Com_sprintf(filename, sizeof(filename), "scripts/lua/%s.lua", buf); G_LoadLuaScript(NULL, filename); G_Printf("-----------------------------------\n"); }
/* ============ G_InitLua ============ */ void G_InitLua() { char buf[MAX_STRING_CHARS]; G_Printf( "------- Game Lua Initialization -------\n" ); g_luaState = lua_open(); // Lua standard lib luaopen_base( g_luaState ); luaopen_string( g_luaState ); // Quake lib luaopen_entity( g_luaState ); luaopen_game( g_luaState ); luaopen_qmath( g_luaState ); luaopen_vector( g_luaState ); // Create the callback table (at location 1 in the registry) lua_pushnumber( g_luaState, 1 ); lua_newtable( g_luaState ); lua_settable( g_luaState, LUA_REGISTRYINDEX ); // load global scripts G_Printf( "global lua scripts:\n" ); G_InitLua_Global(); // load map-specific lua scripts G_Printf( "map specific lua scripts:\n" ); g_cvars->Cvar_VariableStringBuffer( "mapname", buf, sizeof( buf ) ); G_InitLua_Local( buf ); G_LoadLuaScript( va( "maps/%s.lua", buf ) ); G_Printf( "-----------------------------------\n" ); G_RunLuaFunction( g_luaState, "G_InitGame", "" ); }
game_status state_in_game(SDL_Surface *screen, game* p_game) { int i=0; bool done=false; game_status ret_code = GAME_OVER; SDL_Rect srcrect = {0,0,160,144}; dstrect = set_rect(0,0,SCREEN_HEIGHT,SCREEN_WIDTH); #ifdef USE_LUA lua_State *L; L = lua_open(); luaopen_base(L); luaL_openlibs(L); luaopen_globals(L); luaopen_game(L); luaopen_unit(L); luaopen_sprite(L); luaopen_var(L); luaopen_map(L); //register lua functions lua_register(L ,"say", script_lua_unit_say); lua_register(L ,"unit_get_x", script_lua_unit_get_x); lua_register(L ,"unit_get_y", script_lua_unit_get_y); lua_register(L ,"unit_set_index_x", script_lua_unit_set_index_x); lua_register(L ,"unit_set_index_y", script_lua_unit_set_index_y); lua_register(L ,"event_text", script_lua_event_exec_text); lua_register(L ,"event_teleport", script_lua_event_exec_teleport); lua_register(L ,"unit_set_life", script_lua_unit_set_life); lua_register(L ,"unit_set_speed", script_lua_unit_set_speed); p_game->L = L; g_game = p_game; #endif long timelastcall=SDL_GetTicks(); if(map_get_current(p_game->p_map, p_game->cur_map)->music != NULL){ char* music = strdup(map_get_current(p_game->p_map, p_game->cur_map)->music); Mix_Music* ingame_music = music_load(music); music_play(ingame_music); } message_box* p_menu = NULL; p_menu = menu_ingame_create(p_game); bool action; while (!done) { action = false; SDL_Event event; SDL_JoystickUpdate(); while (SDL_PollEvent(&event)){ switch ( event.type ){ case SDL_QUIT: ret_code = QUIT; done = true; break; case SDL_KEYUP: switch ( event.key.keysym.sym ){ case SDLK_RETURN: switch(menu_start(get_backbuffer_surface(), p_game)){ case 0: menu_status(get_backbuffer_surface(), p_game); break; case 1: menu_save(get_backbuffer_surface(), p_game); break; case 2: ret_code = LOAD; done=true; break; } break; default:break; } break; case SDL_JOYBUTTONDOWN: switch(event.jbutton.button){ case 0: switch(menu_start(get_backbuffer_surface(), p_game)){ case 0: menu_status(get_backbuffer_surface(), p_game); break; case 1: menu_save(get_backbuffer_surface(), p_game); break; case 2: ret_code = LOAD; done=true; break; } break; case 2: action = true; break; } break; default:break; } unit_handle_key(p_game->p_unit, &event, p_game); } int joystate = SDL_JoystickGetHat(p_game->p_unit->joystick, 0); switch (joystate){ case SDL_HAT_DOWN: unit_set_vel_y(p_game->p_unit, p_game->p_unit->speed); unit_set_vel_x(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = DOWN; p_game->p_unit->dir = DOWN; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_UP: unit_set_vel_y(p_game->p_unit, -p_game->p_unit->speed); unit_set_vel_x(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = UP; p_game->p_unit->dir = UP; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_RIGHT: unit_set_vel_x(p_game->p_unit, p_game->p_unit->speed); unit_set_vel_y(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = RIGHT; p_game->p_unit->dir = RIGHT; p_game->p_unit->current_action = NOTHING; break; case SDL_HAT_LEFT: unit_set_vel_x(p_game->p_unit, -p_game->p_unit->speed); unit_set_vel_y(p_game->p_unit, 0); p_game->p_unit->p_sprite->animation_current = LEFT; p_game->p_unit->dir = LEFT; p_game->p_unit->current_action = NOTHING; break; default: break; } //drawing SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); map_draw(map_get_current(p_game->p_map, p_game->cur_map), backbuffer); if(p_game->p_unit->current_action == FIGHT){ anim_sprite_draw(p_game->p_unit->weapon, get_backbuffer_surface()); } anim_sprite_draw(p_game->p_unit->p_sprite, backbuffer); for(i=0;i<p_game->NPC_nb;i++){ NPC_update(p_game->p_NPC+i, map_get_current(p_game->p_map, p_game->cur_map), p_game->p_unit); anim_sprite_draw(p_game->p_NPC[i].p_sprite, backbuffer); } for(i=0;i<p_game->ennemie_nb;i++){ ennemie_update(p_game->p_ennemie+i, map_get_current(p_game->p_map, p_game->cur_map), p_game->p_unit); anim_sprite_draw(p_game->p_ennemie[i].p_sprite, backbuffer); } Uint8* key = SDL_GetKeyState(NULL); if(key[p_game->p_unit->key_b] || action){ for(i=0;i<p_game->NPC_nb;i++){ if(sprite_intersect((sprite*)p_game->p_unit->p_sprite, (sprite*)p_game->p_NPC[i].p_sprite)){ struct t_event* p_event = malloc(sizeof(struct t_event)); p_event->is_auto = true; p_event->p_next = NULL; p_event->type = EVENT_SCRIPT; event_script* p_event_script = malloc(sizeof(event_script)); p_event_script->language = strdup("lua"); p_event_script->script = strdup(p_game->p_NPC[i].script); p_event_script->version = strdup("1.0"); p_event->data = p_event_script; event_exec_script(p_event, p_game); event_free(p_event); } } } //logic sprite_calc_bounding_box((sprite*)p_game->p_unit->p_sprite, true); //events when entering a tile point* p_point_enter = unit_check_on_tile_enter(p_game->p_unit, map_get_current(p_game->p_map, p_game->cur_map)); if( p_point_enter != NULL && map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event != NULL && map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event->is_auto){ p_game->p_unit->p_sprite->v_anim[p_game->p_unit->p_sprite->animation_current].frame_current = 1; event_dispatch(map_get_current(p_game->p_map, p_game->cur_map)->pp_tile[p_point_enter->y][p_point_enter->x].p_event, p_game); free(p_point_enter); } map* p_cur_map = map_get_current(p_game->p_map, p_game->cur_map); if(SDL_GetTicks() - timelastcall>1000/40) { unit_update(p_game->p_unit, p_cur_map); for(i=0;i<p_game->ennemie_nb;i++){ //Check weapon / ennemie collides if(p_game->p_unit->current_action == FIGHT){ if(sprite_intersect((sprite*)p_game->p_unit->weapon, (sprite*)p_game->p_ennemie[i].p_sprite)){ p_game->p_ennemie[i].HP--; if(p_game->p_ennemie[i].HP <= 0){ //ennemie dead //sound dead //animation dead p_game->p_unit->XP += p_game->p_ennemie[i].XP; //increase unit status p_game->p_unit->gold += p_game->p_ennemie[i].gold; ennemie_remove(p_game->p_ennemie, i, &p_game->ennemie_nb); //remove ennemie break; } } } //Check unit / ennemie collides if(p_game->p_unit->invincible_time <= 0 && sprite_intersect((sprite*)p_game->p_unit->p_sprite, (sprite*)p_game->p_ennemie[i].p_sprite)){ //sample_play(unit_hitted); p_game->p_unit->invincible_time = 40; unit_hitted(p_game->p_unit, p_game->p_ennemie+i, p_cur_map); if(p_game->p_unit->HP <= 0){ //sound dead //animation dead //message game over ret_code = GAME_OVER; done = true; break; } } } //Game events menu_ingame_update(get_backbuffer_surface(), p_menu, p_game); //update timer timelastcall=SDL_GetTicks(); } #if RENDER == 3 && !GEKKO SDL_GL_SwapBuffers(); #else SDL_SoftStretch(backbuffer, &srcrect, screen, &dstrect); SDL_Flip(screen); #endif } game_free(p_game); return ret_code; }
/** G_LuaStartVM( vm ) * Starts one individual virtual machine. */ qboolean G_LuaStartVM(lua_vm_t * vm) { int res = 0; char homepath[MAX_QPATH], gamepath[MAX_QPATH]; // Open a new lua state vm->L = luaL_newstate(); if(!vm->L) { LOG("Lua API: Lua failed to initialise.\n"); return qfalse; } // Initialise the lua state luaL_openlibs(vm->L); // set LUA_PATH and LUA_CPATH // TODO: add "fs_basepath/fs_game/?.lua;" to LUA_PATH // and LUA_CPATH for linux machines trap_Cvar_VariableStringBuffer("fs_homepath", homepath, sizeof(homepath)); trap_Cvar_VariableStringBuffer("fs_game", gamepath, sizeof(gamepath)); lua_getglobal(vm->L, LUA_LOADLIBNAME); if(lua_istable(vm->L, -1)) { lua_pushstring(vm->L, va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua", homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP)); lua_setfield(vm->L, -2, "path"); lua_pushstring(vm->L, va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s", homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION, homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION)); lua_setfield(vm->L, -2, "cpath"); } lua_pop(vm->L, 1); // register globals lua_registerglobal(vm->L, "LUA_PATH", va("%s%s%s%s?.lua;%s%s%s%slualib%slua%s?.lua", homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP)); lua_registerglobal(vm->L, "LUA_CPATH", va("%s%s%s%s?.%s;%s%s%s%slualib%sclibs%s?.%s", homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, EXTENSION, homepath, LUA_DIRSEP, gamepath, LUA_DIRSEP, LUA_DIRSEP, LUA_DIRSEP, EXTENSION)); lua_registerglobal(vm->L, "LUA_DIRSEP", LUA_DIRSEP); // register predefined constants lua_newtable(vm->L); lua_regconstinteger(vm->L, CS_PLAYERS); lua_regconstinteger(vm->L, EXEC_NOW); lua_regconstinteger(vm->L, EXEC_INSERT); lua_regconstinteger(vm->L, EXEC_APPEND); lua_regconstinteger(vm->L, FS_READ); lua_regconstinteger(vm->L, FS_WRITE); lua_regconstinteger(vm->L, FS_APPEND); lua_regconstinteger(vm->L, FS_APPEND_SYNC); lua_regconstinteger(vm->L, SAY_ALL); lua_regconstinteger(vm->L, SAY_TEAM); //xreal //lua_regconstinteger(vm->L, SAY_BUDDY); //lua_regconstinteger(vm->L, SAY_TEAMNL); lua_regconststring(vm->L, HOSTARCH); lua_setglobal(vm->L, "et"); // register functions luaopen_et(vm->L); luaopen_game(vm->L); luaopen_qmath(vm->L); luaopen_mover(vm->L); luaopen_vector(vm->L); // Load the code res = luaL_loadbuffer(vm->L, vm->code, vm->code_size, vm->file_name); if(res == LUA_ERRSYNTAX) { LOG("Lua API: syntax error during pre-compilation: %s\n", lua_tostring(vm->L, -1)); lua_pop(vm->L, 1); vm->err++; return qfalse; } else if(res == LUA_ERRMEM) { LOG("Lua API: memory allocation error #1 ( %s )\n", vm->file_name); vm->err++; return qfalse; } // Execute the code if(!G_LuaCall(vm, "G_LuaStartVM", 0, 0)) return qfalse; LOG("Lua API: Loading %s\n", vm->file_name); return qtrue; }