Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

research.cpp: When upgrade change value is negative, use iDivFloor #3795

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading