Skip to content

Commit

Permalink
Codechange: Cache train curve speed limit can be stored in 16 bits.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PeterN committed Mar 17, 2024
1 parent f08da1d commit 3fc7b3b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

/**
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -328,7 +326,7 @@ struct Train final : public GroundVehicle<Train, VEH_TRAIN> {
* 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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/train_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 3fc7b3b

Please sign in to comment.