From 6ac12cb7d7664d45b74e76153f986f88b48f5c01 Mon Sep 17 00:00:00 2001 From: T'kael Date: Sat, 22 Jun 2024 13:13:45 -0700 Subject: [PATCH] Refactor and enhance error handling for jump range calculation exceptions. --- DataDefinitions/Ship.cs | 45 ++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/DataDefinitions/Ship.cs b/DataDefinitions/Ship.cs index 5a67b9c289..cc07431f01 100644 --- a/DataDefinitions/Ship.cs +++ b/DataDefinitions/Ship.cs @@ -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; } @@ -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 + { + [ "exception" ] = e, + [ "inputs" ] = new Dictionary + { + [ "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)