diff --git a/src/engine.cpp b/src/engine.cpp index ccd5f0d914..afa54cb33a 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -508,11 +508,11 @@ bool Engine::IsVariantHidden(CompanyID c) const */ void EngineOverrideManager::ResetToDefaultMapping() { - EngineID id = ENGINE_BEGIN; + EngineID id = EngineID::Begin(); for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) { auto &map = this->mappings[type]; map.clear(); - for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++, id++) { + for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++, ++id) { map.emplace_back(INVALID_GRFID, internal_id, type, internal_id, id); } } @@ -610,7 +610,7 @@ void SetupEngines() assert(std::size(mapping) >= _engine_counts[type]); for (const EngineIDMapping &eid : mapping) { - new (eid.engine) Engine(type, eid.internal_id); + new (eid.engine.base()) Engine(type, eid.internal_id); } } } @@ -755,7 +755,7 @@ void StartupOneEngine(Engine *e, const TimerGameCalendar::YearMonthDay &aging_ym } SetRandomSeed(_settings_game.game_creation.generation_seed ^ seed ^ - (re->index << 16) ^ (re->info.base_intro.base() << 12) ^ (re->info.decay_speed << 8) ^ + (re->index.base() << 16) ^ (re->info.base_intro.base() << 12) ^ (re->info.decay_speed << 8) ^ (re->info.lifelength.base() << 4) ^ re->info.retire_early ^ e->type ^ e->GetGRFID()); diff --git a/src/engine_base.h b/src/engine_base.h index 5911ac27d3..15b28ee59a 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_type.h b/src/engine_type.h index 74299be2a8..02fc763cf7 100644 --- a/src/engine_type.h +++ b/src/engine_type.h @@ -21,12 +21,8 @@ #include "strings_type.h" /** 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) +using EngineID = PoolID; +static constexpr EngineID INVALID_ENGINE = EngineID::Invalid(); ///< Constant denoting an invalid engine. struct Engine; @@ -214,7 +210,7 @@ enum class EngineNameContext : uint8_t { /** Combine an engine ID and a name context to an engine name dparam. */ inline uint64_t PackEngineNameDParam(EngineID engine_id, EngineNameContext context, uint32_t extra_data = 0) { - return engine_id | (static_cast(context) << 32) | (static_cast(extra_data) << 40); + return engine_id.base() | (static_cast(context) << 32) | (static_cast(extra_data) << 40); } static const uint MAX_LENGTH_ENGINE_NAME_CHARS = 32; ///< The maximum length of an engine name in characters including '\0' diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 1f57980e89..74b3a22f75 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -345,7 +345,7 @@ struct GRFTempEngineData { } }; -static std::vector _gted; ///< Temporary engine data used during NewGRF loading +static ReferenceThroughBaseContainer> _gted; ///< Temporary engine data used during NewGRF loading /** * Contains the GRF ID of the owner of a vehicle if it has been reserved. @@ -6378,7 +6378,7 @@ static void FeatureNewName(ByteReader &buf) if (!generic) { Engine *e = GetNewEngine(_cur.grffile, (VehicleType)feature, id, _cur.grfconfig->flags.Test(GRFConfigFlag::Static)); if (e == nullptr) break; - StringID string = AddGRFString(_cur.grffile->grfid, GRFStringID{e->index}, lang, new_scheme, false, name, e->info.string_id); + StringID string = AddGRFString(_cur.grffile->grfid, GRFStringID{e->index.base()}, lang, new_scheme, false, name, e->info.string_id); e->info.string_id = string; } else { AddGRFString(_cur.grffile->grfid, GRFStringID{id}, lang, new_scheme, true, name, STR_UNDEFINED); @@ -9245,7 +9245,7 @@ static void FinaliseEngineArray() /* Do final mapping on variant engine ID. */ if (e->info.variant_id != INVALID_ENGINE) { - e->info.variant_id = GetNewEngineID(e->grf_prop.grffile, e->type, e->info.variant_id); + e->info.variant_id = GetNewEngineID(e->grf_prop.grffile, e->type, e->info.variant_id.base()); } if (!e->info.climates.Test(_settings_game.game_creation.landscape)) continue; diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 430bc4eed6..5f7b1ad06b 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), ENGINE_BEGIN); + std::iota(std::begin(ordering), std::end(ordering), EngineID::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 8a4486c1e6..cd5baf816e 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -924,7 +924,7 @@ uint32_t SerialiseNewsReference(const NewsReference &reference) uint32_t operator()(const StationID s) { return s; } uint32_t operator()(const IndustryID i) { return i; } uint32_t operator()(const TownID t) { return t; } - uint32_t operator()(const EngineID e) { return e; } + uint32_t operator()(const EngineID e) { return e.base(); } }; return std::visit(visitor{}, reference); diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index d42443b87e..d41fcdf43b 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -41,7 +41,7 @@ static const SaveLoad _engine_desc[] = { SLE_CONDSSTR(Engine, name, SLE_STR, SLV_84, SL_MAX_VERSION), }; -static std::vector _temp_engine; +static ReferenceThroughBaseContainer> _temp_engine; Engine *GetTempDataEngine(EngineID index) { @@ -134,12 +134,12 @@ struct ENGSChunkHandler : ChunkHandler { { /* Load old separate String ID list into a temporary array. This * was always 256 entries. */ - StringID names[256]; + ReferenceThroughBaseContainer> names{}; - SlCopy(names, lengthof(names), SLE_STRINGID); + SlCopy(names.data(), std::size(names), SLE_STRINGID); /* Copy each string into the temporary engine array. */ - for (EngineID engine = ENGINE_BEGIN; engine < lengthof(names); engine++) { + for (EngineID engine = EngineID::Begin(); engine < std::size(names); ++engine) { Engine *e = GetTempDataEngine(engine); e->name = CopyFromOldName(names[engine]); } diff --git a/src/saveload/oldloader_sl.cpp b/src/saveload/oldloader_sl.cpp index 378490f940..9640e85c8b 100644 --- a/src/saveload/oldloader_sl.cpp +++ b/src/saveload/oldloader_sl.cpp @@ -373,23 +373,23 @@ static bool FixTTOEngines() for (Vehicle *v : Vehicle::Iterate()) { if (v->engine_type >= lengthof(tto_to_ttd)) return false; - v->engine_type = static_cast(tto_to_ttd[v->engine_type]); + v->engine_type = static_cast(tto_to_ttd[v->engine_type.base()]); } /* Load the default engine set. Many of them will be overridden later */ { - 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); - for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++, j++) new (GetTempDataEngine(j)) Engine(VEH_AIRCRAFT, i); + EngineID j = EngineID::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); + for (uint16_t i = 0; i < lengthof(_orig_aircraft_vehicle_info); ++i, ++j) new (GetTempDataEngine(j)) Engine(VEH_AIRCRAFT, i); } 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 = ENGINE_BEGIN; i < 256; i++) { - OldEngineID oi = ttd_to_tto[i]; + for (EngineID i = EngineID::Begin(); i < 256; ++i) { + OldEngineID oi = ttd_to_tto[i.base()]; Engine *e = GetTempDataEngine(i); if (oi == 255) { diff --git a/src/script/api/script_enginelist.cpp b/src/script/api/script_enginelist.cpp index b3235cf20e..3b86bd5ebc 100644 --- a/src/script/api/script_enginelist.cpp +++ b/src/script/api/script_enginelist.cpp @@ -19,6 +19,6 @@ ScriptEngineList::ScriptEngineList(ScriptVehicle::VehicleType vehicle_type) bool is_deity = ScriptCompanyMode::IsDeity(); ::CompanyID owner = ScriptObject::GetCompany(); for (const Engine *e : Engine::IterateType((::VehicleType)vehicle_type)) { - if (is_deity || e->company_avail.Test(owner)) this->AddItem(e->index); + if (is_deity || e->company_avail.Test(owner)) this->AddItem(e->index.base()); } } diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index ccab1e57f4..5b7054f0d3 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -1519,7 +1519,7 @@ static bool VehicleMaxSpeedSorter(const Vehicle * const &a, const Vehicle * cons /** Sort vehicles by model */ static bool VehicleModelSorter(const Vehicle * const &a, const Vehicle * const &b) { - int r = a->engine_type - b->engine_type; + int r = a->engine_type.base() - b->engine_type.base(); return (r != 0) ? r < 0 : VehicleNumberSorter(a, b); }