void LuaShipDef::Register() { lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); lua_newtable(l); for (std::map<ShipType::Id,ShipType>::const_iterator i = ShipType::types.begin(); i != ShipType::types.end(); ++i) { const ShipType &st = (*i).second; lua_newtable(l); pi_lua_settable(l, "id", (*i).first.c_str()); pi_lua_settable(l, "name", st.name.c_str()); pi_lua_settable(l, "shipClass", st.shipClass.c_str()); pi_lua_settable(l, "manufacturer", st.manufacturer.c_str()); pi_lua_settable(l, "modelName", st.modelName.c_str()); pi_lua_settable(l, "cockpitName", st.cockpitName.c_str()); pi_lua_settable(l, "tag", EnumStrings::GetString("ShipTypeTag", st.tag)); pi_lua_settable(l, "angularThrust", st.angThrust); pi_lua_settable(l, "capacity", st.capacity); pi_lua_settable(l, "hullMass", st.hullMass); pi_lua_settable(l, "fuelTankMass", st.fuelTankMass); pi_lua_settable(l, "basePrice", double(st.baseprice)*0.01); pi_lua_settable(l, "minCrew", st.minCrew); pi_lua_settable(l, "maxCrew", st.maxCrew); pi_lua_settable(l, "defaultHyperdrive", EnumStrings::GetString("EquipType", st.hyperdrive)); pi_lua_settable(l, "effectiveExhaustVelocity", st.effectiveExhaustVelocity); pi_lua_settable(l, "thrusterFuelUse", st.GetFuelUseRate()); lua_newtable(l); for (int t = ShipType::THRUSTER_REVERSE; t < ShipType::THRUSTER_MAX; t++) pi_lua_settable(l, EnumStrings::GetString("ShipTypeThruster", t), st.linThrust[t]); pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, "linearThrust"); lua_pop(l, 1); lua_newtable(l); for (int slot = Equip::SLOT_CARGO; slot < Equip::SLOT_MAX; slot++) pi_lua_settable(l, EnumStrings::GetString("EquipSlot", slot), st.equipSlotCapacity[slot]); pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, "equipSlotCapacity"); lua_pop(l, 1); pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, (*i).first.c_str()); lua_pop(l, 1); } lua_getfield(l, LUA_REGISTRYINDEX, "CoreImports"); pi_lua_readonly_table_proxy(l, -2); lua_setfield(l, -2, "ShipDef"); lua_pop(l, 2); LUA_DEBUG_END(l, 0); }
static void _create_constant_table(lua_State *l, const char *ns, const EnumItem *c) { LUA_DEBUG_START(l); lua_getglobal(l, "Constants"); if (lua_isnil(l, -1)) { lua_pop(l, 1); lua_newtable(l); // publish a read-only proxy wrapper lua_rawgeti(l, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); lua_pushstring(l, "Constants"); pi_lua_readonly_table_proxy(l, -3); lua_rawset(l, -3); lua_pop(l, 1); } else { pi_lua_readonly_table_original(l, -1); lua_remove(l, -2); } assert(lua_istable(l, -1)); lua_newtable(l); // 'Constants' table, enum table int enum_table_idx = lua_gettop(l); // publish a read-only proxy to the enum table lua_pushstring(l, ns); pi_lua_readonly_table_proxy(l, enum_table_idx); lua_rawset(l, -4); int index = 1; for (; c->name; c++) { lua_pushinteger(l, index); lua_pushstring(l, c->name); lua_rawset(l, enum_table_idx); ++index; } lua_pop(l, 2); LUA_DEBUG_END(l, 0); }
void LuaShipDef::Register() { lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); lua_newtable(l); for (auto iter : ShipType::types) { const ShipType &st = iter.second; lua_newtable(l); pi_lua_settable(l, "id", iter.first.c_str()); pi_lua_settable(l, "name", st.name.c_str()); pi_lua_settable(l, "shipClass", st.shipClass.c_str()); pi_lua_settable(l, "manufacturer", st.manufacturer.c_str()); pi_lua_settable(l, "modelName", st.modelName.c_str()); pi_lua_settable(l, "cockpitName", st.cockpitName.c_str()); pi_lua_settable(l, "tag", EnumStrings::GetString("ShipTypeTag", st.tag)); pi_lua_settable(l, "angularThrust", st.angThrust); pi_lua_settable(l, "capacity", st.capacity); pi_lua_settable(l, "hullMass", st.hullMass); pi_lua_settable(l, "fuelTankMass", st.fuelTankMass); pi_lua_settable(l, "basePrice", st.baseprice); pi_lua_settable(l, "minCrew", st.minCrew); pi_lua_settable(l, "maxCrew", st.maxCrew); pi_lua_settable(l, "hyperdriveClass", st.hyperdriveClass); pi_lua_settable(l, "effectiveExhaustVelocity", st.effectiveExhaustVelocity); pi_lua_settable(l, "thrusterFuelUse", st.GetFuelUseRate()); lua_newtable(l); for (int t = Thruster::THRUSTER_REVERSE; t < Thruster::THRUSTER_MAX; t++) pi_lua_settable(l, EnumStrings::GetString("ShipTypeThruster", t), st.linThrust[t]); pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, "linearThrust"); lua_pop(l, 1); lua_newtable(l); for (auto it = st.slots.cbegin(); it != st.slots.cend(); ++it) { pi_lua_settable(l, it->first.c_str(), it->second); } pi_lua_readonly_table_proxy(l, -1); luaL_getmetafield(l, -1, "__index"); if (!lua_getmetatable(l, -1)) { lua_newtable(l); } pi_lua_import(l, "EquipSet"); luaL_getsubtable(l, -1, "default"); lua_setfield(l, -3, "__index"); lua_pop(l, 1); lua_setmetatable(l, -2); lua_pop(l, 1); lua_setfield(l, -3, "equipSlotCapacity"); lua_pop(l, 1); lua_newtable(l); for (auto it = st.roles.cbegin(); it != st.roles.cend(); ++it) { pi_lua_settable(l, it->first.c_str(), it->second); } pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, "roles"); lua_pop(l, 1); pi_lua_readonly_table_proxy(l, -1); lua_setfield(l, -3, iter.first.c_str()); lua_pop(l, 1); } lua_getfield(l, LUA_REGISTRYINDEX, "CoreImports"); pi_lua_readonly_table_proxy(l, -2); lua_setfield(l, -2, "ShipDef"); lua_pop(l, 2); LUA_DEBUG_END(l, 0); }