Skip to content

Commit

Permalink
fuel scooping: improve algorithm
Browse files Browse the repository at this point in the history
Surprisingly, time acceleration did not affect fuel scooping rate.

But if it was, scooping rate should be less than 1.0f to keep it
efficient since we have a random [0, 1) number compared with it.

In case of fuel_scoop_cap == 3, time acceleration == 10,
(speed * density > 10000) will bring scooping rate > 1.0f.

Enforcing time acceleration even further makes scooping much worse.
  • Loading branch information
Mc-Pain committed Sep 20, 2023
1 parent 1957b8d commit 01566db
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions data/modules/FuelScoop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ local lc = Lang.GetResource("core")
-- These callbacks are triggered from C++ or Lua and are responsible for
-- providing UI feedback about the scooping process.

Event.Register("onShipScoopFuel", function(ship, body)
Event.Register("onShipScoopFuel", function(ship, body, amount)
---@type CargoManager
local cargoMgr = ship:GetComponent('CargoManager')

cargoMgr:AddCommodity(Commodities.hydrogen, 1)
cargoMgr:AddCommodity(Commodities.hydrogen, amount)

if ship == Game.player then
Game.AddCommsLogLine(lc.FUEL_SCOOP_ACTIVE_N_TONNES_H_COLLECTED:gsub('%%quantity',
Expand Down
7 changes: 5 additions & 2 deletions src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,11 @@ void Ship::StaticUpdate(const float timeStep)
const double dot = vdir.Dot(pdir);
if ((m_stats.free_capacity) && (dot > 0.90) && (speed > 100.0) && (density > 0.3)) {
const double rate = speed * density * 0.00000333 * double(m_stats.fuel_scoop_cap);
if (Pi::rng.Double() < rate) {
LuaEvent::Queue("onShipScoopFuel", this, p);
m_hydrogenScoopedAccumulator += rate * Pi::game->GetTimeAccelRate();
if (m_hydrogenScoopedAccumulator > 1) {
const double scoopedTons = floor(m_hydrogenScoopedAccumulator);
LuaEvent::Queue("onShipScoopFuel", this, p, scoopedTons);
m_hydrogenScoopedAccumulator -= scoopedTons;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ class Ship : public DynamicBody {

std::string m_shipName;

double m_hydrogenScoopedAccumulator = 0;

public:
// FIXME: these methods are deprecated; all calls should use the propulsion object directly.
void ClearAngThrusterState() { m_propulsion->ClearAngThrusterState(); }
Expand Down

0 comments on commit 01566db

Please sign in to comment.