Changeset - r27271:e0993573a0ba
[Not reviewed]
master
0 10 0
Rubidium - 19 months ago 2023-04-29 07:25:51
rubidium@openttd.org
Codechange: replace ClampToI32/U16 with ClampTo<int32_t/uint16_t>
10 files changed with 67 insertions and 98 deletions:
0 comments (0 inline, 0 general)
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -185,25 +185,25 @@ static bool EngineReliabilitySorter(cons
 
}
 

	
 
/**
 
 * Determines order of engines by purchase cost
 
 * @param a first engine to compare
 
 * @param b second engine to compare
 
 * @return for descending order: returns true if a < b. Vice versa for ascending order
 
 */
 
static bool EngineCostSorter(const GUIEngineListItem &a, const GUIEngineListItem &b)
 
{
 
	Money va = Engine::Get(a.engine_id)->GetCost();
 
	Money vb = Engine::Get(b.engine_id)->GetCost();
 
	int r = ClampToI32(va - vb);
 
	int r = ClampTo<int32_t>(va - vb);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
	return _engine_sort_direction ? r > 0 : r < 0;
 
}
 

	
 
/**
 
 * Determines order of engines by speed
 
 * @param a first engine to compare
 
 * @param b second engine to compare
 
 * @return for descending order: returns true if a < b. Vice versa for ascending order
 
 */
 
@@ -253,25 +253,25 @@ static bool EngineTractiveEffortSorter(c
 
}
 

	
 
/**
 
 * Determines order of engines by running costs
 
 * @param a first engine to compare
 
 * @param b second engine to compare
 
 * @return for descending order: returns true if a < b. Vice versa for ascending order
 
 */
 
static bool EngineRunningCostSorter(const GUIEngineListItem &a, const GUIEngineListItem &b)
 
{
 
	Money va = Engine::Get(a.engine_id)->GetRunningCost();
 
	Money vb = Engine::Get(b.engine_id)->GetRunningCost();
 
	int r = ClampToI32(va - vb);
 
	int r = ClampTo<int32_t>(va - vb);
 

	
 
	/* Use EngineID to sort instead since we want consistent sorting */
 
	if (r == 0) return EngineNumberSorter(a, b);
 
	return _engine_sort_direction ? r > 0 : r < 0;
 
}
 

	
 
/**
 
 * Determines order of engines by running costs
 
 * @param a first engine to compare
 
 * @param b second engine to compare
 
 * @return for descending order: returns true if a < b. Vice versa for ascending order
 
 */
src/core/math_func.hpp
Show inline comments
 
@@ -181,55 +181,24 @@ constexpr To ClampTo(From value)
 

	
 
		/* The output type is smaller than the input type. */
 
		using BiggerSignedType = typename std::make_signed<BiggerType>::type;
 
		return static_cast<To>(std::clamp<BiggerSignedType>(value,
 
				std::numeric_limits<To>::lowest(), std::numeric_limits<To>::max()));
 
	}
 

	
 
	/* The input and output are unsigned, just clamp at the high side. */
 
	return static_cast<To>(std::min<BiggerType>(value, std::numeric_limits<To>::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<int32>(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<uint16>(a);
 
}
 

	
 
/**
 
 * Returns the (absolute) difference between two (scalar) variables
 
 *
 
 * @param a The first scalar
 
 * @param b The second scalar
 
 * @return The absolute difference between the given scalars
 
 */
 
template <typename T>
 
static inline T Delta(const T a, const T b)
 
{
 
	return (a < b) ? b - a : a - b;
 
}
 

	
src/ground_vehicle.cpp
Show inline comments
 
@@ -166,28 +166,28 @@ int GroundVehicle<T, Type>::GetAccelerat
 
		force = std::max(force, (mass * 8) + resistance);
 
	}
 

	
 
	if (mode == AS_ACCEL) {
 
		/* Easy way out when there is no acceleration. */
 
		if (force == resistance) return 0;
 

	
 
		/* When we accelerate, make sure we always keep doing that, even when
 
		 * the excess force is more than the mass. Otherwise a vehicle going
 
		 * 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<int32_t>((force - resistance) / (mass * 4));
 
		return force < resistance ? std::min(-1, accel) : std::max(1, accel);
 
	} else {
 
		return ClampToI32(std::min<int64>(-force - resistance, -10000) / mass);
 
		return ClampTo<int32_t>(std::min<int64>(-force - resistance, -10000) / mass);
 
	}
 
}
 

	
 
/**
 
 * Check whether the whole vehicle chain is in the depot.
 
 * @return true if and only if the whole chain is in the depot.
 
 */
 
template <class T, VehicleType Type>
 
bool GroundVehicle<T, Type>::IsChainInDepot() const
 
{
 
	const T *v = this->First();
 
	/* Is the front engine stationary in the depot? */
src/newgrf_engine.cpp
Show inline comments
 
@@ -510,25 +510,25 @@ static uint32 VehicleGetVariable(Vehicle
 
			if (!HasBit(v->grf_cache.cache_valid, NCVV_COMPANY_INFORMATION)) {
 
				v->grf_cache.company_information = GetCompanyInfo(v->owner, LiveryHelper(v->engine_type, v));
 
				SetBit(v->grf_cache.cache_valid, NCVV_COMPANY_INFORMATION);
 
			}
 
			return v->grf_cache.company_information;
 

	
 
		case 0x44: // Aircraft information
 
			if (v->type != VEH_AIRCRAFT || !Aircraft::From(v)->IsNormalAircraft()) return UINT_MAX;
 

	
 
			{
 
				const Vehicle *w = v->Next();
 
				assert(w != nullptr);
 
				uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height
 
				uint16 altitude = ClampTo<uint16_t>(v->z_pos - w->z_pos); // Aircraft height - shadow height
 
				byte airporttype = ATP_TTDP_LARGE;
 

	
 
				const Station *st = GetTargetAirportIfValid(Aircraft::From(v));
 

	
 
				if (st != nullptr && st->airport.tile != INVALID_TILE) {
 
					airporttype = st->airport.GetSpec()->ttd_airport_type;
 
				}
 

	
 
				return (Clamp(altitude, 0, 0xFF) << 8) | airporttype;
 
			}
 

	
 
		case 0x45: { // Curvature info
 
@@ -811,63 +811,63 @@ static uint32 VehicleGetVariable(Vehicle
 
		case 0x30: break; // not implemented
 
		case 0x31: break; // not implemented
 
		case 0x32: return v->vehstatus;
 
		case 0x33: return 0; // non-existent high byte of vehstatus
 
		case 0x34: return v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed;
 
		case 0x35: return GB(v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed, 8, 8);
 
		case 0x36: return v->subspeed;
 
		case 0x37: return v->acceleration;
 
		case 0x38: break; // not implemented
 
		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<uint16_t>(v->cargo.StoredCount());
 
		case 0x3D: return GB(ClampTo<uint16_t>(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<uint16_t>(v->age);
 
		case 0x41: return GB(ClampTo<uint16_t>(v->age), 8, 8);
 
		case 0x42: return ClampTo<uint16_t>(v->max_age);
 
		case 0x43: return GB(ClampTo<uint16_t>(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;
 
		case 0x47: return GB(v->GetEngine()->grf_prop.local_id, 8, 8);
 
		case 0x48:
 
			if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum;
 
			return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD;
 

	
 
		case 0x49: return v->day_counter;
 
		case 0x4A: return v->breakdowns_since_last_service;
 
		case 0x4B: return v->breakdown_ctr;
 
		case 0x4C: return v->breakdown_delay;
 
		case 0x4D: return v->breakdown_chance;
 
		case 0x4E: return v->reliability;
 
		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<int32_t>(v->GetDisplayProfitThisYear());
 
		case 0x53: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()),  8, 24);
 
		case 0x54: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 16, 16);
 
		case 0x55: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 24,  8);
 
		case 0x56: return ClampTo<int32_t>(v->GetDisplayProfitLastYear());
 
		case 0x57: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()),  8, 24);
 
		case 0x58: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 16, 16);
 
		case 0x59: return GB(ClampTo<int32_t>(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<int32_t>(v->value);
 
		case 0x5D: return GB(ClampTo<int32_t>(v->value),  8, 24);
 
		case 0x5E: return GB(ClampTo<int32_t>(v->value), 16, 16);
 
		case 0x5F: return GB(ClampTo<int32_t>(v->value), 24,  8);
 
		case 0x60: break; // not implemented
 
		case 0x61: break; // not implemented
 
		case 0x62: break; // vehicle specific, see below
 
		case 0x63: break; // not implemented
 
		case 0x64: break; // vehicle specific, see below
 
		case 0x65: break; // vehicle specific, see below
 
		case 0x66: break; // vehicle specific, see below
 
		case 0x67: break; // vehicle specific, see below
 
		case 0x68: break; // vehicle specific, see below
 
		case 0x69: break; // vehicle specific, see below
 
		case 0x6A: break; // not implemented
 
		case 0x6B: break; // not implemented
src/newgrf_town.cpp
Show inline comments
 
@@ -37,75 +37,75 @@
 

	
 
			std::list<PersistentStorage *>::iterator iter;
 
			for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) {
 
				if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter);
 
			}
 

	
 
			return 0;
 
		}
 

	
 
		/* 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<uint16_t>(this->t->cache.population);
 
		case 0x83: return GB(ClampTo<uint16_t>(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<uint16_t>(this->t->cache.squared_town_zone_radius[0]);
 
		case 0x95: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[0]), 8, 8);
 
		case 0x96: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]);
 
		case 0x97: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[1]), 8, 8);
 
		case 0x98: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]);
 
		case 0x99: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[2]), 8, 8);
 
		case 0x9A: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]);
 
		case 0x9B: return GB(ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[3]), 8, 8);
 
		case 0x9C: return ClampTo<uint16_t>(this->t->cache.squared_town_zone_radius[4]);
 
		case 0x9D: return GB(ClampTo<uint16_t>(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];
 
		case 0xA1: return GB(this->t->ratings[1], 8, 8);
 
		case 0xA2: return this->t->ratings[2];
 
		case 0xA3: return GB(this->t->ratings[2], 8, 8);
 
		case 0xA4: return this->t->ratings[3];
 
		case 0xA5: return GB(this->t->ratings[3], 8, 8);
 
		case 0xA6: return this->t->ratings[4];
 
		case 0xA7: return GB(this->t->ratings[4], 8, 8);
 
		case 0xA8: return this->t->ratings[5];
 
		case 0xA9: return GB(this->t->ratings[5], 8, 8);
 
		case 0xAA: return this->t->ratings[6];
 
		case 0xAB: return GB(this->t->ratings[6], 8, 8);
 
		case 0xAC: return this->t->ratings[7];
 
		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<uint16_t>(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<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max);
 
		case 0xBB: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_max), 8, 8);
 
		case 0xBC: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max);
 
		case 0xBD: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_max), 8, 8);
 
		case 0xBE: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act);
 
		case 0xBF: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].new_act), 8, 8);
 
		case 0xC0: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act);
 
		case 0xC1: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].new_act), 8, 8);
 
		case 0xC2: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max);
 
		case 0xC3: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_max), 8, 8);
 
		case 0xC4: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max);
 
		case 0xC5: return GB(ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_max), 8, 8);
 
		case 0xC6: return ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act);
 
		case 0xC7: return GB(ClampTo<uint16_t>(this->t->supplied[CT_PASSENGERS].old_act), 8, 8);
 
		case 0xC8: return ClampTo<uint16_t>(this->t->supplied[CT_MAIL].old_act);
 
		case 0xC9: return GB(ClampTo<uint16_t>(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;
 
		case 0xCD: return GB(this->t->received[TE_FOOD].new_act, 8, 8);
 
		case 0xCE: return this->t->received[TE_WATER].new_act;
 
		case 0xCF: return GB(this->t->received[TE_WATER].new_act, 8, 8);
 
		case 0xD0: return this->t->received[TE_FOOD].old_act;
 
		case 0xD1: return GB(this->t->received[TE_FOOD].old_act, 8, 8);
 
		case 0xD2: return this->t->received[TE_WATER].old_act;
 
		case 0xD3: return GB(this->t->received[TE_WATER].old_act, 8, 8);
 
		case 0xD4: return this->t->road_build_months;
 
		case 0xD5: return this->t->fund_buildings_months;
src/script/script_info.cpp
Show inline comments
 
@@ -137,60 +137,60 @@ SQInteger ScriptInfo::AddSetting(HSQUIRR
 
			while ((s = strchr(name, ',')) != nullptr) *s = '_';
 
			config.name = name;
 
			items |= 0x001;
 
		} else if (strcmp(key, "description") == 0) {
 
			const SQChar *sqdescription;
 
			if (SQ_FAILED(sq_getstring(vm, -1, &sqdescription))) return SQ_ERROR;
 
			config.description = stredup(sqdescription);
 
			StrMakeValidInPlace(const_cast<char *>(config.description));
 
			items |= 0x002;
 
		} 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<int32_t>(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<int32_t>(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<int32_t>(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<int32_t>(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<int32_t>(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<int32_t>(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<int32_t>(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<int32_t>(res);
 
		} else if (strcmp(key, "flags") == 0) {
 
			SQInteger res;
 
			if (SQ_FAILED(sq_getinteger(vm, -1, &res))) return SQ_ERROR;
 
			config.flags = (ScriptConfigFlags)res;
 
			items |= 0x100;
 
		} else {
 
			char error[1024];
 
			seprintf(error, lastof(error), "unknown setting property '%s'", key);
 
			this->engine->ThrowError(error);
 
			return SQ_ERROR;
 
		}
 

	
src/settings_gui.cpp
Show inline comments
 
@@ -2425,25 +2425,25 @@ struct GameSettingsWindow : Window {
 
		if (str == nullptr) return;
 

	
 
		assert(this->valuewindow_entry != nullptr);
 
		const IntSettingDesc *sd = this->valuewindow_entry->setting;
 

	
 
		int32 value;
 
		if (!StrEmpty(str)) {
 
			long long llvalue = atoll(str);
 

	
 
			/* Save the correct currency-translated value */
 
			if (sd->flags & SF_GUI_CURRENCY) llvalue /= _currency->rate;
 

	
 
			value = (int32)ClampToI32(llvalue);
 
			value = ClampTo<int32_t>(llvalue);
 
		} else {
 
			value = sd->def;
 
		}
 

	
 
		SetSettingValue(this->valuewindow_entry->setting, value);
 
		this->SetDirty();
 
	}
 

	
 
	void OnDropdownSelect(int widget, int index) override
 
	{
 
		switch (widget) {
 
			case WID_GS_RESTRICT_DROPDOWN:
src/strgen/strgen.cpp
Show inline comments
 
@@ -90,25 +90,25 @@ struct FileStringReader : StringReader {
 
		this->fh = fopen(file, "rb");
 
		if (this->fh == nullptr) FatalError("Could not open {}", file);
 
	}
 

	
 
	/** Free/close the file. */
 
	virtual ~FileStringReader()
 
	{
 
		fclose(this->fh);
 
	}
 

	
 
	char *ReadLine(char *buffer, const char *last) override
 
	{
 
		return fgets(buffer, ClampToU16(last - buffer + 1), this->fh);
 
		return fgets(buffer, ClampTo<uint16_t>(last - buffer + 1), this->fh);
 
	}
 

	
 
	void HandlePragma(char *str) override;
 

	
 
	void ParseFile() override
 
	{
 
		this->StringReader::ParseFile();
 

	
 
		if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) {
 
			FatalError("Language must include ##name, ##ownname and ##isocode");
 
		}
 
	}
src/town_cmd.cpp
Show inline comments
 
@@ -2948,25 +2948,25 @@ CommandCost CmdTownRating(DoCommandFlag 
 
 * @param grow_amount Amount to grow, or 0 to grow a random size up to the current amount of houses.
 
 * @return Empty cost or an error.
 
 */
 
CommandCost CmdExpandTown(DoCommandFlag flags, TownID town_id, uint32 grow_amount)
 
{
 
	if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) return CMD_ERROR;
 
	Town *t = Town::GetIfValid(town_id);
 
	if (t == nullptr) return CMD_ERROR;
 

	
 
	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<uint16_t>(t->cache.num_houses / 10)) + 3;
 
			t->cache.num_houses += amount;
 
			UpdateTownRadius(t);
 

	
 
			uint n = amount * 10;
 
			do GrowTown(t); while (--n);
 

	
 
			t->cache.num_houses -= amount;
 
		} else {
 
			for (; grow_amount > 0; grow_amount--) {
 
				/* Try several times to grow, as we are really suppose to grow */
 
				for (uint i = 0; i < 25; i++) if (GrowTown(t)) break;
 
			}
src/vehicle_gui.cpp
Show inline comments
 
@@ -1350,32 +1350,32 @@ static bool VehicleNameSorter(const Vehi
 
}
 

	
 
/** Sort vehicles by their age */
 
static bool VehicleAgeSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = a->age - b->age;
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, 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<int32_t>(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<int32_t>(a->GetDisplayProfitLastYear() - b->GetDisplayProfitLastYear());
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** Sort vehicles by their cargo */
 
static bool VehicleCargoSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	const Vehicle *v;
 
	CargoArray diff;
 

	
 
	/* Append the cargo of the connected waggons */
 
	for (v = a; v != nullptr; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
 
	for (v = b; v != nullptr; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
 
@@ -1410,39 +1410,39 @@ static bool VehicleModelSorter(const Veh
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** Sort vehicles by their value */
 
static bool VehicleValueSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	const Vehicle *u;
 
	Money diff = 0;
 

	
 
	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<int32_t>(diff);
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** Sort vehicles by their length */
 
static bool VehicleLengthSorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = a->GetGroundVehicleCache()->cached_total_length - b->GetGroundVehicleCache()->cached_total_length;
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** 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<int32_t>((a->max_age - a->age) - (b->max_age - b->age));
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
/** Sort vehicles by the timetable delay */
 
static bool VehicleTimetableDelaySorter(const Vehicle * const &a, const Vehicle * const &b)
 
{
 
	int r = a->lateness_counter - b->lateness_counter;
 
	return (r != 0) ? r < 0 : VehicleNumberSorter(a, b);
 
}
 

	
 
void InitializeGUI()
 
{
0 comments (0 inline, 0 general)