/** * Gets a property of a units attack (__index metamethod). * - Arg 1: table containing the userdata containing the unit id. and a string identifying the attack. * - Arg 2: string * - Ret 1: */ static int impl_unit_attack_get(lua_State *L) { attack_ref& atk_ref = luaW_checkweapon_ref(L, 1); const attack_type& attack = *atk_ref.cattack; char const *m = luaL_checkstring(L, 2); return_bool_attrib("read_only", atk_ref.attack == nullptr); return_string_attrib("description", attack.name()); return_string_attrib("name", attack.id()); return_string_attrib("type", attack.type()); return_string_attrib("icon", attack.icon()); return_string_attrib("range", attack.range()); return_int_attrib("damage", attack.damage()); return_int_attrib("number", attack.num_attacks()); return_int_attrib("attack_weight", attack.attack_weight()); return_int_attrib("defense_weight", attack.defense_weight()); return_int_attrib("accuracy", attack.accuracy()); return_int_attrib("movement_used", attack.movement_used()); return_int_attrib("parry", attack.parry()); return_cfgref_attrib("specials", attack.specials()); return_cfgref_attrib("__cfg", attack.to_config()); if(luaW_getmetafield(L, 1, m)) { return 1; } std::string err_msg = "unknown property of attack: "; err_msg += m; return luaL_argerror(L, 2, err_msg.c_str()); }
/** * Gets some data on a unit type (__index metamethod). * - Arg 1: table containing an "id" field. * - Arg 2: string containing the name of the property. * - Ret 1: something containing the attribute. */ static int impl_unit_type_get(lua_State *L) { char const *m = luaL_checkstring(L, 2); lua_pushstring(L, "id"); lua_rawget(L, 1); const unit_type *utp = unit_types.find(lua_tostring(L, -1)); if (!utp) return luaL_argerror(L, 1, "unknown unit type"); unit_type const &ut = *utp; // Find the corresponding attribute. return_tstring_attrib("name", ut.type_name()); return_int_attrib("max_hitpoints", ut.hitpoints()); return_int_attrib("max_moves", ut.movement()); return_int_attrib("max_experience", ut.experience_needed()); return_int_attrib("cost", ut.cost()); return_int_attrib("level", ut.level()); return_int_attrib("recall_cost", ut.recall_cost()); return_cfgref_attrib("__cfg", ut.get_cfg()); if (strcmp(m, "traits") == 0) { lua_newtable(L); BOOST_FOREACH(const config& trait, ut.possible_traits()) { const std::string& id = trait["id"]; lua_pushlstring(L, id.c_str(), id.length()); luaW_pushconfig(L, trait); lua_rawset(L, -3); } return 1; }
/** * Gets some data on a unit type (__index metamethod). * - Arg 1: table containing an "id" field. * - Arg 2: string containing the name of the property. * - Ret 1: something containing the attribute. */ static int impl_unit_type_get(lua_State *L) { const unit_type& ut = luaW_checkunittype(L, 1); char const *m = luaL_checkstring(L, 2); // Find the corresponding attribute. return_tstring_attrib("name", ut.type_name()); return_string_attrib("id", ut.id()); return_string_attrib("alignment", ut.alignment().to_string()); return_string_attrib("race", ut.race_id()); return_int_attrib("max_hitpoints", ut.hitpoints()); return_int_attrib("max_moves", ut.movement()); return_int_attrib("max_experience", ut.experience_needed()); return_int_attrib("cost", ut.cost()); return_int_attrib("level", ut.level()); return_int_attrib("recall_cost", ut.recall_cost()); return_cfgref_attrib("__cfg", ut.get_cfg()); if (strcmp(m, "traits") == 0) { lua_newtable(L); for (const config& trait : ut.possible_traits()) { const std::string& id = trait["id"]; lua_pushlstring(L, id.c_str(), id.length()); luaW_pushconfig(L, trait); lua_rawset(L, -3); } return 1; } if (strcmp(m, "abilities") == 0) { lua_push(L, ut.get_ability_list()); return 1; } if (strcmp(m, "attacks") == 0) { push_unit_attacks_table(L, 1); return 1; } // TODO: Should this only exist for base units? if(strcmp(m, "variations") == 0) { *new(L) const unit_type* = &ut; luaL_setmetatable(L, UnitTypeTable); return 1; } return 0; }