From 4a374e46aeaa1be47c980ae7f164a3139b09699e Mon Sep 17 00:00:00 2001 From: Fiora Date: Sun, 25 Jun 2023 17:00:30 -0700 Subject: [PATCH] Fix WarpToFundTarget with high incomes (#2064) The algorithm used to detect a fund target failed when daily income became relatively large, which caused it to error out towards the mid to late game. With the new algorithm, it will warp to the soonest time that it determines the listed amount of money will be available, even if it's very slightly above the desired fund target. --- .../BuildItems/FundTarget.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Source/KerbalConstructionTime/BuildItems/FundTarget.cs b/Source/KerbalConstructionTime/BuildItems/FundTarget.cs index 0c9bbf81921..6da788a63e2 100644 --- a/Source/KerbalConstructionTime/BuildItems/FundTarget.cs +++ b/Source/KerbalConstructionTime/BuildItems/FundTarget.cs @@ -48,23 +48,27 @@ public double GetTimeLeft() double totalFunds = 0d; // outside the loop since we'll error-check this value. double time = -1; // outside the loop, this is what we'll return. + double bestTime = -1; for (int i = MaxIterations; i-- > 0 && timeUpper - timeLower > EpsilonTime;) { time = (timeUpper + timeLower) * 0.5d; // This is the post-CMQ delta. double fundDelta = KCTGameStates.GetBudgetDelta(time); totalFunds = baseFunds + fundDelta; - if (System.Math.Abs(targetFunds - totalFunds) < EpsilonFunds) - return time; - if (totalFunds > targetFunds) + if (totalFunds >= targetFunds) + { timeUpper = time; + bestTime = time; + } else + { timeLower = time; + } } - if (System.Math.Abs(targetFunds - totalFunds) < EpsilonFunds) - return time; + if (bestTime > 0) + return bestTime; return -1d; }