Skip to content

Commit

Permalink
SpawnManager: Defer insertion of spawns during SpawnManager::Update
Browse files Browse the repository at this point in the history
Also doing away with the sorting because its not being used

#640
  • Loading branch information
killerwife committed Nov 29, 2023
1 parent 517473c commit 2bd3e03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/game/Maps/SpawnManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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();)
{
Expand All @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/game/Maps/SpawnManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class SpawnManager
private:
Map& m_map;

std::vector<SpawnInfo> m_deferredSpawns;
std::vector<SpawnInfo> m_spawns; // must only be erased from in Update
std::map<uint32, SpawnGroup*> m_spawnGroups;
bool m_updated;
};

#endif

0 comments on commit 2bd3e03

Please sign in to comment.