Skip to content

Commit

Permalink
Dynguid: Refactor non spawn group dynguid code to account for despawn…
Browse files Browse the repository at this point in the history
… on grid load
  • Loading branch information
killerwife committed Nov 21, 2023
1 parent 543c52d commit fd90b98
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,12 @@ void instance_sunwell_plateau::SetData(uint32 type, uint32 data)
for (uint32 i = 0; i < entries.size(); ++i)
{
if (Creature* bossNpc = GetSingleCreatureFromStorage(entries[i]))
{
bossNpc->SetRespawnDelay(30, true);
bossNpc->ForcedDespawn();
instance->GetSpawnManager().AddCreature(30, guids[i]);
}
else
instance->GetSpawnManager().RespawnCreature(guids[i], 30);
}
DespawnGuids(m_twinsSpawns);
}
Expand Down
11 changes: 9 additions & 2 deletions src/game/Entities/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1636,14 +1636,21 @@ bool Creature::LoadFromDB(uint32 dbGuid, Map* map, uint32 newGuid, uint32 forced
m_respawnradius = data->spawndist;

m_respawnDelay = data->GetRandomRespawnTime();
if (!IsUsingNewSpawningSystem())
bool isUsingNewSpawningSystem = IsUsingNewSpawningSystem();
if (!isUsingNewSpawningSystem)
m_corpseDelay = std::min(m_respawnDelay * 9 / 10, m_corpseDelay); // set corpse delay to 90% of the respawn delay
m_deathState = ALIVE;

m_respawnTime = map->GetPersistentState()->GetCreatureRespawnTime(dbGuid);

if (m_respawnTime > time(nullptr)) // not ready to respawn
{
if (isUsingNewSpawningSystem && !group) // only at this point we know if marked as dynguid per entry
{
GetMap()->GetPersistentState()->RemoveCreatureFromGrid(GetDbGuid(), data);
GetMap()->GetSpawnManager().AddCreature(GetDbGuid());
return false;
}
m_deathState = DEAD;
SetHealth(0);
if (CanFly())
Expand Down Expand Up @@ -1833,7 +1840,7 @@ void Creature::SetDeathState(DeathState s)
{
m_respawnTime = std::numeric_limits<time_t>::max();
if (m_respawnDelay && s == JUST_DIED && !GetCreatureGroup())
GetMap()->GetSpawnManager().AddCreature(m_respawnDelay, GetDbGuid());
GetMap()->GetSpawnManager().AddCreature(GetDbGuid());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/Entities/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ void GameObject::Update(const uint32 diff)
{
m_respawnTime = std::numeric_limits<time_t>::max();
if (m_respawnDelay && !GetGameObjectGroup())
GetMap()->GetSpawnManager().AddGameObject(m_respawnDelay, GetDbGuid());
GetMap()->GetSpawnManager().AddGameObject(GetDbGuid());

if (m_respawnDelay || !m_spawnedByDefault || m_forcedDespawn)
AddObjectToRemoveList();
Expand Down
12 changes: 6 additions & 6 deletions src/game/Maps/MapPersistentStateMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,15 +1023,15 @@ void MapPersistentStateManager::LoadCreatureRespawnTimes()
Field* fields = queryResult->Fetch();
bar.step();

uint32 loguid = fields[0].GetUInt32();
uint32 dbGuid = fields[0].GetUInt32();
uint64 respawn_time = fields[1].GetUInt64();
uint32 mapId = fields[2].GetUInt32();
uint32 instanceId = fields[3].GetUInt32();
uint8 difficulty = fields[4].GetUInt8();
time_t resetTime = (time_t)fields[5].GetUInt64();
uint32 completedEncounters = fields[6].GetUInt32();

CreatureData const* data = sObjectMgr.GetCreatureData(loguid);
CreatureData const* data = sObjectMgr.GetCreatureData(dbGuid);
if (!data)
continue;

Expand All @@ -1057,7 +1057,7 @@ void MapPersistentStateManager::LoadCreatureRespawnTimes()
if (!state)
continue;

state->SetCreatureRespawnTime(loguid, time_t(respawn_time));
state->SetCreatureRespawnTime(dbGuid, time_t(respawn_time));

++count;
}
Expand Down Expand Up @@ -1093,15 +1093,15 @@ void MapPersistentStateManager::LoadGameobjectRespawnTimes()
Field* fields = queryResult->Fetch();
bar.step();

uint32 loguid = fields[0].GetUInt32();
uint32 dbGuid = fields[0].GetUInt32();
uint64 respawn_time = fields[1].GetUInt64();
uint32 mapId = fields[2].GetUInt32();
uint32 instanceId = fields[3].GetUInt32();
uint8 difficulty = fields[4].GetUInt8();
time_t resetTime = (time_t)fields[5].GetUInt64();
uint32 completedEncounters = fields[6].GetUInt32();

GameObjectData const* data = sObjectMgr.GetGOData(loguid);
GameObjectData const* data = sObjectMgr.GetGOData(dbGuid);
if (!data)
continue;

Expand All @@ -1127,7 +1127,7 @@ void MapPersistentStateManager::LoadGameobjectRespawnTimes()
if (!state)
continue;

state->SetGORespawnTime(loguid, time_t(respawn_time));
state->SetGORespawnTime(dbGuid, time_t(respawn_time));

++count;
}
Expand Down
20 changes: 14 additions & 6 deletions src/game/Maps/SpawnManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ void SpawnManager::Initialize()
}
}

void SpawnManager::AddCreature(uint32 respawnDelay, uint32 dbguid)
void SpawnManager::AddCreature(uint32 dbguid)
{
m_spawns.emplace_back(m_map.GetCurrentClockTime() + std::chrono::seconds(respawnDelay), dbguid, HIGHGUID_UNIT);
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());
}

void SpawnManager::AddGameObject(uint32 respawnDelay, uint32 dbguid)
void SpawnManager::AddGameObject(uint32 dbguid)
{
m_spawns.emplace_back(m_map.GetCurrentClockTime() + std::chrono::seconds(respawnDelay), dbguid, HIGHGUID_GAMEOBJECT);
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());
}

Expand All @@ -105,7 +107,10 @@ void SpawnManager::RespawnCreature(uint32 dbguid, uint32 respawnDelay)
++itr;
}
if (!found)
AddCreature(respawnDelay, dbguid);
{
m_map.GetPersistentState()->SaveCreatureRespawnTime(dbguid, time(nullptr) + respawnDelay);
AddCreature(dbguid);
}
else if (respawnDelay == 0)
(*itr).ConstructForMap(m_map);
if (respawnDelay > 0)
Expand All @@ -130,7 +135,10 @@ void SpawnManager::RespawnGameObject(uint32 dbguid, uint32 respawnDelay)
++itr;
}
if (!found)
AddGameObject(respawnDelay, dbguid);
{
m_map.GetPersistentState()->SaveGORespawnTime(dbguid, time(nullptr) + respawnDelay);
AddGameObject(dbguid);
}
else if (respawnDelay == 0)
(*itr).ConstructForMap(m_map);
if (respawnDelay > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/game/Maps/SpawnManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class SpawnManager
~SpawnManager();
void Initialize();

void AddCreature(uint32 respawnDelay, uint32 dbguid);
void AddGameObject(uint32 respawnDelay, uint32 dbguid);
void AddCreature(uint32 dbguid);
void AddGameObject(uint32 dbguid);

void RespawnCreature(uint32 dbguid, uint32 respawnDelay = 0); // seconds
void RespawnGameObject(uint32 dbguid, uint32 respawnDelay = 0); // seconds
Expand Down

0 comments on commit fd90b98

Please sign in to comment.