From 3fc7b3b9a0a4cc34ae45a75a89a799b665a7bb09 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Thu, 14 Mar 2024 20:40:32 +0000 Subject: [PATCH] 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. --- src/train.h | 10 ++++------ src/train_cmd.cpp | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/train.h b/src/train.h index d637d92d37..75fd0e259d 100644 --- a/src/train.h +++ b/src/train.h @@ -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 { 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); } diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index f969f0203e..4413a2e777 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -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(max_speed); } /**