43 static const char *
Team =
"side";
44 static const char teamVar[] =
"side variables";
56 char const *m = luaL_checkstring(L, 2);
97 if (strcmp(m,
"recruit") == 0) {
98 const std::set<std::string>& recruits =
t.recruits();
99 lua_createtable(L, recruits.size(), 0);
101 for (
const std::string& r :
t.recruits()) {
102 lua_pushstring(L, r.c_str());
103 lua_rawseti(L, -2,
i++);
107 if(strcmp(m,
"variables") == 0) {
108 lua_createtable(L, 1, 0);
110 lua_rawseti(L, -2, 1);
114 if(strcmp(m,
"starting_location") == 0) {
143 std::ostringstream str;
151 lua_push(L, str.str());
161 static int impl_side_set(lua_State *L)
163 // Hidden metamethod, so arg1 has to be a pointer to a team.
164 team &t = luaW_checkteam(L, 1);
165 char const *m = luaL_checkstring(L, 2);
167 const auto& reinit_flag_for_team = [&L] (const team& t) -> void {
168 auto* disp = lua_kernel_base::get_lua_kernel<game_lua_kernel>(L).get_display();
170 disp->reinit_flags_for_team(t);
173 // Find the corresponding attribute.
174 modify_int_attrib("gold", t.set_gold(value));
175 modify_tstring_attrib("objectives", t.set_objectives(value, true));
176 //maybe add a setter for save_id too?
177 modify_int_attrib("village_gold", t.set_village_gold(value));
178 modify_int_attrib("village_support", t.set_village_support(value));
179 modify_int_attrib("recall_cost", t.set_recall_cost(value));
180 modify_int_attrib("base_income", t.set_base_income(value));
181 modify_bool_attrib("objectives_changed", t.set_objectives_changed(value));
182 modify_bool_attrib("hidden", t.set_hidden(value));
183 modify_bool_attrib("scroll_to_leader", t.set_scroll_to_leader(value));
184 modify_string_attrib("flag", {
186 reinit_flag_for_team(t);
188 modify_string_attrib("flag_icon", t.set_flag_icon(value));
189 modify_tstring_attrib("user_team_name", t.change_team(t.team_name(), value));
190 modify_string_attrib("team_name", t.change_team(value, t.user_team_name()));
191 modify_string_attrib("controller", t.change_controller_by_wml(value));
192 modify_string_attrib("color", {
194 reinit_flag_for_team(t);
196 modify_string_attrib("defeat_condition", t.set_defeat_condition_string(value));
197 modify_int_attrib("carryover_percentage", t.set_carryover_percentage(value));
198 modify_bool_attrib("carryover_add", t.set_carryover_add(value));
199 modify_bool_attrib("lost", t.set_lost(value));
200 modify_bool_attrib("persistent", t.set_persistent(value));
201 modify_bool_attrib("suppress_end_turn_confirmation", t.set_no_turn_confirmation(value));
202 modify_bool_attrib("shroud", t.set_shroud(value));
203 modify_bool_attrib("fog", t.set_fog(value));
204 modify_string_attrib("flag_icon", t.set_flag_icon(value));
205 modify_tstring_attrib("side_name", t.set_side_name(value));
206 modify_string_attrib("shroud_data", t.reshroud(); t.merge_shroud_map_data(value));
207 modify_string_attrib("share_vision", {
208 auto v = team_shared_vision::get_enum(value);
210 t.set_share_vision(*v);
212 return luaL_argerror(L, 3, "Invalid share_vision value (should be 'all
', 'none
', or 'shroud
')");
216 if (strcmp(m, "carryover_bonus") == 0) {
217 t.set_carryover_bonus(luaL_checknumber(L, 3));
221 if (strcmp(m, "recruit") == 0) {
222 t.set_recruits(std::set<std::string>());
223 if (!lua_istable(L, 3)) return 0;
224 for (int i = 1;; ++i) {
225 lua_rawgeti(L, 3, i);
226 if (lua_isnil(L, -1)) break;
227 t.add_recruit(lua_tostring(L, -1));
233 std::string err_msg = "unknown modifiable property of side: ";
235 return luaL_argerror(L, 2, err_msg.c_str());
238 static int impl_side_equal(lua_State *L)
240 // Hidden metamethod, so arg1 has to be a pointer to a team.
241 team &t1 = luaW_checkteam(L, 1);
242 if(team* t2 = luaW_toteam(L, 2)) {
243 lua_pushboolean(L, t1.side() == t2->side());
245 lua_pushboolean(L, false);
256 static int impl_side_variables_get(lua_State *L)
258 if(!lua_istable(L, 1)) {
259 return luaW_type_error(L, 1, "side variables");
261 lua_rawgeti(L, 1, 1);
262 const team& side = luaW_checkteam(L, -1);
264 char const *m = luaL_checkstring(L, 2);
265 return_cfgref_attrib("__cfg", side.variables());
267 variable_access_const v(m, side.variables());
268 return luaW_pushvariable(L, v) ? 1 : 0;
277 static int impl_side_variables_set(lua_State *L)
279 if(!lua_istable(L, 1)) {
280 return luaW_type_error(L, 1, "side variables");
282 lua_rawgeti(L, 1, 1);
283 team& side = luaW_checkteam(L, -1);
285 char const *m = luaL_checkstring(L, 2);
286 if(strcmp(m, "__cfg") == 0) {
287 side.variables() = luaW_checkconfig(L, 3);
290 config& vars = side.variables();
291 if(lua_isnoneornil(L, 3)) {
293 variable_access_throw(m, vars).clear(false);
294 } catch(const invalid_variablename_exception&) {
298 variable_access_create v(m, vars);
299 luaW_checkvariable(L, v, 3);
305 std::string register_metatable(lua_State * L)
307 std::ostringstream cmd_out;
309 cmd_out << "Adding getside metatable...\n";
311 luaL_newmetatable(L, Team);
313 static luaL_Reg const callbacks[] {
314 { "__index", &impl_side_get},
315 { "__newindex", &impl_side_set},
316 { "__eq", &impl_side_equal},
317 { "__tostring", &impl_side_tostring},
320 luaL_setfuncs(L, callbacks, 0);
322 lua_pushstring(L, Team);
323 lua_setfield(L, -2, "__metatable");
325 // Create the side variables metatable.
326 cmd_out << "Adding side variables metatable...\n";
328 luaL_newmetatable(L, teamVar);
329 lua_pushcfunction(L, impl_side_variables_get);
330 lua_setfield(L, -2, "__index");
331 lua_pushcfunction(L, impl_side_variables_set);
332 lua_setfield(L, -2, "__newindex");
333 lua_pushstring(L, "side variables");
334 lua_setfield(L, -2, "__metatable");
336 return cmd_out.str();
340 void luaW_pushteam(lua_State *L, team & tm)
342 team** t = static_cast<team**>(lua_newuserdatauv(L, sizeof(team*), 0));
344 luaL_setmetatable(L, Team);
347 team& luaW_checkteam(lua_State* L, int idx)
349 return **static_cast<team **>(luaL_checkudata(L, idx, Team));
352 team& luaW_checkteam(lua_State* L, int idx, game_board& board)
354 if(lua_isinteger(L, idx)) {
355 int side = lua_tointeger(L, idx);
356 if(!board.has_team(side)) {
357 std::string error = "side " + std::to_string(side) + " does not exist";
358 luaL_argerror(L, 1, error.c_str());
361 return board.get_team(side);
363 return **static_cast<team **>(luaL_checkudata(L, idx, Team));
366 team* luaW_toteam(lua_State* L, int idx)
368 if(void* p = luaL_testudata(L, idx, Team)) {
369 return *static_cast<team **>(p);
virtual const gamemap & map() const override
map_location starting_position(int side) const
This class stores all the data for a single 'side' (in game nomenclature).
const std::string & side_name() const
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
bool luaW_getglobal(lua_State *L, const std::vector< std::string > &path)
Pushes the value found by following the variadic names (char *), if the value is not nil.
#define return_float_attrib(name, accessor)
#define return_string_attrib(name, accessor)
#define return_int_attrib(name, accessor)
#define return_bool_attrib(name, accessor)
#define return_tstring_attrib(name, accessor)
#define return_cfg_attrib(name, accessor)
static int impl_side_get(lua_State *L)
Gets some data on a side (__index metamethod).
static const char teamVar[]
static const char * Team
Implementation for a lua reference to a team, used by the wesnoth in-game sides table.
static int impl_side_tostring(lua_State *L)
Turns a lua proxy side to string.
team & luaW_checkteam(lua_State *L, int idx)
Test if the top stack element is a team, and if not, error.
Encapsulates the map of the game.
static std::string get_string(enum_type key)
Converts a enum to its string equivalent.