Skip to content

Commit

Permalink
Merge pull request #5721 from JonBooth78/save-hyperspace-route
Browse files Browse the repository at this point in the history
Save and load the current planned hyperjump in the save file
  • Loading branch information
Web-eWorks committed Jan 29, 2024
2 parents 02213d1 + d018a27 commit 72bd4ad
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
3 changes: 3 additions & 0 deletions data/pigui/modules/hyperjump-planner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ end
function hyperJumpPlanner.onGameStart()
-- get notified when the player buys hydrogen
Game.player:GetComponent('CargoManager'):AddListener('hyperjump-planner', hyperJumpPlanner.onPlayerCargoChanged)
-- we may have just loaded a jump route list, so lets build it fresh now
buildJumpRouteList()

end

function hyperJumpPlanner.onGameEnd()
Expand Down
88 changes: 68 additions & 20 deletions data/pigui/views/map-sector-view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local Color = _G.Color
local ui = require 'pigui'
local layout = require 'pigui.libs.window-layout'

local Serializer = require 'Serializer'

local player = nil
local colors = ui.theme.colors
local icons = ui.theme.icons
Expand All @@ -39,10 +41,16 @@ local buttonState = {
[false] = ui.theme.buttonColors.transparent
}

local draw_vertical_lines = false
local draw_out_range_labels = false
local draw_uninhabited_labels = true
local automatic_system_selection = true
local settings =
{
draw_vertical_lines=false,
draw_out_range_labels=false,
draw_uninhabited_labels=true,
automatic_system_selection=true
}

local loaded_data = nil


local function textIcon(icon, tooltip)
ui.icon(icon, Vector2(ui.getTextLineHeight()), svColor.FONT, tooltip)
Expand All @@ -65,13 +73,29 @@ local onGameStart = function ()
hyperJumpPlanner.setSectorView(sectorView)
-- reset hyperspace details cache on new game
hyperspaceDetailsCache = {}

-- apply any data loaded earlier
if loaded_data then
if loaded_data.jump_targets then
local targets = loaded_data.jump_targets
for _, target in pairs( loaded_data.jump_targets) do
sectorView:AddToRoute(target)
end
end
if loaded_data.settings then
settings = loaded_data.settings
end
loaded_data = nil
end

-- update visibility states
sectorView:SetAutomaticSystemSelection(automatic_system_selection)
sectorView:SetDrawOutRangeLabels(draw_out_range_labels)
sectorView:GetMap():SetDrawUninhabitedLabels(draw_uninhabited_labels)
sectorView:GetMap():SetDrawVerticalLines(draw_vertical_lines)
sectorView:SetAutomaticSystemSelection(settings.automatic_system_selection)
sectorView:SetDrawOutRangeLabels(settings.draw_out_range_labels)
sectorView:GetMap():SetDrawUninhabitedLabels(settings.draw_uninhabited_labels)
sectorView:GetMap():SetDrawVerticalLines(settings.draw_vertical_lines)
sectorView:GetMap():SetLabelParams("orbiteer", font.size, 2.0, svColor.LABEL_HIGHLIGHT, svColor.LABEL_SHADE)
-- allow hyperjump planner to register its events

-- allow hyperjump planner to register its events:
hyperJumpPlanner.onGameStart()
end

Expand Down Expand Up @@ -245,21 +269,21 @@ end

local function showSettings()
local changed
changed, draw_vertical_lines = ui.checkbox(lc.DRAW_VERTICAL_LINES, draw_vertical_lines)
changed, settings.draw_vertical_lines = ui.checkbox(lc.DRAW_VERTICAL_LINES, settings.draw_vertical_lines)
if changed then
sectorView:GetMap():SetDrawVerticalLines(draw_vertical_lines)
sectorView:GetMap():SetDrawVerticalLines(settings.draw_vertical_lines)
end
changed, draw_out_range_labels = ui.checkbox(lc.DRAW_OUT_RANGE_LABELS, draw_out_range_labels)
changed, settings.draw_out_range_labels = ui.checkbox(lc.DRAW_OUT_RANGE_LABELS, settings.draw_out_range_labels)
if changed then
sectorView:SetDrawOutRangeLabels(draw_out_range_labels)
sectorView:SetDrawOutRangeLabels(settings.draw_out_range_labels)
end
changed, draw_uninhabited_labels = ui.checkbox(lc.DRAW_UNINHABITED_LABELS, draw_uninhabited_labels)
changed, settings.draw_uninhabited_labels = ui.checkbox(lc.DRAW_UNINHABITED_LABELS, settings.draw_uninhabited_labels)
if changed then
sectorView:GetMap():SetDrawUninhabitedLabels(draw_uninhabited_labels)
sectorView:GetMap():SetDrawUninhabitedLabels(settings.draw_uninhabited_labels)
end
changed, automatic_system_selection = ui.checkbox(lc.AUTOMATIC_SYSTEM_SELECTION, automatic_system_selection)
changed, settings.automatic_system_selection = ui.checkbox(lc.AUTOMATIC_SYSTEM_SELECTION, settings.automatic_system_selection)
if changed then
sectorView:SetAutomaticSystemSelection(automatic_system_selection)
sectorView:SetAutomaticSystemSelection(settings.automatic_system_selection)
end
-- end
end
Expand Down Expand Up @@ -445,9 +469,9 @@ ui.registerModule("game", { id = 'map-sector-view', draw = function()
end})

Event.Register("onGameStart", onGameStart)
Event.Register("onEnterSystem", function()
hyperJumpPlanner.onEnterSystem()
hyperspaceDetailsCache = {}
Event.Register("onEnterSystem", function(ship)
hyperJumpPlanner.onEnterSystem(ship)
if ship:IsPlayer() then hyperspaceDetailsCache = {} end
end)

-- reset cached data
Expand All @@ -468,4 +492,28 @@ Event.Register("onShipTypeChanged", function(ship, ...)
if ship:IsPlayer() then hyperspaceDetailsCache = {} end
end)


local serialize = function ()

local data =
{
version = 1,
jump_targets = {},
settings = settings
}

for jumpIndex, jump_sys in pairs(sectorView:GetRoute()) do
table.insert( data.jump_targets, jump_sys )
end

return data
end

local unserialize = function (data)
loaded_data = data
end

Serializer:Register("HyperJumpPlanner", serialize, unserialize)


return {}
23 changes: 11 additions & 12 deletions src/SectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@

#include "SectorView.h"

#include <assert.h>
#include <cmath>
#include <sstream>
#include "AnimationCurves.h"
#include "Game.h"
#include "GameSaveError.h"
#include "Input.h"
#include "Json.h"
#include "JsonUtils.h"
#include "lua/LuaObject.h"
#include "MathUtil.h"
#include "Pi.h"
#include "Player.h"
Expand All @@ -24,9 +20,13 @@
#include "galaxy/Galaxy.h"
#include "galaxy/Sector.h"
#include "galaxy/StarSystem.h"
#include "lua/LuaObject.h"
#include "lua/LuaRef.h"
#include "lua/LuaTable.h"
#include "matrix4x4.h"
#include <assert.h>
#include <cmath>
#include <sstream>

#include <unordered_set>

Expand Down Expand Up @@ -73,8 +73,10 @@ void SectorView::InputBinding::RegisterBindings()
// callbacks for the SectorMap
class SectorView::SectorMapCallbacks : public SectorMapContext::Callbacks {
SectorView &sv;

public:
SectorMapCallbacks(SectorView &sv) : sv(sv) {}
SectorMapCallbacks(SectorView &sv) :
sv(sv) {}

void OnClickLabel(const SystemPath &clickedLabel) override
{
Expand All @@ -96,9 +98,9 @@ class SectorView::SectorMapCallbacks : public SectorMapContext::Callbacks {

if (dist > sv.m_playerHyperspaceRange) {
if (sv.m_drawOutRangeLabels) {
return DisplayModes::HIDE_LABEL;
} else {
return DisplayModes::SHADOW_LABEL;
} else {
return DisplayModes::HIDE_LABEL;
}
}
return DisplayModes::DEFAULT;
Expand Down Expand Up @@ -139,7 +141,6 @@ SectorView::SectorView(Game *game) :
m_detailBoxVisible = DETAILBOX_INFO;

InitObject();

}

SectorView::SectorView(const Json &jsonObj, Game *game) :
Expand Down Expand Up @@ -195,7 +196,7 @@ void SectorView::InitObject()
});
m_onViewReset =
InputBindings.mapViewReset->onPressed.connect([&]() {
m_map->ResetView();
m_map->ResetView();
});
}

Expand Down Expand Up @@ -242,7 +243,7 @@ void SectorView::Draw3D()
// prior to modelview transformation, rotate in the opposite direction so
// that the billboard is always facing the camera
matrix4x4f rot = modelview;
rot.ClearToRotOnly();
rot.ClearToRotOnly();
rot = rot.Inverse();
// move this disk 0.03 light years further so that it does not overlap the star, and selected indicator and hyperspace target indicator
m_map->AddStarBillboard(trans * rot, vector3f(0.f, 0.f, -0.03f), Color(0, 0, 204), 1.5f);
Expand Down Expand Up @@ -272,7 +273,6 @@ void SectorView::Draw3D()

// actually rendering
m_map->Draw3D();

}

void SectorView::DrawPiGui()
Expand Down Expand Up @@ -641,7 +641,6 @@ void SectorView::Update()
}

m_playerHyperspaceRange = LuaObject<Player>::CallMethod<float>(Pi::player, "GetHyperspaceRange");

}

void SectorView::ResetView()
Expand Down

0 comments on commit 72bd4ad

Please sign in to comment.