Skip to content

Commit

Permalink
Further refine fuel calcs, fuel calc unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tkael committed May 21, 2024
1 parent 8955c86 commit 4d94822
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
14 changes: 9 additions & 5 deletions DataDefinitions/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ public List<LaunchBay> launchbays

public decimal activeFuelReservoirCapacity { get; set; }

public decimal? fuelInReservoir { get; set; }

// Ship jump and mass properties

[PublicAPI]
Expand Down Expand Up @@ -676,7 +678,7 @@ public void Augment()

public JumpDetail JumpDetails(string type, decimal? fuelInTanksOverride = null, int? cargoCarriedOverride = null)
{
var currentFuel = fuelInTanksOverride ?? fuelInTanks ?? 0;
var currentFuel = (fuelInTanksOverride ?? fuelInTanks ?? 0);
var cargoTonnage = cargoCarriedOverride ?? cargoCarried;

if (!string.IsNullOrEmpty(type))
Expand All @@ -685,7 +687,8 @@ public JumpDetail JumpDetails(string type, decimal? fuelInTanksOverride = null,
{
case "next":
{
decimal jumpRange = JumpRange( currentFuel + activeFuelReservoirCapacity, cargoTonnage );
currentFuel += fuelInReservoir ?? activeFuelReservoirCapacity;
decimal jumpRange = JumpRange( currentFuel, cargoTonnage );
return new JumpDetail(jumpRange, 1);
}
case "max":
Expand All @@ -695,24 +698,25 @@ public JumpDetail JumpDetails(string type, decimal? fuelInTanksOverride = null,
}
case "total":
{
currentFuel += fuelInReservoir ?? activeFuelReservoirCapacity;
decimal total = 0;
int jumps = 0;
while (currentFuel > 0)
{
total += JumpRange( Math.Min( currentFuel + activeFuelReservoirCapacity, maxfuelperjump ), cargoTonnage );
total += JumpRange( Math.Min( currentFuel, maxfuelperjump ), cargoTonnage );
jumps++;
currentFuel -= Math.Min( currentFuel, maxfuelperjump );
}
return new JumpDetail(total, jumps);
}
case "full":
{
currentFuel = fueltanktotalcapacity ?? 0;
currentFuel = ( fueltanktotalcapacity ?? 0 ) + activeFuelReservoirCapacity;
decimal total = 0;
int jumps = 0;
while ( currentFuel > 0)
{
total += JumpRange( Math.Min( currentFuel + activeFuelReservoirCapacity, maxfuelperjump ), cargoTonnage );
total += JumpRange( Math.Min( currentFuel, maxfuelperjump ), cargoTonnage );
jumps++;
currentFuel -= Math.Min( currentFuel, maxfuelperjump );
}
Expand Down
1 change: 1 addition & 0 deletions StatusMonitor/StatusMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private void HandleStatus(object sender, EventArgs e)
{
EDDI.Instance.CurrentShip.cargoCarried = thisStatus.cargo_carried ?? 0;
EDDI.Instance.CurrentShip.fuelInTanks = thisStatus.fuelInTanks ?? 0;
EDDI.Instance.CurrentShip.fuelInReservoir = thisStatus.fuelInReservoir ?? 0;
}

// Trigger events for changed status, as applicable
Expand Down
34 changes: 17 additions & 17 deletions Tests/ShipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ public void TestLoadoutParsingEmpireTrader()
Assert.AreEqual( 1547.139526, ship.frameshiftdrive.GetFsdOptimalMass() );
Assert.AreEqual( 5.199397M, ship.maxfuelperjump );
// With zero fuel and zero cargo
Assert.AreEqual( 0, ship.JumpDetails( "next" ).distance );
Assert.AreEqual( 34.403M, Math.Round(ship.JumpDetails( "max" ).distance, 3) );
Assert.AreEqual( 0, ship.JumpDetails( "total" ).distance );
Assert.AreEqual( 165.947M, Math.Round(ship.JumpDetails( "full" ).distance, 3) );
Assert.AreEqual( 15.654M, Math.Round( ship.JumpDetails( "next" ).distance, 3 ) );
Assert.AreEqual( 34.403M, Math.Round (ship.JumpDetails( "max" ).distance, 3) );
Assert.AreEqual( 15.654M, Math.Round( ship.JumpDetails( "total" ).distance, 3 ) );
Assert.AreEqual( 168.413M, Math.Round(ship.JumpDetails( "full" ).distance, 3) );
// With with max fuel and zero cargo
Assert.AreEqual( 33.720M, Math.Round(ship.JumpDetails( "next", 16 ).distance), 3 );
Assert.AreEqual( 34.403M, Math.Round( ship.JumpDetails( "max", 16 ).distance, 3 ) );
Assert.AreEqual( 115.418M, Math.Round( ship.JumpDetails( "total", 16 ).distance, 3 ) );
Assert.AreEqual( 165.947M, Math.Round( ship.JumpDetails( "full", 16 ).distance, 3 ) );
Assert.AreEqual( 121.881M, Math.Round( ship.JumpDetails( "total", 16 ).distance, 3 ) );
Assert.AreEqual( 168.413M, Math.Round( ship.JumpDetails( "full", 16 ).distance, 3 ) );
// With with max fuel and max cargo
Assert.AreEqual( 30.734M, Math.Round( ship.JumpDetails( "next", 16, 64 ).distance ), 3 );
Assert.AreEqual( 30.734M, Math.Round( ship.JumpDetails( "max", 16, 64 ).distance, 3 ) );
Assert.AreEqual( 103.096M, Math.Round( ship.JumpDetails( "total", 16, 64 ).distance, 3 ) );
Assert.AreEqual( 148.236M, Math.Round( ship.JumpDetails( "full", 16, 64 ).distance, 3 ) );
Assert.AreEqual( 108.867M, Math.Round( ship.JumpDetails( "total", 16, 64 ).distance, 3 ) );
Assert.AreEqual( 150.442M, Math.Round( ship.JumpDetails( "full", 16, 64 ).distance, 3 ) );
}

[TestMethod]
Expand Down Expand Up @@ -176,20 +176,20 @@ public void TestLoadoutParsingPythonNX ()
Assert.AreEqual( 1175, ship.frameshiftdrive.GetFsdOptimalMass() );
Assert.AreEqual( 5.2M, ship.maxfuelperjump );
// With zero fuel and zero cargo
Assert.AreEqual( 0, ship.JumpDetails( "next" ).distance );
Assert.AreEqual( 9.237M, Math.Round( ship.JumpDetails( "next" ).distance, 3 ) );
Assert.AreEqual( 19.412M, Math.Round( ship.JumpDetails( "max" ).distance, 3 ) );
Assert.AreEqual( 0, ship.JumpDetails( "total" ).distance );
Assert.AreEqual( 65.096M, Math.Round( ship.JumpDetails( "full" ).distance, 3 ) );
Assert.AreEqual( 9.237M, Math.Round( ship.JumpDetails( "total" ).distance, 3 ) );
Assert.AreEqual( 69.074M, Math.Round( ship.JumpDetails( "full" ).distance, 3 ) );
// With with max fuel and zero cargo
Assert.AreEqual( 19.116M, Math.Round( ship.JumpDetails( "next", 16 ).distance, 3 ) );
Assert.AreEqual( 19.094M, Math.Round( ship.JumpDetails( "next", 16 ).distance, 3 ) );
Assert.AreEqual( 19.412M, Math.Round( ship.JumpDetails( "max", 16 ).distance, 3 ) );
Assert.AreEqual( 65.096M, Math.Round( ship.JumpDetails( "total", 16 ).distance, 3 ) );
Assert.AreEqual( 65.096M, Math.Round( ship.JumpDetails( "full", 16 ).distance, 3 ) );
Assert.AreEqual( 69.074M, Math.Round( ship.JumpDetails( "total", 16 ).distance, 3 ) );
Assert.AreEqual( 69.074M, Math.Round( ship.JumpDetails( "full", 16 ).distance, 3 ) );
// With with max fuel and max cargo
Assert.AreEqual( 18.903M, Math.Round( ship.JumpDetails( "next", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 18.881M, Math.Round( ship.JumpDetails( "next", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 19.192M, Math.Round( ship.JumpDetails( "max", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 64.358M, Math.Round( ship.JumpDetails( "total", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 64.358M, Math.Round( ship.JumpDetails( "full", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 68.291M, Math.Round( ship.JumpDetails( "total", 16, 8 ).distance, 3 ) );
Assert.AreEqual( 68.291M, Math.Round( ship.JumpDetails( "full", 16, 8 ).distance, 3 ) );
}

[TestMethod]
Expand Down

0 comments on commit 4d94822

Please sign in to comment.