1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

Add helper functions for integer division rounding

Added set of functions that perform integer division with different
rounding modes:
- divideAndCeil - rounds up to next integer
- divideAndRound - rounds to nearest integer
- divideAndFloor - rounds to previous integer (equivalent to default
division)

Intended for use in library, where usage of floating point might lead to
desync in multiplayer games.

Replaced some cases that I knew of, including recent handicap PR
This commit is contained in:
Ivan Savenko
2024-08-01 20:48:55 +00:00
parent 257fb8c70c
commit 81e6207df0
5 changed files with 33 additions and 5 deletions

View File

@@ -227,7 +227,7 @@ TResources CGTownInstance::dailyIncome() const
auto playerSettings = cb->gameState()->scenarioOps->getIthPlayersSettings(getOwner());
for(TResources::nziterator it(ret); it.valid(); it++)
// always round up income - we don't want to always produce zero if handicap in use
ret[it->resType] = (ret[it->resType] * playerSettings.handicap.percentIncome + 99) / 100;
ret[it->resType] = vstd::divideAndCeil(ret[it->resType] * playerSettings.handicap.percentIncome, 100);
return ret;
}
@@ -1271,7 +1271,7 @@ int GrowthInfo::totalGrowth() const
ret += entry.count;
// always round up income - we don't want buildings to always produce zero if handicap in use
return (ret * handicapPercentage + 99) / 100;
return vstd::divideAndCeil(ret * handicapPercentage, 100);
}
void CGTownInstance::fillUpgradeInfo(UpgradeInfo & info, const CStackInstance &stack) const