Skip to content

Commit

Permalink
Refactor and enhance error handling for jump range calculation except…
Browse files Browse the repository at this point in the history
…ions.
  • Loading branch information
Tkael committed Jun 22, 2024
1 parent 289b5b7 commit 6ac12cb
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions DataDefinitions/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ public JumpDetail JumpDetails(string type, decimal? fuelInTanksOverride = null,
return null;
}

private decimal JumpRange ( decimal currentFuel, int carriedCargo, double boostModifier = 1)
private decimal JumpRange ( decimal currentFuel, int carriedCargo, double boostModifier = 1 )
{
if ( frameshiftdrive is null || unladenmass == 0 ) { return 0; }

Expand All @@ -738,15 +738,42 @@ private decimal JumpRange ( decimal currentFuel, int carriedCargo, double boostM
var powerConstant = frameshiftdrive.GetFsdPowerConstant();
var guardianFsdBoosterRange = compartments.FirstOrDefault(c => c.module.edname.Contains("Int_GuardianFSDBooster"))?.module?.GetGuardianFSDBoost() ?? 0;

// Calculate our base max range
var baseMaxRange = optimalMass / mass * Math.Pow( ( fuel * 1000 / linearConstant ), ( 1 / powerConstant ) );
if ( baseMaxRange == 0 ) { return 0; }
return JumpRange( optimalMass, mass, fuel, linearConstant, powerConstant, guardianFsdBoosterRange, boostModifier );
}

// Return the maximum range with the specified fuel and cargo levels, with a boost modifier if using synthesis or a jet cone boost
var guardianBoostedMaxRange = ( baseMaxRange + guardianFsdBoosterRange ) / baseMaxRange * optimalMass / mass * Math.Pow( ( fuel * 1000 / linearConstant ), ( 1 / powerConstant ) );

var result = Convert.ToDecimal(guardianBoostedMaxRange * boostModifier);
return result;
private decimal JumpRange ( double optimalMass, double mass, double fuel, double linearConstant, double powerConstant, double guardianFsdBoosterRange, double boostModifier )
{
try
{
// Calculate our base max range
var baseMaxRange = optimalMass / mass * Math.Pow( ( fuel * 1000 / linearConstant ), ( 1 / powerConstant ) );
if ( baseMaxRange == 0 ) { return 0; }

// Return the maximum range with the specified fuel and cargo levels, with a boost modifier if using synthesis or a jet cone boost
var guardianBoostedMaxRange = ( baseMaxRange + guardianFsdBoosterRange ) / baseMaxRange * optimalMass / mass * Math.Pow( ( fuel * 1000 / linearConstant ), ( 1 / powerConstant ) );

var result = Convert.ToDecimal( guardianBoostedMaxRange * boostModifier );
return result;
}
catch ( Exception e )
{
var data = new Dictionary<string, object>
{
[ "exception" ] = e,
[ "inputs" ] = new Dictionary<string, object>
{
[ "optimalMass" ] = optimalMass,
[ "mass" ] = mass,
[ "fuel" ] = fuel,
[ "linearConstant" ] = linearConstant,
[ "powerConstant" ] = powerConstant,
[ "guardianFsdBoosterRange" ] = guardianFsdBoosterRange,
[ "boostModifier" ] = boostModifier
}
};
Logging.Error( "Failed to calculate jump range", data );
return 0;
}
}

public static Ship FromShipyardInfo(ShipyardInfoItem item)
Expand Down

0 comments on commit 6ac12cb

Please sign in to comment.