static int impl_unit_type_equal(lua_State* L) { const unit_type& ut1 = luaW_checkunittype(L, 1); if(const unit_type* ut2 = luaW_tounittype(L, 2)) { lua_pushboolean(L, &ut1 == ut2); } else { lua_pushboolean(L, false); } return 1; }
static int impl_unit_type_count(lua_State* L) { lua_pushstring(L, "base"); lua_rawget(L, 1); if(const unit_type* base = luaW_tounittype(L, -1)) { lua_pushnumber(L, base->variations().size() + base->genders().size()); } else { lua_pushnumber(L, unit_types.types().size()); } return 1; }
/** * Counts the attacks of a unit (__len metamethod). * - Arg 1: table containing the userdata containing the unit id. * - Ret 1: size of unit attacks vector. */ static int impl_unit_attacks_len(lua_State *L) { if(!lua_istable(L, 1)) { return luaW_type_error(L, 1, "unit attacks"); } lua_rawgeti(L, 1, 0); const unit* u = luaW_tounit(L, -1); const unit_type* ut = luaW_tounittype(L, -1); if(!u && !ut) { return luaL_argerror(L, 1, "unknown unit"); } lua_pushinteger(L, (u ? u->attacks() : ut->attacks()).size()); return 1; }
static int impl_unit_attacks_set(lua_State* L) { if(!lua_istable(L, 1)) { return luaW_type_error(L, 1, "unit attacks"); } lua_rawgeti(L, 1, 0); const unit_type* ut = luaW_tounittype(L, -1); if(ut) { return luaL_argerror(L, 1, "unit type attack table is immutable"); } unit& u = luaW_checkunit(L, -1); attack_ptr atk = lua_isnumber(L, 2) ? find_attack(&u, luaL_checkinteger(L, 2) - 1) : find_attack(&u, luaL_checkstring(L, 2)); if(lua_isnumber(L, 2) && lua_tonumber(L, 2) - 1 > u.attacks().size()) { return luaL_argerror(L, 2, "attack can only be added at the end of the list"); } if(lua_isnil(L, 3)) { // Delete the attack u.remove_attack(atk); return 0; } auto iter = get_attack_iter(u, atk), end = u.attacks().end(); if(const_attack_ptr atk2 = luaW_toweapon(L, 3)) { if(iter == end) { atk = u.add_attack(end, *atk2); } else { iter.base()->reset(new attack_type(*atk2)); atk = *iter.base(); } } else { config cfg = luaW_checkconfig(L, 3); if(iter == end) { atk = u.add_attack(end, cfg); } else { iter.base()->reset(new attack_type(cfg)); atk = *iter.base(); } } if(!lua_isnumber(L, 2)) { atk->set_id(lua_tostring(L, 2)); } return 0; }
/** * Gets the attacks of a unit or unit type (__index metamethod). * - Arg 1: table containing the userdata containing the unit or unit type. * - Arg 2: index (int) or id (string) identifying a particular attack. * - Ret 1: the unit's attacks. */ static int impl_unit_attacks_get(lua_State *L) { if(!lua_istable(L, 1)) { return luaW_type_error(L, 1, "unit attacks"); } lua_rawgeti(L, 1, 0); lua_unit* lu = luaW_tounit_ref(L, -1); const unit_type* ut = luaW_tounittype(L, -1); if(lu && lu->get()) { unit* u = lu->get(); attack_ptr atk = lua_isnumber(L, 2) ? find_attack(u, luaL_checkinteger(L, 2) - 1) : find_attack(u, luaL_checkstring(L, 2)); luaW_pushweapon(L, atk); } else if(ut) { const_attack_ptr atk = lua_isnumber(L, 2) ? find_attack(ut, luaL_checkinteger(L, 2) - 1) : find_attack(ut, luaL_checkstring(L, 2)); luaW_pushweapon(L, atk); } else { return luaL_argerror(L, 1, "unit not found"); } return 1; }