From 2bd3e030e1d201aa0b0a9e8c56770983bff4edd4 Mon Sep 17 00:00:00 2001 From: killerwife Date: Wed, 29 Nov 2023 18:04:07 +0100 Subject: [PATCH] SpawnManager: Defer insertion of spawns during SpawnManager::Update Also doing away with the sorting because its not being used https://github.com/cmangos/mangos-tbc/pull/640 --- src/game/Maps/SpawnManager.cpp | 24 ++++++++++++++++-------- src/game/Maps/SpawnManager.h | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/game/Maps/SpawnManager.cpp b/src/game/Maps/SpawnManager.cpp index b5237b37a7..95d46c972a 100644 --- a/src/game/Maps/SpawnManager.cpp +++ b/src/game/Maps/SpawnManager.cpp @@ -78,15 +78,19 @@ void SpawnManager::Initialize() void SpawnManager::AddCreature(uint32 dbguid) { time_t respawnTime = m_map.GetPersistentState()->GetCreatureRespawnTime(dbguid); - m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_UNIT); - std::sort(m_spawns.begin(), m_spawns.end()); + if (m_updated) + m_deferredSpawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_UNIT); + else + m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_UNIT); } void SpawnManager::AddGameObject(uint32 dbguid) { time_t respawnTime = m_map.GetPersistentState()->GetGORespawnTime(dbguid); - m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_GAMEOBJECT); - std::sort(m_spawns.begin(), m_spawns.end()); + if (m_updated) + m_deferredSpawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_GAMEOBJECT); + else + m_spawns.emplace_back(TimePoint(std::chrono::seconds(respawnTime)), dbguid, HIGHGUID_GAMEOBJECT); } void SpawnManager::RespawnCreature(uint32 dbguid, uint32 respawnDelay) @@ -113,8 +117,6 @@ void SpawnManager::RespawnCreature(uint32 dbguid, uint32 respawnDelay) } else if (respawnDelay == 0) (*itr).ConstructForMap(m_map); - if (respawnDelay > 0) - std::sort(m_spawns.begin(), m_spawns.end()); } void SpawnManager::RespawnGameObject(uint32 dbguid, uint32 respawnDelay) @@ -141,8 +143,6 @@ void SpawnManager::RespawnGameObject(uint32 dbguid, uint32 respawnDelay) } else if (respawnDelay == 0) (*itr).ConstructForMap(m_map); - if (respawnDelay > 0) - std::sort(m_spawns.begin(), m_spawns.end()); } void SpawnManager::RespawnAll() @@ -160,6 +160,12 @@ void SpawnManager::RespawnAll() void SpawnManager::Update() { + m_updated = true; + if (!m_deferredSpawns.empty()) // cannot insert during update + { + m_spawns.emplace_back(m_deferredSpawns); + m_deferredSpawns.clear(); + } auto now = m_map.GetCurrentClockTime(); for (auto itr = m_spawns.begin(); itr != m_spawns.end();) { @@ -170,7 +176,9 @@ void SpawnManager::Update() else ++itr; } + m_updated = false; + // spawn groups are safe from this for (auto& group : m_spawnGroups) group.second->Update(); } diff --git a/src/game/Maps/SpawnManager.h b/src/game/Maps/SpawnManager.h index d0354ddf3c..c1b3adb9dc 100644 --- a/src/game/Maps/SpawnManager.h +++ b/src/game/Maps/SpawnManager.h @@ -73,8 +73,10 @@ class SpawnManager private: Map& m_map; + std::vector m_deferredSpawns; std::vector m_spawns; // must only be erased from in Update std::map m_spawnGroups; + bool m_updated; }; #endif \ No newline at end of file