diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp index 217e6d7ec..d2b65acab 100644 --- a/src/game/Creature.cpp +++ b/src/game/Creature.cpp @@ -1353,6 +1353,19 @@ bool Creature::HasInvolvedQuest(uint32 quest_id) const return false; } + +struct CreatureRespawnDeleteWorker +{ + explicit CreatureRespawnDeleteWorker(uint32 guid) : i_guid(guid) {} + + void operator() (MapPersistentState* state) + { + state->SaveCreatureRespawnTime(i_guid, 0); + } + + uint32 i_guid; +}; + void Creature::DeleteFromDB() { if (!m_DBTableGuid) @@ -1361,9 +1374,7 @@ void Creature::DeleteFromDB() return; } - // FIXME: this not safe for another map copies can be - if (MapPersistentState* save = sMapPersistentStateMgr.GetPersistentState(GetMapId(), GetInstanceId())) - save->SaveCreatureRespawnTime(m_DBTableGuid, 0); + sMapPersistentStateMgr.DoForAllStatesWithMapId(GetMapId(), CreatureRespawnDeleteWorker(m_DBTableGuid)); sObjectMgr.DeleteCreatureData(m_DBTableGuid); diff --git a/src/game/GameObject.cpp b/src/game/GameObject.cpp index a5a23f215..7012e4b4b 100644 --- a/src/game/GameObject.cpp +++ b/src/game/GameObject.cpp @@ -618,11 +618,28 @@ bool GameObject::LoadFromDB(uint32 guid, Map *map) return true; } +struct GameObjectRespawnDeleteWorker +{ + explicit GameObjectRespawnDeleteWorker(uint32 guid) : i_guid(guid) {} + + void operator() (MapPersistentState* state) + { + state->SaveGORespawnTime(i_guid, 0); + } + + uint32 i_guid; +}; + + void GameObject::DeleteFromDB() { - // FIXME: this can be not safe in case multiply loaded instance copies - if (MapPersistentState* save = sMapPersistentStateMgr.GetPersistentState(GetMapId(), GetInstanceId())) - save->SaveGORespawnTime(m_DBTableGuid, 0); + if (!m_DBTableGuid) + { + DEBUG_LOG("Trying to delete not saved gameobject!"); + return; + } + + sMapPersistentStateMgr.DoForAllStatesWithMapId(GetMapId(), GameObjectRespawnDeleteWorker(m_DBTableGuid)); sObjectMgr.DeleteGOData(m_DBTableGuid); WorldDatabase.PExecuteLog("DELETE FROM gameobject WHERE guid = '%u'", m_DBTableGuid); diff --git a/src/game/MapManager.h b/src/game/MapManager.h index 79355a18e..d920d988c 100644 --- a/src/game/MapManager.h +++ b/src/game/MapManager.h @@ -150,6 +150,9 @@ class MANGOS_DLL_DECL MapManager : public MaNGOS::Singleton + void DoForAllMapsWithMapId(uint32 mapId, Do& _do); + private: // debugging code, should be deleted some day @@ -176,6 +179,16 @@ class MANGOS_DLL_DECL MapManager : public MaNGOS::Singleton +inline void MapManager::DoForAllMapsWithMapId(uint32 mapId, Do& _do) +{ + MapMapType::const_iterator start = i_maps.lower_bound(MapID(mapId,0)); + MapMapType::const_iterator end = i_maps.lower_bound(MapID(mapId+1,0)); + for(MapMapType::const_iterator itr = start; itr != end; ++itr) + _do(itr->second); +} + + #define sMapMgr MapManager::Instance() #endif diff --git a/src/game/MapPersistentStateMgr.h b/src/game/MapPersistentStateMgr.h index 27caf84ac..e14f68a95 100644 --- a/src/game/MapPersistentStateMgr.h +++ b/src/game/MapPersistentStateMgr.h @@ -297,6 +297,9 @@ class MANGOS_DLL_DECL MapPersistentStateManager : public MaNGOS::Singleton + void DoForAllStatesWithMapId(uint32 mapId, Do& _do); + public: // DungeonPersistentState specific void CleanupInstances(); void PackInstances(); @@ -329,5 +332,30 @@ class MANGOS_DLL_DECL MapPersistentStateManager : public MaNGOS::Singleton +inline void MapPersistentStateManager::DoForAllStatesWithMapId(uint32 mapId, Do& _do) +{ + MapEntry const* mapEntry = sMapStore.LookupEntry(mapId); + if (!mapEntry) + return; + + if (mapEntry->Instanceable()) + { + for(PersistentStateMap::iterator itr = m_instanceSaveByInstanceId.begin(); itr != m_instanceSaveByInstanceId.end();) + { + if (itr->second->GetMapId() == mapId) + _do((itr++)->second); + else + ++itr; + } + + } + else + { + if (MapPersistentState* state = GetPersistentState(mapId, 0)) + _do(state); + } +} + #define sMapPersistentStateMgr MaNGOS::Singleton::Instance() #endif diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index caf9becb2..cf2896320 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "11132" + #define REVISION_NR "11133" #endif // __REVISION_NR_H__