From 2cb59b185657beeb10acdc1377a9b4a5f0592805 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 19 Jan 2025 16:10:24 +0100 Subject: [PATCH] Codechange: make VehicleID an enum --- src/intro_gui.cpp | 4 ++-- src/saveload/oldloader_sl.cpp | 2 +- src/script/api/script_vehicle.cpp | 8 ++++---- src/vehicle_base.h | 2 +- src/vehicle_gui.cpp | 2 +- src/vehicle_type.h | 8 +++++--- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/intro_gui.cpp b/src/intro_gui.cpp index 7150b77ad5..0cf2826bea 100644 --- a/src/intro_gui.cpp +++ b/src/intro_gui.cpp @@ -141,7 +141,7 @@ struct SelectGameWindow : public Window { for (char c : match[2].str()) { if (isdigit(c)) { if (id_type == ID_VEHICLE) { - vc.vehicle = vc.vehicle * 10 + (c - '0'); + vc.vehicle = static_cast(vc.vehicle * 10 + (c - '0')); } } else { id_type = ID_NONE; @@ -155,7 +155,7 @@ struct SelectGameWindow : public Window { case 'C': vc.align_h = IntroGameViewportCommand::CENTRE; break; case 'R': vc.align_h = IntroGameViewportCommand::RIGHT; break; case 'P': vc.pan_to_next = true; break; - case 'V': id_type = ID_VEHICLE; vc.vehicle = 0; break; + case 'V': id_type = ID_VEHICLE; vc.vehicle = VEHICLE_BEGIN; break; } } } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 3b46a86433..378490f940 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -1250,7 +1250,7 @@ bool LoadOldVehicle(LoadgameState &ls, int num) ReadTTDPatchFlags(ls); for (uint i = 0; i < ls.vehicle_multiplier; i++) { - _current_vehicle_id = num * ls.vehicle_multiplier + i; + _current_vehicle_id = static_cast(num * ls.vehicle_multiplier + i); Vehicle *v; diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp index 9a95d1fc6e..2cafdfa825 100644 --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -84,7 +84,7 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, engine_id, true, cargo, INVALID_CLIENT_ID)) return VEHICLE_INVALID; /* In case of test-mode, we return VehicleID 0 */ - return 0; + return ::VEHICLE_BEGIN; } /* static */ VehicleID ScriptVehicle::BuildVehicle(TileIndex depot, EngineID engine_id) @@ -115,16 +115,16 @@ if (!ScriptObject::Command::Do(&ScriptInstance::DoCommandReturnVehicleID, depot, vehicle_id, share_orders)) return VEHICLE_INVALID; /* In case of test-mode, we return VehicleID 0 */ - return 0; + return ::VEHICLE_BEGIN; } /* static */ bool ScriptVehicle::_MoveWagonInternal(VehicleID source_vehicle_id, SQInteger source_wagon, bool move_attached_wagons, SQInteger dest_vehicle_id, SQInteger dest_wagon) { EnforceCompanyModeValid(false); EnforcePrecondition(false, IsValidVehicle(source_vehicle_id) && source_wagon < GetNumWagons(source_vehicle_id)); - EnforcePrecondition(false, dest_vehicle_id == -1 || (IsValidVehicle(dest_vehicle_id) && dest_wagon < GetNumWagons(dest_vehicle_id))); + EnforcePrecondition(false, dest_vehicle_id == -1 || (IsValidVehicle(static_cast(dest_vehicle_id)) && dest_wagon < GetNumWagons(static_cast(dest_vehicle_id)))); EnforcePrecondition(false, ::Vehicle::Get(source_vehicle_id)->type == VEH_TRAIN); - EnforcePrecondition(false, dest_vehicle_id == -1 || ::Vehicle::Get(dest_vehicle_id)->type == VEH_TRAIN); + EnforcePrecondition(false, dest_vehicle_id == -1 || ::Vehicle::Get(static_cast(dest_vehicle_id))->type == VEH_TRAIN); const Train *v = ::Train::Get(source_vehicle_id); while (source_wagon-- > 0) v = v->GetNextUnit(); diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 3b0bceb61f..6fa7fb165d 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -196,7 +196,7 @@ struct MutableSpriteCache { }; /** A vehicle pool for a little over 1 million vehicles. */ -typedef Pool VehiclePool; +typedef Pool VehiclePool; extern VehiclePool _vehicle_pool; /* Some declarations of functions, so we can make them friendly */ diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index f6013056ab..ccab1e57f4 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -3382,7 +3382,7 @@ public: { if (!str.has_value()) return; - Command::Post(STR_ERROR_CAN_T_RENAME_TRAIN + Vehicle::Get(this->window_number)->type, this->window_number, *str); + Command::Post(STR_ERROR_CAN_T_RENAME_TRAIN + Vehicle::Get(this->window_number)->type, static_cast(this->window_number), *str); } void OnMouseOver([[maybe_unused]] Point pt, WidgetID widget) override diff --git a/src/vehicle_type.h b/src/vehicle_type.h index 34f4e8e0d8..b4e900b790 100644 --- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -13,7 +13,11 @@ #include "core/enum_type.hpp" /** The type all our vehicle IDs have. */ -typedef uint32_t VehicleID; +enum VehicleID : uint32_t { + VEHICLE_BEGIN = 0, + VEHICLE_END = 0xFF000, + INVALID_VEHICLE = 0xFFFFF ///< Constant representing a non-existing vehicle. +}; static const int GROUND_ACCELERATION = 9800; ///< Acceleration due to gravity, 9.8 m/s^2 @@ -51,8 +55,6 @@ struct BaseVehicle VehicleType type; ///< Type of vehicle }; -static const VehicleID INVALID_VEHICLE = 0xFFFFF; ///< Constant representing a non-existing vehicle. - /** Flags for goto depot commands. */ enum class DepotCommandFlag : uint8_t { Service, ///< The vehicle will leave the depot right after arrival (service only)