1
0
Fork 0

Codechange: replace some min/clamp constructs to ClampTo

pull/10779/head
Rubidium 2023-04-29 10:19:43 +02:00 committed by rubidium42
parent 19ec4e8beb
commit fb856e16c1
18 changed files with 59 additions and 60 deletions

View File

@ -938,7 +938,7 @@ Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16 transit_days,
/* Use callback to calculate cargo profit, if available */ /* Use callback to calculate cargo profit, if available */
if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) {
uint32 var18 = std::min(dist, 0xFFFFu) | (std::min(num_pieces, 0xFFu) << 16) | (std::min<uint16>(transit_days, 0xFFu) << 24); uint32 var18 = ClampTo<uint16_t>(dist) | (ClampTo<uint8_t>(num_pieces) << 16) | (ClampTo<uint8_t>(transit_days) << 24);
uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs);
if (callback != CALLBACK_FAILED) { if (callback != CALLBACK_FAILED) {
int result = GB(callback, 0, 14); int result = GB(callback, 0, 14);
@ -1126,7 +1126,7 @@ static void TriggerIndustryProduction(Industry *i)
if (cargo_waiting == 0) continue; if (cargo_waiting == 0) continue;
for (uint ci_out = 0; ci_out < lengthof(i->produced_cargo_waiting); ci_out++) { for (uint ci_out = 0; ci_out < lengthof(i->produced_cargo_waiting); ci_out++) {
i->produced_cargo_waiting[ci_out] = std::min(i->produced_cargo_waiting[ci_out] + (cargo_waiting * indspec->input_cargo_multiplier[ci_in][ci_out] / 256), 0xFFFFu); i->produced_cargo_waiting[ci_out] = ClampTo<uint16_t>(i->produced_cargo_waiting[ci_out] + (cargo_waiting * indspec->input_cargo_multiplier[ci_in][ci_out] / 256));
} }
i->incoming_cargo_waiting[ci_in] = 0; i->incoming_cargo_waiting[ci_in] = 0;
@ -1728,8 +1728,8 @@ static void LoadUnloadVehicle(Vehicle *front)
} }
/* if last speed is 0, we treat that as if no vehicle has ever visited the station. */ /* if last speed is 0, we treat that as if no vehicle has ever visited the station. */
ge->last_speed = std::min(t, 255); ge->last_speed = ClampTo<uint8_t>(t);
ge->last_age = std::min(TimerGameCalendar::year - front->build_year, 255); ge->last_age = ClampTo<uint8_t>(TimerGameCalendar::year - front->build_year);
assert(v->cargo_cap >= v->cargo.StoredCount()); assert(v->cargo_cap >= v->cargo.StoredCount());
/* Capacity available for loading more cargo. */ /* Capacity available for loading more cargo. */

View File

@ -132,7 +132,7 @@ void SaveToHighScore()
for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) { for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) {
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) { for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
/* First character is a command character, so strlen will fail on that */ /* First character is a command character, so strlen will fail on that */
byte length = std::min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : strlen(&hs->company[1]) + 1); byte length = ClampTo<byte>(std::min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : strlen(&hs->company[1]) + 1));
if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length
fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too

View File

@ -532,7 +532,7 @@ static bool TransportIndustryGoods(TileIndex tile)
bool moved_cargo = false; bool moved_cargo = false;
for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) { for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
uint cw = std::min<uint>(i->produced_cargo_waiting[j], 255u); uint cw = ClampTo<uint8_t>(i->produced_cargo_waiting[j]);
if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) { if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
i->produced_cargo_waiting[j] -= cw; i->produced_cargo_waiting[j] -= cw;
@ -1141,7 +1141,7 @@ static void ChopLumberMillTrees(Industry *i)
TileIndex tile = i->location.tile; TileIndex tile = i->location.tile;
if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, nullptr)) { // 40x40 tiles to search. if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, nullptr)) { // 40x40 tiles to search.
i->produced_cargo_waiting[0] = std::min(0xffff, i->produced_cargo_waiting[0] + 45); // Found a tree, add according value to waiting cargo. i->produced_cargo_waiting[0] = ClampTo<uint16_t>(i->produced_cargo_waiting[0] + 45); // Found a tree, add according value to waiting cargo.
} }
} }
@ -1173,7 +1173,7 @@ static void ProduceIndustryGoods(Industry *i)
IndustryBehaviour indbehav = indsp->behaviour; IndustryBehaviour indbehav = indsp->behaviour;
for (size_t j = 0; j < lengthof(i->produced_cargo_waiting); j++) { for (size_t j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
i->produced_cargo_waiting[j] = std::min(0xffff, i->produced_cargo_waiting[j] + i->production_rate[j]); i->produced_cargo_waiting[j] = ClampTo<uint16_t>(i->produced_cargo_waiting[j] + i->production_rate[j]);
} }
if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) { if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
@ -1784,7 +1784,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
/* Randomize inital production if non-original economy is used and there are no production related callbacks. */ /* Randomize inital production if non-original economy is used and there are no production related callbacks. */
if (!indspec->UsesOriginalEconomy()) { if (!indspec->UsesOriginalEconomy()) {
for (size_t ci = 0; ci < lengthof(i->production_rate); ci++) { for (size_t ci = 0; ci < lengthof(i->production_rate); ci++) {
i->production_rate[ci] = std::min((RandomRange(256) + 128) * i->production_rate[ci] >> 8, 255u); i->production_rate[ci] = ClampTo<byte>((RandomRange(256) + 128) * i->production_rate[ci] >> 8);
} }
} }
@ -2422,7 +2422,7 @@ static void UpdateIndustryStatistics(Industry *i)
byte pct = 0; byte pct = 0;
if (i->this_month_production[j] != 0) { if (i->this_month_production[j] != 0) {
i->last_prod_year = TimerGameCalendar::year; i->last_prod_year = TimerGameCalendar::year;
pct = std::min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255); pct = ClampTo<byte>(i->this_month_transported[j] * 256 / i->this_month_production[j]);
} }
i->last_month_pct_transported[j] = pct; i->last_month_pct_transported[j] = pct;
@ -2446,7 +2446,7 @@ void Industry::RecomputeProductionMultipliers()
/* Rates are rounded up, so e.g. oilrig always produces some passengers */ /* Rates are rounded up, so e.g. oilrig always produces some passengers */
for (size_t i = 0; i < lengthof(this->production_rate); i++) { for (size_t i = 0; i < lengthof(this->production_rate); i++) {
this->production_rate[i] = std::min(CeilDiv(indspec->production_rate[i] * this->prod_level, PRODLEVEL_DEFAULT), 0xFFu); this->production_rate[i] = ClampTo<byte>(CeilDiv(indspec->production_rate[i] * this->prod_level, PRODLEVEL_DEFAULT));
} }
} }

View File

@ -1052,7 +1052,7 @@ public:
if (i->production_rate[line - IL_RATE1] >= 255) return; if (i->production_rate[line - IL_RATE1] >= 255) return;
/* a zero production industry is unlikely to give anything but zero, so push it a little bit */ /* a zero production industry is unlikely to give anything but zero, so push it a little bit */
int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2; int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
i->production_rate[line - IL_RATE1] = std::min<uint>(new_prod, 255); i->production_rate[line - IL_RATE1] = ClampTo<byte>(new_prod);
} }
break; break;

View File

@ -212,7 +212,7 @@ void AirportOverrideManager::SetEntitySpec(AirportSpec *as)
case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0; case 0x7C: return (this->st->airport.psa != nullptr) ? this->st->airport.psa->GetValue(parameter) : 0;
case 0xF0: return this->st->facilities; case 0xF0: return this->st->facilities;
case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); case 0xFA: return ClampTo<uint16_t>(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR);
} }
return this->st->GetNewGRFVariable(this->ro, variable, parameter, available); return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);

View File

@ -437,7 +437,7 @@ uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8)
/* Return 0 if the tile is a land tile */ /* Return 0 if the tile is a land tile */
byte terrain_type = (HasTileWaterClass(tile) ? (GetWaterClass(tile) + 1) & 3 : 0) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1; byte terrain_type = (HasTileWaterClass(tile) ? (GetWaterClass(tile) + 1) & 3 : 0) << 5 | GetTerrainType(tile) << 2 | (tile_type == MP_WATER ? 1 : 0) << 1;
if (grf_version8) z /= TILE_HEIGHT; if (grf_version8) z /= TILE_HEIGHT;
return tile_type << 24 | Clamp(z, 0, 0xFF) << 16 | terrain_type << 8 | tileh; return tile_type << 24 | ClampTo<uint8_t>(z) << 16 | terrain_type << 8 | tileh;
} }
/** /**

View File

@ -528,7 +528,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
airporttype = st->airport.GetSpec()->ttd_airport_type; airporttype = st->airport.GetSpec()->ttd_airport_type;
} }
return (Clamp(altitude, 0, 0xFF) << 8) | airporttype; return (ClampTo<uint8_t>(altitude) << 8) | airporttype;
} }
case 0x45: { // Curvature info case 0x45: { // Curvature info
@ -766,8 +766,8 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
} }
return (variable - 0x80) == 0x10 ? ticks : GB(ticks, 8, 8); return (variable - 0x80) == 0x10 ? ticks : GB(ticks, 8, 8);
} }
case 0x12: return Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF); case 0x12: return ClampTo<uint16_t>(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR);
case 0x13: return GB(Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8); case 0x13: return GB(ClampTo<uint16_t>(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR), 8, 8);
case 0x14: return v->GetServiceInterval(); case 0x14: return v->GetServiceInterval();
case 0x15: return GB(v->GetServiceInterval(), 8, 8); case 0x15: return GB(v->GetServiceInterval(), 8, 8);
case 0x16: return v->last_station_visited; case 0x16: return v->last_station_visited;
@ -823,7 +823,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
case 0x3C: return ClampTo<uint16_t>(v->cargo.StoredCount()); case 0x3C: return ClampTo<uint16_t>(v->cargo.StoredCount());
case 0x3D: return GB(ClampTo<uint16_t>(v->cargo.StoredCount()), 8, 8); case 0x3D: return GB(ClampTo<uint16_t>(v->cargo.StoredCount()), 8, 8);
case 0x3E: return v->cargo.Source(); case 0x3E: return v->cargo.Source();
case 0x3F: return ClampU(v->cargo.DaysInTransit(), 0, 0xFF); case 0x3F: return ClampTo<uint8_t>(v->cargo.DaysInTransit());
case 0x40: return ClampTo<uint16_t>(v->age); case 0x40: return ClampTo<uint16_t>(v->age);
case 0x41: return GB(ClampTo<uint16_t>(v->age), 8, 8); case 0x41: return GB(ClampTo<uint16_t>(v->age), 8, 8);
case 0x42: return ClampTo<uint16_t>(v->max_age); case 0x42: return ClampTo<uint16_t>(v->max_age);
@ -973,8 +973,8 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object,
case 0x48: return Engine::Get(this->self_type)->flags; // Vehicle Type Info case 0x48: return Engine::Get(this->self_type)->flags; // Vehicle Type Info
case 0x49: return TimerGameCalendar::year; // 'Long' format build year case 0x49: return TimerGameCalendar::year; // 'Long' format build year
case 0x4B: return TimerGameCalendar::date; // Long date of last service case 0x4B: return TimerGameCalendar::date; // Long date of last service
case 0x92: return Clamp(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF); // Date of last service case 0x92: return ClampTo<uint16_t>(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR); // Date of last service
case 0x93: return GB(Clamp(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8); case 0x93: return GB(ClampTo<uint16_t>(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR), 8, 8);
case 0xC4: return Clamp(TimerGameCalendar::year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; // Build year case 0xC4: return Clamp(TimerGameCalendar::year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; // Build year
case 0xC6: return Engine::Get(this->self_type)->grf_prop.local_id; case 0xC6: return Engine::Get(this->self_type)->grf_prop.local_id;
case 0xC7: return GB(Engine::Get(this->self_type)->grf_prop.local_id, 8, 8); case 0xC7: return GB(Engine::Get(this->self_type)->grf_prop.local_id, 8, 8);

View File

@ -152,13 +152,12 @@ void DecreaseBuildingCount(Town *t, HouseID house_id)
static uint32 GetNumHouses(HouseID house_id, const Town *town) static uint32 GetNumHouses(HouseID house_id, const Town *town)
{ {
uint8 map_id_count, town_id_count, map_class_count, town_class_count;
HouseClassID class_id = HouseSpec::Get(house_id)->class_id; HouseClassID class_id = HouseSpec::Get(house_id)->class_id;
map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255); uint8_t map_id_count = ClampTo<uint8_t>(_building_counts.id_count[house_id]);
map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255); uint8_t map_class_count = ClampTo<uint8_t>(_building_counts.class_count[class_id]);
town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255); uint8_t town_id_count = ClampTo<uint8_t>(town->cache.building_counts.id_count[house_id]);
town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255); uint8_t town_class_count = ClampTo<uint8_t>(town->cache.building_counts.class_count[class_id]);
return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count; return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count;
} }

View File

@ -141,7 +141,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
/* If the filter is 0, it could be because none was specified as well as being really a 0. /* If the filter is 0, it could be because none was specified as well as being really a 0.
* In either case, just do the regular var67 */ * In either case, just do the regular var67 */
closest_dist = GetClosestIndustry(current->location.tile, ind_index, current); closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
count = std::min<uint>(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit count = ClampTo<byte>(Industry::GetIndustryTypeCount(ind_index));
} else { } else {
/* Count only those who match the same industry type and layout filter /* Count only those who match the same industry type and layout filter
* Unfortunately, we have to do it manually */ * Unfortunately, we have to do it manually */
@ -181,16 +181,16 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile); case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
/* Manhattan distance of the closest town */ /* Manhattan distance of the closest town */
case 0x89: return std::min(DistanceManhattan(this->industry->town->xy, this->tile), 255u); case 0x89: return ClampTo<uint8_t>(DistanceManhattan(this->industry->town->xy, this->tile));
/* Lowest height of the tile */ /* Lowest height of the tile */
case 0x8A: return Clamp(GetTileZ(this->tile) * (this->ro.grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF); case 0x8A: return ClampTo<uint8_t>(GetTileZ(this->tile) * (this->ro.grffile->grf_version >= 8 ? 1 : TILE_HEIGHT));
/* Distance to the nearest water/land tile */ /* Distance to the nearest water/land tile */
case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0); case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
/* Square of Euclidian distance from town */ /* Square of Euclidian distance from town */
case 0x8D: return std::min(DistanceSquare(this->industry->town->xy, this->tile), 65535u); case 0x8D: return ClampTo<uint16_t>(DistanceSquare(this->industry->town->xy, this->tile));
/* 32 random bits */ /* 32 random bits */
case 0x8F: return this->random_bits; case 0x8F: return this->random_bits;
@ -214,9 +214,9 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) { if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) { if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
if (this->industry->prod_level == 0) return 0; if (this->industry->prod_level == 0) return 0;
return std::min<uint16>(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level, 0xFFFFu); return ClampTo<uint16>(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level);
} else { } else {
return std::min<uint16>(this->industry->incoming_cargo_waiting[variable - 0x40], 0xFFFFu); return ClampTo<uint16>(this->industry->incoming_cargo_waiting[variable - 0x40]);
} }
} else { } else {
return 0; return 0;
@ -285,7 +285,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
case 0x65: { case 0x65: {
if (this->tile == INVALID_TILE) break; if (this->tile == INVALID_TILE) break;
TileIndex tile = GetNearbyTile(parameter, this->tile, true); TileIndex tile = GetNearbyTile(parameter, this->tile, true);
return GetTownRadiusGroup(this->industry->town, tile) << 16 | std::min(DistanceManhattan(tile, this->industry->town->xy), 0xFFFFu); return GetTownRadiusGroup(this->industry->town, tile) << 16 | ClampTo<uint16_t>(DistanceManhattan(tile, this->industry->town->xy));
} }
/* Get square of Euclidian distance of closest town */ /* Get square of Euclidian distance of closest town */
case 0x66: { case 0x66: {
@ -396,16 +396,16 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout
case 0xA6: return indspec->grf_prop.local_id; case 0xA6: return indspec->grf_prop.local_id;
case 0xA7: return this->industry->founder; case 0xA7: return this->industry->founder;
case 0xA8: return this->industry->random_colour; case 0xA8: return this->industry->random_colour;
case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255); case 0xA9: return ClampTo<uint8_t>(this->industry->last_prod_year - ORIGINAL_BASE_YEAR);
case 0xAA: return this->industry->counter; case 0xAA: return this->industry->counter;
case 0xAB: return GB(this->industry->counter, 8, 8); case 0xAB: return GB(this->industry->counter, 8, 8);
case 0xAC: return this->industry->was_cargo_delivered; case 0xAC: return this->industry->was_cargo_delivered;
case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days) case 0xB0: return ClampTo<uint16_t>(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR); // Date when built since 1920 (in days)
case 0xB3: return this->industry->construction_type; // Construction type case 0xB3: return this->industry->construction_type; // Construction type
case 0xB4: { case 0xB4: {
TimerGameCalendar::Date *latest = std::max_element(this->industry->last_cargo_accepted_at, endof(this->industry->last_cargo_accepted_at)); TimerGameCalendar::Date *latest = std::max_element(this->industry->last_cargo_accepted_at, endof(this->industry->last_cargo_accepted_at));
return Clamp((*latest) - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days) return ClampTo<uint16_t>((*latest) - DAYS_TILL_ORIGINAL_BASE_YEAR); // Date last cargo accepted since 1920 (in days)
} }
} }
@ -643,22 +643,22 @@ void IndustryProductionCallback(Industry *ind, int reason)
if (group->version < 2) { if (group->version < 2) {
/* Callback parameters map directly to industry cargo slot indices */ /* Callback parameters map directly to industry cargo slot indices */
for (uint i = 0; i < group->num_input; i++) { for (uint i = 0; i < group->num_input; i++) {
ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF); ind->incoming_cargo_waiting[i] = ClampTo<uint16_t>(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier);
} }
for (uint i = 0; i < group->num_output; i++) { for (uint i = 0; i < group->num_output; i++) {
ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF); ind->produced_cargo_waiting[i] = ClampTo<uint16_t>(ind->produced_cargo_waiting[i] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier);
} }
} else { } else {
/* Callback receives list of cargos to apply for, which need to have their cargo slots in industry looked up */ /* Callback receives list of cargos to apply for, which need to have their cargo slots in industry looked up */
for (uint i = 0; i < group->num_input; i++) { for (uint i = 0; i < group->num_input; i++) {
int cargo_index = ind->GetCargoAcceptedIndex(group->cargo_input[i]); int cargo_index = ind->GetCargoAcceptedIndex(group->cargo_input[i]);
if (cargo_index < 0) continue; if (cargo_index < 0) continue;
ind->incoming_cargo_waiting[cargo_index] = Clamp(ind->incoming_cargo_waiting[cargo_index] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF); ind->incoming_cargo_waiting[cargo_index] = ClampTo<uint16_t>(ind->incoming_cargo_waiting[cargo_index] - DerefIndProd(group->subtract_input[i], deref) * multiplier);
} }
for (uint i = 0; i < group->num_output; i++) { for (uint i = 0; i < group->num_output; i++) {
int cargo_index = ind->GetCargoProducedIndex(group->cargo_output[i]); int cargo_index = ind->GetCargoProducedIndex(group->cargo_output[i]);
if (cargo_index < 0) continue; if (cargo_index < 0) continue;
ind->produced_cargo_waiting[cargo_index] = Clamp(ind->produced_cargo_waiting[cargo_index] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF); ind->produced_cargo_waiting[cargo_index] = ClampTo<uint16_t>(ind->produced_cargo_waiting[cargo_index] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier);
} }
} }

View File

@ -249,7 +249,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid,
/* If the object type is invalid, there is none and the closest is far away. */ /* If the object type is invalid, there is none and the closest is far away. */
if (idx >= NUM_OBJECTS) return 0 | 0xFFFF; if (idx >= NUM_OBJECTS) return 0 | 0xFFFF;
return Object::GetTypeCount(idx) << 16 | std::min(GetClosestObject(tile, idx, current), 0xFFFFu); return Object::GetTypeCount(idx) << 16 | ClampTo<uint16_t>(GetClosestObject(tile, idx, current));
} }
/** Used by the resolver to get values for feature 0F deterministic spritegroups. */ /** Used by the resolver to get values for feature 0F deterministic spritegroups. */
@ -324,7 +324,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte local_id, uint32 grfid,
case 0x44: return GetTileOwner(this->tile); case 0x44: return GetTileOwner(this->tile);
/* Get town zone and Manhattan distance of closest town */ /* Get town zone and Manhattan distance of closest town */
case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | std::min(DistanceManhattan(this->tile, t->xy), 0xFFFFu); case 0x45: return GetTownRadiusGroup(t, this->tile) << 16 | ClampTo<uint16_t>(DistanceManhattan(this->tile, t->xy));
/* Get square of Euclidian distance of closest town */ /* Get square of Euclidian distance of closest town */
case 0x46: return DistanceSquare(this->tile, t->xy); case 0x46: return DistanceSquare(this->tile, t->xy);

View File

@ -105,7 +105,7 @@ uint32 RoadStopScopeResolver::GetVariable(byte variable, uint32 parameter, bool
case 0x45: { case 0x45: {
if (this->tile == INVALID_TILE) return HZB_TOWN_EDGE << 16; if (this->tile == INVALID_TILE) return HZB_TOWN_EDGE << 16;
const Town *t = (this->st == nullptr) ? ClosestTownFromTile(this->tile, UINT_MAX) : this->st->town; const Town *t = (this->st == nullptr) ? ClosestTownFromTile(this->tile, UINT_MAX) : this->st->town;
return t != nullptr ? (GetTownRadiusGroup(t, this->tile) << 16 | std::min(DistanceManhattan(this->tile, t->xy), 0xFFFFu)) : HZB_TOWN_EDGE << 16; return t != nullptr ? (GetTownRadiusGroup(t, this->tile) << 16 | ClampTo<uint16_t>(DistanceManhattan(this->tile, t->xy))) : HZB_TOWN_EDGE << 16;
} }
/* Get square of Euclidian distance of closest town */ /* Get square of Euclidian distance of closest town */
@ -176,7 +176,7 @@ uint32 RoadStopScopeResolver::GetVariable(byte variable, uint32 parameter, bool
case 0xF0: return this->st == nullptr ? 0 : this->st->facilities; // facilities case 0xF0: return this->st == nullptr ? 0 : this->st->facilities; // facilities
case 0xFA: return Clamp((this->st == nullptr ? TimerGameCalendar::date : this->st->build_date) - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // build date case 0xFA: return ClampTo<uint16_t>((this->st == nullptr ? TimerGameCalendar::date : this->st->build_date) - DAYS_TILL_ORIGINAL_BASE_YEAR); // build date
} }
if (this->st != nullptr) return this->st->GetNewGRFVariable(this->ro, variable, parameter, available); if (this->st != nullptr) return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);

View File

@ -294,7 +294,7 @@ TownScopeResolver *StationResolverObject::GetTown()
} }
break; break;
case 0xFA: return Clamp(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Build date, clamped to a 16 bit value case 0xFA: return ClampTo<uint16_t>(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR); // Build date, clamped to a 16 bit value
} }
*available = false; *available = false;
@ -384,7 +384,7 @@ TownScopeResolver *StationResolverObject::GetTown()
case 0x84: return this->st->string_id; case 0x84: return this->st->string_id;
case 0x86: return 0; case 0x86: return 0;
case 0xF0: return this->st->facilities; case 0xF0: return this->st->facilities;
case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); case 0xFA: return ClampTo<uint16_t>(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR);
} }
return this->st->GetNewGRFVariable(this->ro, variable, parameter, available); return this->st->GetNewGRFVariable(this->ro, variable, parameter, available);

View File

@ -1923,7 +1923,7 @@ bool AfterLoadGame()
/* Replace "house construction year" with "house age" */ /* Replace "house construction year" with "house age" */
if (IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)) { if (IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)) {
t.m5() = Clamp(TimerGameCalendar::year - (t.m5() + ORIGINAL_BASE_YEAR), 0, 0xFF); t.m5() = ClampTo<uint8_t>(TimerGameCalendar::year - (t.m5() + ORIGINAL_BASE_YEAR));
} }
} }
} }

View File

@ -182,7 +182,7 @@
0, 0,
source_industry, source_industry,
goal_industry, goal_industry,
std::min<SQInteger>(255, distance / 2), ClampTo<uint8_t>(distance / 2),
AICE_STATION_GET_STATION_ID, AICE_STATION_GET_STATION_ID,
source_station ? 0 : 1, source_station ? 0 : 1,
std::min<SQInteger>(15u, num_platforms) << 4 | std::min<SQInteger>(15u, platform_length), std::min<SQInteger>(15u, num_platforms) << 4 | std::min<SQInteger>(15u, platform_length),

View File

@ -3660,9 +3660,9 @@ static void UpdateStationRating(Station *st)
/* NewGRFs expect last speed to be 0xFF when no vehicle has arrived yet. */ /* NewGRFs expect last speed to be 0xFF when no vehicle has arrived yet. */
uint last_speed = ge->HasVehicleEverTriedLoading() ? ge->last_speed : 0xFF; uint last_speed = ge->HasVehicleEverTriedLoading() ? ge->last_speed : 0xFF;
uint32 var18 = std::min<uint>(ge->time_since_pickup, 0xFFu) uint32 var18 = ClampTo<uint8_t>(ge->time_since_pickup)
| (std::min<uint>(ge->max_waiting_cargo, 0xFFFFu) << 8) | (ClampTo<uint16_t>(ge->max_waiting_cargo) << 8)
| (std::min<uint>(last_speed, 0xFFu) << 24); | (ClampTo<uint8_t>(last_speed) << 24);
/* Convert to the 'old' vehicle types */ /* Convert to the 'old' vehicle types */
uint32 var10 = (st->last_vehicle_type == VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10); uint32 var10 = (st->last_vehicle_type == VEH_INVALID) ? 0x0 : (st->last_vehicle_type + 0x10);
uint16 callback = GetCargoCallback(CBID_CARGO_STATION_RATING_CALC, var10, var18, cs); uint16 callback = GetCargoCallback(CBID_CARGO_STATION_RATING_CALC, var10, var18, cs);
@ -3705,7 +3705,7 @@ static void UpdateStationRating(Station *st)
int or_ = ge->rating; // old rating int or_ = ge->rating; // old rating
/* only modify rating in steps of -2, -1, 0, 1 or 2 */ /* only modify rating in steps of -2, -1, 0, 1 or 2 */
ge->rating = rating = or_ + Clamp(Clamp(rating, 0, 255) - or_, -2, 2); ge->rating = rating = or_ + Clamp(ClampTo<uint8_t>(rating) - or_, -2, 2);
/* if rating is <= 64 and more than 100 items waiting on average per destination, /* if rating is <= 64 and more than 100 items waiting on average per destination,
* remove some random amount of goods from the station */ * remove some random amount of goods from the station */
@ -4019,7 +4019,7 @@ void ModifyStationRatingAround(TileIndex tile, Owner owner, int amount, uint rad
GoodsEntry *ge = &st->goods[i]; GoodsEntry *ge = &st->goods[i];
if (ge->status != 0) { if (ge->status != 0) {
ge->rating = Clamp(ge->rating + amount, 0, 255); ge->rating = ClampTo<uint8_t>(ge->rating + amount);
} }
} }
} }

View File

@ -124,11 +124,11 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
if (IsWidgetLowered(WID_TF_WRAPTEXT)) { if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
/* Reflow is mandatory if text wrapping is on */ /* Reflow is mandatory if text wrapping is on */
uint height = this->ReflowContent(); uint height = this->ReflowContent();
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height)); this->vscroll->SetCount(ClampTo<uint16_t>(height));
this->hscroll->SetCount(0); this->hscroll->SetCount(0);
} else { } else {
uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight(); uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight();
this->vscroll->SetCount(std::min<uint>(UINT16_MAX, height)); this->vscroll->SetCount(ClampTo<uint16_t>(height));
this->hscroll->SetCount(this->max_length + WidgetDimensions::scaled.frametext.Horizontal()); this->hscroll->SetCount(this->max_length + WidgetDimensions::scaled.frametext.Horizontal());
} }

View File

@ -675,9 +675,9 @@ struct TimetableWindow : Window {
val = ConvertDisplaySpeedToKmhishSpeed(val, v->type); val = ConvertDisplaySpeedToKmhishSpeed(val, v->type);
if (this->change_timetable_all) { if (this->change_timetable_all) {
Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min<uint32>(val, UINT16_MAX)); Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo<uint16_t>(val));
} else { } else {
Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min<uint32>(val, UINT16_MAX)); Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo<uint16_t>(val));
} }
break; break;
} }
@ -686,9 +686,9 @@ struct TimetableWindow : Window {
if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS; if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS;
if (this->change_timetable_all) { if (this->change_timetable_all) {
Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min<uint32>(val, UINT16_MAX)); Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo<uint16_t>(val));
} else { } else {
Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min<uint32>(val, UINT16_MAX)); Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo<uint16_t>(val));
} }
break; break;

View File

@ -1279,7 +1279,7 @@ void CheckVehicleBreakdown(Vehicle *v)
/* increase chance of failure */ /* increase chance of failure */
int chance = v->breakdown_chance + 1; int chance = v->breakdown_chance + 1;
if (Chance16I(1, 25, r)) chance += 25; if (Chance16I(1, 25, r)) chance += 25;
v->breakdown_chance = std::min(255, chance); v->breakdown_chance = ClampTo<uint8_t>(chance);
/* calculate reliability value to use in comparison */ /* calculate reliability value to use in comparison */
rel = v->reliability; rel = v->reliability;
@ -1289,7 +1289,7 @@ void CheckVehicleBreakdown(Vehicle *v)
if (_settings_game.difficulty.vehicle_breakdowns == 1) rel += 0x6666; if (_settings_game.difficulty.vehicle_breakdowns == 1) rel += 0x6666;
/* check if to break down */ /* check if to break down */
if (_breakdown_chance[(uint)std::min(rel, 0xffff) >> 10] <= v->breakdown_chance) { if (_breakdown_chance[ClampTo<uint16_t>(rel) >> 10] <= v->breakdown_chance) {
v->breakdown_ctr = GB(r, 16, 6) + 0x3F; v->breakdown_ctr = GB(r, 16, 6) + 0x3F;
v->breakdown_delay = GB(r, 24, 7) + 0x80; v->breakdown_delay = GB(r, 24, 7) + 0x80;
v->breakdown_chance = 0; v->breakdown_chance = 0;