From 3643130a8dc94e626f66aea12e418561d3f777a4 Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Sat, 4 Jul 2020 16:19:38 +0300 Subject: [PATCH 1/3] Fix some things with regards to the sector map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - clear route strings when enging the game, otherwise, when the game restarts, a segfault occurs - fix that the "remove jump" button continued to remove jumps even when the route became empty - reordered sections in the object information widget, added icons to the names of stars in the multisystem - it turns out that a hyper jump is possible not only to the stars, but also to the "substellar objects", but they do not return in the method GetStars. Since a specific body should be shown in the route planner, implemented an additional method GetJumpable, that gets all the bodies available for jumping (IsJumpable == true), and this is synchronized with what you can click in the systeminfo view - also I had to change the route check, now it’s not the star index that is checked, but just the path (this check is performed for multisystems, so that only the specific body that registered in the route is always selected) - remove method SectorView::SwapSelectedHyperspaceTarget, because it is not used anywhere - clang satisfaction --- data/pigui/modules/hyperjump-planner.lua | 9 ++- data/pigui/modules/map-sector-view.lua | 70 +++++++++++++----------- src/SectorView.cpp | 32 +++-------- src/SectorView.h | 3 +- src/SystemInfoView.cpp | 2 +- src/galaxy/SystemBody.h | 52 +++++++++--------- src/lua/LuaStarSystem.cpp | 15 +++++ 7 files changed, 98 insertions(+), 85 deletions(-) diff --git a/data/pigui/modules/hyperjump-planner.lua b/data/pigui/modules/hyperjump-planner.lua index 55e01c69a76..2747d422f9a 100644 --- a/data/pigui/modules/hyperjump-planner.lua +++ b/data/pigui/modules/hyperjump-planner.lua @@ -176,6 +176,7 @@ local function updateHyperspaceTarget() sectorView:SetHyperspaceTarget(hyperjump_route[1].path) else sectorView:ResetHyperspaceTarget() + selected_jump = nil end end @@ -225,7 +226,6 @@ local function showJumpRoute() mainButton(icons.retrograde_thin, lui.CLEAR_ROUTE, function() sectorView:ClearRoute() - selected_jump = nil updateHyperspaceTarget() end) ui.sameLine() @@ -361,4 +361,11 @@ function hyperJumpPlanner.onEnterSystem(ship) updateHyperspaceTarget() end +function hyperJumpPlanner.onGameEnd(ship) + -- clear the route out so it doesn't show up if the user starts a new game + sectorView:ClearRoute() + -- also clear the route list, saved in this module + buildJumpRouteList() +end + return hyperJumpPlanner diff --git a/data/pigui/modules/map-sector-view.lua b/data/pigui/modules/map-sector-view.lua index 147ba1981d2..ae379f797ad 100644 --- a/data/pigui/modules/map-sector-view.lua +++ b/data/pigui/modules/map-sector-view.lua @@ -104,11 +104,7 @@ function Windows.systemInfo.Show() local starsystem = systempath:GetStarSystem() local clicked = false ui.withID(label, function() - local jumpData = "" - if not current_systempath:IsSameSystem(systempath) then - local jumpStatus, distance, fuelRequired, duration = player:GetHyperspaceDetails(current_systempath, systempath) - jumpData = jumpStatus .. " " .. string.format("%.2f", distance) .. lc.UNIT_LY .. " " .. fuelRequired .. lc.UNIT_TONNES .. " " .. ui.Format.Duration(duration, 2) - end + -- selected system label textIcon(icons.info) ui.sameLine() ui.text(starsystem.name .. " (" .. math.floor(systempath.sectorX) .. ", " .. math.floor(systempath.sectorY) .. ", " .. math.floor(systempath.sectorZ) .. ")") @@ -119,14 +115,47 @@ function Windows.systemInfo.Show() sectorView:GotoSystemPath(systempath) end end + -- selected system alternative labels + if next(starsystem.other_names) ~= nil then + ui.pushTextWrapPos(ui.getContentRegion().x) + ui.textWrapped(table.concat(starsystem.other_names, ", ")) + ui.popTextWrapPos() + end + -- jump data + if not current_systempath:IsSameSystem(systempath) then ui.separator() - ui.text(jumpData) + local jumpStatus, distance, fuelRequired, duration = player:GetHyperspaceDetails(current_systempath, systempath) + ui.text(jumpStatus .. " " .. string.format("%.2f", distance) .. lc.UNIT_LY .. " " .. fuelRequired .. lc.UNIT_TONNES .. " " .. ui.Format.Duration(duration, 2)) + end + -- description + ui.pushTextWrapPos(ui.getContentRegion().x) + ui.textWrapped(starsystem.shortDescription) + ui.popTextWrapPos() ui.separator() - local stars = starsystem:GetStars() + -- number of stars + local numstars = starsystem.numberOfStars + local numstarstext = "" + if numstars == 4 then + numstarstext = lc.QUADRUPLE_SYSTEM + elseif numstars == 3 then + numstarstext = lc.TRIPLE_SYSTEM + elseif numstars == 2 then + numstarstext = lc.BINARY_SYSTEM + else + numstarstext = starsystem.rootSystemBody.astroDescription + end + ui.text(numstarstext) + -- star list + local stars = starsystem:GetJumpable() for _,star in pairs(stars) do - if ui.selectable(star.name, star.path == systempath, {}) then + local pos = ui.getCursorPos() + Vector2(0, 1) -- add vertical alignment, not quite necessary + if ui.selectable("## " .. star.name, star.path == systempath, {}) then clicked = star.path end + ui.setCursorPos(pos) + textIcon(icons.sun) + ui.sameLine() + ui.text(star.name) end if clicked then sectorView:SwitchToPath(clicked) @@ -138,25 +167,6 @@ function Windows.systemInfo.Show() hyperJumpPlanner.updateInRoute(systempath) prevSystemPath = systempath end - - local numstars = starsystem.numberOfStars - local numstarstext = "" - if numstars == 4 then - numstarstext = lc.QUADRUPLE_SYSTEM - elseif numstars == 3 then - numstarstext = lc.TRIPLE_SYSTEM - elseif numstars == 2 then - numstarstext = lc.BINARY_SYSTEM - else - numstarstext = starsystem.rootSystemBody.astroDescription - end - ui.text(numstarstext) - if next(starsystem.other_names) ~= nil then - ui.text(table.concat(starsystem.other_names, ", ")) - end - ui.pushTextWrapPos(ui.getContentRegion().x) - ui.textWrapped(starsystem.shortDescription) - ui.popTextWrapPos() end) end end @@ -416,11 +426,7 @@ Event.Register("onLeaveSystem", function() hyperspaceDetailsCache = {} end) -- events moved from hyperJumpPlanner -Event.Register("onGameEnd", -function(ship) - -- clear the route out so it doesn't show up if the user starts a new game - sectorView:ClearRoute() -end) +Event.Register("onGameEnd", hyperJumpPlanner.onGameEnd) Event.Register("onEnterSystem", hyperJumpPlanner.onEnterSystem) Event.Register("onShipEquipmentChanged", hyperJumpPlanner.onShipEquipmentChanged) diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 9c726353e90..0dd9b2394bf 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -367,7 +367,7 @@ void SectorView::SetSelected(const SystemPath &path) m_selected = path; else if (path.IsSystemPath()) { RefCountedPtr system = m_galaxy->GetStarSystem(path); - m_selected = system->GetStars()[CheckIndexInRoute(path)]->GetPath(); + m_selected = CheckPathInRoute(system->GetStars()[0]->GetPath()); } } @@ -378,34 +378,18 @@ void SectorView::SwitchToPath(const SystemPath &path) GotoSystem(path); } -void SectorView::SwapSelectedHyperspaceTarget() -{ - SystemPath tmpTarget = GetHyperspaceTarget(); - SetHyperspaceTarget(GetSelected()); - if (m_automaticSystemSelection) { - GotoSystem(tmpTarget); - } else { - RefCountedPtr system = m_galaxy->GetStarSystem(tmpTarget); - SetSelected(system->GetStars()[CheckIndexInRoute(tmpTarget)]->GetPath()); - } -} - void SectorView::OnClickSystem(const SystemPath &path) { SwitchToPath(path); } -// check the route, maybe this system is there, then we'll take star number from there -// if single system, bodyindex = 0; if multisystem, bodyindex = (0 or 1),2 ... -int SectorView::CheckIndexInRoute(const SystemPath &path) +// check the route, maybe this system is there, then we'll take the path from the route +const SystemPath &SectorView::CheckPathInRoute(const SystemPath &path) { - for (auto &p : m_route) - if (p.IsSameSystem(path)) { - if (p.bodyIndex > 0) - return p.bodyIndex - 1; - return 0; - } - return 0; + for (const auto &p : m_route) + if (p.IsSameSystem(path)) + return p; + return path; } void SectorView::PutSystemLabels(RefCountedPtr sec, const vector3f &origin, int drawRadius) @@ -1160,7 +1144,7 @@ void SectorView::Update() if (!m_selected.IsSameSystem(new_selected)) { RefCountedPtr system = m_galaxy->GetStarSystem(new_selected); - SetSelected(system->GetStars()[CheckIndexInRoute(new_selected)]->GetPath()); + SetSelected(CheckPathInRoute(system->GetStars()[0]->GetPath())); } } } diff --git a/src/SectorView.h b/src/SectorView.h index fe06d73e237..7f3d6be7a37 100644 --- a/src/SectorView.h +++ b/src/SectorView.h @@ -45,7 +45,6 @@ class SectorView : public UIView, public DeleteEmitter { void GotoCurrentSystem() { GotoSystem(m_current); } void GotoSelectedSystem() { GotoSystem(m_selected); } void GotoHyperspaceTarget() { GotoSystem(m_hyperspaceTarget); } - void SwapSelectedHyperspaceTarget(); bool IsCenteredOn(const SystemPath &path); void SaveToJson(Json &jsonObj) override; @@ -131,7 +130,7 @@ class SectorView : public UIView, public DeleteEmitter { void AddStarBillboard(const matrix4x4f &modelview, const vector3f &pos, const Color &col, float size); void OnClickSystem(const SystemPath &path); - int CheckIndexInRoute(const SystemPath &path); + const SystemPath &CheckPathInRoute(const SystemPath &path); RefCountedPtr GetCached(const SystemPath &loc) { return m_sectorCache->GetCached(loc); } void ShrinkCache(); diff --git a/src/SystemInfoView.cpp b/src/SystemInfoView.cpp index b99653b9112..21126698760 100644 --- a/src/SystemInfoView.cpp +++ b/src/SystemInfoView.cpp @@ -50,7 +50,7 @@ void SystemInfoView::OnBodySelected(SystemBody *b) Body *body = m_game->GetSpace()->FindBodyForPath(&path); if (body != 0) Pi::player->SetNavTarget(body); - } else if (b->GetSuperType() == SystemBody::SUPERTYPE_STAR) { // We allow hyperjump to any star of the system + } else if (b->IsJumpable()) { m_game->GetSectorView()->SwitchToPath(path); } } diff --git a/src/galaxy/SystemBody.h b/src/galaxy/SystemBody.h index 515535785e6..8ac6711a171 100644 --- a/src/galaxy/SystemBody.h +++ b/src/galaxy/SystemBody.h @@ -52,22 +52,22 @@ class SystemBody : public RefCounted { TYPE_STAR_A_HYPER_GIANT = 28, TYPE_STAR_B_HYPER_GIANT = 29, TYPE_STAR_O_HYPER_GIANT = 30, // these various stars do exist = they are transitional states and are rare - TYPE_STAR_M_WF = 31, //Wolf-Rayet star - TYPE_STAR_B_WF = 32, // while you do not specifically get class M,B or O WF stars, - TYPE_STAR_O_WF = 33, // you do get red = blue and purple from the colour of the gasses = so spectral class is an easy way to define them. - TYPE_STAR_S_BH = 34, //stellar blackhole - TYPE_STAR_IM_BH = 35, //Intermediate-mass blackhole - TYPE_STAR_SM_BH = 36, //Supermassive blackhole + TYPE_STAR_M_WF = 31, //Wolf-Rayet star + TYPE_STAR_B_WF = 32, // while you do not specifically get class M,B or O WF stars, + TYPE_STAR_O_WF = 33, // you do get red = blue and purple from the colour of the gasses = so spectral class is an easy way to define them. + TYPE_STAR_S_BH = 34, //stellar blackhole + TYPE_STAR_IM_BH = 35, //Intermediate-mass blackhole + TYPE_STAR_SM_BH = 36, //Supermassive blackhole TYPE_PLANET_GAS_GIANT = 37, TYPE_PLANET_ASTEROID = 38, TYPE_PLANET_TERRESTRIAL = 39, TYPE_STARPORT_ORBITAL = 40, TYPE_STARPORT_SURFACE = 41, - TYPE_MIN = TYPE_BROWN_DWARF, // + TYPE_MIN = TYPE_BROWN_DWARF, // TYPE_MAX = TYPE_STARPORT_SURFACE, // TYPE_STAR_MIN = TYPE_BROWN_DWARF, // - TYPE_STAR_MAX = TYPE_STAR_SM_BH, // - // XXX need larger atmosphereless thing + TYPE_STAR_MAX = TYPE_STAR_SM_BH, // + // XXX need larger atmosphereless thing }; enum BodySuperType { // @@ -83,6 +83,8 @@ class SystemBody : public RefCounted { bool IsPlanet() const; bool IsMoon() const { return GetSuperType() == SUPERTYPE_ROCKY_PLANET && !IsPlanet(); } + // We allow hyperjump to any star of the system + bool IsJumpable() const { return GetSuperType() == SUPERTYPE_STAR; } bool HasChildren() const { return !m_children.empty(); } Uint32 GetNumChildren() const { return static_cast(m_children.size()); } @@ -98,7 +100,7 @@ class SystemBody : public RefCounted { BodySuperType GetSuperType() const; bool IsCustomBody() const { return m_isCustomBody; } bool IsCoOrbitalWith(const SystemBody *other) const; //this and other form a binary pair - bool IsCoOrbital() const; //is part of any binary pair + bool IsCoOrbital() const; //is part of any binary pair fixed GetRadiusAsFixed() const { return m_radius; } // the aspect ratio adjustment is converting from equatorial to polar radius to account for ellipsoid bodies, used for calculating terrains etc @@ -228,38 +230,38 @@ class SystemBody : public RefCounted { void ClearParentAndChildPointers(); - SystemBody *m_parent; // these are only valid if the StarSystem + SystemBody *m_parent; // these are only valid if the StarSystem std::vector m_children; // that create them still exists SystemPath m_path; Orbit m_orbit; Uint32 m_seed; // Planet.cpp can use to generate terrain std::string m_name; - fixed m_radius; // in earth radii for planets, sol radii for stars. equatorial radius in case of bodies which are flattened at the poles - fixed m_aspectRatio; // ratio between equatorial and polar radius for bodies with eqatorial bulges - fixed m_mass; // earth masses if planet, solar masses if star - fixed m_orbMin, m_orbMax; // periapsism, apoapsis in AUs - fixed m_rotationPeriod; // in days + fixed m_radius; // in earth radii for planets, sol radii for stars. equatorial radius in case of bodies which are flattened at the poles + fixed m_aspectRatio; // ratio between equatorial and polar radius for bodies with eqatorial bulges + fixed m_mass; // earth masses if planet, solar masses if star + fixed m_orbMin, m_orbMax; // periapsism, apoapsis in AUs + fixed m_rotationPeriod; // in days fixed m_rotationalPhaseAtStart; // 0 to 2 pi - fixed m_humanActivity; // 0 - 1 - fixed m_semiMajorAxis; // in AUs + fixed m_humanActivity; // 0 - 1 + fixed m_semiMajorAxis; // in AUs fixed m_eccentricity; fixed m_orbitalOffset; fixed m_orbitalPhaseAtStart; // 0 to 2 pi - fixed m_axialTilt; // in radians - fixed m_inclination; // in radians, for surface bodies = latitude + fixed m_axialTilt; // in radians + fixed m_inclination; // in radians, for surface bodies = latitude int m_averageTemp; BodyType m_type; bool m_isCustomBody; /* composition */ - fixed m_metallicity; // (crust) 0.0 = light (Al, SiO2, etc), 1.0 = heavy (Fe, heavy metals) - fixed m_volatileGas; // 1.0 = earth atmosphere density + fixed m_metallicity; // (crust) 0.0 = light (Al, SiO2, etc), 1.0 = heavy (Fe, heavy metals) + fixed m_volatileGas; // 1.0 = earth atmosphere density fixed m_volatileLiquid; // 1.0 = 100% ocean cover (earth = 70%) - fixed m_volatileIces; // 1.0 = 100% ice cover (earth = 3%) - fixed m_volcanicity; // 0 = none, 1.0 = fucking volcanic + fixed m_volatileIces; // 1.0 = 100% ice cover (earth = 3%) + fixed m_volcanicity; // 0 = none, 1.0 = fucking volcanic fixed m_atmosOxidizing; // 0.0 = reducing (H2, NH3, etc), 1.0 = oxidising (CO2, O2, etc) - fixed m_life; // 0.0 = dead, 1.0 = teeming + fixed m_life; // 0.0 = dead, 1.0 = teeming RingStyle m_rings; diff --git a/src/lua/LuaStarSystem.cpp b/src/lua/LuaStarSystem.cpp index 9e3bc9cacc3..78d9503e2c7 100644 --- a/src/lua/LuaStarSystem.cpp +++ b/src/lua/LuaStarSystem.cpp @@ -316,6 +316,20 @@ static int l_starsystem_get_stars(lua_State *l) return 1; } +static int l_starsystem_get_jumpable(lua_State *l) +{ + const StarSystem *s = LuaObject::CheckFromLua(1); + lua_newtable(l); + int i = 1; + for (RefCountedPtr sb : s->GetBodies()) + if (sb->IsJumpable()) { + lua_pushnumber(l, i++); + LuaObject::PushToLua(sb.Get()); + lua_settable(l, -3); + } + return 1; +} + /* * Method: DistanceTo * @@ -647,6 +661,7 @@ void LuaObject::RegisterClass() { "GetStationPaths", l_starsystem_get_station_paths }, { "GetBodyPaths", l_starsystem_get_body_paths }, { "GetStars", l_starsystem_get_stars }, + { "GetJumpable", l_starsystem_get_jumpable }, { "GetCommodityBasePriceAlterations", l_starsystem_get_commodity_base_price_alterations }, { "IsCommodityLegal", l_starsystem_is_commodity_legal }, From be91a6c71196b7768389a91f5654c514215c2e22 Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Fri, 10 Jul 2020 15:02:25 +0300 Subject: [PATCH 2/3] Add a module for creating message boxes --- data/pigui/libs/message-box.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 data/pigui/libs/message-box.lua diff --git a/data/pigui/libs/message-box.lua b/data/pigui/libs/message-box.lua new file mode 100644 index 00000000000..96db273720b --- /dev/null +++ b/data/pigui/libs/message-box.lua @@ -0,0 +1,25 @@ +-- Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details +-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt + +local ui = require 'pigui' +local ModalWindow = require 'pigui.libs.modal-win' + +local OK_BUTTON_WIDTH = 100 + +local msgbox = {} + +msgbox.OK = function(msg) + ModalWindow.New('PopupMessageBox', function(self) + ui.text(msg) + local width = ui.getContentRegion().x + if width > OK_BUTTON_WIDTH then + ui.dummy(Vector2((width - OK_BUTTON_WIDTH) / 2, 0)) + ui.sameLine() + end + if ui.button("OK", Vector2(OK_BUTTON_WIDTH, 0)) then + self:close() + end + end):open() +end + +return msgbox From 548ce5ec01c39dc0b912348213416770d0163840 Mon Sep 17 00:00:00 2001 From: Gliese852 Date: Fri, 10 Jul 2020 15:30:27 +0300 Subject: [PATCH 3/3] Another 2 fixes to the sector map - fix that Auto Route button crashes game when no hyperdrive fitted. Add 3 possible options - successful route building, no drive, no valid route. Also turn the last static function in LuaSectorView.cpp into a lambda, because it turned out that it was not needed in other functions. - fix that when moving the SectorView camera by entering coordinates to the search field, the camera position gets "locked" to the coordinates and you can't move it (now it moves only when the search bar changes) --- data/lang/ui-core/en.json | 4 +++ data/pigui/modules/hyperjump-planner.lua | 8 ++++- data/pigui/modules/map-sector-view.lua | 4 +-- src/SectorView.cpp | 9 ++++-- src/SectorView.h | 2 +- src/lua/LuaRef.h | 1 + src/lua/LuaSectorView.cpp | 39 ++++++++++++------------ 7 files changed, 41 insertions(+), 26 deletions(-) diff --git a/data/lang/ui-core/en.json b/data/lang/ui-core/en.json index cf73c6c2446..05c938b9d04 100644 --- a/data/lang/ui-core/en.json +++ b/data/lang/ui-core/en.json @@ -1311,6 +1311,10 @@ "description": "", "message": "No missions." }, + "NO_VALID_ROUTE": { + "description": "Message in the sector map, issued by the autoroute function.", + "message": "No valid route to the selected system." + }, "N_LIGHT_YEARS_N_MAX": { "description": "", "message": "{range} light years ({maxRange} max)" diff --git a/data/pigui/modules/hyperjump-planner.lua b/data/pigui/modules/hyperjump-planner.lua index 2747d422f9a..5df15ab53ee 100644 --- a/data/pigui/modules/hyperjump-planner.lua +++ b/data/pigui/modules/hyperjump-planner.lua @@ -10,6 +10,7 @@ local lc = Lang.GetResource("core") local lui = Lang.GetResource("ui-core"); local ui = require 'pigui' +local mb = require 'pigui.libs.message-box' local player = nil local colors = ui.theme.colors @@ -232,7 +233,12 @@ local function showJumpRoute() mainButton(icons.hyperspace, lui.AUTO_ROUTE, function() - sectorView:AutoRoute() + local result = sectorView:AutoRoute() + if result == "NO_DRIVE" then + mb.OK(lui.NO_DRIVE) + elseif result == "NO_VALID_ROUTE" then + mb.OK(lui.NO_VALID_ROUTE) + end updateHyperspaceTarget() end) ui.sameLine() diff --git a/data/pigui/modules/map-sector-view.lua b/data/pigui/modules/map-sector-view.lua index ae379f797ad..b8f9c4038e4 100644 --- a/data/pigui/modules/map-sector-view.lua +++ b/data/pigui/modules/map-sector-view.lua @@ -278,8 +278,8 @@ function Windows.searchBar.Show() ui.text(lc.SEARCH) search_text, changed = ui.inputText("", search_text, {}) if search_text ~= "" then - local parsedSystem = SystemPath.ParseString(search_text) - if parsedSystem ~= nil then + local parsedSystem = changed and SystemPath.ParseString(search_text) + if parsedSystem and parsedSystem ~= nil then sectorView:GotoSectorPath(parsedSystem) else local systempaths = sectorView:SearchNearbyStarSystemsByName(search_text) diff --git a/src/SectorView.cpp b/src/SectorView.cpp index 0dd9b2394bf..5056853fce8 100644 --- a/src/SectorView.cpp +++ b/src/SectorView.cpp @@ -620,13 +620,16 @@ std::vector SectorView::GetRoute() return m_route; } -void SectorView::AutoRoute(const SystemPath &start, const SystemPath &target, std::vector &outRoute) const +const std::string SectorView::AutoRoute(const SystemPath &start, const SystemPath &target, std::vector &outRoute) const { const RefCountedPtr start_sec = m_galaxy->GetSector(start); const RefCountedPtr target_sec = m_galaxy->GetSector(target); + LuaRef try_hdrive = LuaObject::CallMethod(Pi::player, "GetEquip", "engine", 1); + if (try_hdrive.IsNil()) + return "NO_DRIVE"; // Get the player's hyperdrive from Lua, later used to calculate the duration between systems - const ScopedTable hyperdrive = ScopedTable(LuaObject::CallMethod(Pi::player, "GetEquip", "engine", 1)); + const ScopedTable hyperdrive = ScopedTable(try_hdrive); // Cache max range so it doesn't get recalculated every time we call GetDuration const float max_range = hyperdrive.CallMethod("GetMaximumRange", Pi::player); @@ -748,7 +751,9 @@ void SectorView::AutoRoute(const SystemPath &start, const SystemPath &target, st u = path_prev[u]; } std::reverse(std::begin(outRoute), std::end(outRoute)); + return "OKAY"; } + return "NO_VALID_ROUTE"; } void SectorView::DrawRouteLines(const vector3f &playerAbsPos, const matrix4x4f &trans) diff --git a/src/SectorView.h b/src/SectorView.h index 7f3d6be7a37..75a58f85a63 100644 --- a/src/SectorView.h +++ b/src/SectorView.h @@ -75,7 +75,7 @@ class SectorView : public UIView, public DeleteEmitter { bool RemoveRouteItem(const std::vector::size_type element); void ClearRoute(); std::vector GetRoute(); - void AutoRoute(const SystemPath &start, const SystemPath &target, std::vector &outRoute) const; + const std::string AutoRoute(const SystemPath &start, const SystemPath &target, std::vector &outRoute) const; void SetDrawRouteLines(bool value) { m_drawRouteLines = value; } static struct InputBinding : public Input::InputFrame { diff --git a/src/lua/LuaRef.h b/src/lua/LuaRef.h index 33e58422481..55f090a667c 100644 --- a/src/lua/LuaRef.h +++ b/src/lua/LuaRef.h @@ -26,6 +26,7 @@ class LuaRef { lua_State *GetLua() const { return m_lua; } bool IsValid() const { return m_lua && m_id != LUA_NOREF; } + bool IsNil() const { return m_lua && m_id == LUA_REFNIL; } void SaveToJson(Json &jsonObj); void LoadFromJson(const Json &jsonObj); diff --git a/src/lua/LuaSectorView.cpp b/src/lua/LuaSectorView.cpp index a1be838ace5..e3c5dea9a3a 100644 --- a/src/lua/LuaSectorView.cpp +++ b/src/lua/LuaSectorView.cpp @@ -6,20 +6,6 @@ #include "LuaVector.h" #include "SectorView.h" -static int l_sectorview_get_route(lua_State *l, SectorView *sv) -{ - std::vector route = sv->GetRoute(); - - lua_newtable(l); - int i = 1; - for (const SystemPath &j : route) { - lua_pushnumber(l, i++); - LuaObject::PushToLua(j); - lua_settable(l, -3); - } - return 1; -} - template <> const char *LuaObject::s_type = "SectorView"; @@ -125,14 +111,27 @@ void LuaObject::RegisterClass() SystemPath current_path = sv->GetCurrent(); SystemPath target_path = sv->GetSelected(); std::vector route; - sv->AutoRoute(current_path, target_path, route); - sv->ClearRoute(); - for (auto it = route.begin(); it != route.end(); it++) { - sv->AddToRoute(*it); + const std::string result = sv->AutoRoute(current_path, target_path, route); + if (result == "OKAY") { + sv->ClearRoute(); + for (auto it = route.begin(); it != route.end(); it++) { + sv->AddToRoute(*it); + } } - return l_sectorview_get_route(l, sv); + LuaPush(l, result); + return 1; + }) + .AddFunction("GetRoute", [](lua_State *l, SectorView *sv) { + std::vector route = sv->GetRoute(); + lua_newtable(l); + int i = 1; + for (const SystemPath &j : route) { + lua_pushnumber(l, i++); + LuaObject::PushToLua(j); + lua_settable(l, -3); + } + return 1; }) - .AddFunction("GetRoute", &l_sectorview_get_route) .StopRecording(); LuaObjectBase::CreateClass(&metaType); }