diff --git a/src/engine.cpp b/src/engine.cpp index 74aa3d751c..f8d3710d33 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -509,14 +509,10 @@ bool Engine::IsVariantHidden(CompanyID c) const */ void EngineOverrideManager::ResetToDefaultMapping() { - this->clear(); + this->mappings.clear(); for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) { for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) { - EngineIDMapping &eid = this->emplace_back(); - eid.type = type; - eid.grfid = INVALID_GRFID; - eid.internal_id = internal_id; - eid.substitute_id = internal_id; + this->mappings.emplace_back(INVALID_GRFID, internal_id, type, internal_id); } } } @@ -533,7 +529,7 @@ void EngineOverrideManager::ResetToDefaultMapping() EngineID EngineOverrideManager::GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid) { EngineID index = 0; - for (const EngineIDMapping &eid : *this) { + for (const EngineIDMapping &eid : this->mappings) { if (eid.type == type && eid.grfid == grfid && eid.internal_id == grf_local_id) { return index; } @@ -568,9 +564,9 @@ void SetupEngines() CloseWindowByClass(WC_ENGINE_PREVIEW); _engine_pool.CleanPool(); - assert(_engine_mngr.size() >= _engine_mngr.NUM_DEFAULT_ENGINES); + assert(_engine_mngr.mappings.size() >= EngineOverrideManager::NUM_DEFAULT_ENGINES); [[maybe_unused]] uint index = 0; - for (const EngineIDMapping &eid : _engine_mngr) { + for (const EngineIDMapping &eid : _engine_mngr.mappings) { /* Assert is safe; there won't be more than 256 original vehicles * in any case, and we just cleaned the pool. */ assert(Engine::CanAllocateItem()); diff --git a/src/engine_base.h b/src/engine_base.h index 3d7ce8d207..3e9b7ab439 100644 --- a/src/engine_base.h +++ b/src/engine_base.h @@ -194,17 +194,23 @@ struct Engine : EnginePool::PoolItem<&_engine_pool> { }; struct EngineIDMapping { - uint32_t grfid; ///< The GRF ID of the file the entity belongs to - uint16_t internal_id; ///< The internal ID within the GRF file - VehicleType type; ///< The engine type - uint8_t substitute_id; ///< The (original) entity ID to use if this GRF is not available (currently not used) + uint32_t grfid; ///< The GRF ID of the file the entity belongs to + uint16_t internal_id; ///< The internal ID within the GRF file + VehicleType type; ///< The engine type + uint8_t substitute_id; ///< The (original) entity ID to use if this GRF is not available (currently not used) + + EngineIDMapping() {} + EngineIDMapping(uint32_t grfid, uint16_t internal_id, VehicleType type, uint8_t substitute_id) + : grfid(grfid), internal_id(internal_id),type(type), substitute_id(substitute_id) {} }; /** * Stores the mapping of EngineID to the internal id of newgrfs. * Note: This is not part of Engine, as the data in the EngineOverrideManager and the engine pool get resetted in different cases. */ -struct EngineOverrideManager : std::vector { +struct EngineOverrideManager { + std::vector mappings; + static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries void ResetToDefaultMapping(); diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 2a4449fd19..bbc623eeed 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -653,8 +653,8 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte /* Reserve the engine slot */ if (!static_access) { - EngineIDMapping *eid = _engine_mngr.data() + engine; - eid->grfid = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation + EngineIDMapping &eid = _engine_mngr.mappings[engine]; + eid.grfid = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation } return e; @@ -675,13 +675,13 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte e->grf_prop.grffile = file; /* Reserve the engine slot */ - assert(_engine_mngr.size() == e->index); - _engine_mngr.push_back({ + assert(_engine_mngr.mappings.size() == e->index); + _engine_mngr.mappings.emplace_back( scope_grfid, // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation internal_id, type, std::min(internal_id, _engine_counts[type]) // substitute_id == _engine_counts[subtype] means "no substitute" - }); + ); if (engine_pool_size != Engine::GetPoolSize()) { /* Resize temporary engine data ... */ @@ -9250,7 +9250,7 @@ static void FinaliseEngineArray() { for (Engine *e : Engine::Iterate()) { if (e->GetGRF() == nullptr) { - const EngineIDMapping &eid = _engine_mngr[e->index]; + const EngineIDMapping &eid = _engine_mngr.mappings[e->index]; if (eid.grfid != INVALID_GRFID || eid.internal_id != eid.substitute_id) { e->info.string_id = STR_NEWGRF_INVALID_ENGINE; } @@ -9302,7 +9302,7 @@ static void FinaliseEngineArray() /* Engine looped back on itself, so clear the variant. */ e->info.variant_id = INVALID_ENGINE; - GrfMsg(1, "FinaliseEngineArray: Variant of engine {:x} in '{}' loops back on itself", _engine_mngr[e->index].internal_id, e->GetGRF()->filename); + GrfMsg(1, "FinaliseEngineArray: Variant of engine {:x} in '{}' loops back on itself", _engine_mngr.mappings[e->index].internal_id, e->GetGRF()->filename); break; } diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index f74f53dda4..3be7c65101 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -1311,8 +1311,8 @@ void AlterVehicleListOrder(EngineID engine, uint target) */ static bool EnginePreSort(const EngineID &a, const EngineID &b) { - const EngineIDMapping &id_a = _engine_mngr.at(a); - const EngineIDMapping &id_b = _engine_mngr.at(b); + const EngineIDMapping &id_a = _engine_mngr.mappings.at(a); + const EngineIDMapping &id_b = _engine_mngr.mappings.at(b); /* 1. Sort by engine type */ if (id_a.type != id_b.type) return (int)id_a.type < (int)id_b.type; @@ -1341,10 +1341,10 @@ void CommitVehicleListOrderChanges() EngineID source = it.engine; uint local_target = it.target; - const EngineIDMapping *id_source = _engine_mngr.data() + source; - if (id_source->internal_id == local_target) continue; + const EngineIDMapping &id_source = _engine_mngr.mappings[source]; + if (id_source.internal_id == local_target) continue; - EngineID target = _engine_mngr.GetID(id_source->type, local_target, id_source->grfid); + EngineID target = _engine_mngr.GetID(id_source.type, local_target, id_source.grfid); if (target == INVALID_ENGINE) continue; int source_index = find_index(ordering, source); diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index ab62d756a8..b3584d4f8e 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -190,7 +190,7 @@ struct EIDSChunkHandler : ChunkHandler { SlTableHeader(_engine_id_mapping_desc); uint index = 0; - for (EngineIDMapping &eid : _engine_mngr) { + for (EngineIDMapping &eid : _engine_mngr.mappings) { SlSetArrayIndex(index); SlObject(&eid, _engine_id_mapping_desc); index++; @@ -201,10 +201,10 @@ struct EIDSChunkHandler : ChunkHandler { { const std::vector slt = SlCompatTableHeader(_engine_id_mapping_desc, _engine_id_mapping_sl_compat); - _engine_mngr.clear(); + _engine_mngr.mappings.clear(); while (SlIterateArray() != -1) { - EngineIDMapping *eid = &_engine_mngr.emplace_back(); + EngineIDMapping *eid = &_engine_mngr.mappings.emplace_back(); SlObject(eid, slt); } }