TrinityCore
Loading...
Searching...
No Matches
ObjectGridLoader.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "ObjectGridLoader.h"
19#include "AreaTrigger.h"
21#include "CellImpl.h"
22#include "Conversation.h"
23#include "Corpse.h"
24#include "Creature.h"
25#include "DynamicObject.h"
26#include "GameObject.h"
27#include "GameTime.h"
28#include "Log.h"
29#include "ObjectMgr.h"
30#include "PhasingHandler.h"
31#include "SceneObject.h"
32
34{
35 // creature in unloading grid can have respawn point in another grid
36 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
37 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
38 for (CreatureMapType::iterator iter = m.begin(); iter != m.end();)
39 {
40 Creature* c = iter->GetSource();
41 ++iter;
42
43 ASSERT(!c->IsPet() && "ObjectGridRespawnMover must not be called for pets");
44 c->GetMap()->CreatureRespawnRelocation(c, true);
45 }
46}
47
49{
50 // gameobject in unloading grid can have respawn point in another grid
51 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
52 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
53 for (GameObjectMapType::iterator iter = m.begin(); iter != m.end();)
54 {
55 GameObject* go = iter->GetSource();
56 ++iter;
57
58 go->GetMap()->GameObjectRespawnRelocation(go, true);
59 }
60}
61
62template <class T>
63void ObjectGridLoaderBase::AddToMap(T* obj, Map* map, uint32& objectCount)
64{
65 CellCoord cellCoord = Trinity::ComputeCellCoord(obj->GetPositionX(), obj->GetPositionY());
66 Cell cell(cellCoord);
67
68 map->AddToGrid<T>(obj, cell);
69 obj->AddToWorld();
70 if (obj->isActiveObject())
71 map->AddToActive(obj);
72
73 ++objectCount;
74}
75
76template <class T>
77void LoadHelper(GridGuidSet const& guid_set, uint32& count, Map* map, uint32 phaseId = 0, Optional<ObjectGuid> phaseOwner = {})
78{
79 for (GridGuidSet::const_iterator i_guid = guid_set.begin(); i_guid != guid_set.end(); ++i_guid)
80 {
81 // Don't spawn at all if there's a respawn timer
82 ObjectGuid::LowType guid = *i_guid;
83 if (!map->ShouldBeSpawnedOnGridLoad<T>(guid))
84 continue;
85
86 T* obj = new T;
87 //TC_LOG_INFO("misc", "DEBUG: LoadHelper from table: {} for (guid: {}) Loading", table, guid);
88 if (!obj->LoadFromDB(guid, map, false, phaseOwner.has_value() /*allowDuplicate*/))
89 {
90 delete obj;
91 continue;
92 }
93
94 if (phaseOwner)
95 {
96 PhasingHandler::InitDbPersonalOwnership(obj->GetPhaseShift(), *phaseOwner);
97 map->GetMultiPersonalPhaseTracker().RegisterTrackedObject(phaseId, *phaseOwner, obj);
98 }
99
100 ObjectGridLoaderBase::AddToMap(obj, map, count);
101 }
102}
103
105{
107
108 //Load creatures and game objects
109 if (GridObjectGuids const* grid_guids = sObjectMgr->GetGridObjectGuids(i_map->GetId(), i_map->GetDifficultyID(), i_grid.GetGridId()))
110 {
111 LoadHelper<GameObject>(grid_guids->gameobjects, i_gameObjects, i_map);
112 LoadHelper<Creature>(grid_guids->creatures, i_creatures, i_map);
113 }
114
115 //Load areatriggers
116 if (GridGuidSet const* areaTriggers = sAreaTriggerDataStore->GetAreaTriggersForMapAndGrid(i_map->GetId(), i_map->GetDifficultyID(), i_grid.GetGridId()))
117 LoadHelper<AreaTrigger>(*areaTriggers, i_areaTriggers, i_map);
118
119 //Load corpses (not bones)
120 if (std::unordered_set<Corpse*> const* corpses = i_map->GetCorpsesInGrid(i_grid.GetGridId()))
121 for (Corpse* corpse : *corpses)
122 AddToMap(corpse, i_map, i_corpses);
123
124 TC_LOG_DEBUG("maps", "{} GameObjects, {} Creatures, {} AreaTrriggers, and {} Corpses/Bones loaded for grid {} on map {}",
126}
127
129{
130 if (GridObjectGuids const* grid_guids = sObjectMgr->GetCellPersonalObjectGuids(i_map->GetId(), i_map->GetDifficultyID(), phaseId, i_grid.GetGridId()))
131 {
132 LoadHelper<GameObject>(grid_guids->gameobjects, i_gameObjects, i_map, phaseId, _phaseOwner);
133 LoadHelper<Creature>(grid_guids->creatures, i_creatures, i_map, phaseId, _phaseOwner);
134 }
135}
136
137template<class T>
139{
140 while (!m.empty())
141 {
142 T* obj = m.front()->GetSource();
143 //Some creatures may summon other temp summons in CleanupsBeforeDelete()
144 //So we need this even after cleaner (maybe we can remove cleaner)
145 //Example: Flame Leviathan Turret 33139 is summoned when a creature is deleted
147 obj->CleanupsBeforeDelete();
149 delete obj;
150 }
151}
152
154{
155 // stop any fights at grid de-activation and remove dynobjects/areatriggers created at cast by creatures
156 for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
157 {
158 iter->GetSource()->RemoveAllDynObjects();
159 iter->GetSource()->RemoveAllAreaTriggers();
160 if (iter->GetSource()->IsInCombat())
161 iter->GetSource()->CombatStop();
162 }
163}
164
165template<class T>
167{
168 for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
169 {
170 iter->GetSource()->SetDestroyedObject(true);
171 iter->GetSource()->CleanupsBeforeDelete();
172 }
173}
174
179
186
188template void ObjectGridCleaner::Visit<GameObject>(GameObjectMapType &);
189template void ObjectGridCleaner::Visit<DynamicObject>(DynamicObjectMapType &);
190template void ObjectGridCleaner::Visit<Corpse>(CorpseMapType &);
191template void ObjectGridCleaner::Visit<AreaTrigger>(AreaTriggerMapType &);
192template void ObjectGridCleaner::Visit<SceneObject>(SceneObjectMapType &);
193template void ObjectGridCleaner::Visit<Conversation>(ConversationMapType &);
#define sAreaTriggerDataStore
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:72
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:173
void LoadHelper(GridGuidSet const &guid_set, uint32 &count, Map *map, uint32 phaseId=0, Optional< ObjectGuid > phaseOwner={})
#define sObjectMgr
Definition ObjectMgr.h:1873
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
bool empty() const
Definition LinkedList.h:107
Definition Map.h:227
bool GameObjectRespawnRelocation(GameObject *go, bool diffGridOnly)
Definition Map.cpp:1552
MultiPersonalPhaseTracker & GetMultiPersonalPhaseTracker()
Definition Map.h:838
Difficulty GetDifficultyID() const
Definition Map.h:363
void AddToGrid(T *object, Cell const &cell)
Definition Map.cpp:175
uint32 GetId() const
Definition Map.cpp:3288
void AddToActive(WorldObject *obj)
Definition Map.cpp:2772
std::unordered_set< Corpse * > const * GetCorpsesInGrid(uint32 cellId) const
Definition Map.h:477
bool CreatureRespawnRelocation(Creature *c, bool diffGridOnly)
Definition Map.cpp:1522
bool ShouldBeSpawnedOnGridLoad(SpawnObjectType type, ObjectGuid::LowType spawnId) const
Definition Map.cpp:2331
uint32 GetGridId(void) const
Definition NGrid.h:90
void Visit(GridRefManager< T > &)
void Visit(CreatureMapType &m)
static void AddToMap(T *obj, Map *map, uint32 &objectCount)
void Visit(CreatureMapType &m)
void Visit(CorpseMapType &)
uint64 LowType
Definition ObjectGuid.h:321
void Load(uint32 phaseId)
static void InitDbPersonalOwnership(PhaseShift &phaseShift, ObjectGuid const &personalGuid)
iterator end()
Definition RefManager.h:36
iterator begin()
Definition RefManager.h:35
ReferenceType * front()
Definition RefManager.h:32
typename KeyContainer::const_iterator const_iterator
Definition FlatSet.h:31
bool IsPet() const
Definition Unit.h:763
Map * GetMap() const
Definition Object.h:411
constexpr CellCoord ComputeCellCoord(float x, float y)
Definition Cell.h:38
void RegisterTrackedObject(uint32 phaseId, ObjectGuid const &phaseOwner, WorldObject *object)