Codechange: Cache train curve speed limit can be stored in 16 bits.

Cache curve speed modifier and max curve speed are both 16 bit values so can be stored in 16 bit types instead of 32 bit types.
This commit is contained in:
Peter Nelson 2024-03-14 20:40:32 +00:00 committed by Peter Nelson
parent f08da1d373
commit 3fc7b3b9a0
2 changed files with 7 additions and 9 deletions

View File

@ -75,12 +75,10 @@ struct TrainCache {
/* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */
bool cached_tilt; ///< train can tilt; feature provides a bonus in curves
int cached_curve_speed_mod; ///< curve speed modifier of the entire train
uint8_t user_def_data; ///< Cached property 0x25. Can be set by Callback 0x36.
/* cached max. speed / acceleration data */
int cached_max_curve_speed; ///< max consist speed limited by curves
int16_t cached_curve_speed_mod; ///< curve speed modifier of the entire train
uint16_t cached_max_curve_speed; ///< max consist speed limited by curves
};
/**
@ -132,7 +130,7 @@ struct Train final : public GroundVehicle<Train, VEH_TRAIN> {
void ReserveTrackUnderConsist() const;
int GetCurveSpeedLimit() const;
uint16_t GetCurveSpeedLimit() const;
void ConsistChanged(ConsistChangeFlags allowed_changes);
@ -328,7 +326,7 @@ protected: // These functions should not be called outside acceleration code.
* Returns the curve speed modifier of this vehicle.
* @return Current curve speed modifier, in fixed-point binary representation with 8 fractional bits.
*/
inline int GetCurveSpeedModifier() const
inline int16_t GetCurveSpeedModifier() const
{
return GetVehicleProperty(this, PROP_TRAIN_CURVE_SPEED_MOD, RailVehInfo(this->engine_type)->curve_speed_mod, true);
}

View File

@ -120,7 +120,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes)
this->compatible_railtypes = RAILTYPES_NONE;
bool train_can_tilt = true;
int min_curve_speed_mod = INT_MAX;
int16_t min_curve_speed_mod = INT16_MAX;
for (Train *u = this; u != nullptr; u = u->Next()) {
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
@ -305,7 +305,7 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, i
* Computes train speed limit caused by curves
* @return imposed speed limit
*/
int Train::GetCurveSpeedLimit() const
uint16_t Train::GetCurveSpeedLimit() const
{
assert(this->First() == this);
@ -372,7 +372,7 @@ int Train::GetCurveSpeedLimit() const
max_speed = Clamp(max_speed, 2, absolute_max_speed);
}
return max_speed;
return static_cast<uint16_t>(max_speed);
}
/**