Skip to content

Commit

Permalink
research.cpp: When upgrade change value is negative, use iDivFloor
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed May 7, 2024
1 parent e025ff0 commit 2f00d6d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/research.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,14 @@ static inline int64_t iDivCeil(int64_t dividend, int64_t divisor)
return (dividend / divisor) + static_cast<int64_t>((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<int64_t>((dividend % divisor != 0 && hasNegQuotient));
}

static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRUCTURE *psStruct, int player)
{
if (cachedStatsObject.is_null()) { cachedStatsObject = wzapi::constructStatsObject(); }
Expand Down Expand Up @@ -789,7 +797,8 @@ static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRU
continue;
}
int64_t currentUpgradesValue = currentUpgradesValue_json.get<int64_t>();
int64_t newUpgradesChange = iDivCeil((statsOriginalValueX.get<int64_t>() * value), 100);
int64_t scaledChange = (statsOriginalValueX.get<int64_t>() * value);
int64_t newUpgradesChange = (value < 0) ? iDivFloor(scaledChange, 100) : iDivCeil(scaledChange, 100);
int64_t newUpgradesValue = (currentUpgradesValue + newUpgradesChange);
if (currentUpgradesValue_json.is_number_unsigned())
{
Expand Down Expand Up @@ -820,7 +829,8 @@ static void eventResearchedHandleUpgrades(const RESEARCH *psResearch, const STRU
continue;
}
int64_t currentUpgradesValue = currentUpgradesValue_json.get<int64_t>();
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())
{
Expand Down

0 comments on commit 2f00d6d

Please sign in to comment.