void PiGui::Init(SDL_Window *window) { m_handlers.Unref(); lua_State *l = Lua::manager->GetLuaState(); lua_newtable(l); m_handlers = LuaRef(l, -1); lua_newtable(l); m_keys = LuaRef(l, -1); LuaTable keys(l, -1); for(auto p : keycodes) { keys.Set(p.first, p.second); } ImGui_ImplSdlGL3_Init(window); ImGuiIO &io = ImGui::GetIO(); static unsigned short glyph_ranges[] = { 0x1, 0x3c0, 0x0, 0x0 }; pionillium12 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "PionilliumText22L-Medium.ttf").c_str(), 12.0f, nullptr, glyph_ranges); pionillium15 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "PionilliumText22L-Medium.ttf").c_str(), 15.0f, nullptr, glyph_ranges); pionillium18 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "PionilliumText22L-Medium.ttf").c_str(), 18.0f, nullptr, glyph_ranges); pionillium30 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "PionilliumText22L-Medium.ttf").c_str(), 30.0f, nullptr, glyph_ranges); pionillium36 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "PionilliumText22L-Medium.ttf").c_str(), 36.0f, nullptr, glyph_ranges); orbiteer18 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "Orbiteer-Bold.ttf").c_str(), 18.0f, nullptr, glyph_ranges); orbiteer30 = io.Fonts->AddFontFromFileTTF(FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "fonts"), "Orbiteer-Bold.ttf").c_str(), 30.0f, nullptr, glyph_ranges); }
LuaRef registerClass(lua_State* state)const { util::ScopedSavedStack save(state); if (class_userdata::newmetatable<class_type>(state)) { LuaTable metatable(state, StackTop()); metatable.push(); registerMember(state); if (!traits::is_same<base_class_type, void>::value || !property_map_.empty())//if base class has property and derived class hasnt property. need property access metamethod { for (PropMapType::const_iterator it = property_map_.begin(); it != property_map_.end(); ++it) { int count = lua_type_traits<FunctorType>::push(state, it->second); if (count > 1) { lua_pop(state, count - 1); count = 1; } if (count == 1) { lua_setfield(state, -2, ("_prop_" + it->first).c_str()); } } LuaFunction indexfun = kaguya::LuaFunction::loadstring(state, "local arg = {...};local metatable = arg[1];" "return function(table, index)" // " if type(table) == 'userdata' then " " local propfun = metatable['_prop_'..index];" " if propfun then return propfun(table) end " // " end " " return metatable[index]" " end")(metatable); metatable.setField("__index", indexfun); LuaFunction newindexfn = LuaFunction::loadstring(state, "local arg = {...};local metatable = arg[1];" " return function(table, index, value) " " if type(table) == 'userdata' then " " local propfun = metatable['_prop_'..index];" " if propfun then return propfun(table,value) end " " end " " rawset(table,index,value) " " end")(metatable); metatable.setField("__newindex", newindexfn); } else { metatable.setField("__index", metatable); } set_base_metatable(state, metatable, types::typetag<base_class_type>()); return metatable; } else { except::OtherError(state, typeid(class_type*).name() + std::string(" is already registered")); } return LuaRef(state); }
PropertyMap::PropertyMap(LuaManager *lua) { lua_State *l = lua->GetLuaState(); LUA_DEBUG_START(l); lua_newtable(l); m_table = LuaRef(l, -1); lua_pop(l, 1); LUA_DEBUG_END(l, 0); }
/* * Method: SpawnCargo * * Spawns a container right next to the ship. * * > success = ship:SpawnCargo(item, lifeTime) * * Parameters: * * item - the item to put in the container. * * lifeTime - optional. time in seconds until self destruct. Setting * to 0 sec means no self destruct while player is in system. * * Result: * * success: true if the container was spawned, false otherwise. * * Example: * * > Game.player:SpawnCargo(Equipment.cargo.hydrogen, 0) * > Game.player:SpawnCargo(Equipment.cargo.hydrogen) * * Availability: * * alpha 26 * * Status: * * experimental */ static int l_ship_spawn_cargo(lua_State *l) { Ship *s = LuaObject<Ship>::CheckFromLua(1); if (!lua_istable(l, 2)) { lua_pushboolean(l, false); } CargoBody * c_body; if (lua_gettop(l) >= 3){ float lifeTime = lua_tonumber(l, 3); c_body = new CargoBody(LuaRef(l, 2), lifeTime); } else c_body = new CargoBody(LuaRef(l, 2)); lua_pushboolean(l, s->SpawnCargo(c_body)); return 1; }
/* * Method: SpawnCargo * * Spawns a container right next to the ship. * * > success = ship:SpawnCargo(item) * * Parameters: * * item - the item to put in the container. * * Result: * * success: true if the container was spawned, false otherwise. * * Availability: * * alpha 26 * * Status: * * experimental */ static int l_ship_spawn_cargo(lua_State *l) { Ship *s = LuaObject<Ship>::CheckFromLua(1); if (!lua_istable(l, 2)) { lua_pushboolean(l, false); } else { CargoBody * c_body = new CargoBody(LuaRef(l, 2)); lua_pushboolean(l, s->SpawnCargo(c_body)); } return 1; }
/* * Method: InitiateHyperjumpTo * * Ready the ship to jump to the given system. * * > status = ship:InitiateHyperjumpTo(path, warmup, duration, checks) * * Parameters: * * path - a <SystemPath> for the destination system * * warmup - the time, in seconds, needed for the engines to warm up. * Minimum time is one second, for saftey reasons. * * duration - travel time, in seconds. * * checks - optional. A function that doesn't take any parameter and returns * true as long as all the conditions for hyperspace are met. * * Result: * * status - a <Constants.ShipJumpStatus> string that tells if the ship can * hyperspace and if not, describes the reason * * Availability: * * February 2014 * * Status: * * experimental */ static int l_ship_initiate_hyperjump_to(lua_State *l) { Ship *s = LuaObject<Ship>::CheckFromLua(1); SystemPath *dest = LuaObject<SystemPath>::CheckFromLua(2); int warmup_time = lua_tointeger(l, 3); int duration = lua_tointeger(l, 4); LuaRef checks; if (lua_gettop(l) >= 5) checks = LuaRef(l, 5); Ship::HyperjumpStatus status = s->InitiateHyperjumpTo(*dest, warmup_time, duration, checks); lua_pushstring(l, EnumStrings::GetString("ShipJumpStatus", status)); return 1; }
Context::Context(LuaManager *lua, Graphics::Renderer *renderer, int width, int height, const std::string &lang) : Container(this), m_renderer(renderer), m_width(width), m_height(height), m_scale(std::min(float(m_height)/SCALE_CUTOFF_HEIGHT, 1.0f)), m_needsLayout(false), m_eventDispatcher(this), m_skin("ui/Skin.ini", renderer, GetScale()), m_lua(lua) { lua_State *l = m_lua->GetLuaState(); lua_newtable(l); m_templateStore = LuaRef(l, -1); SetSize(Point(m_width,m_height)); m_visible = true; NewLayer(); // XXX should do point sizes, but we need display DPI first // XXX TextureFont could load multiple sizes into the same object/atlas { const Text::FontDescriptor baseFontDesc(Text::FontDescriptor::Load(FileSystem::gameDataFiles, "fonts/UIFont.ini", lang)); for (int i = FONT_SMALLEST; i <= FONT_LARGEST; i++) { const Text::FontDescriptor fontDesc(baseFontDesc.filename, baseFontDesc.pixelWidth*FONT_SCALE[i]*GetScale(), baseFontDesc.pixelHeight*FONT_SCALE[i]*GetScale(), baseFontDesc.outline, baseFontDesc.advanceXAdjustment); m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(fontDesc, renderer)); } } { const Text::FontDescriptor baseFontDesc(Text::FontDescriptor::Load(FileSystem::gameDataFiles, "fonts/UIHeadingFont.ini", lang)); for (int i = FONT_HEADING_SMALLEST; i <= FONT_HEADING_LARGEST; i++) { const Text::FontDescriptor fontDesc(baseFontDesc.filename, baseFontDesc.pixelWidth*FONT_SCALE[i]*GetScale(), baseFontDesc.pixelHeight*FONT_SCALE[i]*GetScale(), baseFontDesc.outline, baseFontDesc.advanceXAdjustment); m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(fontDesc, renderer)); } } { const Text::FontDescriptor baseFontDesc(Text::FontDescriptor::Load(FileSystem::gameDataFiles, "fonts/UIMonoFont.ini", lang)); for (int i = FONT_MONO_SMALLEST; i <= FONT_MONO_LARGEST; i++) { const Text::FontDescriptor fontDesc(baseFontDesc.filename, baseFontDesc.pixelWidth*FONT_SCALE[i]*GetScale(), baseFontDesc.pixelHeight*FONT_SCALE[i]*GetScale(), baseFontDesc.outline, baseFontDesc.advanceXAdjustment); m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(fontDesc, renderer)); } } m_scissorStack.push(std::make_pair(Point(0,0), Point(m_width,m_height))); }
Context::Context(LuaManager *lua, Graphics::Renderer *renderer, int width, int height) : Container(this), m_renderer(renderer), m_width(width), m_height(height), m_scale(std::min(float(m_height)/SCALE_CUTOFF_HEIGHT, 1.0f)), m_needsLayout(false), m_mousePointer(nullptr), m_mousePointerEnabled(true), m_eventDispatcher(this), m_skin("ui/Skin.ini", renderer, GetScale()), m_lua(lua) { lua_State *l = m_lua->GetLuaState(); lua_newtable(l); m_templateStore = LuaRef(l, -1); SetSize(Point(m_width,m_height)); m_visible = true; NewLayer(); // XXX should do point sizes, but we need display DPI first // XXX TextureFont could load multiple sizes into the same object/atlas { const Text::FontConfig config("UIFont"); for (int i = FONT_SMALLEST; i <= FONT_LARGEST; i++) m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(config, renderer, FONT_SCALE[i]*GetScale())); } { const Text::FontConfig config("UIHeadingFont"); for (int i = FONT_HEADING_SMALLEST; i <= FONT_HEADING_LARGEST; i++) m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(config, renderer, FONT_SCALE[i]*GetScale())); } { const Text::FontConfig config("UIMonoFont"); for (int i = FONT_MONO_SMALLEST; i <= FONT_MONO_LARGEST; i++) m_font[i] = RefCountedPtr<Text::TextureFont>(new Text::TextureFont(config, renderer, FONT_SCALE[i]*GetScale())); } m_scissorStack.push(std::make_pair(Point(0,0), Point(m_width,m_height))); }
void Ship::LoadFromJson(const Json::Value &jsonObj, Space *space) { DynamicBody::LoadFromJson(jsonObj, space); if (!jsonObj.isMember("ship")) throw SavedGameCorruptException(); Json::Value shipObj = jsonObj["ship"]; if (!shipObj.isMember("ang_thrusters")) throw SavedGameCorruptException(); if (!shipObj.isMember("thrusters")) throw SavedGameCorruptException(); if (!shipObj.isMember("wheel_transition")) throw SavedGameCorruptException(); if (!shipObj.isMember("wheel_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("launch_lock_timeout")) throw SavedGameCorruptException(); if (!shipObj.isMember("test_landed")) throw SavedGameCorruptException(); if (!shipObj.isMember("flight_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("alert_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("last_firing_alert")) throw SavedGameCorruptException(); if (!shipObj.isMember("hyperspace_destination")) throw SavedGameCorruptException(); if (!shipObj.isMember("hyperspace_countdown")) throw SavedGameCorruptException(); if (!shipObj.isMember("guns")) throw SavedGameCorruptException(); if (!shipObj.isMember("ecm_recharge")) throw SavedGameCorruptException(); if (!shipObj.isMember("ship_type_id")) throw SavedGameCorruptException(); if (!shipObj.isMember("docked_with_port")) throw SavedGameCorruptException(); if (!shipObj.isMember("index_for_body_docked_with")) throw SavedGameCorruptException(); if (!shipObj.isMember("hull_mass_left")) throw SavedGameCorruptException(); if (!shipObj.isMember("shield_mass_left")) throw SavedGameCorruptException(); if (!shipObj.isMember("shield_cooldown")) throw SavedGameCorruptException(); if (!shipObj.isMember("ai_message")) throw SavedGameCorruptException(); if (!shipObj.isMember("thruster_fuel")) throw SavedGameCorruptException(); if (!shipObj.isMember("reserve_fuel")) throw SavedGameCorruptException(); if (!shipObj.isMember("controller_type")) throw SavedGameCorruptException(); if (!shipObj.isMember("name")) throw SavedGameCorruptException(); m_skin.LoadFromJson(shipObj); m_skin.Apply(GetModel()); // needs fixups JsonToVector(&m_angThrusters, shipObj, "ang_thrusters"); JsonToVector(&m_thrusters, shipObj, "thrusters"); m_wheelTransition = shipObj["wheel_transition"].asInt(); m_wheelState = StrToFloat(shipObj["wheel_state"].asString()); m_launchLockTimeout = StrToFloat(shipObj["launch_lock_timeout"].asString()); m_testLanded = shipObj["test_landed"].asBool(); m_flightState = static_cast<FlightState>(shipObj["flight_state"].asInt()); m_alertState = static_cast<AlertState>(shipObj["alert_state"].asInt()); Properties().Set("flightState", EnumStrings::GetString("ShipFlightState", m_flightState)); Properties().Set("alertStatus", EnumStrings::GetString("ShipAlertStatus", m_alertState)); m_lastFiringAlert = StrToDouble(shipObj["last_firing_alert"].asString()); Json::Value hyperspaceDestObj = shipObj["hyperspace_destination"]; m_hyperspace.dest = SystemPath::FromJson(hyperspaceDestObj); m_hyperspace.countdown = StrToFloat(shipObj["hyperspace_countdown"].asString()); m_hyperspace.duration = 0; Json::Value gunArray = shipObj["guns"]; if (!gunArray.isArray()) throw SavedGameCorruptException(); assert(ShipType::GUNMOUNT_MAX == gunArray.size()); for (unsigned int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { Json::Value gunArrayEl = gunArray[i]; if (!gunArrayEl.isMember("state")) throw SavedGameCorruptException(); if (!gunArrayEl.isMember("recharge")) throw SavedGameCorruptException(); if (!gunArrayEl.isMember("temperature")) throw SavedGameCorruptException(); m_gun[i].state = gunArrayEl["state"].asUInt(); m_gun[i].recharge = StrToFloat(gunArrayEl["recharge"].asString()); m_gun[i].temperature = StrToFloat(gunArrayEl["temperature"].asString()); } m_ecmRecharge = StrToFloat(shipObj["ecm_recharge"].asString()); SetShipId(shipObj["ship_type_id"].asString()); // XXX handle missing thirdparty ship m_dockedWithPort = shipObj["docked_with_port"].asInt(); m_dockedWithIndex = shipObj["index_for_body_docked_with"].asUInt(); Init(); m_stats.hull_mass_left = StrToFloat(shipObj["hull_mass_left"].asString()); // must be after Init()... m_stats.shield_mass_left = StrToFloat(shipObj["shield_mass_left"].asString()); m_shieldCooldown = StrToFloat(shipObj["shield_cooldown"].asString()); m_curAICmd = 0; m_curAICmd = AICommand::LoadFromJson(shipObj); m_aiMessage = AIError(shipObj["ai_message"].asInt()); SetFuel(StrToDouble(shipObj["thruster_fuel"].asString())); m_stats.fuel_tank_mass_left = GetShipType()->fuelTankMass * GetFuel(); m_reserveFuel = StrToDouble(shipObj["reserve_fuel"].asString()); PropertyMap &p = Properties(); p.Set("hullMassLeft", m_stats.hull_mass_left); p.Set("hullPercent", 100.0f * (m_stats.hull_mass_left / float(m_type->hullMass))); p.Set("shieldMassLeft", m_stats.shield_mass_left); p.Set("fuelMassLeft", m_stats.fuel_tank_mass_left); p.PushLuaTable(); lua_State *l = Lua::manager->GetLuaState(); lua_getfield(l, -1, "equipSet"); m_equipSet = LuaRef(l, -1); lua_pop(l, 2); UpdateLuaStats(); m_controller = 0; const ShipController::Type ctype = static_cast<ShipController::Type>(shipObj["controller_type"].asInt()); if (ctype == ShipController::PLAYER) SetController(new PlayerShipController()); else SetController(new ShipController()); m_controller->LoadFromJson(shipObj); m_navLights->LoadFromJson(shipObj); m_shipName = shipObj["name"].asString(); Properties().Set("shipName", m_shipName); }
void Ship::AbortHyperjump() { m_hyperspace.countdown = 0; m_hyperspace.now = false; m_hyperspace.duration = 0; m_hyperspace.checks = LuaRef(); }
LuaRef LuaRef::fromTop( lua_State* L ) { xassert(L != NULL); int handle = lua_ref(L, true); return LuaRef(L, handle); }