Skip to content

Commit

Permalink
Fixes-3 to be squashed
Browse files Browse the repository at this point in the history
- add a caveat for using the CreateInLua function
- do some refactoring in Flow.lua
- add a reset of the view in the system map when entering the system, so
  that there is no hanging pointer to the selected ship
- remove the const qualifier from the arguments of functions and
  methods, where the argument is passed by value (PrecalcPath.*)
  • Loading branch information
Gliese852 committed Apr 26, 2021
1 parent fbe2249 commit f6e1e7c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 80 deletions.
60 changes: 25 additions & 35 deletions data/modules/TradeShips/Flow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,8 @@ Flow.calculateSystemParams = function()
local local_routes_map = {}
for _,routes_for_ship in pairs(local_routes) do
for _, route_for_ship in ipairs(routes_for_ship) do
if local_routes_map[route_for_ship.to] then
local_routes_map[route_for_ship.to] = local_routes_map[route_for_ship.to] + route_for_ship.weight
else
local_routes_map[route_for_ship.to] = route_for_ship.weight
end
local current = local_routes_map[route_for_ship.to] or 0
local_routes_map[route_for_ship.to] = current + route_for_ship.weight
end
end
local max_route_weight = 0
Expand All @@ -226,9 +223,8 @@ Flow.calculateSystemParams = function()
-- it may be necessary to recalculate the flow if the required number of
-- ships turns out to be too large
do
local allowed_ships = nil
for _ = 1, 2 do
local ships_in_space
local ships_in_space
local function calculateFlowForLocalRoutes()
ships_in_space = 0
total_flow = 0
for _, ship_routes in pairs(local_routes) do
Expand All @@ -239,20 +235,17 @@ Flow.calculateSystemParams = function()
ships_in_space = ships_in_space + ship_route.amount
end
end

if allowed_ships then break end -- second pass - don't need to check
-- first pass
-- checking if we shouldn't reduce the number of ships
allowed_ships = utils.asymptote(ships_in_space, Core.MAX_SHIPS, 0.6)
if ships_in_space > allowed_ships then
print(string.format("WARNING: lowering ships flow by %.2f for performance reasons", allowed_ships / ships_in_space))
-- the required amount is too large
-- lowering the flow for performance purposes
flow_per_ship = flow_per_ship * allowed_ships / ships_in_space
else
-- the number of ships may not be changed, the calculation is completed
break
end
end
calculateFlowForLocalRoutes()
-- checking if we shouldn't reduce the number of ships
local allowed_ships = utils.asymptote(ships_in_space, Core.MAX_SHIPS, 0.6)
if ships_in_space > allowed_ships then
print(string.format("WARNING: lowering ships flow by %.2f for performance reasons", allowed_ships / ships_in_space))
-- the required amount is too large
-- lowering the flow for performance purposes
flow_per_ship = flow_per_ship * allowed_ships / ships_in_space
-- recalculate for new conditions
calculateFlowForLocalRoutes()
end
end

Expand Down Expand Up @@ -422,21 +415,18 @@ Flow.spawnInitialShips = function()
-- if we generate ships after jumping into the system, and not when starting a new game,
-- count the number of ships that were transferred to this system from the
-- source system, and reduce the need for generation by this amount
if Core.ships then
for ship, trader in pairs(Core.ships) do
if trader.status == 'hyperspace' then
ships_in_cloud = math.max(0, ships_in_cloud - 1)
-- and also assign a random port for this ship, if there are any ports for it at all
local routes_for_ship = Core.params.local_routes[ship.shipId]
if routes_for_ship then
trader.route = utils.chooseNormalized(routes_for_ship)
else
if Core.ships == nil then Core.ships = {} end
for ship, trader in pairs(Core.ships) do
if trader.status == 'hyperspace' then
ships_in_cloud = math.max(0, ships_in_cloud - 1)
-- and also assign a random port for this ship, if there are any ports for it at all
local routes_for_ship = Core.params.local_routes[ship.shipId]
if routes_for_ship then
trader.route = utils.chooseNormalized(routes_for_ship)
else
-- TODO but what if there are no ports for it?
end
end
end
else
Core.ships = {}
end

-- selection table where the ship will be at the start
Expand Down Expand Up @@ -532,4 +522,4 @@ Flow.updateTradeShipsTable = function()
-- print('updateTSTable:total:'..total..',removed:'..removed)
end

return Flow
return Flow
7 changes: 7 additions & 0 deletions data/pigui/modules/system-view-ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ local onGameStart = function ()
systemView:SetVisibility(show_grid)
end

local onEnterSystem = function (ship)
if ship == Game.player then
Game.systemView:SetVisibility("RESET_VIEW");
end
end

local function textIcon(icon, tooltip)
ui.icon(icon, textIconSize, svColor.FONT, tooltip)
end
Expand Down Expand Up @@ -625,5 +631,6 @@ local function displaySystemViewUI()
end

Event.Register("onGameStart", onGameStart)
Event.Register("onEnterSystem", onEnterSystem)
ui.registerHandler("system-view", ui.makeFullScreenHandler("system-view", displaySystemViewUI))
return {}
2 changes: 2 additions & 0 deletions src/lua/LuaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
// LuaObject<SystemPath>::PushToLua(path);
//
// // Create an object, fully owned by lua
// // WARNING! the lifetime of an object will be determined by the lua garbage
// // collector, so a pointer to it should not be stored in any form on the C++ side
// LuaObject<Ship>::CreateInLua(ship_id); // constructor arguments are passed
//
// Get an object from the Lua stack at index n. Causes a Lua exception if the
Expand Down
64 changes: 32 additions & 32 deletions src/ship/PrecalcPath.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
// Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#include "PrecalcPath.h"
Expand Down Expand Up @@ -37,7 +37,7 @@
// example:
// S1_from_t1_norm() - calculate acceleration path from acceleration time, with no acceleration limit

using val = const double;
using val = double;

static double t_from_dm_norm(val dm, val EV, val F) { return dm * EV / F; }

Expand Down Expand Up @@ -253,29 +253,29 @@ enum class TestMode {
static void compare_with_simulation(
const PrecalcPath &pp,
const TestMode mode, // what kind of calculation we check
const double catch_param, // could be time or path, depending on the mode
double catch_param, // could be time or path, depending on the mode
// route / simulation parameters
const double Stotal, // total path length, m
const double V0, // velocity at start
double Stotal, // total path length, m
double V0, // velocity at start
// internal ship params:
const double EV, // effective exhaust velocity, m/s
const double F, // main (forward) thruster force , N
const double acap, // acceleration limit (forward), m/m^2
const double mass, // full mass at start, kg
const double fuel, // fuel mass at start, kg
const double margin // breaking reserve
double EV, // effective exhaust velocity, m/s
double F, // main (forward) thruster force , N
double acap, // acceleration limit (forward), m/m^2
double mass, // full mass at start, kg
double fuel, // fuel mass at start, kg
double margin // breaking reserve
);
#endif

PrecalcPath::PrecalcPath(
const double Stotal,
const double V0,
const double EV,
const double F,
const double acap,
const double mass,
const double fuel,
const double margin) :
double Stotal,
double V0,
double EV,
double F,
double acap,
double mass,
double fuel,
double margin) :
Stotal(Stotal), V0(V0), EV(EV), F(F), acap(acap), mass(mass), fuel(fuel), margin(margin)
{
// calculate full time
Expand Down Expand Up @@ -303,7 +303,7 @@ PrecalcPath::PrecalcPath(
#endif
}

void PrecalcPath::setDist(const double S)
void PrecalcPath::setDist(double S)
{
m_S = S;
if (m_S < m_S1) {
Expand Down Expand Up @@ -332,7 +332,7 @@ void PrecalcPath::setDist(const double S)
#endif
}

void PrecalcPath::setTime(const double t)
void PrecalcPath::setTime(double t)
{
m_time = t;
if (t < m_t1) {
Expand Down Expand Up @@ -361,7 +361,7 @@ void PrecalcPath::setTime(const double t)
}

#ifdef PRECALCPATH_TESTMODE
static int match_and_report(const char *s, const double x1, const double x2)
static int match_and_report(const char *s, double x1, double x2)
{
const double tolerance = 0.1; // %
const double base = (std::abs(x1) + std::abs(x2)) / 2;
Expand Down Expand Up @@ -395,18 +395,18 @@ static int match_and_report(const char *s, const int x1, const int x2)

static void compare_with_simulation(
const PrecalcPath &pp,
const TestMode mode, // what kind of calculation we check
const double catch_param, // could be time or path, depending on the mode
TestMode mode, // what kind of calculation we check
double catch_param, // could be time or path, depending on the mode
// route / simulation parameters
const double Stotal, // total path length, m
const double V0, // velocity at start
double Stotal, // total path length, m
double V0, // velocity at start
// internal ship params:
const double EV, // effective exhaust velocity, m/s
const double F, // main (forward) thruster force , N
const double acap, // acceleration limit (forward), m/m^2
const double mass, // full mass at start, kg
const double fuel, // fuel mass at start, kg
const double margin // breaking reserve
double EV, // effective exhaust velocity, m/s
double F, // main (forward) thruster force , N
double acap, // acceleration limit (forward), m/m^2
double mass, // full mass at start, kg
double fuel, // fuel mass at start, kg
double margin // breaking reserve
)
{
// one-dimensional space ship flight simulator
Expand Down
26 changes: 13 additions & 13 deletions src/ship/PrecalcPath.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2008-2020 Pioneer Developers. See AUTHORS.txt for details
// Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#pragma once
Expand Down Expand Up @@ -34,14 +34,14 @@ class PrecalcPath {

public:
PrecalcPath(
const double Stotal,
const double V0,
const double EV,
const double F,
const double acap,
const double mass,
const double fuel,
const double margin);
double Stotal,
double V0,
double EV,
double F,
double acap,
double mass,
double fuel,
double margin);

// this function is available after the constructor
double getFullTime() const { return m_t1 + m_t2 + (Stotal - m_S1 - m_S2) / m_Vmax; }
Expand All @@ -56,11 +56,11 @@ class PrecalcPath {

// set current point of path
// by ratio (0.0 .. 1.0) of path completion (in distance)
void setSRatio(const double ratio) { setDist(Stotal * ratio); }
void setSRatio(double ratio) { setDist(Stotal * ratio); }
// by ratio (0.0 .. 1.0) of path completion (in time)
void setTRatio(const double ratio) { setTime(getFullTime() * ratio); }
void setTRatio(double ratio) { setTime(getFullTime() * ratio); }
// by distance from start of path
void setDist(const double S);
void setDist(double S);
// by time from start
void setTime(const double t);
void setTime(double t);
};

0 comments on commit f6e1e7c

Please sign in to comment.