Changeset - r25890:31b085404537
[Not reviewed]
master
0 2 0
Michael Lutz - 3 years ago 2021-08-15 10:14:53
michi@icosahedron.de
Fix 2183fd4d: [NewGRF] Use divide instead of right shift for signed numbers. (#9480)

"For negative a, the value of a >> b is implementation-defined (in most implementations, this performs arithmetic right shift, so that the result remains negative)."
2 files changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/newgrf_engine.cpp
Show inline comments
 
@@ -1180,25 +1180,25 @@ uint16 GetVehicleCallbackParent(Callback
 
int GetVehicleProperty(const Vehicle *v, PropertyID property, int orig_value, bool is_signed)
 
{
 
	return GetEngineProperty(v->engine_type, property, orig_value, v, is_signed);
 
}
 

	
 

	
 
int GetEngineProperty(EngineID engine, PropertyID property, int orig_value, const Vehicle *v, bool is_signed)
 
{
 
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, property, 0, engine, v);
 
	if (callback != CALLBACK_FAILED) {
 
		if (is_signed) {
 
			/* Sign extend 15 bit integer */
 
			return static_cast<int16>(callback << 1) >> 1;
 
			return static_cast<int16>(callback << 1) / 2;
 
		} else {
 
			return callback;
 
		}
 
	}
 

	
 
	return orig_value;
 
}
 

	
 

	
 
static void DoTriggerVehicle(Vehicle *v, VehicleTrigger trigger, byte base_random_bits, bool first)
 
{
 
	/* We can't trigger a non-existent vehicle... */
src/train_cmd.cpp
Show inline comments
 
@@ -354,25 +354,25 @@ int Train::GetCurveSpeedLimit() const
 
	if (max_speed != absolute_max_speed) {
 
		/* Apply the current railtype's curve speed advantage */
 
		const RailtypeInfo *rti = GetRailTypeInfo(GetRailType(this->tile));
 
		max_speed += (max_speed / 2) * rti->curve_speed;
 

	
 
		if (this->tcache.cached_tilt) {
 
			/* Apply max_speed bonus of 20% for a tilting train */
 
			max_speed += max_speed / 5;
 
		}
 

	
 
		/* Apply max_speed modifier (cached value is fixed-point binary with 8 fractional bits)
 
		 * and clamp the result to an acceptable range. */
 
		max_speed += (max_speed * this->tcache.cached_curve_speed_mod) >> 8;
 
		max_speed += (max_speed * this->tcache.cached_curve_speed_mod) / 256;
 
		max_speed = Clamp(max_speed, 2, absolute_max_speed);
 
	}
 

	
 
	return max_speed;
 
}
 

	
 
/**
 
 * Calculates the maximum speed of the vehicle under its current conditions.
 
 * @return Maximum speed of the vehicle.
 
 */
 
int Train::GetCurrentMaxSpeed() const
 
{
0 comments (0 inline, 0 general)