bool GameChat::handle_special_commands(GameState* gs, const std::string& command) { ChatMessage printed; const char* content; PlayerInst* p = gs->local_player(); //Spawn monster if (starts_with(command, "!spawn ", &content)) { const char* rest = content; int amnt = strtol(content, (char**)&rest, 10); if (content == rest) amnt = 1; rest = skip_whitespace(rest); int enemy = get_enemy_by_name(rest, false); if (enemy == -1) { printed.message = "No such monster, '" + std::string(rest) + "'!"; printed.message_colour = Colour(255, 50, 50); } else { printed.message = std::string(rest) + " has spawned !"; generate_enemy_after_level_creation(gs, enemy, amnt); printed.message_colour = Colour(50, 255, 50); } add_message(printed); return true; } //Set game speed if (starts_with(command, "!gamespeed ", &content)) { int gamespeed = squish(atoi(content), 1, 200); gs->game_settings().time_per_step = gamespeed; printed.message = std::string("Game speed set."); printed.message_colour = Colour(50, 255, 50); add_message(printed); return true; } //Gain XP if (starts_with(command, "!gainxp ", &content)) { int xp = atoi(content); if (xp > 0 && xp < 999999) { printed.message = std::string("You have gained ") + content + " experience."; printed.message_colour = Colour(50, 255, 50); add_message(printed); p->gain_xp(gs, xp); } else { printed.message = "Invalid experience amount!"; printed.message_colour = Colour(255, 50, 50); add_message(printed); } return true; } //Create item if (starts_with(command, "!item ", &content)) { const char* rest = content; int amnt = strtol(content, (char**)&rest, 10); if (content == rest) amnt = 1; rest = skip_whitespace(rest); int item = get_item_by_name(rest, false); if (item == -1) { printed.message = "No such item, '" + std::string(rest) + "'!"; printed.message_colour = Colour(255, 50, 50); } else { printed.message = std::string(rest) + " put in your inventory !"; p->stats().equipment.inventory.add(Item(item), amnt); printed.message_colour = Colour(50, 255, 50); } add_message(printed); return true; } //Kill all monsters if (starts_with(command, "!killall", &content)) { MonsterController& mc = gs->monster_controller(); for (int i = 0; i < mc.monster_ids().size(); i++) { EnemyInst* inst = (EnemyInst*)gs->get_instance(mc.monster_ids()[i]); if (inst) { inst->damage(gs, 99999); } } printed.message = "Killed all monsters."; printed.message_colour = Colour(50, 255, 50); add_message(printed); return true; } lua_State* L = gs->get_luastate(); static LuaValue script_globals; if (script_globals.empty()) { script_globals.table_initialize(L); // script_globals.push(L); // int script = lua_gettop(L); // lua_pushvalue(L, LUA_GLOBALSINDEX); // lua_setmetatable(L, script); // lua_pop(L, 1); } lua_push_gameinst(L, p); script_globals.table_pop_value(L, "player"); lua_push_combatstats(L, p); script_globals.table_pop_value(L, "stats"); //Run lua command if (starts_with(command, "!lua ", &content)) { // std::string luafunc = std::string(content); int prior_top = lua_gettop(L); luaL_loadstring(L, content); if (lua_isstring(L, -1)) { const char* val = lua_tostring(L, -1); add_message(val, /*iserr ? Colour(255,50,50) :*/ Colour(120, 120, 255)); return true; } int lfunc = lua_gettop(L); script_globals.push(L); lua_setfenv(L, lfunc); bool iserr = (lua_pcall(L, 0, LUA_MULTRET, 0) != 0); int current_top = lua_gettop(L); for (; prior_top < current_top; prior_top++) { if (lua_isstring(L, -1)) { const char* val = lua_tostring(L, -1); add_message(val, iserr ? Colour(255, 50, 50) : Colour(120, 120, 255)); } lua_pop(L, 1); } return true; } //Run lua file if (starts_with(command, "!luafile ", &content)) { int prior_top = lua_gettop(L); int err_func = luaL_loadfile(L, content); if (err_func) { const char* val = lua_tostring(L, -1); add_message(val, Colour(120, 120, 255)); lua_pop(L, 1); return true; } int lfunc = lua_gettop(L); script_globals.push(L); lua_setfenv(L, lfunc); bool err_call = (lua_pcall(L, 0, 0, 0) != 0); if (err_call) { const char* val = lua_tostring(L, -1); add_message(val, Colour(120, 120, 255)); lua_pop(L, 1); } return true; } return false; }
static void register_input_table(lua_State* L) { LuaValue input = luawrap::ensure_table(luawrap::globals(L)["input"]); LuaValue meta = luameta_new(L, "<InputTable>"); LuaValue getters = luameta_getters(meta); getters["events"].bind_function(get_events); input.push(); meta.push(); lua_setmetatable(L, -2); lua_pop(L, 1); }
int luaopen_mtwist(lua_State *L) { luawrap::install_userdata_type<MTwist, &lua_mtwistmetatable>(); LuaValue module = LuaValue::newtable(L); module["create"].bind_function(newmtwist); module.push(); return 1; }
static bool lua_spell_get_target(GameState* gs, PlayerInst* p, LuaValue& action, Pos& pos) { bool nilresult = false; lua_State* L = gs->get_luastate(); obj_id target_id = p->target(); GameInst* target = gs->get_instance(target_id); int beforecall_idx = lua_gettop(L); action.push(L); lua_push_gameinst(L, p); if (!target) { lua_pushnil(L); } else { lua_push_gameinst(L, target); } // Allow for multiple return values // read the stack size difference to find out how many were returned lua_call(L, 2, LUA_MULTRET); int nret = lua_gettop(L) - beforecall_idx; if (nret != 2 || lua_isnil(L, -1)) { nilresult = true; } else if (nret == 2) { pos.x = lua_tointeger(L, -2); pos.y = lua_tointeger(L, -1); } lua_pop(L, nret); return !nilresult; }
int luaopen_GameView(lua_State *L) { luawrap::install_userdata_type<GameView, &lua_gameviewmetatable>(); LuaValue module = LuaValue::newtable(L); module["create"].bind_function(new_gameview); module.push(); return 1; }
int luaopen_TextField(lua_State* L) { LuaValue textfield = LuaValue::newtable(L); textfield["create"].bind_function(lua_newtextinput); luawrap::install_userdata_type<TextField, &lua_textfieldmetatable>(); textfield.push(); return 1; }
static void player_use_luacallback_spell(GameState* gs, PlayerInst* p, SpellEntry& spl_entry, LuaValue& action, const Pos& target) { lua_State* L = gs->get_luastate(); action.push(L); lua_push_gameinst(L, p); lua_pushnumber(L, target.x); lua_pushnumber(L, target.y); lua_call(L, 3, 0); }
static void lua_hit_callback(lua_State* L, LuaValue& callback, GameInst* user, GameInst* target) { callback.push(L); if (!lua_isnil(L, -1)) { lua_push_gameinst(L, user); lua_push_gameinst(L, target); lua_call(L, 2, 0); } else { lua_pop(L, 1); } }
static void lua_effect_func_callback(lua_State* L, LuaValue& value, Effect& effect, CombatGameInst* inst) { value.push(L); if (lua_isnil(L, -1)) { lua_pop(L, 1); return; } effect.state.push(L); lua_push_gameinst(L, inst); lua_call(L, 2, 0); }
bool LuaState::callFunction(std::string fname, LuaValue& value) { //place the function on the stack getGlobal(fname); //push the argument value.push(L); //do it return pcall(1, 0); }
int add_effect(lua_State* L) { CombatGameInst* combatinst; if ((combatinst = dynamic_cast<CombatGameInst*>(get_inst()))) { LuaValue effect = combatinst->effects().add(lua_get_gamestate(L), combatinst, effect_from_lua(L, 1), lua_tointeger(L, 2)); effect.push(L); } else { lua_pushnil(L); } return 1; }
static void lua_init_metatable(lua_State* L, LuaValue& value, effect_id effect) { value.push(L); /* Set self as metatable*/ lua_pushvalue(L, -1); lua_setmetatable(L, -2); /* Set index as effect object */ EffectEntry& eentry = game_effect_data.at(effect); lua_effects.table_push_value(L, eentry.name.c_str()); lua_setfield(L, -2, "__index"); /* Pop self */ lua_pop(L, 1); }
static void draw_loop(LuaValue draw_func) { int frames = 0; while (1) { frames += 1; SDL_Event event; while (SDL_PollEvent(&event)) { if (!handle_event(&event)) { return; // Exit draw loop } } ldraw::display_draw_start(); draw_func.push(); luawrap::call<void>(draw_func.luastate(), frames); ldraw::display_draw_finish(); SDL_Delay(5); } }
static bool lua_spell_check_prereq(GameState* gs, PlayerInst* p, SpellEntry& spl_entry, LuaValue& action, const Pos& target) { lua_State* L = gs->get_luastate(); bool passes = true; action.push(L); if (!lua_isnil(L, -1)) { lua_push_gameinst(L, p); lua_pushnumber(L, target.x); lua_pushnumber(L, target.y); lua_call(L, 3, 1); passes = lua_toboolean(L, -1); } /*Pop either the nill or the boolean*/ lua_pop(L, 1); return passes; }
static void luayaml_push(LuaValue& value, lua_State *L, const char* name) { value.push(L); int tableind = lua_gettop(L); lua_getfield(L, tableind, name); lua_replace(L, tableind); }
static void register_as_global(lua_State* L, LuaValue& value, const char* name) { value.push(L); lua_setglobal(L, name); }