From 2f00d6def39a0fad713b8210dd7d82268bbe7728 Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Mon, 6 May 2024 19:50:56 -0400 Subject: [PATCH] research.cpp: When upgrade change value is negative, use iDivFloor --- src/research.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/research.cpp b/src/research.cpp index 20b3cee465e..2b49d749405 100644 --- a/src/research.cpp +++ b/src/research.cpp @@ -663,6 +663,14 @@ static inline int64_t iDivCeil(int64_t dividend, int64_t divisor) return (dividend / divisor) + static_cast((dividend % divisor != 0 && hasPosQuotient)); } +static inline int64_t iDivFloor(int64_t dividend, int64_t divisor) +{ + ASSERT_OR_RETURN(0, divisor != 0, "Divide by 0"); + bool hasNegQuotient = (dividend >= 0) != (divisor >= 0); + // C++11 defines the behavior of % to be truncated + return (dividend / divisor) - static_cast((dividend % divisor != 0 && hasNegQuotient)); +} + static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRUCTURE *psStruct, int player) { if (cachedStatsObject.is_null()) { cachedStatsObject = wzapi::constructStatsObject(); } @@ -789,7 +797,8 @@ static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRU continue; } int64_t currentUpgradesValue = currentUpgradesValue_json.get(); - int64_t newUpgradesChange = iDivCeil((statsOriginalValueX.get() * value), 100); + int64_t scaledChange = (statsOriginalValueX.get() * value); + int64_t newUpgradesChange = (value < 0) ? iDivFloor(scaledChange, 100) : iDivCeil(scaledChange, 100); int64_t newUpgradesValue = (currentUpgradesValue + newUpgradesChange); if (currentUpgradesValue_json.is_number_unsigned()) { @@ -820,7 +829,8 @@ static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRU continue; } int64_t currentUpgradesValue = currentUpgradesValue_json.get(); - int64_t newUpgradesChange = iDivCeil((statsOriginalValue * value), 100); + int64_t scaledChange = (statsOriginalValue * value); + int64_t newUpgradesChange = (value < 0) ? iDivFloor(scaledChange, 100) : iDivCeil(scaledChange, 100); int64_t newUpgradesValue = (currentUpgradesValue + newUpgradesChange); if (currentUpgradesValue_json.is_number_unsigned()) {