1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-09-03 11:59:15 +00:00

(svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.

This commit is contained in:
rubidium
2007-10-20 14:51:09 +00:00
parent 54369cba0f
commit 8212088c03
12 changed files with 164 additions and 39 deletions

View File

@@ -677,25 +677,13 @@ CommandCost CommandCost::AddCost(CommandCost ret)
CommandCost CommandCost::AddCost(Money cost)
{
/* Overflow protection */
if (cost < 0 && (this->cost + cost) > this->cost) {
this->cost = INT64_MIN;
} else if (cost > 0 && (this->cost + cost) < this->cost) {
this->cost = INT64_MAX;
} else {
this->cost += cost;
}
this->cost += cost;
return *this;
}
CommandCost CommandCost::MultiplyCost(int factor)
{
/* Overflow protection */
if (factor != 0 && (INT64_MAX / myabs(factor)) < myabs(this->cost)) {
this->cost = (this->cost < 0 == factor < 0) ? INT64_MAX : INT64_MIN;
} else {
this->cost *= factor;
}
this->cost *= factor;
return *this;
}