From fb856e16c14c80c885adaec532dd3a4e96587169 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 29 Apr 2023 10:19:43 +0200 Subject: [PATCH] Codechange: replace some min/clamp constructs to ClampTo --- src/economy.cpp | 8 ++++---- src/highscore.cpp | 2 +- src/industry_cmd.cpp | 12 ++++++------ src/industry_gui.cpp | 2 +- src/newgrf_airport.cpp | 2 +- src/newgrf_commons.cpp | 2 +- src/newgrf_engine.cpp | 12 ++++++------ src/newgrf_house.cpp | 9 ++++----- src/newgrf_industries.cpp | 28 ++++++++++++++-------------- src/newgrf_object.cpp | 4 ++-- src/newgrf_roadstop.cpp | 4 ++-- src/newgrf_station.cpp | 4 ++-- src/saveload/afterload.cpp | 2 +- src/script/api/script_rail.cpp | 2 +- src/station_cmd.cpp | 10 +++++----- src/textfile_gui.cpp | 4 ++-- src/timetable_gui.cpp | 8 ++++---- src/vehicle.cpp | 4 ++-- 18 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index 820a033d9d..b4d9f59aaa 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -938,7 +938,7 @@ Money GetTransportedGoodsIncome(uint num_pieces, uint dist, uint16 transit_days, /* Use callback to calculate cargo profit, if available */ if (HasBit(cs->callback_mask, CBM_CARGO_PROFIT_CALC)) { - uint32 var18 = std::min(dist, 0xFFFFu) | (std::min(num_pieces, 0xFFu) << 16) | (std::min(transit_days, 0xFFu) << 24); + uint32 var18 = ClampTo(dist) | (ClampTo(num_pieces) << 16) | (ClampTo(transit_days) << 24); uint16 callback = GetCargoCallback(CBID_CARGO_PROFIT_CALC, 0, var18, cs); if (callback != CALLBACK_FAILED) { int result = GB(callback, 0, 14); @@ -1126,7 +1126,7 @@ static void TriggerIndustryProduction(Industry *i) if (cargo_waiting == 0) continue; 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(i->produced_cargo_waiting[ci_out] + (cargo_waiting * indspec->input_cargo_multiplier[ci_in][ci_out] / 256)); } 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. */ - ge->last_speed = std::min(t, 255); - ge->last_age = std::min(TimerGameCalendar::year - front->build_year, 255); + ge->last_speed = ClampTo(t); + ge->last_age = ClampTo(TimerGameCalendar::year - front->build_year); assert(v->cargo_cap >= v->cargo.StoredCount()); /* Capacity available for loading more cargo. */ diff --git a/src/highscore.cpp b/src/highscore.cpp index e371b4054b..e48ed5e01b 100644 --- a/src/highscore.cpp +++ b/src/highscore.cpp @@ -132,7 +132,7 @@ void SaveToHighScore() for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) { for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) { /* 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(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 fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 67860ebfd4..3c3c6d8068 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -532,7 +532,7 @@ static bool TransportIndustryGoods(TileIndex tile) bool moved_cargo = false; for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) { - uint cw = std::min(i->produced_cargo_waiting[j], 255u); + uint cw = ClampTo(i->produced_cargo_waiting[j]); if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) { i->produced_cargo_waiting[j] -= cw; @@ -1141,7 +1141,7 @@ static void ChopLumberMillTrees(Industry *i) TileIndex tile = i->location.tile; 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(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; 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(i->produced_cargo_waiting[j] + i->production_rate[j]); } 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. */ if (!indspec->UsesOriginalEconomy()) { 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((RandomRange(256) + 128) * i->production_rate[ci] >> 8); } } @@ -2422,7 +2422,7 @@ static void UpdateIndustryStatistics(Industry *i) byte pct = 0; if (i->this_month_production[j] != 0) { i->last_prod_year = TimerGameCalendar::year; - pct = std::min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255); + pct = ClampTo(i->this_month_transported[j] * 256 / i->this_month_production[j]); } 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 */ 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(CeilDiv(indspec->production_rate[i] * this->prod_level, PRODLEVEL_DEFAULT)); } } diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 3308ac10b8..3c9081f442 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -1052,7 +1052,7 @@ public: 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 */ 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(new_prod, 255); + i->production_rate[line - IL_RATE1] = ClampTo(new_prod); } break; diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp index b5e0b40a53..2dc2f2d847 100644 --- a/src/newgrf_airport.cpp +++ b/src/newgrf_airport.cpp @@ -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 0xF0: return this->st->facilities; - case 0xFA: return Clamp(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); + case 0xFA: return ClampTo(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR); } return this->st->GetNewGRFVariable(this->ro, variable, parameter, available); diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp index a44d4662e2..2da03dc5b5 100644 --- a/src/newgrf_commons.cpp +++ b/src/newgrf_commons.cpp @@ -437,7 +437,7 @@ uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8) /* 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; if (grf_version8) z /= TILE_HEIGHT; - return tile_type << 24 | Clamp(z, 0, 0xFF) << 16 | terrain_type << 8 | tileh; + return tile_type << 24 | ClampTo(z) << 16 | terrain_type << 8 | tileh; } /** diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 836a8948e7..b276cb1385 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -528,7 +528,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, airporttype = st->airport.GetSpec()->ttd_airport_type; } - return (Clamp(altitude, 0, 0xFF) << 8) | airporttype; + return (ClampTo(altitude) << 8) | airporttype; } 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); } - case 0x12: return Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF); - case 0x13: return GB(Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8); + case 0x12: return ClampTo(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR); + case 0x13: return GB(ClampTo(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR), 8, 8); case 0x14: return v->GetServiceInterval(); case 0x15: return GB(v->GetServiceInterval(), 8, 8); case 0x16: return v->last_station_visited; @@ -823,7 +823,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, case 0x3C: return ClampTo(v->cargo.StoredCount()); case 0x3D: return GB(ClampTo(v->cargo.StoredCount()), 8, 8); case 0x3E: return v->cargo.Source(); - case 0x3F: return ClampU(v->cargo.DaysInTransit(), 0, 0xFF); + case 0x3F: return ClampTo(v->cargo.DaysInTransit()); case 0x40: return ClampTo(v->age); case 0x41: return GB(ClampTo(v->age), 8, 8); case 0x42: return ClampTo(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 0x49: return TimerGameCalendar::year; // 'Long' format build year 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 0x93: return GB(Clamp(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8); + case 0x92: return ClampTo(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR); // Date of last service + case 0x93: return GB(ClampTo(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 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); diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index 53ff4b8f72..b6fcdccc50 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -152,13 +152,12 @@ void DecreaseBuildingCount(Town *t, HouseID house_id) 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; - map_id_count = ClampU(_building_counts.id_count[house_id], 0, 255); - map_class_count = ClampU(_building_counts.class_count[class_id], 0, 255); - town_id_count = ClampU(town->cache.building_counts.id_count[house_id], 0, 255); - town_class_count = ClampU(town->cache.building_counts.class_count[class_id], 0, 255); + uint8_t map_id_count = ClampTo(_building_counts.id_count[house_id]); + uint8_t map_class_count = ClampTo(_building_counts.class_count[class_id]); + uint8_t town_id_count = ClampTo(town->cache.building_counts.id_count[house_id]); + uint8_t town_class_count = ClampTo(town->cache.building_counts.class_count[class_id]); return map_class_count << 24 | town_class_count << 16 | map_id_count << 8 | town_id_count; } diff --git a/src/newgrf_industries.cpp b/src/newgrf_industries.cpp index 6606c29972..0ace93b0e2 100644 --- a/src/newgrf_industries.cpp +++ b/src/newgrf_industries.cpp @@ -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. * In either case, just do the regular var67 */ closest_dist = GetClosestIndustry(current->location.tile, ind_index, current); - count = std::min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit + count = ClampTo(Industry::GetIndustryTypeCount(ind_index)); } else { /* Count only those who match the same industry type and layout filter * 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); /* Manhattan distance of the closest town */ - case 0x89: return std::min(DistanceManhattan(this->industry->town->xy, this->tile), 255u); + case 0x89: return ClampTo(DistanceManhattan(this->industry->town->xy, this->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(GetTileZ(this->tile) * (this->ro.grffile->grf_version >= 8 ? 1 : TILE_HEIGHT)); /* Distance to the nearest water/land tile */ case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0); /* Square of Euclidian distance from town */ - case 0x8D: return std::min(DistanceSquare(this->industry->town->xy, this->tile), 65535u); + case 0x8D: return ClampTo(DistanceSquare(this->industry->town->xy, this->tile)); /* 32 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 ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) { if (this->industry->prod_level == 0) return 0; - return std::min(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level, 0xFFFFu); + return ClampTo(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level); } else { - return std::min(this->industry->incoming_cargo_waiting[variable - 0x40], 0xFFFFu); + return ClampTo(this->industry->incoming_cargo_waiting[variable - 0x40]); } } else { return 0; @@ -285,7 +285,7 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout case 0x65: { if (this->tile == INVALID_TILE) break; 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(DistanceManhattan(tile, this->industry->town->xy)); } /* Get square of Euclidian distance of closest town */ case 0x66: { @@ -396,16 +396,16 @@ static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout case 0xA6: return indspec->grf_prop.local_id; case 0xA7: return this->industry->founder; 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(this->industry->last_prod_year - ORIGINAL_BASE_YEAR); case 0xAA: return this->industry->counter; case 0xAB: return GB(this->industry->counter, 8, 8); 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(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 0xB4: { 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((*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) { /* Callback parameters map directly to industry cargo slot indices */ 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(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier); } 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(ind->produced_cargo_waiting[i] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier); } } else { /* 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++) { int cargo_index = ind->GetCargoAcceptedIndex(group->cargo_input[i]); 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(ind->incoming_cargo_waiting[cargo_index] - DerefIndProd(group->subtract_input[i], deref) * multiplier); } for (uint i = 0; i < group->num_output; i++) { int cargo_index = ind->GetCargoProducedIndex(group->cargo_output[i]); 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(ind->produced_cargo_waiting[cargo_index] + std::max(DerefIndProd(group->add_output[i], deref), 0) * multiplier); } } diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index 076c89ca15..60c0684303 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -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 (idx >= NUM_OBJECTS) return 0 | 0xFFFF; - return Object::GetTypeCount(idx) << 16 | std::min(GetClosestObject(tile, idx, current), 0xFFFFu); + return Object::GetTypeCount(idx) << 16 | ClampTo(GetClosestObject(tile, idx, current)); } /** 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); /* 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(DistanceManhattan(this->tile, t->xy)); /* Get square of Euclidian distance of closest town */ case 0x46: return DistanceSquare(this->tile, t->xy); diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index d59457d94b..b746f2ffe8 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -105,7 +105,7 @@ uint32 RoadStopScopeResolver::GetVariable(byte variable, uint32 parameter, bool case 0x45: { if (this->tile == INVALID_TILE) return HZB_TOWN_EDGE << 16; 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(DistanceManhattan(this->tile, t->xy))) : HZB_TOWN_EDGE << 16; } /* 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 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((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); diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index b40674250b..f13f09974a 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -294,7 +294,7 @@ TownScopeResolver *StationResolverObject::GetTown() } 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(TimerGameCalendar::date - DAYS_TILL_ORIGINAL_BASE_YEAR); // Build date, clamped to a 16 bit value } *available = false; @@ -384,7 +384,7 @@ TownScopeResolver *StationResolverObject::GetTown() case 0x84: return this->st->string_id; case 0x86: return 0; 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(this->st->build_date - DAYS_TILL_ORIGINAL_BASE_YEAR); } return this->st->GetNewGRFVariable(this->ro, variable, parameter, available); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 822de66802..0731865b06 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1923,7 +1923,7 @@ bool AfterLoadGame() /* Replace "house construction year" with "house age" */ if (IsTileType(t, MP_HOUSE) && IsHouseCompleted(t)) { - t.m5() = Clamp(TimerGameCalendar::year - (t.m5() + ORIGINAL_BASE_YEAR), 0, 0xFF); + t.m5() = ClampTo(TimerGameCalendar::year - (t.m5() + ORIGINAL_BASE_YEAR)); } } } diff --git a/src/script/api/script_rail.cpp b/src/script/api/script_rail.cpp index e9bdbd0658..fbbb2038f9 100644 --- a/src/script/api/script_rail.cpp +++ b/src/script/api/script_rail.cpp @@ -182,7 +182,7 @@ 0, source_industry, goal_industry, - std::min(255, distance / 2), + ClampTo(distance / 2), AICE_STATION_GET_STATION_ID, source_station ? 0 : 1, std::min(15u, num_platforms) << 4 | std::min(15u, platform_length), diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index e3b2e632f4..0ca841e26c 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3660,9 +3660,9 @@ static void UpdateStationRating(Station *st) /* NewGRFs expect last speed to be 0xFF when no vehicle has arrived yet. */ uint last_speed = ge->HasVehicleEverTriedLoading() ? ge->last_speed : 0xFF; - uint32 var18 = std::min(ge->time_since_pickup, 0xFFu) - | (std::min(ge->max_waiting_cargo, 0xFFFFu) << 8) - | (std::min(last_speed, 0xFFu) << 24); + uint32 var18 = ClampTo(ge->time_since_pickup) + | (ClampTo(ge->max_waiting_cargo) << 8) + | (ClampTo(last_speed) << 24); /* Convert to the 'old' vehicle types */ 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); @@ -3705,7 +3705,7 @@ static void UpdateStationRating(Station *st) int or_ = ge->rating; // old rating /* 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(rating) - or_, -2, 2); /* if rating is <= 64 and more than 100 items waiting on average per destination, * 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]; if (ge->status != 0) { - ge->rating = Clamp(ge->rating + amount, 0, 255); + ge->rating = ClampTo(ge->rating + amount); } } } diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp index 86a370045f..89a5747bb4 100644 --- a/src/textfile_gui.cpp +++ b/src/textfile_gui.cpp @@ -124,11 +124,11 @@ void TextfileWindow::SetupScrollbars(bool force_reflow) if (IsWidgetLowered(WID_TF_WRAPTEXT)) { /* Reflow is mandatory if text wrapping is on */ uint height = this->ReflowContent(); - this->vscroll->SetCount(std::min(UINT16_MAX, height)); + this->vscroll->SetCount(ClampTo(height)); this->hscroll->SetCount(0); } else { uint height = force_reflow ? this->ReflowContent() : this->GetContentHeight(); - this->vscroll->SetCount(std::min(UINT16_MAX, height)); + this->vscroll->SetCount(ClampTo(height)); this->hscroll->SetCount(this->max_length + WidgetDimensions::scaled.frametext.Horizontal()); } diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index 2f924f3cf1..40106e8a65 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -675,9 +675,9 @@ struct TimetableWindow : Window { val = ConvertDisplaySpeedToKmhishSpeed(val, v->type); if (this->change_timetable_all) { - Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min(val, UINT16_MAX)); + Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo(val)); } else { - Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min(val, UINT16_MAX)); + Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo(val)); } break; } @@ -686,9 +686,9 @@ struct TimetableWindow : Window { if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS; if (this->change_timetable_all) { - Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min(val, UINT16_MAX)); + Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, ClampTo(val)); } else { - Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min(val, UINT16_MAX)); + Command::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, ClampTo(val)); } break; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 96e9871f4d..31d01af038 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1279,7 +1279,7 @@ void CheckVehicleBreakdown(Vehicle *v) /* increase chance of failure */ int chance = v->breakdown_chance + 1; if (Chance16I(1, 25, r)) chance += 25; - v->breakdown_chance = std::min(255, chance); + v->breakdown_chance = ClampTo(chance); /* calculate reliability value to use in comparison */ rel = v->reliability; @@ -1289,7 +1289,7 @@ void CheckVehicleBreakdown(Vehicle *v) if (_settings_game.difficulty.vehicle_breakdowns == 1) rel += 0x6666; /* check if to break down */ - if (_breakdown_chance[(uint)std::min(rel, 0xffff) >> 10] <= v->breakdown_chance) { + if (_breakdown_chance[ClampTo(rel) >> 10] <= v->breakdown_chance) { v->breakdown_ctr = GB(r, 16, 6) + 0x3F; v->breakdown_delay = GB(r, 24, 7) + 0x80; v->breakdown_chance = 0;