From 64bc95722badc8faa99c02d6f84ec926644bffb0 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. (cherry picked from commit 3fc7b3b9a0a4cc34ae45a75a89a799b665a7bb09) --- src/sl/vehicle_sl.cpp | 12 ++++++------ src/train.h | 11 +++++------ src/train_cmd.cpp | 6 +++--- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/sl/vehicle_sl.cpp b/src/sl/vehicle_sl.cpp index 73dff8efb0..33b21242ad 100644 --- a/src/sl/vehicle_sl.cpp +++ b/src/sl/vehicle_sl.cpp @@ -1268,7 +1268,6 @@ struct vehicle_venc { struct train_venc { VehicleID id; GroundVehicleCache gvcache; - int cached_curve_speed_mod; uint8_t cached_tflags; uint8_t cached_num_engines; uint16_t cached_centre_mass; @@ -1277,7 +1276,8 @@ struct train_venc { uint16_t cached_uncapped_decel; uint8_t cached_deceleration; byte user_def_data; - int cached_max_curve_speed; + int16_t cached_curve_speed_mod; + uint16_t cached_max_curve_speed; }; struct roadvehicle_venc { @@ -1340,7 +1340,6 @@ void Save_VENC() for (Train *t : Train::Iterate()) { SlWriteUint32(t->index); write_gv_cache(t->gcache); - SlWriteUint32(t->tcache.cached_curve_speed_mod); SlWriteByte(t->tcache.cached_tflags); SlWriteByte(t->tcache.cached_num_engines); SlWriteUint16(t->tcache.cached_centre_mass); @@ -1349,7 +1348,8 @@ void Save_VENC() SlWriteUint16(t->tcache.cached_uncapped_decel); SlWriteByte(t->tcache.cached_deceleration); SlWriteByte(t->tcache.user_def_data); - SlWriteUint32(t->tcache.cached_max_curve_speed); + SlWriteUint16((uint16_t)t->tcache.cached_curve_speed_mod); + SlWriteUint16(t->tcache.cached_max_curve_speed); } /* road vehicle */ @@ -1403,7 +1403,6 @@ void Load_VENC() for (train_venc &venc : _train_vencs) { venc.id = SlReadUint32(); read_gv_cache(venc.gvcache); - venc.cached_curve_speed_mod = SlReadUint32(); venc.cached_tflags = SlReadByte(); venc.cached_num_engines = SlReadByte(); venc.cached_centre_mass = SlReadUint16(); @@ -1412,7 +1411,8 @@ void Load_VENC() venc.cached_uncapped_decel = SlReadUint16(); venc.cached_deceleration = SlReadByte(); venc.user_def_data = SlReadByte(); - venc.cached_max_curve_speed = SlReadUint32(); + venc.cached_curve_speed_mod = (int16_t)SlReadUint16(); + venc.cached_max_curve_speed = SlReadUint16(); } _roadvehicle_vencs.resize(SlReadUint32()); diff --git a/src/train.h b/src/train.h index 7f6781244b..313bdaa96c 100644 --- a/src/train.h +++ b/src/train.h @@ -110,11 +110,7 @@ struct TrainCache { /* Cached wagon override spritegroup */ const struct SpriteGroup *cached_override; - /* cached max. speed / acceleration data */ - int cached_max_curve_speed; ///< max consist speed limited by curves - /* cached values, recalculated on load and each time a vehicle is added to/removed from the consist. */ - int cached_curve_speed_mod; ///< curve speed modifier of the entire train TrainCacheFlags cached_tflags; ///< train cached flags uint8_t cached_num_engines; ///< total number of engines, including rear ends of multiheaded engines uint16_t cached_centre_mass; ///< Cached position of the centre of mass, from the front @@ -124,6 +120,9 @@ struct TrainCache { uint8_t cached_deceleration; ///< Cached deceleration for realistic braking lookahead purposes byte user_def_data; ///< Cached property 0x25. Can be set by Callback 0x36. + + 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 }; /** @@ -184,7 +183,7 @@ struct Train final : public GroundVehicle { void ReserveTrackUnderConsist() const; - int GetCurveSpeedLimit() const; + uint16_t GetCurveSpeedLimit() const; void ConsistChanged(ConsistChangeFlags allowed_changes); @@ -479,7 +478,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 c5eed110d4..3c6c15de2b 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -262,7 +262,7 @@ void Train::ConsistChanged(ConsistChangeFlags allowed_changes) bool train_can_tilt = true; bool speed_varies_by_railtype = false; - 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); @@ -607,7 +607,7 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, Train *v, bool up * Computes train speed limit caused by curves * @return imposed speed limit */ -int Train::GetCurveSpeedLimit() const +uint16_t Train::GetCurveSpeedLimit() const { dbg_assert(this->First() == this); @@ -674,7 +674,7 @@ int Train::GetCurveSpeedLimit() const max_speed = Clamp(max_speed, 2, absolute_max_speed); } - return max_speed; + return static_cast(max_speed); } void AdvanceOrderIndex(const Vehicle *v, VehicleOrderID &index)