1
0
Fork 0

Codechange: Don't inherit EngineOverrideManager from std::vector.

Inheriting from std::vector means some operations are needlessly complex, and shouldn't really be done anyway.
pull/13124/head
Peter Nelson 2024-11-21 22:05:08 +00:00 committed by Peter Nelson
parent e73d6fcaac
commit bc2513975f
5 changed files with 31 additions and 29 deletions

View File

@ -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());

View File

@ -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<EngineIDMapping> {
struct EngineOverrideManager {
std::vector<EngineIDMapping> mappings;
static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries
void ResetToDefaultMapping();

View File

@ -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<uint8_t>(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;
}

View File

@ -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);

View File

@ -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<SaveLoad> 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);
}
}