Beispiel #1
0
/**
 * Gets some data on a side (__index metamethod).
 * - Arg 1: full userdata containing the team.
 * - Arg 2: string containing the name of the property.
 * - Ret 1: something containing the attribute.
 */
static int impl_side_get(lua_State *L)
{
    // Hidden metamethod, so arg1 has to be a pointer to a team.
    team &t = **static_cast<team **>(luaL_checkudata(L, 1, Team));
    char const *m = luaL_checkstring(L, 2);

    // Find the corresponding attribute.
    return_int_attrib("side", t.side());
    return_string_attrib("save_id", t.save_id());
    return_int_attrib("gold", t.gold());
    return_tstring_attrib("objectives", t.objectives());
    return_int_attrib("village_gold", t.village_gold());
    return_int_attrib("village_support", t.village_support());
    return_int_attrib("recall_cost", t.recall_cost());
    return_int_attrib("base_income", t.base_income());
    return_int_attrib("total_income", t.total_income());
    return_bool_attrib("objectives_changed", t.objectives_changed());
    return_bool_attrib("fog", t.uses_fog());
    return_bool_attrib("shroud", t.uses_shroud());
    return_bool_attrib("hidden", t.hidden());
    return_bool_attrib("scroll_to_leader", t.get_scroll_to_leader());
    return_string_attrib("flag", t.flag());
    return_string_attrib("flag_icon", t.flag_icon());
    return_tstring_attrib("user_team_name", t.user_team_name());
    return_string_attrib("team_name", t.team_name());
    return_string_attrib("color", t.color());
    return_cstring_attrib("controller", t.controller().to_string().c_str());
    return_string_attrib("defeat_condition", t.defeat_condition().to_string());
    return_string_attrib("share_vision", t.share_vision().to_string());
    return_float_attrib("carryover_bonus", t.carryover_bonus());
    return_int_attrib("carryover_percentage", t.carryover_percentage());
    return_bool_attrib("carryover_add", t.carryover_add());
    return_bool_attrib("lost", t.lost());

    if (strcmp(m, "recruit") == 0) {
        std::set<std::string> const &recruits = t.recruits();
        lua_createtable(L, recruits.size(), 0);
        int i = 1;
        BOOST_FOREACH(std::string const &r, t.recruits()) {
            lua_pushstring(L, r.c_str());
            lua_rawseti(L, -2, i++);
        }
        return 1;
    }
Beispiel #2
0
/**
 * Gets some data on a side (__index metamethod).
 * - Arg 1: full userdata containing the team.
 * - Arg 2: string containing the name of the property.
 * - Ret 1: something containing the attribute.
 */
static int impl_side_get(lua_State *L)
{
	// Hidden metamethod, so arg1 has to be a pointer to a team.
	team &t = luaW_checkteam(L, 1);
	char const *m = luaL_checkstring(L, 2);

	// Find the corresponding attribute.
	return_int_attrib("side", t.side());
	return_string_attrib("save_id", t.save_id());
	return_int_attrib("gold", t.gold());
	return_tstring_attrib("objectives", t.objectives());
	return_int_attrib("village_gold", t.village_gold());
	return_int_attrib("village_support", t.village_support());
	return_int_attrib("recall_cost", t.recall_cost());
	return_int_attrib("base_income", t.base_income());
	return_int_attrib("total_income", t.total_income());
	return_bool_attrib("objectives_changed", t.objectives_changed());
	return_bool_attrib("fog", t.uses_fog());
	return_bool_attrib("shroud", t.uses_shroud());
	return_bool_attrib("hidden", t.hidden());
	return_bool_attrib("scroll_to_leader", t.get_scroll_to_leader());
	return_string_attrib("flag", t.flag().empty() ? game_config::images::flag : t.flag());
	return_string_attrib("flag_icon", t.flag_icon().empty() ? game_config::images::flag_icon : t.flag_icon());
	return_tstring_attrib("user_team_name", t.user_team_name());
	return_string_attrib("team_name", t.team_name());
	return_string_attrib("faction", t.faction());
	return_tstring_attrib("faction_name", t.faction_name());
	return_string_attrib("color", t.color());
	return_cstring_attrib("controller", t.controller().to_string().c_str());
	return_bool_attrib("is_local", t.is_local());
	return_string_attrib("defeat_condition", t.defeat_condition().to_string());
	return_string_attrib("share_vision", t.share_vision().to_string());
	return_float_attrib("carryover_bonus", t.carryover_bonus());
	return_int_attrib("carryover_percentage", t.carryover_percentage());
	return_bool_attrib("carryover_add", t.carryover_add());
	return_bool_attrib("lost", t.lost());
	return_bool_attrib("persistent", t.persistent());
	return_bool_attrib("suppress_end_turn_confirmation", t.no_turn_confirmation());
	return_string_attrib("share_vision", t.share_vision().to_string());
	return_bool_attrib("share_maps", t.share_maps());
	return_bool_attrib("share_view", t.share_view());

	if (strcmp(m, "recruit") == 0) {
		const std::set<std::string>& recruits = t.recruits();
		lua_createtable(L, recruits.size(), 0);
		int i = 1;
		for (const std::string& r : t.recruits()) {
			lua_pushstring(L, r.c_str());
			lua_rawseti(L, -2, i++);
		}
		return 1;
	}

	// These are blocked together because they are all part of the team_data struct.
	// Some of these values involve iterating over the units map to calculate them.
	auto d = [&](){ return resources::gameboard->calculate_team_data(t); };
	return_int_attrib("num_units", d().units);
	return_int_attrib("total_upkeep", d().upkeep);
	return_int_attrib("num_villages", d().villages);
	return_int_attrib("expenses", d().expenses);
	return_int_attrib("net_income", d().net_income);

	return_cfg_attrib("__cfg", t.write(cfg));
	if(luaW_getmetafield(L, 1, m)) {
		return 1;
	}
	return 0;
}