24 #include "formula/callable_objects.hpp"
46 std::vector<variant> values;
47 std::size_t
n = lua_rawlen(mState, table_i);
51 for(std::size_t
i = 1;
i <=
n;
i++) {
52 lua_pushinteger(mState,
i);
53 lua_gettable(mState, table_i);
57 }
else if(key ==
"__map") {
58 std::map<variant,variant> values;
59 for(lua_pushnil(mState); lua_next(mState, table_i); lua_pop(mState, 1)) {
64 lua_pushlstring(mState, key.c_str(), key.size());
65 lua_gettable(mState, table_i);
71 add_input(inputs,
"__list");
72 add_input(inputs,
"__map");
73 for(lua_pushnil(mState); lua_next(mState, table_i); lua_pop(mState,1)) {
74 lua_pushvalue(mState, -2);
75 bool is_valid_key = (lua_type(mState, -1) == LUA_TSTRING) && !lua_isnumber(mState, -1);
78 std::string key = lua_tostring(mState, -2);
80 add_input(inputs, key);
90 if(mState == lua->
mState) {
91 if(lua_compare(mState, table_i, lua->
table_i, LUA_OPEQ)) {
94 int top = lua_gettop(mState);
95 if(lua_getmetatable(mState, table_i)) {
96 lua_getfield(mState, -1,
"__lt");
97 if(!lua_isnoneornil(mState, -1)) {
98 if(lua_getmetatable(mState, lua->
table_i)) {
99 lua_getfield(mState, -1,
"__lt");
100 if(!lua_isnoneornil(mState, -1)) {
101 lua_settop(mState, top);
102 return lua_compare(mState, table_i, lua->
table_i, LUA_OPLT) ? -1 : 1;
104 if(lua_compare(mState, -4, -2, LUA_OPEQ)) {
105 lua_settop(mState, top);
108 const void* lhs = lua_topointer(mState, -4);
109 const void* rhs = lua_topointer(mState, -2);
110 lua_settop(mState, top);
111 return lhs < rhs ? -1 : (lhs > rhs ? 1 : 0);
115 lua_settop(mState, top);
116 return lua_topointer(mState, -2) < lua_topointer(mState, -1) ? -1 : 1;
118 return mState < lua->
mState ? -1 : 1;
124 lua_pushinteger(L, val.
as_int());
128 const std::string result_string = val.
as_string();
129 lua_pushlstring(L, result_string.c_str(), result_string.size());
133 lua_pushinteger(L, lua_rawlen(L, -1) + 1);
138 typedef std::map<variant,variant>::value_type kv_type;
140 for(
const kv_type& v : val.
as_map()) {
148 const unit& u = u_ref->get_unit();
156 const unit_type& ut = ut_ref->get_unit_type();
159 const auto& atk = atk_ref->get_attack_type();
162 auto t = team_ref->get_team();
170 obj->get_inputs(inputs);
176 lua_pushstring(L, attr.name.c_str());
187 switch(lua_type(L,
i)) {
189 return variant(lua_tointeger(L,
i));
195 return variant(std::make_shared<lua_callable>(L,
i));
205 return variant(std::make_shared<unit_callable>(*u));
207 return variant(std::make_shared<unit_type_callable>(*ut));
209 return variant(std::make_shared<attack_type_callable>(*atk));
211 return variant(std::make_shared<team_callable>(*
t));
213 return variant(std::make_shared<location_callable>(
loc));
229 if(
void* ud = luaL_testudata(L, idx,
formulaKey)) {
230 form.get_deleter() = [](
fwrapper*) {};
231 form.reset(
static_cast<fwrapper*
>(ud));
233 }
else if(allow_str) {
234 form.get_deleter() = std::default_delete<fwrapper>();
235 form.reset(
new fwrapper(luaL_checkstring(L, idx)));
252 std::shared_ptr<formula_callable> context, fallback;
261 }
else if(lua_istable(L, 2)) {
266 variant result = form->evaluate(*context);
273 if(!lua_isstring(L, 1)) {
276 new(L)
fwrapper(lua_tostring(L, 1));
312 const std::string str = form->
str();
313 lua_pushlstring(L, str.c_str(), str.size());
321 lua_setfield(L, -2,
"__gc");
323 lua_setfield(L, -2,
"__tostring");
325 lua_setfield(L, -2,
"__call");
326 lua_pushstring(L,
"formula");
327 lua_setfield(L, -2,
"__metatable");
329 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, const 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