From 9015c3651f3bbd4e39593571ca994ef8eec77e0d Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 19 Jan 2025 14:58:40 +0100 Subject: [PATCH] Codechange: make EngineID an enum --- src/engine.cpp | 2 +- src/engine_base.h | 2 +- src/engine_gui.cpp | 8 ++++---- src/engine_type.h | 10 +++++++--- src/newgrf.cpp | 8 ++++---- src/newgrf_engine.cpp | 2 +- src/news_gui.cpp | 8 ++++---- src/saveload/engine_sl.cpp | 6 +++--- src/saveload/oldloader_sl.cpp | 8 ++++---- 9 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index f404d5c5a4..400475a331 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -508,7 +508,7 @@ bool Engine::IsVariantHidden(CompanyID c) const */ void EngineOverrideManager::ResetToDefaultMapping() { - EngineID id = 0; + EngineID id = ENGINE_BEGIN; for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) { auto &map = this->mappings[type]; map.clear(); diff --git a/src/engine_base.h b/src/engine_base.h index a5bcf2317b..5911ac27d3 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -32,7 +32,7 @@ enum class EngineDisplayFlag : uint8_t { using EngineDisplayFlags = EnumBitSet; -typedef Pool EnginePool; +typedef Pool EnginePool; extern EnginePool _engine_pool; struct Engine : EnginePool::PoolItem<&_engine_pool> { diff --git a/src/engine_gui.cpp b/src/engine_gui.cpp index 232497463f..12e4ee6559 100644 --- a/src/engine_gui.cpp +++ b/src/engine_gui.cpp @@ -83,7 +83,7 @@ struct EnginePreviewWindow : Window { if (widget != WID_EP_QUESTION) return; /* Get size of engine sprite, on loan from depot_gui.cpp */ - EngineID engine = this->window_number; + EngineID engine = static_cast(this->window_number); EngineImageType image_type = EIT_PURCHASE; uint x, y; int x_offs, y_offs; @@ -109,7 +109,7 @@ struct EnginePreviewWindow : Window { { if (widget != WID_EP_QUESTION) return; - EngineID engine = this->window_number; + EngineID engine = static_cast(this->window_number); SetDParam(0, GetEngineCategoryName(engine)); int y = DrawStringMultiLine(r, STR_ENGINE_PREVIEW_MESSAGE, TC_FROMSTRING, SA_HOR_CENTER | SA_TOP) + WidgetDimensions::scaled.vsep_wide; @@ -127,7 +127,7 @@ struct EnginePreviewWindow : Window { { switch (widget) { case WID_EP_YES: - Command::Post(this->window_number); + Command::Post(static_cast(this->window_number)); [[fallthrough]]; case WID_EP_NO: if (!_shift_pressed) this->Close(); @@ -139,7 +139,7 @@ struct EnginePreviewWindow : Window { { if (!gui_scope) return; - EngineID engine = this->window_number; + EngineID engine = static_cast(this->window_number); if (Engine::Get(engine)->preview_company != _local_company) this->Close(); } }; diff --git a/src/engine_type.h b/src/engine_type.h index 022d673e6b..74299be2a8 100644 --- a/src/engine_type.h +++ b/src/engine_type.h @@ -20,7 +20,13 @@ #include "sound_type.h" #include "strings_type.h" -typedef uint16_t EngineID; ///< Unique identification number of an engine. +/** Unique identification number of an engine. */ +enum EngineID : uint16_t { + ENGINE_BEGIN = 0, + ENGINE_END = 64000, + INVALID_ENGINE = 0xFFFF ///< Constant denoting an invalid engine. +}; +DECLARE_INCREMENT_DECREMENT_OPERATORS(EngineID) struct Engine; @@ -213,6 +219,4 @@ inline uint64_t PackEngineNameDParam(EngineID engine_id, EngineNameContext conte static const uint MAX_LENGTH_ENGINE_NAME_CHARS = 32; ///< The maximum length of an engine name in characters including '\0' -static const EngineID INVALID_ENGINE = 0xFFFF; ///< Constant denoting an invalid engine. - #endif /* ENGINE_TYPE_H */ diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 88db46b315..1f57980e89 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -1336,7 +1336,7 @@ static ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, B break; case 0x2F: // Engine variant - ei->variant_id = buf.ReadWord(); + ei->variant_id = static_cast(buf.ReadWord()); break; case 0x30: // Extra miscellaneous flags @@ -1547,7 +1547,7 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, B } case 0x26: // Engine variant - ei->variant_id = buf.ReadWord(); + ei->variant_id = static_cast(buf.ReadWord()); break; case 0x27: // Extra miscellaneous flags @@ -1736,7 +1736,7 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint first, uint last, int prop, B } case 0x20: // Engine variant - ei->variant_id = buf.ReadWord(); + ei->variant_id = static_cast(buf.ReadWord()); break; case 0x21: // Extra miscellaneous flags @@ -1919,7 +1919,7 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int pro break; case 0x20: // Engine variant - ei->variant_id = buf.ReadWord(); + ei->variant_id = static_cast(buf.ReadWord()); break; case 0x21: // Extra miscellaneous flags diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 808dda722e..430bc4eed6 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -1332,7 +1332,7 @@ void CommitVehicleListOrderChanges() { /* Build a list of EngineIDs. EngineIDs are sequential from 0 up to the number of pool items with no gaps. */ std::vector ordering(Engine::GetNumItems()); - std::iota(std::begin(ordering), std::end(ordering), 0); + std::iota(std::begin(ordering), std::end(ordering), ENGINE_BEGIN); /* Pre-sort engines by scope-grfid and local index */ std::ranges::sort(ordering, EnginePreSort); diff --git a/src/news_gui.cpp b/src/news_gui.cpp index 0c9eb7d731..57e5761f1f 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -458,7 +458,7 @@ struct NewsWindow : Window { case WID_N_VEH_INFO: { assert(this->ni->reftype1 == NewsReferenceType::Engine); - EngineID engine = this->ni->ref1; + EngineID engine = static_cast(this->ni->ref1); str = GetEngineInfoString(engine); break; } @@ -542,14 +542,14 @@ struct NewsWindow : Window { case WID_N_VEH_SPR: { assert(this->ni->reftype1 == NewsReferenceType::Engine); - EngineID engine = this->ni->ref1; + EngineID engine = static_cast(this->ni->ref1); DrawVehicleEngine(r.left, r.right, CenterBounds(r.left, r.right, 0), CenterBounds(r.top, r.bottom, 0), engine, GetEnginePalette(engine, _local_company), EIT_PREVIEW); GfxFillRect(r.left, r.top, r.right, r.bottom, PALETTE_NEWSPAPER, FILLRECT_RECOLOUR); break; } case WID_N_VEH_INFO: { assert(this->ni->reftype1 == NewsReferenceType::Engine); - EngineID engine = this->ni->ref1; + EngineID engine = static_cast(this->ni->ref1); DrawStringMultiLine(r.left, r.right, r.top, r.bottom, GetEngineInfoString(engine), TC_FROMSTRING, SA_CENTER); break; } @@ -678,7 +678,7 @@ private: StringID GetNewVehicleMessageString(WidgetID widget) const { assert(this->ni->reftype1 == NewsReferenceType::Engine); - EngineID engine = this->ni->ref1; + EngineID engine = static_cast(this->ni->ref1); switch (widget) { case WID_N_VEH_TITLE: diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index 048157129d..d42443b87e 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -76,7 +76,7 @@ struct ENGNChunkHandler : ChunkHandler { * engine pool after processing NewGRFs by CopyTempEngineData(). */ int index; while ((index = SlIterateArray()) != -1) { - Engine *e = GetTempDataEngine(index); + Engine *e = GetTempDataEngine(static_cast(index)); SlObject(e, slt); if (IsSavegameVersionBefore(SLV_179)) { @@ -139,7 +139,7 @@ struct ENGSChunkHandler : ChunkHandler { SlCopy(names, lengthof(names), SLE_STRINGID); /* Copy each string into the temporary engine array. */ - for (EngineID engine = 0; engine < lengthof(names); engine++) { + for (EngineID engine = ENGINE_BEGIN; engine < lengthof(names); engine++) { Engine *e = GetTempDataEngine(engine); e->name = CopyFromOldName(names[engine]); } @@ -193,7 +193,7 @@ struct EIDSChunkHandler : ChunkHandler { while ((index = SlIterateArray()) != -1) { EngineIDMapping eid; SlObject(&eid, slt); - _engine_mngr.SetID(eid.type, eid.internal_id, eid.grfid, eid.substitute_id, index); + _engine_mngr.SetID(eid.type, eid.internal_id, eid.grfid, eid.substitute_id, static_cast(index)); } } }; diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 0a827f3ada..3b46a86433 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -378,7 +378,7 @@ static bool FixTTOEngines() /* Load the default engine set. Many of them will be overridden later */ { - uint j = 0; + EngineID j = ENGINE_BEGIN; for (uint16_t i = 0; i < lengthof(_orig_rail_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_TRAIN, i); for (uint16_t i = 0; i < lengthof(_orig_road_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_ROAD, i); for (uint16_t i = 0; i < lengthof(_orig_ship_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_SHIP, i); @@ -388,7 +388,7 @@ static bool FixTTOEngines() TimerGameCalendar::Date aging_date = std::min(TimerGameCalendar::date + CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR, TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year{2050}, 0, 1)); TimerGameCalendar::YearMonthDay aging_ymd = TimerGameCalendar::ConvertDateToYMD(aging_date); - for (EngineID i = 0; i < 256; i++) { + for (EngineID i = ENGINE_BEGIN; i < 256; i++) { OldEngineID oi = ttd_to_tto[i]; Engine *e = GetTempDataEngine(i); @@ -1455,13 +1455,13 @@ static const OldChunks engine_chunk[] = { static bool LoadOldEngine(LoadgameState &ls, int num) { - Engine *e = _savegame_type == SGT_TTO ? &_old_engines[num] : GetTempDataEngine(num); + Engine *e = _savegame_type == SGT_TTO ? &_old_engines[num] : GetTempDataEngine(static_cast(num)); return LoadChunk(ls, e, engine_chunk); } static bool LoadOldEngineName(LoadgameState &ls, int num) { - Engine *e = GetTempDataEngine(num); + Engine *e = GetTempDataEngine(static_cast(num)); e->name = CopyFromOldName(RemapOldStringID(ReadUint16(ls))); return true; }