Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
ObjectGridLoader.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "AreaTrigger.h"
7#include "CellImpl.h"
8#include "Corpse.h"
9#include "Creature.h"
10#include "CreatureAI.h"
11#include "DynamicObject.h"
12#include "GameObject.h"
13#include "ObjectAccessor.h"
14#include "ObjectGridLoader.h"
15#include "ObjectMgr.h"
16#include "Vehicle.h"
17#include "World.h"
18
20{
21 // creature in unloading grid can have respawn point in another grid
22 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
23 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
24 for (CreatureMapType::iterator iter = m.begin(); iter != m.end();)
25 {
26 Creature* c = iter->GetSource();
27 ++iter;
28
29 ASSERT(!c->IsPet() && "ObjectGridRespawnMover must not be called for pets");
30 c->GetMap()->CreatureRespawnRelocation(c, true);
31 }
32}
33
35{
36 // gameobject in unloading grid can have respawn point in another grid
37 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
38 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
39 for (GameObjectMapType::iterator iter = m.begin(); iter != m.end();)
40 {
41 GameObject* go = iter->GetSource();
42 ++iter;
43
44 go->GetMap()->GameObjectRespawnRelocation(go, true);
45 }
46}
47
48// for loading world object at grid loading (Corpses)
51{
52public:
54 : i_cell(gloader.i_cell), i_map(gloader.i_map), i_corpses(0)
55 { }
56
57 void Visit(CorpseMapType& m);
58
59 template<class T> void Visit(GridRefManager<T>&) { }
60
61private:
64public:
66};
67
68template<class T> void ObjectGridLoader::SetObjectCell(T* /*obj*/, CellCoord const& /*cellCoord*/) { }
69
70template<> void ObjectGridLoader::SetObjectCell(Creature* obj, CellCoord const& cellCoord)
71{
72 Cell cell(cellCoord);
73 obj->SetCurrentCell(cell);
74}
75
76template<> void ObjectGridLoader::SetObjectCell(GameObject* obj, CellCoord const& cellCoord)
77{
78 Cell cell(cellCoord);
79 obj->SetCurrentCell(cell);
80}
81
82template <class T>
83void AddObjectHelper(CellCoord& cell, GridRefManager<T>& m, uint32& count, Map* /*map*/, T* obj)
84{
85 obj->AddToGrid(m);
87 obj->AddToWorld();
88 ++count;
89}
90
91template <>
92void AddObjectHelper(CellCoord& cell, CreatureMapType& m, uint32& count, Map* map, Creature* obj)
93{
94 obj->AddToGrid(m);
96 obj->AddToWorld();
97 if (obj->isActiveObject())
98 map->AddToActive(obj);
99
100 ++count;
101}
102
103template <class T>
104void LoadHelper(CellGuidSet const& guid_set, CellCoord& cell, GridRefManager<T>& m, uint32& count, Map* map)
105{
106 for (CellGuidSet::const_iterator i_guid = guid_set.begin(); i_guid != guid_set.end(); ++i_guid)
107 {
108 T* obj = new T;
109 uint32 guid = *i_guid;
110 //SF_LOG_INFO("misc", "DEBUG: LoadHelper from table: %s for (guid: %u) Loading", table, guid);
111 if (!obj->LoadFromDB(guid, map))
112 {
113 delete obj;
114 continue;
115 }
116
117 AddObjectHelper(cell, m, count, map, obj);
118 }
119}
120
121void LoadHelper(CellCorpseSet const& cell_corpses, CellCoord& cell, CorpseMapType& m, uint32& count, Map* map)
122{
123 if (cell_corpses.empty())
124 return;
125
126 for (CellCorpseSet::const_iterator itr = cell_corpses.begin(); itr != cell_corpses.end(); ++itr)
127 {
128 if (itr->second != map->GetInstanceId())
129 continue;
130
131 uint32 player_guid = itr->first;
132
133 Corpse* obj = sObjectAccessor->GetCorpseForPlayerGUID(player_guid);
134 if (!obj)
135 continue;
136
138 // corpse's map should be reset when the map is unloaded
139 // but it may still exist when the grid is unloaded but map is not
140 // in that case map == currMap
141 obj->SetMap(map);
142
143 if (obj->IsInGrid())
144 {
145 obj->AddToWorld();
146 continue;
147 }
148
149 AddObjectHelper(cell, m, count, map, obj);
150 }
151}
152
154{
155 CellCoord cellCoord = i_cell.GetCellCoord();
156 CellObjectGuids const& cell_guids = sObjectMgr->GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cellCoord.GetId());
157 LoadHelper(cell_guids.gameobjects, cellCoord, m, i_gameObjects, i_map);
158}
159
161{
162 CellCoord cellCoord = i_cell.GetCellCoord();
163 CellObjectGuids const& cell_guids = sObjectMgr->GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cellCoord.GetId());
164 LoadHelper(cell_guids.creatures, cellCoord, m, i_creatures, i_map);
165}
166
168{
169 CellCoord cellCoord = i_cell.GetCellCoord();
170 // corpses are always added to spawn mode 0 and they are spawned by their instance id
171 CellObjectGuids const& cell_guids = sObjectMgr->GetCellObjectGuids(i_map->GetId(), 0, cellCoord.GetId());
172 LoadHelper(cell_guids.corpses, cellCoord, m, i_corpses, i_map);
173}
174
176{
177 i_gameObjects = 0; i_creatures = 0; i_corpses = 0;
178 i_cell.data.Part.cell_y = 0;
179 for (unsigned int x = 0; x < MAX_NUMBER_OF_CELLS; ++x)
180 {
181 i_cell.data.Part.cell_x = x;
182 for (unsigned int y = 0; y < MAX_NUMBER_OF_CELLS; ++y)
183 {
184 i_cell.data.Part.cell_y = y;
185
186 //Load creatures and game objects
187 {
189 i_grid.VisitGrid(x, y, visitor);
190 }
191
192 //Load corpses (not bones)
193 {
194 ObjectWorldLoader worker(*this);
196 i_grid.VisitGrid(x, y, visitor);
197 i_corpses += worker.i_corpses;
198 }
199 }
200 }
201 SF_LOG_DEBUG("maps", "%u GameObjects, %u Creatures, and %u Corpses/Bones loaded for grid %u on map %u", i_gameObjects, i_creatures, i_corpses, i_grid.GetGridId(), i_map->GetId());
202}
203
204template<class T>
206{
207 while (!m.isEmpty())
208 {
209 T* obj = m.getFirst()->GetSource();
210 // if option set then object already saved at this moment
212 obj->SaveRespawnTime();
213 //Some creatures may summon other temp summons in CleanupsBeforeDelete()
214 //So we need this even after cleaner (maybe we can remove cleaner)
215 //Example: Flame Leviathan Turret 33139 is summoned when a creature is deleted
217 obj->CleanupsBeforeDelete();
219 delete obj;
220 }
221}
222
224{
225 // stop any fights at grid de-activation and remove dynobjects created at cast by creatures
226 for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
227 {
228 iter->GetSource()->RemoveAllDynObjects();
229 if (iter->GetSource()->IsInCombat())
230 {
231 iter->GetSource()->CombatStop();
232 iter->GetSource()->DeleteThreatList();
233 iter->GetSource()->AI()->EnterEvadeMode();
234 }
235 }
236}
237
238template<class T>
240{
241 for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
242 iter->GetSource()->CleanupsBeforeDelete();
243}
244
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
GridRefManager< Corpse > CorpseMapType
Definition GridDefines.h:50
GridRefManager< DynamicObject > DynamicObjectMapType
Definition GridDefines.h:52
#define MAX_NUMBER_OF_CELLS
Definition GridDefines.h:22
GridRefManager< Creature > CreatureMapType
Definition GridDefines.h:51
GridRefManager< AreaTrigger > AreaTriggerMapType
Definition GridDefines.h:55
CoordPair< TOTAL_NUMBER_OF_CELLS_PER_MAP > CellCoord
GridRefManager< GameObject > GameObjectMapType
Definition GridDefines.h:53
#define SF_LOG_DEBUG(filterType__,...)
Definition Log.h:134
#define sObjectAccessor
void LoadHelper(CellGuidSet const &guid_set, CellCoord &cell, GridRefManager< T > &m, uint32 &count, Map *map)
void AddObjectHelper(CellCoord &cell, GridRefManager< T > &m, uint32 &count, Map *, T *obj)
std::set< uint32 > CellGuidSet
Definition ObjectMgr.h:413
std::map< uint32, uint32 > CellCorpseSet
Definition ObjectMgr.h:414
#define sObjectMgr
Definition ObjectMgr.h:1617
void AddToWorld()
Definition Corpse.cpp:26
void AddToWorld() OVERRIDE
Definition Creature.cpp:177
bool IsInGrid() const
Definition Object.h:556
void AddToGrid(GridRefManager< T > &m)
Definition Object.h:557
LinkedListHead::Iterator< GridReference< Creature > > iterator
GridReference< OBJECT > * getFirst()
iterator begin()
bool isEmpty() const
Definition LinkedList.h:86
Definition Map.h:238
bool GameObjectRespawnRelocation(GameObject *go, bool diffGridOnly)
Definition Map.cpp:1260
void AddToActive(T *obj)
Definition Map.cpp:2569
uint32 GetInstanceId() const
Definition Map.h:357
bool CreatureRespawnRelocation(Creature *c, bool diffGridOnly)
Definition Map.cpp:1230
void SetCurrentCell(Cell const &cell)
Definition Object.h:598
void Visit(GridRefManager< T > &)
void Visit(CreatureMapType &m)
static void SetObjectCell(T *obj, CellCoord const &cellCoord)
void Visit(GameObjectMapType &m)
friend class ObjectWorldLoader
void Visit(CreatureMapType &m)
void Visit(GridRefManager< T > &m)
ObjectWorldLoader(ObjectGridLoader &gloader)
void Visit(CorpseMapType &m)
void Visit(GridRefManager< T > &)
bool IsPet() const
Definition Unit.h:1511
Map * GetMap() const
Definition Object.h:740
bool isActiveObject() const
Definition Object.h:776
virtual void SetMap(Map *map)
Definition Object.cpp:2514
#define sWorld
Definition World.h:910
@ CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY
Definition World.h:111
Definition Cell.h:37
CellGuidSet gameobjects
Definition ObjectMgr.h:418
CellGuidSet creatures
Definition ObjectMgr.h:417
CellCorpseSet corpses
Definition ObjectMgr.h:419
uint32 GetId() const