Beispiel #1
8
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 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;
}
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);
    }
}
Beispiel #4
0
void EffectStats::process(GameState* gs, CombatGameInst* inst,
		EffectiveStats& effective) const {
	if (!has_active_effect())
		return;
	lua_State* L = gs->get_luastate();
	lua_push_combatstats(L, inst->stats());
	lua_push_effectivestats(L, effective);

	int affind = lua_gettop(L);
	int baseind = affind - 1;

	for (int i = 0; i < EFFECTS_MAX; i++) {
		if (effects[i].t_remaining > 0) {
			game_effect_data.at(effects[i].effectid).stat_func.push(L);
			if (!lua_isnil(L, -1)) {
				effects[i].state.push(L);
				lua_push_gameinst(L, inst);

				lua_pushvalue(L, baseind);
				lua_pushvalue(L, affind);
				lua_call(L, 4, 0);
			} else {
				lua_pop(L, 1);
			}
		}
	}
//	basestats = lua_get_combatstats(L, baseind);
	effective = lua_get_effectivestats(L, affind);
	lua_pop(L, 2);
	//pop base&affected
}
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);
}
Beispiel #6
0
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);
}
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;
}