From 4762ed8f8cd0be1454b76055e79b3c65eea8a570 Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 15 Nov 2024 19:07:50 +0100 Subject: Core/DataStores: Rewrite DB2Manager::GetDefaultMapDifficulty to only return entries that point to valid Difficulty.db2 rows (cherry picked from commit 441d7e5dc1522680d52fde03e0f3cbe751350f3b) --- src/server/game/DataStores/DB2Stores.cpp | 36 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'src/server') diff --git a/src/server/game/DataStores/DB2Stores.cpp b/src/server/game/DataStores/DB2Stores.cpp index 734c0caa392..fe635b9ffd2 100644 --- a/src/server/game/DataStores/DB2Stores.cpp +++ b/src/server/game/DataStores/DB2Stores.cpp @@ -2094,32 +2094,34 @@ uint32 DB2Manager::GetLiquidFlags(uint32 liquidType) MapDifficultyEntry const* DB2Manager::GetDefaultMapDifficulty(uint32 mapId, Difficulty* difficulty /*= nullptr*/) const { - auto itr = _mapDifficulties.find(mapId); - if (itr == _mapDifficulties.end()) + std::unordered_map* difficultiesForMap = Trinity::Containers::MapGetValuePtr(_mapDifficulties, mapId); + if (!difficultiesForMap) return nullptr; - if (itr->second.empty()) - return nullptr; + auto difficultyEnd = difficultiesForMap->end(); - for (auto& p : itr->second) - { - DifficultyEntry const* difficultyEntry = sDifficultyStore.LookupEntry(p.first); - if (!difficultyEntry) - continue; + // first find any valid difficulty + auto foundDifficulty = std::ranges::find_if(difficultiesForMap->begin(), difficultyEnd, + [](std::pair const& p) { return sDifficultyStore.HasRecord(p.first); }); - if (difficultyEntry->Flags & DIFFICULTY_FLAG_DEFAULT) - { - if (difficulty) - *difficulty = Difficulty(p.first); + if (foundDifficulty == difficultyEnd) + return nullptr; // nothing valid was found - return p.second; - } + if (!(sDifficultyStore.AssertEntry(foundDifficulty->first)->Flags & DIFFICULTY_FLAG_DEFAULT)) + { + // first valid difficulty wasn't default, try finding one + auto defaultDifficulty = std::ranges::find_if(foundDifficulty, difficultyEnd, + [](DifficultyEntry const* difficultyEntry) { return difficultyEntry && (difficultyEntry->Flags & DIFFICULTY_FLAG_DEFAULT) != 0; }, + [](std::pair const& p) { return sDifficultyStore.LookupEntry(p.first); }); + + if (defaultDifficulty != difficultyEnd) + foundDifficulty = defaultDifficulty; // got a default } if (difficulty) - *difficulty = Difficulty(itr->second.begin()->first); + *difficulty = Difficulty(foundDifficulty->first); - return itr->second.begin()->second; + return foundDifficulty->second; } MapDifficultyEntry const* DB2Manager::GetMapDifficultyData(uint32 mapId, Difficulty difficulty) const -- cgit v1.2.3