From c719566a090d202ae8fed44f9f7fc4d26087e800 Mon Sep 17 00:00:00 2001 From: Michael Lutz Date: Thu, 19 Jun 2025 21:09:37 +0200 Subject: [PATCH] Codechange: Use an enum for vehicle acceleration model. --- src/build_vehicle_gui.cpp | 2 +- src/engine_gui.cpp | 2 +- src/engine_type.h | 7 +++++++ src/ground_vehicle.cpp | 2 +- src/newgrf/newgrf_act0_railtypes.cpp | 2 +- src/rail.h | 2 +- src/roadveh.h | 6 +++--- src/table/railtypes.h | 8 ++++---- src/train.h | 2 +- src/train_cmd.cpp | 4 ++-- src/vehicle_gui.cpp | 2 +- 11 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index e713a7b5ec..578fc30644 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -637,7 +637,7 @@ static int DrawRailEnginePurchaseInfo(int left, int right, int y, EngineID engin y += GetCharacterHeight(FS_NORMAL); /* Max tractive effort - not applicable if old acceleration or maglev */ - if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(rvi->railtype)->acceleration_type != 2) { + if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(rvi->railtype)->acceleration_type != VehicleAccelerationModel::Maglev) { DrawString(left, right, y, GetString(STR_PURCHASE_INFO_MAX_TE, e->GetDisplayMaxTractiveEffort())); y += GetCharacterHeight(FS_NORMAL); } diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index e219a45ff2..63a8a82620 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -181,7 +181,7 @@ static std::string GetTrainEngineInfoString(const Engine &e) res << GetString(STR_ENGINE_PREVIEW_COST_WEIGHT, e.GetCost(), e.GetDisplayWeight()); res << '\n'; - if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(e.u.rail.railtype)->acceleration_type != 2) { + if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(e.u.rail.railtype)->acceleration_type != VehicleAccelerationModel::Maglev) { res << GetString(STR_ENGINE_PREVIEW_SPEED_POWER_MAX_TE, PackVelocity(e.GetDisplayMaxSpeed(), e.type), e.GetPower(), e.GetDisplayMaxTractiveEffort()); res << '\n'; } else { diff --git a/src/engine_type.h b/src/engine_type.h index 94f797430e..5d35a11bd4 100644 --- a/src/engine_type.h +++ b/src/engine_type.h @@ -43,6 +43,13 @@ enum EngineClass : uint8_t { EC_MAGLEV, ///< Maglev engine. }; +/** Acceleration model of a vehicle. */ +enum class VehicleAccelerationModel : uint8_t { + Normal, ///< Default acceleration model. + Monorail, ///< Monorail acceleration model. + Maglev, ///< Maglev acceleration model. +}; + /** Information about a rail vehicle. */ struct RailVehicleInfo { uint8_t image_index = 0; diff --git a/src/ground_vehicle.cpp b/src/ground_vehicle.cpp index ca8fe65fcf..a5b638f09a 100644 --- a/src/ground_vehicle.cpp +++ b/src/ground_vehicle.cpp @@ -131,7 +131,7 @@ int GroundVehicle::GetAcceleration() const */ int64_t resistance = 0; - bool maglev = v->GetAccelerationType() == 2; + bool maglev = v->GetAccelerationType() == VehicleAccelerationModel::Maglev; const int area = v->GetAirDragArea(); if (!maglev) { diff --git a/src/newgrf/newgrf_act0_railtypes.cpp b/src/newgrf/newgrf_act0_railtypes.cpp index fe9e52132d..3d503cfc76 100644 --- a/src/newgrf/newgrf_act0_railtypes.cpp +++ b/src/newgrf/newgrf_act0_railtypes.cpp @@ -117,7 +117,7 @@ static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, Byte break; case 0x15: // Acceleration model - rti->acceleration_type = Clamp(buf.ReadByte(), 0, 2); + rti->acceleration_type = static_cast(Clamp(buf.ReadByte(), 0, 2)); break; case 0x16: // Map colour diff --git a/src/rail.h b/src/rail.h index 323dd00c24..6e428382d7 100644 --- a/src/rail.h +++ b/src/rail.h @@ -214,7 +214,7 @@ public: /** * Acceleration type of this rail type */ - uint8_t acceleration_type; + VehicleAccelerationModel acceleration_type; /** * Maximum speed for vehicles travelling on this rail type diff --git a/src/roadveh.h b/src/roadveh.h index 56aae919c4..8657512d1e 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -250,11 +250,11 @@ protected: // These functions should not be called outside acceleration code. /** * Allows to know the acceleration type of a vehicle. - * @return Zero, road vehicles always use a normal acceleration method. + * @return \c VehicleAccelerationModel::Normal, road vehicles always use a normal acceleration method. */ - inline int GetAccelerationType() const + inline VehicleAccelerationModel GetAccelerationType() const { - return 0; + return VehicleAccelerationModel::Normal; } /** diff --git a/src/table/railtypes.h b/src/table/railtypes.h index 3f687545bb..b8065561a3 100644 --- a/src/table/railtypes.h +++ b/src/table/railtypes.h @@ -86,7 +86,7 @@ static const RailTypeInfo _original_railtypes[] = { 8, /* acceleration type */ - 0, + VehicleAccelerationModel::Normal, /* max speed */ 0, @@ -188,7 +188,7 @@ static const RailTypeInfo _original_railtypes[] = { 12, /* acceleration type */ - 0, + VehicleAccelerationModel::Normal, /* max speed */ 0, @@ -286,7 +286,7 @@ static const RailTypeInfo _original_railtypes[] = { 16, /* acceleration type */ - 1, + VehicleAccelerationModel::Monorail, /* max speed */ 0, @@ -384,7 +384,7 @@ static const RailTypeInfo _original_railtypes[] = { 24, /* acceleration type */ - 2, + VehicleAccelerationModel::Maglev, /* max speed */ 0, diff --git a/src/train.h b/src/train.h index b7ff1a6581..ee1b499c1c 100644 --- a/src/train.h +++ b/src/train.h @@ -302,7 +302,7 @@ protected: // These functions should not be called outside acceleration code. * Allows to know the acceleration type of a vehicle. * @return Acceleration type of the vehicle. */ - inline int GetAccelerationType() const + inline VehicleAccelerationModel GetAccelerationType() const { return GetRailTypeInfo(this->railtype)->acceleration_type; } diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 4d3428e9f5..3715e87f94 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -3083,7 +3083,7 @@ static inline void AffectSpeedByZChange(Train *v, int old_z) { if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return; - const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type]; + const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast(GetRailTypeInfo(v->railtype)->acceleration_type)]; if (old_z < v->z_pos) { v->cur_speed -= (v->cur_speed * asp->z_up >> 8); @@ -3490,7 +3490,7 @@ bool TrainController(Train *v, Vehicle *nomove, bool reverse) if (chosen_dir != v->direction) { if (prev == nullptr && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) { - const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type]; + const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast(GetRailTypeInfo(v->railtype)->acceleration_type)]; DirDiff diff = DirDifference(v->direction, chosen_dir); v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8; } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 6727ed5796..306df4c62b 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2596,7 +2596,7 @@ struct VehicleDetailsWindow : Window { (v->type == VEH_ROAD && _settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL)) { const GroundVehicleCache *gcache = v->GetGroundVehicleCache(); if (v->type == VEH_TRAIN && (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL || - GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type == 2)) { + GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type == VehicleAccelerationModel::Maglev)) { DrawString(tr, GetString(STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED, gcache->cached_weight, gcache->cached_power, max_speed)); } else { DrawString(tr, GetString(STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE, gcache->cached_weight, gcache->cached_power, max_speed, gcache->cached_max_te));