Skip to content

Commit

Permalink
Fix for ship's properties
Browse files Browse the repository at this point in the history
1. The ship's properties "totalCargo" and "usedCargo" were reinitialized by CargoManager's constructor to zero;
2. maxHyperspaceRange would be zero after save load.
  • Loading branch information
Max5377 committed Oct 7, 2023
1 parent 1957b8d commit 6c58e39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
13 changes: 10 additions & 3 deletions data/libs/CargoManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ function CargoManager:Constructor(ship)
self.usedCargoMass = 0

-- Initialize property variables on owning ship for backwards compatibility
ship:setprop("totalCargo", self:GetTotalSpace())
ship:setprop("usedCargo", 0)
-- don't initialize them if they're already created after first save
if not self.ship:hasprop("totalCargo") then
ship:setprop("totalCargo", self:GetTotalSpace())
end

if not self.ship:hasprop("usedCargo") then
ship:setprop("usedCargo", 0)
end

-- TODO: stored commodities should be represented as array of { name, count, meta } entries
-- to allow for e.g. tracking stolen/scooped cargo, or special mission-related cargoes
Expand Down Expand Up @@ -99,7 +105,7 @@ end
function CargoManager:AddCommodity(type, count)
-- TODO: use a cargo volume metric with variable mass instead of fixed 1m^3 == 1t
local required_space = (type.mass or 1) * (count or 1)

if self:GetFreeSpace() < required_space then
return false
end
Expand Down Expand Up @@ -245,6 +251,7 @@ end
function CargoManager:Unserialize()
setmetatable(self, CargoManager.meta)
self.listeners = {}

return self
end

Expand Down
23 changes: 18 additions & 5 deletions src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ void Ship::UpdateLuaStats()
m_stats.hyperspace_range = m_stats.hyperspace_range_max = 0;
int hyperclass = p.Get("hyperclass_cap");
if (hyperclass) {
std::tie(m_stats.hyperspace_range_max, m_stats.hyperspace_range) =
std::tie(m_stats.hyperspace_range, m_stats.hyperspace_range_max) =
LuaObject<Ship>::CallMethod<double, double>(this, "GetHyperspaceRange");
}

Expand Down Expand Up @@ -1256,10 +1256,23 @@ void Ship::StaticUpdate(const float timeStep)
const vector3d vdir = GetVelocity().Normalized();
const vector3d pdir = -GetOrient().VectorZ();
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);
const double speed_times_density = speed * density;
/*
* speed = m/s, density = g/cm^3 -> T/m^3, pressure = Pa -> N/m^2 -> kg/(m*s^2)
* m T kg m*kg^2 kg^2
* - * --- * ----- = 1000 ------- = 1000 -------
* s m^3 m*s^2 m^4*s^3 m^3*s^3
*
* fuel_scoop_cap = area, m^2. rate = kg^2/(m*s^3) = (Pa*kg)/s^2
*/
const double hydrogen_density = 0.0002;
if ((m_stats.free_capacity) && (dot > 0.90) && speed_times_density > (100.0 * 0.3)) {
const double rate = speed_times_density * hydrogen_density * double(m_stats.fuel_scoop_cap);
m_hydrogenScoopedAccumulator += rate * timeStep;
if (m_hydrogenScoopedAccumulator > 1) {
const double scoopedTons = floor(m_hydrogenScoopedAccumulator);
LuaEvent::Queue("onShipScoopFuel", this, p, scoopedTons);
m_hydrogenScoopedAccumulator -= scoopedTons;
}
}
}
Expand Down

0 comments on commit 6c58e39

Please sign in to comment.