mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-17 08:00:48 +01:00
Prevent duplicate spawns for same spawn ID
If there is an alive instance of the creature upon creature spawn, skip spawn entirely. If there are only dead instances, despawn them. A new dead corpse in the creatures home position will be respawned. Closes: #16462
This commit is contained in:
@@ -1292,8 +1292,38 @@ bool Creature::CreateFromProto(ObjectGuid::LowType guidlow, uint32 entry, Creatu
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Creature::LoadCreatureFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap)
|
||||
bool Creature::LoadCreatureFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap, bool allowDuplicate)
|
||||
{
|
||||
if (!allowDuplicate)
|
||||
{
|
||||
// If an alive instance of this spawnId is already found, skip creation
|
||||
// If only dead instance(s) exist, despawn them and spawn a new (maybe also dead) version
|
||||
const auto creatureBounds = map->GetCreatureBySpawnIdStore().equal_range(spawnId);
|
||||
std::vector <Creature*> despawnList;
|
||||
|
||||
if (creatureBounds.first != creatureBounds.second)
|
||||
{
|
||||
for (auto itr = creatureBounds.first; itr != creatureBounds.second; ++itr)
|
||||
{
|
||||
if (itr->second->IsAlive())
|
||||
{
|
||||
TC_LOG_DEBUG("maps", "Would have spawned %u but %s already exists", spawnId, creatureBounds.first->second->GetGUID().ToString().c_str());
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
despawnList.push_back(itr->second);
|
||||
TC_LOG_DEBUG("maps", "Despawned dead instance of spawn %u (%s)", spawnId, itr->second->GetGUID().ToString().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
for (Creature* despawnCreature : despawnList)
|
||||
{
|
||||
despawnCreature->AddObjectToRemoveList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CreatureData const* data = sObjectMgr->GetCreatureData(spawnId);
|
||||
|
||||
if (!data)
|
||||
|
||||
@@ -535,7 +535,7 @@ class Creature : public Unit, public GridObject<Creature>, public MapObject
|
||||
void setDeathState(DeathState s) override; // override virtual Unit::setDeathState
|
||||
|
||||
bool LoadFromDB(ObjectGuid::LowType spawnId, Map* map) { return LoadCreatureFromDB(spawnId, map, false); }
|
||||
bool LoadCreatureFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap = true);
|
||||
bool LoadCreatureFromDB(ObjectGuid::LowType spawnId, Map* map, bool addToMap = true, bool allowDuplicate = false);
|
||||
void SaveToDB();
|
||||
// overriden in Pet
|
||||
virtual void SaveToDB(uint32 mapid, uint8 spawnMask, uint32 phaseMask);
|
||||
|
||||
Reference in New Issue
Block a user