From 6b5e4108a96e95fa67968556be397ccc95140673 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 4 Jun 2024 18:06:19 +0100 Subject: [PATCH] Codefix: Find GRF override entries properly. (#12743) My naive code from 16 years ago tested for GRF overrides in a way that will create empty 0 entries for each source GRF tested. (cherry picked from commit 579ce8eae0d6342077ce899d982f1675a3df602c) --- src/newgrf.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 966a1b4580..90a00a07ad 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -543,8 +543,13 @@ static btree::btree_map _grf_id_overrides; */ static void SetNewGRFOverride(uint32_t source_grfid, uint32_t target_grfid) { - _grf_id_overrides[source_grfid] = target_grfid; - GrfMsg(5, "SetNewGRFOverride: Added override of 0x{:X} to 0x{:X}", BSWAP32(source_grfid), BSWAP32(target_grfid)); + if (target_grfid == 0) { + _grf_id_overrides.erase(source_grfid); + GrfMsg(5, "SetNewGRFOverride: Removed override of 0x{:X}", BSWAP32(source_grfid)); + } else { + _grf_id_overrides[source_grfid] = target_grfid; + GrfMsg(5, "SetNewGRFOverride: Added override of 0x{:X} to 0x{:X}", BSWAP32(source_grfid), BSWAP32(target_grfid)); + } } /** @@ -563,14 +568,13 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte if (_settings_game.vehicle.dynamic_engines) { /* If dynamic_engies is enabled, there can be multiple independent ID ranges. */ scope_grfid = file->grfid; - uint32_t override = _grf_id_overrides[file->grfid]; - if (override != 0) { - scope_grfid = override; - const GRFFile *grf_match = GetFileByGRFID(override); + if (auto it = _grf_id_overrides.find(file->grfid); it != std::end(_grf_id_overrides)) { + scope_grfid = it->second; + const GRFFile *grf_match = GetFileByGRFID(scope_grfid); if (grf_match == nullptr) { - GrfMsg(5, "Tried mapping from GRFID {:x} to {:x} but target is not loaded", BSWAP32(file->grfid), BSWAP32(override)); + GrfMsg(5, "Tried mapping from GRFID {:x} to {:x} but target is not loaded", BSWAP32(file->grfid), BSWAP32(scope_grfid)); } else { - GrfMsg(5, "Mapping from GRFID {:x} to {:x}", BSWAP32(file->grfid), BSWAP32(override)); + GrfMsg(5, "Mapping from GRFID {:x} to {:x}", BSWAP32(file->grfid), BSWAP32(scope_grfid)); } } @@ -655,8 +659,9 @@ EngineID GetNewEngineID(const GRFFile *file, VehicleType type, uint16_t internal uint32_t scope_grfid = INVALID_GRFID; // If not using dynamic_engines, all newgrfs share their ID range if (_settings_game.vehicle.dynamic_engines) { scope_grfid = file->grfid; - uint32_t override = _grf_id_overrides[file->grfid]; - if (override != 0) scope_grfid = override; + if (auto it = _grf_id_overrides.find(file->grfid); it != std::end(_grf_id_overrides)) { + scope_grfid = it->second; + } } return _engine_mngr.GetID(type, internal_id, scope_grfid); @@ -11491,8 +11496,9 @@ static void FinalisePriceBaseMultipliers() grf_overrides[i] = -1; GRFFile *source = _grf_files[i]; - uint32_t override = _grf_id_overrides[source->grfid]; - if (override == 0) continue; + auto it = _grf_id_overrides.find(source->grfid); + if (it == std::end(_grf_id_overrides)) continue; + uint32_t override = it->second; GRFFile *dest = GetFileByGRFID(override); if (dest == nullptr) continue;