static void _mission_to_table(lua_State *l, const Mission &m) { LUA_DEBUG_START(l); lua_newtable(l); pi_lua_table_ro(l); pi_lua_settable(l, "ref", m.ref); pi_lua_settable(l, "due", m.due); pi_lua_settable(l, "reward", double(m.reward) * 0.01); lua_pushstring(l, "type"); lua_pushstring(l, m.type.c_str()); lua_rawset(l, -3); lua_pushstring(l, "client"); lua_pushstring(l, m.client.c_str()); lua_rawset(l, -3); lua_pushstring(l, "location"); LuaSystemPath::PushToLuaGC(new SystemPath(m.location)); lua_rawset(l, -3); lua_pushstring(l, "status"); lua_pushstring(l, LuaConstants::GetConstantString(l, "MissionStatus", m.status)); lua_rawset(l, -3); LUA_DEBUG_END(l, 1); }
/* * Function: GetDictionary * * Retrieve a Lua table for the current language. * * > dict = Lang.GetDictionary() * > print(dict['WE_HAVE_NO_BUSINESS_WITH_YOU']) * * Return: * * dict - A Lua table mapping language token to translated string. * * Availability: * * alpha 15 * * Status: * * stable */ static int l_lang_get_dictionary(lua_State *l) { LUA_DEBUG_START(l); lua_getfield(l, LUA_REGISTRYINDEX, "LangCoreDictionary"); if (lua_isnil(l, -1)) { lua_pop(l, 1); _build_dictionary_table(l); pi_lua_table_ro(l); lua_pushvalue(l, -1); lua_setfield(l, LUA_REGISTRYINDEX, "LangCoreDictionary"); } LUA_DEBUG_END(l, 1); return 1; }
/* * Method: GetCommodityBasePriceAlterations * * Get the price alterations for cargo items bought and sold in this system * * > alterations = system:GetCommodityBasePriceAlterations() * * Return: * * alterations - a table. The keys are <Constants.EquipType> strings for * each cargo. The values are numbers that indicate the * percentage change to each cargo base price. Loosely, * positive values make the commodity more expensive, * indicating it is in demand, while negative values make the * commodity cheaper, indicating a surplus. * * Availability: * * alpha 10 * * Status: * * experimental */ static int l_starsystem_get_commodity_base_price_alterations(lua_State *l) { LUA_DEBUG_START(l); StarSystem *s = LuaStarSystem::GetFromLua(1); lua_newtable(l); pi_lua_table_ro(l); for (int e = Equip::FIRST_COMMODITY; e <= Equip::LAST_COMMODITY; e++) { lua_pushstring(l, LuaConstants::GetConstantString(l, "EquipType", e)); lua_pushnumber(l, s->GetCommodityBasePriceModPercent(e)); lua_rawset(l, -3); } LUA_DEBUG_END(l, 1); return 1; }
/* * Method: GetBodyPaths * * Get the <SystemPaths> to bodies (planets, stations, starports) in this system * * > paths = system:GetBodyPaths() * * Return: * * paths - an array of <SystemPath> objects, one for each <SystemBody> * * Availability: * * alpha 13 * * Status: * * experimental */ static int l_starsystem_get_body_paths(lua_State *l) { LUA_DEBUG_START(l); StarSystem *s = LuaStarSystem::GetFromLua(1); lua_newtable(l); pi_lua_table_ro(l); for (std::vector<SystemBody*>::const_iterator i = s->m_bodies.begin(); i != s->m_bodies.end(); i++) { lua_pushinteger(l, lua_rawlen(l, -1)+1); LuaSystemPath::PushToLua(&(*i)->path); lua_rawset(l, -3); } LUA_DEBUG_END(l, 1); return 1; }
/* * Method: GetNearbySystems * * Get a list of nearby <StarSystems> that match some criteria * * > systems = system:GetNearbySystems(range, filter) * * Parameters: * * range - distance from this system to search, in light years * * filter - an optional function. If specified the function will be called * once for each candidate system with the <StarSystem> object * passed as the only parameter. If the filter function returns * true then the system will be included in the array returned by * <GetNearbySystems>, otherwise it will be omitted. If no filter * function is specified then all systems in range are returned. * * Return: * * systems - an array of systems in range that matched the filter * * Availability: * * alpha 10 * * Status: * * experimental */ static int l_starsystem_get_nearby_systems(lua_State *l) { LUA_DEBUG_START(l); StarSystem *s = LuaStarSystem::GetFromLua(1); double dist_ly = luaL_checknumber(l, 2); bool filter = false; if (lua_gettop(l) >= 3) { luaL_checktype(l, 3, LUA_TFUNCTION); // any type of function filter = true; } lua_newtable(l); pi_lua_table_ro(l); SystemPath here = s->GetPath(); int here_x = here.sectorX; int here_y = here.sectorY; int here_z = here.sectorZ; Uint32 here_idx = here.systemIndex; Sector here_sec(here_x, here_y, here_z); int diff_sec = int(ceil(dist_ly/Sector::SIZE)); for (int x = here_x-diff_sec; x <= here_x+diff_sec; x++) { for (int y = here_y-diff_sec; y <= here_y+diff_sec; y++) { for (int z = here_z-diff_sec; z <= here_z+diff_sec; z++) { Sector sec(x, y, z); for (unsigned int idx = 0; idx < sec.m_systems.size(); idx++) { if (x == here_x && y == here_y && z == here_z && idx == here_idx) continue; if (Sector::DistanceBetween(&here_sec, here_idx, &sec, idx) > dist_ly) continue; RefCountedPtr<StarSystem> sys = StarSystem::GetCached(SystemPath(x, y, z, idx)); if (filter) { lua_pushvalue(l, 3); LuaStarSystem::PushToLua(sys.Get()); lua_call(l, 1, 1); if (!lua_toboolean(l, -1)) { lua_pop(l, 1); continue; } lua_pop(l, 1); } lua_pushinteger(l, lua_rawlen(l, -1)+1); LuaStarSystem::PushToLua(sys.Get()); lua_rawset(l, -3); } } } } LUA_DEBUG_END(l, 1); return 1; }