The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_team.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "scripting/lua_team.hpp"
16 
17 #include "scripting/lua_common.hpp"
18 #include "team.hpp"
19 
20 #include <string>
21 
22 #include "lua/lua.h"
23 #include "lua/lauxlib.h"
24 
25 /**
26  * Implementation for a lua reference to a team,
27  * used by the wesnoth in-game sides table.
28  *
29  * (The userdata has type team** because lua holds
30  * only a pointer to a team, not a full-size team.
31  * If it were a full object then we would cast to
32  * type team *, since checkudata returns a pointer
33  * to the type corresponding to the sizeof expr
34  * used when we allocated the userdata.)
35  */
36 
37 // Registry key
38 static const char * Team = "side";
39 
40 /**
41  * Gets some data on a side (__index metamethod).
42  * - Arg 1: full userdata containing the team.
43  * - Arg 2: string containing the name of the property.
44  * - Ret 1: something containing the attribute.
45  */
46 static int impl_side_get(lua_State *L)
47 {
48  // Hidden metamethod, so arg1 has to be a pointer to a team.
49  team &t = **static_cast<team **>(luaL_checkudata(L, 1, Team));
50  char const *m = luaL_checkstring(L, 2);
51 
52  // Find the corresponding attribute.
53  return_int_attrib("side", t.side());
54  return_string_attrib("save_id", t.save_id());
55  return_int_attrib("gold", t.gold());
56  return_tstring_attrib("objectives", t.objectives());
57  return_int_attrib("village_gold", t.village_gold());
58  return_int_attrib("village_support", t.village_support());
59  return_int_attrib("recall_cost", t.recall_cost());
60  return_int_attrib("base_income", t.base_income());
61  return_int_attrib("total_income", t.total_income());
62  return_bool_attrib("objectives_changed", t.objectives_changed());
63  return_bool_attrib("fog", t.uses_fog());
64  return_bool_attrib("shroud", t.uses_shroud());
65  return_bool_attrib("hidden", t.hidden());
66  return_bool_attrib("scroll_to_leader", t.get_scroll_to_leader());
67  return_string_attrib("flag", t.flag());
68  return_string_attrib("flag_icon", t.flag_icon());
69  return_tstring_attrib("user_team_name", t.user_team_name());
70  return_string_attrib("team_name", t.team_name());
71  return_string_attrib("faction", t.faction());
72  return_tstring_attrib("faction_name", t.faction_name());
73  return_string_attrib("color", t.color());
74  return_cstring_attrib("controller", t.controller().to_string().c_str());
75  return_string_attrib("defeat_condition", t.defeat_condition().to_string());
76  return_string_attrib("share_vision", t.share_vision().to_string());
77  return_float_attrib("carryover_bonus", t.carryover_bonus());
78  return_int_attrib("carryover_percentage", t.carryover_percentage());
79  return_bool_attrib("carryover_add", t.carryover_add());
80  return_bool_attrib("lost", t.lost());
81  return_bool_attrib("persistent", t.persistent());
82 
83  if (strcmp(m, "recruit") == 0) {
84  std::set<std::string> const &recruits = t.recruits();
85  lua_createtable(L, recruits.size(), 0);
86  int i = 1;
87  for (std::string const &r : t.recruits()) {
88  lua_pushstring(L, r.c_str());
89  lua_rawseti(L, -2, i++);
90  }
91  return 1;
92  }
93 
94  return_cfg_attrib("__cfg", t.write(cfg));
95  return 0;
96 }
97 
98 /**
99  * Sets some data on a side (__newindex metamethod).
100  * - Arg 1: full userdata containing the team.
101  * - Arg 2: string containing the name of the property.
102  * - Arg 3: something containing the attribute.
103  */
104 static int impl_side_set(lua_State *L)
105 {
106  // Hidden metamethod, so arg1 has to be a pointer to a team.
107  team &t = **static_cast<team **>(luaL_checkudata(L, 1, Team));
108  char const *m = luaL_checkstring(L, 2);
109 
110  // Find the corresponding attribute.
111  modify_int_attrib("gold", t.set_gold(value));
112  modify_tstring_attrib("objectives", t.set_objectives(value, true));
113  //maybe add a setter for save_id too?
114  modify_int_attrib("village_gold", t.set_village_gold(value));
115  modify_int_attrib("village_support", t.set_village_support(value));
116  modify_int_attrib("recall_cost", t.set_recall_cost(value));
117  modify_int_attrib("base_income", t.set_base_income(value));
118  modify_bool_attrib("objectives_changed", t.set_objectives_changed(value));
119  modify_bool_attrib("hidden", t.set_hidden(value));
120  modify_bool_attrib("scroll_to_leader", t.set_scroll_to_leader(value));
121  modify_tstring_attrib("user_team_name", t.change_team(t.team_name(), value));
124  modify_string_attrib("color", t.set_color(value));
126  modify_int_attrib("carryover_percentage", t.set_carryover_percentage(value));
127  modify_bool_attrib("carryover_add", t.set_carryover_add(value));
128  modify_bool_attrib("lost", t.set_lost(value));
129  modify_bool_attrib("persistent", t.set_persistent(value));
130 
131  if (strcmp(m, "carryover_bonus") == 0) {
133  return 0;
134  }
135 
136  if (strcmp(m, "recruit") == 0) {
137  t.set_recruits(std::set<std::string>());
138  if (!lua_istable(L, 3)) return 0;
139  for (int i = 1;; ++i) {
140  lua_rawgeti(L, 3, i);
141  if (lua_isnil(L, -1)) break;
142  t.add_recruit(lua_tostring(L, -1));
143  lua_pop(L, 1);
144  }
145  return 0;
146  }
147 
148  std::string err_msg = "unknown modifiable property of side: ";
149  err_msg += m;
150  return luaL_argerror(L, 2, err_msg.c_str());
151 }
152 
153 namespace lua_team {
154 
156  {
158 
159  static luaL_Reg const callbacks[] = {
160  { "__index", &impl_side_get},
161  { "__newindex", &impl_side_set},
162  { nullptr, nullptr }
163  };
164  luaL_setfuncs(L, callbacks, 0);
165 
166  lua_pushstring(L, "side");
167  lua_setfield(L, -2, "__metatable");
168 
169  return "Adding getside metatable...\n";
170  }
171 }
172 
174 {
175  team** t = static_cast<team**>(lua_newuserdata(L, sizeof(team*)));
176  *t = &tm;
178 }
const t_string & faction_name() const
Definition: team.hpp:309
#define modify_bool_attrib(name, accessor)
Definition: lua_common.hpp:252
LUA_API void lua_rawgeti(lua_State *L, int idx, int n)
Definition: lapi.cpp:643
bool uses_shroud() const
Definition: team.hpp:315
LUALIB_API void * luaL_checkudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:307
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
const std::string & flag() const
Definition: team.hpp:301
const std::string & color() const
Definition: team.hpp:257
#define return_tstring_attrib(name, accessor)
Definition: lua_common.hpp:163
void set_carryover_add(bool value)
Definition: team.hpp:353
void luaW_pushteam(lua_State *L, team &tm)
Definition: lua_team.cpp:173
void set_objectives(const t_string &new_objectives, bool silently=false)
Definition: team.cpp:560
bool persistent() const
Definition: team.hpp:346
const std::set< std::string > & recruits() const
Definition: team.hpp:228
This namespace contains bindings for lua to hold a pointer to a team, and to access and modify it...
Definition: lua_team.cpp:153
bool hidden() const
Definition: team.hpp:344
double carryover_bonus() const
Definition: team.hpp:356
const t_string & objectives() const
Definition: team.hpp:244
#define return_string_attrib(name, accessor)
Definition: lua_common.hpp:175
void set_recall_cost(int cost)
Definition: team.hpp:200
bool objectives_changed() const
Definition: team.hpp:245
DEFEAT_CONDITION defeat_condition() const
Definition: team.hpp:339
void set_persistent(bool value)
Definition: team.hpp:347
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:286
void set_defeat_condition_string(const std::string &value)
sets the defeat condition if
Definition: team.hpp:342
void add_recruit(const std::string &)
Definition: team.cpp:413
-file sdl_utils.hpp
void set_hidden(bool value)
Definition: team.hpp:345
GLdouble GLdouble t
Definition: glew.h:1366
void set_carryover_percentage(int value)
Definition: team.hpp:351
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.cpp:1169
static const char * Team
Implementation for a lua reference to a team, used by the wesnoth in-game sides table.
Definition: lua_team.cpp:38
void set_scroll_to_leader(bool value)
Definition: team.hpp:226
#define lua_pop(L, n)
Definition: lua.h:322
SHARE_VISION share_vision() const
Definition: team.hpp:388
std::string register_metatable(lua_State *L)
Definition: lua_team.cpp:155
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
#define modify_tstring_attrib(name, accessor)
Definition: lua_common.hpp:223
void set_carryover_bonus(double value)
Definition: team.hpp:355
int recall_cost() const
Definition: team.hpp:198
const std::string & flag_icon() const
Definition: team.hpp:302
#define modify_string_attrib(name, accessor)
Definition: lua_common.hpp:230
#define return_int_attrib(name, accessor)
Definition: lua_common.hpp:178
int village_support() const
Definition: team.hpp:204
const t_string & user_team_name() const
Definition: team.hpp:298
int carryover_percentage() const
Definition: team.hpp:352
GLsizei const GLfloat * value
Definition: glew.h:1817
void set_lost(bool value=true)
Definition: team.hpp:348
int base_income() const
Definition: team.hpp:196
bool lost() const
Definition: team.hpp:349
void write(config &cfg) const
Definition: team.cpp:355
const std::string & team_name() const
Definition: team.hpp:297
void set_village_support(int support)
Definition: team.hpp:206
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
Definition: lauxlib.cpp:377
const std::string & save_id() const
Definition: team.hpp:236
void set_village_gold(int income)
Definition: team.hpp:199
#define return_cfg_attrib(name, accessor)
Definition: lua_common.hpp:196
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:274
bool get_scroll_to_leader() const
Definition: team.hpp:225
#define lua_isnil(L, n)
Definition: lua.h:333
void set_gold(int amount)
Definition: team.hpp:211
const std::string & faction() const
Definition: team.hpp:308
static int impl_side_set(lua_State *L)
Sets some data on a side (__newindex metamethod).
Definition: lua_team.cpp:104
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
#define lua_tostring(L, i)
Definition: lua.h:345
LUA_API void lua_rawseti(lua_State *L, int idx, int n)
Definition: lapi.cpp:778
void set_objectives_changed(bool c=true) const
Definition: team.hpp:241
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
int side() const
Definition: team.hpp:193
void change_controller_by_wml(const std::string &new_controller)
Definition: team.cpp:512
CONTROLLER controller() const
Definition: team.hpp:256
#define return_bool_attrib(name, accessor)
Definition: lua_common.hpp:190
int gold() const
Definition: team.hpp:194
const GLdouble * m
Definition: glew.h:6968
#define lua_istable(L, n)
Definition: lua.h:331
bool uses_fog() const
Definition: team.hpp:316
#define return_float_attrib(name, accessor)
Definition: lua_common.hpp:184
static int impl_side_get(lua_State *L)
Gets some data on a side (__index metamethod).
Definition: lua_team.cpp:46
void change_team(const std::string &name, const t_string &user_name)
Definition: team.cpp:534
bool carryover_add() const
Definition: team.hpp:354
int total_income() const
Definition: team.hpp:201
void set_base_income(int amount)
Definition: team.hpp:214
void set_recruits(const std::set< std::string > &recruits)
Definition: team.cpp:406
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:850
int village_gold() const
Definition: team.hpp:197
GLsizei const GLcharARB ** string
Definition: glew.h:4503
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
#define modify_int_attrib(name, accessor)
Definition: lua_common.hpp:237
void set_color(const std::string &color)
Definition: team.hpp:258
#define return_cstring_attrib(name, accessor)
Definition: lua_common.hpp:169
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115