diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index ac5f854c0e..84ff30fb29 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -194,7 +194,7 @@ static bool EngineCostSorter(const GUIEngineListItem &a, const GUIEngineListItem { Money va = Engine::Get(a.engine_id)->GetCost(); Money vb = Engine::Get(b.engine_id)->GetCost(); - int r = ClampToI32(va - vb); + int r = ClampTo(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); @@ -262,7 +262,7 @@ static bool EngineRunningCostSorter(const GUIEngineListItem &a, const GUIEngineL { Money va = Engine::Get(a.engine_id)->GetRunningCost(); Money vb = Engine::Get(b.engine_id)->GetRunningCost(); - int r = ClampToI32(va - vb); + int r = ClampTo(va - vb); /* Use EngineID to sort instead since we want consistent sorting */ if (r == 0) return EngineNumberSorter(a, b); diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp index d11ef1ddd3..9f8c5f0179 100644 --- a/src/core/math_func.hpp +++ b/src/core/math_func.hpp @@ -189,37 +189,6 @@ constexpr To ClampTo(From value) return static_cast(std::min(value, std::numeric_limits::max())); } -/** - * Reduce a signed 64-bit int to a signed 32-bit one - * - * This function clamps a 64-bit integer to a 32-bit integer. - * If the 64-bit value is smaller than the smallest 32-bit integer - * value 0x80000000 this value is returned (the left one bit is the sign bit). - * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF - * this value is returned. In all other cases the 64-bit value 'fits' in a - * 32-bits integer field and so the value is casted to int32 and returned. - * - * @param a The 64-bit value to clamps - * @return The 64-bit value reduced to a 32-bit value - * @see Clamp(int, int, int) - */ -static inline int32 ClampToI32(const int64 a) -{ - return ClampTo(a); -} - -/** - * Reduce an unsigned 64-bit int to an unsigned 16-bit one - * - * @param a The 64-bit value to clamp - * @return The 64-bit value reduced to a 16-bit value - * @see ClampU(uint, uint, uint) - */ -static inline uint16 ClampToU16(const uint64 a) -{ - return ClampTo(a); -} - /** * Returns the (absolute) difference between two (scalar) variables * diff --git a/src/ground_vehicle.cpp b/src/ground_vehicle.cpp index d627e0f80b..3de93fb668 100644 --- a/src/ground_vehicle.cpp +++ b/src/ground_vehicle.cpp @@ -175,10 +175,10 @@ int GroundVehicle::GetAcceleration() const * down hill will never slow down enough, and a vehicle that came up * a hill will never speed up enough to (eventually) get back to the * same (maximum) speed. */ - int accel = ClampToI32((force - resistance) / (mass * 4)); + int accel = ClampTo((force - resistance) / (mass * 4)); return force < resistance ? std::min(-1, accel) : std::max(1, accel); } else { - return ClampToI32(std::min(-force - resistance, -10000) / mass); + return ClampTo(std::min(-force - resistance, -10000) / mass); } } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 1eae6e36d5..836a8948e7 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -519,7 +519,7 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, { const Vehicle *w = v->Next(); assert(w != nullptr); - uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height + uint16 altitude = ClampTo(v->z_pos - w->z_pos); // Aircraft height - shadow height byte airporttype = ATP_TTDP_LARGE; const Station *st = GetTargetAirportIfValid(Aircraft::From(v)); @@ -820,14 +820,14 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, case 0x39: return v->cargo_type; case 0x3A: return v->cargo_cap; case 0x3B: return GB(v->cargo_cap, 8, 8); - case 0x3C: return ClampToU16(v->cargo.StoredCount()); - case 0x3D: return GB(ClampToU16(v->cargo.StoredCount()), 8, 8); + 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 0x40: return ClampToU16(v->age); - case 0x41: return GB(ClampToU16(v->age), 8, 8); - case 0x42: return ClampToU16(v->max_age); - case 0x43: return GB(ClampToU16(v->max_age), 8, 8); + case 0x40: return ClampTo(v->age); + case 0x41: return GB(ClampTo(v->age), 8, 8); + case 0x42: return ClampTo(v->max_age); + case 0x43: return GB(ClampTo(v->max_age), 8, 8); case 0x44: return Clamp(v->build_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; case 0x45: return v->unitnumber; case 0x46: return v->GetEngine()->grf_prop.local_id; @@ -845,20 +845,20 @@ static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, case 0x4F: return GB(v->reliability, 8, 8); case 0x50: return v->reliability_spd_dec; case 0x51: return GB(v->reliability_spd_dec, 8, 8); - case 0x52: return ClampToI32(v->GetDisplayProfitThisYear()); - case 0x53: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 8, 24); - case 0x54: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 16, 16); - case 0x55: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 24, 8); - case 0x56: return ClampToI32(v->GetDisplayProfitLastYear()); - case 0x57: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 8, 24); - case 0x58: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 16, 16); - case 0x59: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 24, 8); + case 0x52: return ClampTo(v->GetDisplayProfitThisYear()); + case 0x53: return GB(ClampTo(v->GetDisplayProfitThisYear()), 8, 24); + case 0x54: return GB(ClampTo(v->GetDisplayProfitThisYear()), 16, 16); + case 0x55: return GB(ClampTo(v->GetDisplayProfitThisYear()), 24, 8); + case 0x56: return ClampTo(v->GetDisplayProfitLastYear()); + case 0x57: return GB(ClampTo(v->GetDisplayProfitLastYear()), 8, 24); + case 0x58: return GB(ClampTo(v->GetDisplayProfitLastYear()), 16, 16); + case 0x59: return GB(ClampTo(v->GetDisplayProfitLastYear()), 24, 8); case 0x5A: return v->Next() == nullptr ? INVALID_VEHICLE : v->Next()->index; case 0x5B: break; // not implemented - case 0x5C: return ClampToI32(v->value); - case 0x5D: return GB(ClampToI32(v->value), 8, 24); - case 0x5E: return GB(ClampToI32(v->value), 16, 16); - case 0x5F: return GB(ClampToI32(v->value), 24, 8); + case 0x5C: return ClampTo(v->value); + case 0x5D: return GB(ClampTo(v->value), 8, 24); + case 0x5E: return GB(ClampTo(v->value), 16, 16); + case 0x5F: return GB(ClampTo(v->value), 24, 8); case 0x60: break; // not implemented case 0x61: break; // not implemented case 0x62: break; // vehicle specific, see below diff --git a/src/newgrf_town.cpp b/src/newgrf_town.cpp index a7671a8ac9..90eeede78d 100644 --- a/src/newgrf_town.cpp +++ b/src/newgrf_town.cpp @@ -46,21 +46,21 @@ /* Town properties */ case 0x80: return this->t->xy; case 0x81: return GB(this->t->xy, 8, 8); - case 0x82: return ClampToU16(this->t->cache.population); - case 0x83: return GB(ClampToU16(this->t->cache.population), 8, 8); + case 0x82: return ClampTo(this->t->cache.population); + case 0x83: return GB(ClampTo(this->t->cache.population), 8, 8); case 0x8A: return this->t->grow_counter / TOWN_GROWTH_TICKS; case 0x92: return this->t->flags; // In original game, 0x92 and 0x93 are really one word. Since flags is a byte, this is to adjust case 0x93: return 0; - case 0x94: return ClampToU16(this->t->cache.squared_town_zone_radius[0]); - case 0x95: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[0]), 8, 8); - case 0x96: return ClampToU16(this->t->cache.squared_town_zone_radius[1]); - case 0x97: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[1]), 8, 8); - case 0x98: return ClampToU16(this->t->cache.squared_town_zone_radius[2]); - case 0x99: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[2]), 8, 8); - case 0x9A: return ClampToU16(this->t->cache.squared_town_zone_radius[3]); - case 0x9B: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[3]), 8, 8); - case 0x9C: return ClampToU16(this->t->cache.squared_town_zone_radius[4]); - case 0x9D: return GB(ClampToU16(this->t->cache.squared_town_zone_radius[4]), 8, 8); + case 0x94: return ClampTo(this->t->cache.squared_town_zone_radius[0]); + case 0x95: return GB(ClampTo(this->t->cache.squared_town_zone_radius[0]), 8, 8); + case 0x96: return ClampTo(this->t->cache.squared_town_zone_radius[1]); + case 0x97: return GB(ClampTo(this->t->cache.squared_town_zone_radius[1]), 8, 8); + case 0x98: return ClampTo(this->t->cache.squared_town_zone_radius[2]); + case 0x99: return GB(ClampTo(this->t->cache.squared_town_zone_radius[2]), 8, 8); + case 0x9A: return ClampTo(this->t->cache.squared_town_zone_radius[3]); + case 0x9B: return GB(ClampTo(this->t->cache.squared_town_zone_radius[3]), 8, 8); + case 0x9C: return ClampTo(this->t->cache.squared_town_zone_radius[4]); + case 0x9D: return GB(ClampTo(this->t->cache.squared_town_zone_radius[4]), 8, 8); case 0x9E: return this->t->ratings[0]; case 0x9F: return GB(this->t->ratings[0], 8, 8); case 0xA0: return this->t->ratings[1]; @@ -79,24 +79,24 @@ case 0xAD: return GB(this->t->ratings[7], 8, 8); case 0xAE: return this->t->have_ratings; case 0xB2: return this->t->statues; - case 0xB6: return ClampToU16(this->t->cache.num_houses); + case 0xB6: return ClampTo(this->t->cache.num_houses); case 0xB9: return this->t->growth_rate / TOWN_GROWTH_TICKS; - case 0xBA: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_max); - case 0xBB: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_max), 8, 8); - case 0xBC: return ClampToU16(this->t->supplied[CT_MAIL].new_max); - case 0xBD: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_max), 8, 8); - case 0xBE: return ClampToU16(this->t->supplied[CT_PASSENGERS].new_act); - case 0xBF: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].new_act), 8, 8); - case 0xC0: return ClampToU16(this->t->supplied[CT_MAIL].new_act); - case 0xC1: return GB(ClampToU16(this->t->supplied[CT_MAIL].new_act), 8, 8); - case 0xC2: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_max); - case 0xC3: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_max), 8, 8); - case 0xC4: return ClampToU16(this->t->supplied[CT_MAIL].old_max); - case 0xC5: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_max), 8, 8); - case 0xC6: return ClampToU16(this->t->supplied[CT_PASSENGERS].old_act); - case 0xC7: return GB(ClampToU16(this->t->supplied[CT_PASSENGERS].old_act), 8, 8); - case 0xC8: return ClampToU16(this->t->supplied[CT_MAIL].old_act); - case 0xC9: return GB(ClampToU16(this->t->supplied[CT_MAIL].old_act), 8, 8); + case 0xBA: return ClampTo(this->t->supplied[CT_PASSENGERS].new_max); + case 0xBB: return GB(ClampTo(this->t->supplied[CT_PASSENGERS].new_max), 8, 8); + case 0xBC: return ClampTo(this->t->supplied[CT_MAIL].new_max); + case 0xBD: return GB(ClampTo(this->t->supplied[CT_MAIL].new_max), 8, 8); + case 0xBE: return ClampTo(this->t->supplied[CT_PASSENGERS].new_act); + case 0xBF: return GB(ClampTo(this->t->supplied[CT_PASSENGERS].new_act), 8, 8); + case 0xC0: return ClampTo(this->t->supplied[CT_MAIL].new_act); + case 0xC1: return GB(ClampTo(this->t->supplied[CT_MAIL].new_act), 8, 8); + case 0xC2: return ClampTo(this->t->supplied[CT_PASSENGERS].old_max); + case 0xC3: return GB(ClampTo(this->t->supplied[CT_PASSENGERS].old_max), 8, 8); + case 0xC4: return ClampTo(this->t->supplied[CT_MAIL].old_max); + case 0xC5: return GB(ClampTo(this->t->supplied[CT_MAIL].old_max), 8, 8); + case 0xC6: return ClampTo(this->t->supplied[CT_PASSENGERS].old_act); + case 0xC7: return GB(ClampTo(this->t->supplied[CT_PASSENGERS].old_act), 8, 8); + case 0xC8: return ClampTo(this->t->supplied[CT_MAIL].old_act); + case 0xC9: return GB(ClampTo(this->t->supplied[CT_MAIL].old_act), 8, 8); case 0xCA: return this->t->GetPercentTransported(CT_PASSENGERS); case 0xCB: return this->t->GetPercentTransported(CT_MAIL); case 0xCC: return this->t->received[TE_FOOD].new_act; diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index 79f4982fb6..59e49bc358 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -146,42 +146,42 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) } else if (strcmp(key, "min_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.min_value = ClampToI32(res); + config.min_value = ClampTo(res); items |= 0x004; } else if (strcmp(key, "max_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.max_value = ClampToI32(res); + config.max_value = ClampTo(res); items |= 0x008; } else if (strcmp(key, "easy_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.easy_value = ClampToI32(res); + config.easy_value = ClampTo(res); items |= 0x010; } else if (strcmp(key, "medium_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.medium_value = ClampToI32(res); + config.medium_value = ClampTo(res); items |= 0x020; } else if (strcmp(key, "hard_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.hard_value = ClampToI32(res); + config.hard_value = ClampTo(res); items |= 0x040; } else if (strcmp(key, "random_deviation") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.random_deviation = ClampToI32(abs(res)); + config.random_deviation = ClampTo(abs(res)); items |= 0x200; } else if (strcmp(key, "custom_value") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.custom_value = ClampToI32(res); + config.custom_value = ClampTo(res); items |= 0x080; } else if (strcmp(key, "step_size") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; - config.step_size = ClampToI32(res); + config.step_size = ClampTo(res); } else if (strcmp(key, "flags") == 0) { SQInteger res; if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR; diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 6e51539b5e..70494b3849 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2434,7 +2434,7 @@ struct GameSettingsWindow : Window { /* Save the correct currency-translated value */ if (sd->flags & SF_GUI_CURRENCY) llvalue /= _currency->rate; - value = (int32)ClampToI32(llvalue); + value = ClampTo(llvalue); } else { value = sd->def; } diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index 3407a3e441..a52b6c20ed 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -99,7 +99,7 @@ struct FileStringReader : StringReader { char *ReadLine(char *buffer, const char *last) override { - return fgets(buffer, ClampToU16(last - buffer + 1), this->fh); + return fgets(buffer, ClampTo(last - buffer + 1), this->fh); } void HandlePragma(char *str) override; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 5f84fbf4f3..6ff0784e56 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2957,7 +2957,7 @@ CommandCost CmdExpandTown(DoCommandFlag flags, TownID town_id, uint32 grow_amoun if (flags & DC_EXEC) { /* The more houses, the faster we grow */ if (grow_amount == 0) { - uint amount = RandomRange(ClampToU16(t->cache.num_houses / 10)) + 3; + uint amount = RandomRange(ClampTo(t->cache.num_houses / 10)) + 3; t->cache.num_houses += amount; UpdateTownRadius(t); diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 384357137b..80c9e7218b 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1359,14 +1359,14 @@ static bool VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b) /** Sort vehicles by this year profit */ static bool VehicleProfitThisYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear()); + int r = ClampTo(a->GetDisplayProfitThisYear() - b->GetDisplayProfitThisYear()); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } /** Sort vehicles by last year profit */ static bool VehicleProfitLastYearSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear()); + int r = ClampTo(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear()); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } @@ -1419,7 +1419,7 @@ static bool VehicleValueSorter(const Vehicle * const &a, const Vehicle * const & for (u = a; u != nullptr; u = u->Next()) diff += u->value; for (u = b; u != nullptr; u = u->Next()) diff -= u->value; - int r = ClampToI32(diff); + int r = ClampTo(diff); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); } @@ -1433,7 +1433,7 @@ static bool VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const /** Sort vehicles by the time they can still live */ static bool VehicleTimeToLiveSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = ClampToI32((a->max_age - a->age) - (b->max_age - b->age)); + int r = ClampTo((a->max_age - a->age) - (b->max_age - b->age)); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); }