25 #include "formula/callable_objects.hpp"
47 std::vector<variant> values;
48 std::size_t
n = lua_rawlen(mState, table_i);
52 for(std::size_t
i = 1;
i <=
n;
i++) {
53 lua_pushinteger(mState,
i);
54 lua_gettable(mState, table_i);
58 }
else if(key ==
"__map") {
59 std::map<variant,variant> values;
60 for(lua_pushnil(mState); lua_next(mState, table_i); lua_pop(mState, 1)) {
65 lua_pushlstring(mState, key.c_str(), key.size());
66 lua_gettable(mState, table_i);
72 add_input(inputs,
"__list");
73 add_input(inputs,
"__map");
74 for(lua_pushnil(mState); lua_next(mState, table_i); lua_pop(mState,1)) {
75 lua_pushvalue(mState, -2);
76 bool is_valid_key = (lua_type(mState, -1) == LUA_TSTRING) && !lua_isnumber(mState, -1);
79 std::string key = lua_tostring(mState, -2);
81 add_input(inputs, key);
91 if(mState == lua->
mState) {
92 if(lua_compare(mState, table_i, lua->
table_i, LUA_OPEQ)) {
95 int top = lua_gettop(mState);
96 if(lua_getmetatable(mState, table_i)) {
97 lua_getfield(mState, -1,
"__lt");
98 if(!lua_isnoneornil(mState, -1)) {
99 if(lua_getmetatable(mState, lua->
table_i)) {
100 lua_getfield(mState, -1,
"__lt");
101 if(!lua_isnoneornil(mState, -1)) {
102 lua_settop(mState, top);
103 return lua_compare(mState, table_i, lua->
table_i, LUA_OPLT) ? -1 : 1;
105 if(lua_compare(mState, -4, -2, LUA_OPEQ)) {
106 lua_settop(mState, top);
109 const void* lhs = lua_topointer(mState, -4);
110 const void* rhs = lua_topointer(mState, -2);
111 lua_settop(mState, top);
112 return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0);
116 lua_settop(mState, top);
117 return lua_topointer(mState, -2) < lua_topointer(mState, -1) ? -1 : 1;
119 return mState < lua->
mState ? -1 : 1;
125 lua_pushinteger(L, val.
as_int());
129 const std::string result_string = val.
as_string();
130 lua_pushlstring(L, result_string.c_str(), result_string.size());
134 lua_pushinteger(L, lua_rawlen(L, -1) + 1);
139 typedef std::map<variant,variant>::value_type kv_type;
141 for(
const kv_type& v : val.
as_map()) {
149 const unit& u = u_ref->get_unit();
157 const unit_type& ut = ut_ref->get_unit_type();
160 const auto& atk = atk_ref->get_attack_type();
163 auto t = team_ref->get_team();
171 obj->get_inputs(inputs);
177 lua_pushstring(L, attr.name.c_str());
188 switch(lua_type(L,
i)) {
190 return variant(lua_tointeger(L,
i));
196 return variant(std::make_shared<lua_callable>(L,
i));
206 return variant(std::make_shared<unit_callable>(*u));
208 return variant(std::make_shared<unit_type_callable>(*ut));
210 return variant(std::make_shared<attack_type_callable>(*atk));
212 return variant(std::make_shared<team_callable>(*
t));
214 return variant(std::make_shared<location_callable>(loc));
229 bool need_delete =
false;
231 if(
void* ud = luaL_testudata(L, 1,
formulaKey)) {
235 form =
new fwrapper(luaL_checkstring(L, 1));
237 std::shared_ptr<formula_callable> context, fallback;
246 }
else if(lua_istable(L, 2)) {
261 if(!lua_isstring(L, 1)) {
264 new(L)
fwrapper(lua_tostring(L, 1));
300 const std::string str = form->
str();
301 lua_pushlstring(L, str.c_str(), str.size());
309 lua_setfield(L, -2,
"__gc");
311 lua_setfield(L, -2,
"__tostring");
313 lua_setfield(L, -2,
"__call");
314 lua_pushstring(L,
"formula");
315 lua_setfield(L, -2,
"__metatable");
317 return "Adding formula metatable...\n";
virtual const unit_map & units() const override
int do_compare(const formula_callable *other) const
void get_inputs(formula_input_vector &inputs) const
variant get_value(const std::string &key) const
lua_callable(lua_State *L, int i)
const std::string & str() const
This class stores all the data for a single 'side' (in game nomenclature).
unit_iterator find(std::size_t id)
A single unit type that the player may recruit.
This class represents a single unit of a specific type.
A variable-expanding proxy for the config class.
static vconfig unconstructed_vconfig()
This is just a wrapper for the default constructor; it exists for historical reasons and to make it c...
config get_parsed_config() const
std::shared_ptr< T > try_convert() const
int as_decimal() const
Returns variant's internal representation of decimal number: ie, 1.234 is represented as 1234.
const_formula_callable_ptr as_callable() const
const std::string & as_string() const
const std::vector< variant > & as_list() const
const std::map< variant, variant > & as_map() const
bool is_null() const
Functions to test the type of the internal value.
int side() const
The side this unit belongs to.
std::size_t underlying_id() const
This unit's unique internal ID.
const map_location & get_location() const
The current map location this unit is at.
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_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
int luaW_type_error(lua_State *L, int narg, const char *tname)
bool luaW_totstring(lua_State *L, int index, t_string &str)
Converts a scalar to a translatable string.
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location object.
void luaW_pushteam(lua_State *L, team &tm)
Create a full userdata containing a pointer to the team.
team * luaW_toteam(lua_State *L, int idx)
Test if the top stack element is a team, and if so, return it.
unit * luaW_tounit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
lua_unit * luaW_pushunit(lua_State *L, Args... args)
const_attack_ptr luaW_toweapon(lua_State *L, int idx)
void luaW_pushweapon(lua_State *L, attack_ptr weapon)
const unit_type * luaW_tounittype(lua_State *L, int idx)
Test if a stack element is a unit type, and return it if so.
void luaW_pushunittype(lua_State *L, const unit_type &ut)
Create a lua object containing a reference to a unittype, and a metatable to access the properties.
std::vector< formula_input > formula_input_vector
std::shared_ptr< formula > formula_ptr
std::shared_ptr< const attack_type > const_attack_ptr
Encapsulates the map of the game.
static map_location::DIRECTION n