std::shared_ptr<aspect_attacks_lua_filter> lua_object<aspect_attacks_lua_filter>::to_type(lua_State *L, int n) { std::shared_ptr<aspect_attacks_lua_filter> att(new aspect_attacks_lua_filter); att->lua = nullptr; att->ref_own_ = att->ref_enemy_ = -1; if(!lua_istable(L, n)) { return att; } lua_getfield(L, n, "own"); if(lua_istable(L, -1)) { vconfig vcfg(config(), true); if(luaW_tovconfig(L, -1, vcfg)) { att->filter_own_.reset(new unit_filter(vcfg)); } } else if(lua_isfunction(L, -1)) { att->lua = L; att->ref_own_ = luaL_ref(L, LUA_REGISTRYINDEX); assert(att->ref_own_ != -1); } lua_getfield(L, n, "enemy"); if(lua_istable(L, -1)) { vconfig vcfg(config(), true); if(luaW_tovconfig(L, -1, vcfg)) { att->filter_enemy_.reset(new unit_filter(vcfg)); } } else if(lua_isfunction(L, -1)) { att->lua = L; att->ref_enemy_ = luaL_ref(L, LUA_REGISTRYINDEX); assert(att->ref_enemy_ != -1); } lua_pop(L, 2); return att; }
vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing) { vconfig result = vconfig::unconstructed_vconfig(); if (!luaW_tovconfig(L, index, result) || (!allow_missing && result.null())) luaW_type_error(L, index, "WML table"); return result; }
inline boost::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n) { // To Crab_: Is this part ok? I tested it, works fine boost::shared_ptr<config> cfg = boost::shared_ptr<config>(new config()); boost::shared_ptr<vconfig> vcfg = boost::shared_ptr<vconfig>(new vconfig(*cfg)); luaW_tovconfig(L, n, *vcfg); boost::shared_ptr<terrain_filter> tf = boost::shared_ptr<terrain_filter>(new terrain_filter(*vcfg, resources::filter_con)); return tf; }
inline std::shared_ptr<terrain_filter> lua_object<terrain_filter>::to_type(lua_State *L, int n) { std::shared_ptr<config> cfg = std::shared_ptr<config>(new config()); std::shared_ptr<vconfig> vcfg = std::shared_ptr<vconfig>(new vconfig(*cfg)); if (!luaW_tovconfig(L, n, *vcfg)) { cfg->add_child("not"); } vcfg->make_safe(); std::shared_ptr<terrain_filter> tf(new terrain_filter(*vcfg, resources::filter_con)); return tf; }
bool luaW_tolocation(lua_State *L, int index, map_location& loc) { if (!lua_checkstack(L, LUA_MINSTACK)) { return false; } if (lua_isnoneornil(L, index)) { // Need this special check because luaW_tovconfig returns true in this case return false; } vconfig dummy_vcfg = vconfig::unconstructed_vconfig(); index = lua_absindex(L, index); if (lua_istable(L, index) || luaW_tounit(L, index) || luaW_tovconfig(L, index, dummy_vcfg)) { map_location result; int x_was_num = 0, y_was_num = 0; lua_getfield(L, index, "x"); result.set_wml_x(lua_tonumberx(L, -1, &x_was_num)); lua_getfield(L, index, "y"); result.set_wml_y(lua_tonumberx(L, -1, &y_was_num)); lua_pop(L, 2); if (!x_was_num || !y_was_num) { // If we get here and it was userdata, checking numeric indices won't help // (It won't help if it was a config either, but there's no easy way to check that.) if (lua_isuserdata(L, index)) { return false; } lua_rawgeti(L, index, 1); result.set_wml_x(lua_tonumberx(L, -1, &x_was_num)); lua_rawgeti(L, index, 2); result.set_wml_y(lua_tonumberx(L, -1, &y_was_num)); lua_pop(L, 2); } if (x_was_num && y_was_num) { loc = result; return true; } } else if (lua_isnumber(L, index) && lua_isnumber(L, index + 1)) { // If it's a number, then we consume two elements on the stack // Since we have no way of notifying the caller that we have // done this, we remove the first number from the stack. loc.set_wml_x(lua_tonumber(L, index)); lua_remove(L, index); loc.set_wml_y(lua_tonumber(L, index)); return true; } return false; }