Skip to content

Commit

Permalink
Another 2 fixes to the sector map
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
Gliese852 committed Jul 10, 2020
1 parent be91a6c commit 548ce5e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
4 changes: 4 additions & 0 deletions data/lang/ui-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
8 changes: 7 additions & 1 deletion data/pigui/modules/hyperjump-planner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions data/pigui/modules/map-sector-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions src/SectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,16 @@ std::vector<SystemPath> SectorView::GetRoute()
return m_route;
}

void SectorView::AutoRoute(const SystemPath &start, const SystemPath &target, std::vector<SystemPath> &outRoute) const
const std::string SectorView::AutoRoute(const SystemPath &start, const SystemPath &target, std::vector<SystemPath> &outRoute) const
{
const RefCountedPtr<const Sector> start_sec = m_galaxy->GetSector(start);
const RefCountedPtr<const Sector> target_sec = m_galaxy->GetSector(target);

LuaRef try_hdrive = LuaObject<Player>::CallMethod<LuaRef>(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<Player>::CallMethod<LuaRef>(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<float>("GetMaximumRange", Pi::player);

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/SectorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SectorView : public UIView, public DeleteEmitter {
bool RemoveRouteItem(const std::vector<SystemPath>::size_type element);
void ClearRoute();
std::vector<SystemPath> GetRoute();
void AutoRoute(const SystemPath &start, const SystemPath &target, std::vector<SystemPath> &outRoute) const;
const std::string AutoRoute(const SystemPath &start, const SystemPath &target, std::vector<SystemPath> &outRoute) const;
void SetDrawRouteLines(bool value) { m_drawRouteLines = value; }

static struct InputBinding : public Input::InputFrame {
Expand Down
1 change: 1 addition & 0 deletions src/lua/LuaRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 19 additions & 20 deletions src/lua/LuaSectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@
#include "LuaVector.h"
#include "SectorView.h"

static int l_sectorview_get_route(lua_State *l, SectorView *sv)
{
std::vector<SystemPath> route = sv->GetRoute();

lua_newtable(l);
int i = 1;
for (const SystemPath &j : route) {
lua_pushnumber(l, i++);
LuaObject<SystemPath>::PushToLua(j);
lua_settable(l, -3);
}
return 1;
}

template <>
const char *LuaObject<SectorView>::s_type = "SectorView";

Expand Down Expand Up @@ -125,14 +111,27 @@ void LuaObject<SectorView>::RegisterClass()
SystemPath current_path = sv->GetCurrent();
SystemPath target_path = sv->GetSelected();
std::vector<SystemPath> 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<std::string>(l, result);
return 1;
})
.AddFunction("GetRoute", [](lua_State *l, SectorView *sv) {
std::vector<SystemPath> route = sv->GetRoute();
lua_newtable(l);
int i = 1;
for (const SystemPath &j : route) {
lua_pushnumber(l, i++);
LuaObject<SystemPath>::PushToLua(j);
lua_settable(l, -3);
}
return 1;
})
.AddFunction("GetRoute", &l_sectorview_get_route)
.StopRecording();
LuaObjectBase::CreateClass(&metaType);
}

0 comments on commit 548ce5e

Please sign in to comment.