1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-15 00:05:02 +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

@ -200,7 +200,7 @@ ui32 CGMine::getProducedQuantity() const
{
auto * playerSettings = cb->getPlayerSettings(getOwner());
// always round up income - we don't want mines to always produce zero if handicap in use
return (producedQuantity * playerSettings->handicap.percentIncome + 99) / 100;
return vstd::divideAndCeil(producedQuantity * playerSettings->handicap.percentIncome, 100);
}
void CGMine::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const