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 "CellImpl.h"
20#include "Corpse.h"
21#include "Creature.h"
22#include "CreatureAI.h"
23#include "DynamicObject.h"
24#include "Log.h"
25#include "GameObject.h"
26#include "GameTime.h"
27#include "ObjectAccessor.h"
28#include "ObjectMgr.h"
29#include "World.h"
30#include "ScriptMgr.h"
31
33{
34 // creature in unloading grid can have respawn point in another grid
35 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
36 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
37 for (CreatureMapType::iterator iter = m.begin(); iter != m.end();)
38 {
39 Creature* c = iter->GetSource();
40 ++iter;
41
42 ASSERT(!c->IsPet() && "ObjectGridRespawnMover must not be called for pets");
43 c->GetMap()->CreatureRespawnRelocation(c, true);
44 }
45}
46
48{
49 // gameobject in unloading grid can have respawn point in another grid
50 // if it will be unloaded then it will not respawn in original grid until unload/load original grid
51 // move to respawn point to prevent this case. For player view in respawn grid this will be normal respawn.
52 for (GameObjectMapType::iterator iter = m.begin(); iter != m.end();)
53 {
54 GameObject* go = iter->GetSource();
55 ++iter;
56
57 go->GetMap()->GameObjectRespawnRelocation(go, true);
58 }
59}
60
61// for loading world object at grid loading (Corpses)
64{
65 public:
67 : i_cell(gloader.i_cell), i_map(gloader.i_map), i_grid(gloader.i_grid), i_corpses(gloader.i_corpses)
68 { }
69
70 void Visit(CorpseMapType &m);
71
72 template<class T> void Visit(GridRefManager<T>&) { }
73
74 private:
78 public:
80};
81
83{
84 Cell cell(cellCoord);
85 obj->SetCurrentCell(cell);
86}
87
88template <class T>
89void AddObjectHelper(CellCoord &cell, GridRefManager<T> &m, uint32 &count, Map* map, T *obj)
90{
91 obj->AddToGrid(m);
93 obj->AddToWorld();
94 if (obj->isActiveObject())
95 map->AddToActive(obj);
96
97 ++count;
98}
99
100template <class T>
101void LoadHelper(CellGuidSet const& guid_set, CellCoord &cell, GridRefManager<T> &m, uint32 &count, Map* map)
102{
103 for (CellGuidSet::const_iterator i_guid = guid_set.begin(); i_guid != guid_set.end(); ++i_guid)
104 {
105 // Don't spawn at all if there's a respawn timer
106 ObjectGuid::LowType guid = *i_guid;
107 if (!map->ShouldBeSpawnedOnGridLoad<T>(guid))
108 continue;
109
110 T* obj = new T;
111 //TC_LOG_INFO("misc", "DEBUG: LoadHelper from table: {} for (guid: {}) Loading", table, guid);
112 if (!obj->LoadFromDB(guid, map, false, false))
113 {
114 delete obj;
115 continue;
116 }
117 AddObjectHelper(cell, m, count, map, obj);
118 }
119}
120
122{
123 CellCoord cellCoord = i_cell.GetCellCoord();
124 if (CellObjectGuids const* cell_guids = sObjectMgr->GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cellCoord.GetId()))
125 LoadHelper(cell_guids->gameobjects, cellCoord, m, i_gameObjects, i_map);
126}
127
129{
130 CellCoord cellCoord = i_cell.GetCellCoord();
131 if (CellObjectGuids const* cell_guids = sObjectMgr->GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cellCoord.GetId()))
132 LoadHelper(cell_guids->creatures, cellCoord, m, i_creatures, i_map);
133}
134
136{
137 CellCoord cellCoord = i_cell.GetCellCoord();
138 if (std::unordered_set<Corpse*> const* corpses = i_map->GetCorpsesInCell(cellCoord.GetId()))
139 {
140 for (Corpse* corpse : *corpses)
141 {
142 corpse->AddToWorld();
144 if (corpse->IsStoredInWorldObjectGridContainer())
145 cell.AddWorldObject(corpse);
146 else
147 cell.AddGridObject(corpse);
148
149 ++i_corpses;
150 }
151 }
152}
153
155{
156 i_gameObjects = 0; i_creatures = 0; i_corpses = 0;
158 for (uint32 x = 0; x < MAX_NUMBER_OF_CELLS; ++x)
159 {
161 for (uint32 y = 0; y < MAX_NUMBER_OF_CELLS; ++y)
162 {
164
165 //Load creatures and game objects
166 {
168 i_grid.VisitGrid(x, y, visitor);
169 }
170
171 //Load corpses (not bones)
172 {
173 ObjectWorldLoader worker(*this);
175 i_grid.VisitGrid(x, y, visitor);
176 }
177 }
178 }
179 TC_LOG_DEBUG("maps", "{} GameObjects, {} Creatures, and {} Corpses/Bones loaded for grid {} on map {}", i_gameObjects, i_creatures, i_corpses, i_grid.GetGridId(), i_map->GetId());
180}
181
182template<class T>
184{
185 while (!m.isEmpty())
186 {
187 T *obj = m.getFirst()->GetSource();
188 //Some creatures may summon other temp summons in CleanupsBeforeDelete()
189 //So we need this even after cleaner (maybe we can remove cleaner)
190 //Example: Flame Leviathan Turret 33139 is summoned when a creature is deleted
192 obj->CleanupsBeforeDelete();
194 delete obj;
195 }
196}
197
199{
200 // stop any fights at grid de-activation and remove dynobjects created at cast by creatures
201 for (CreatureMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
202 {
203 iter->GetSource()->RemoveAllDynObjects();
204 if (iter->GetSource()->IsInCombat())
205 iter->GetSource()->CombatStop();
206 }
207}
208
209template<class T>
211{
212 for (typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
213 iter->GetSource()->CleanupsBeforeDelete();
214}
215
219
221template void ObjectGridCleaner::Visit<GameObject>(GameObjectMapType &);
222template void ObjectGridCleaner::Visit<DynamicObject>(DynamicObjectMapType &);
223template void ObjectGridCleaner::Visit<Corpse>(CorpseMapType &);
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
#define MAX_NUMBER_OF_CELLS
Definition GridDefines.h:34
#define TC_LOG_DEBUG(filterType__,...)
Definition Log.h:156
void AddObjectHelper(CellCoord &cell, GridRefManager< T > &m, uint32 &count, Map *map, T *obj)
void LoadHelper(CellGuidSet const &guid_set, CellCoord &cell, GridRefManager< T > &m, uint32 &count, Map *map)
std::set< ObjectGuid::LowType > CellGuidSet
Definition ObjectMgr.h:515
#define sObjectMgr
Definition ObjectMgr.h:1721
GridReference< OBJECT > * getFirst()
iterator begin()
Definition Grid.h:46
void AddWorldObject(SPECIFIC_OBJECT *obj)
Definition Grid.h:58
void AddGridObject(SPECIFIC_OBJECT *obj)
Definition Grid.h:111
bool isEmpty() const
Definition LinkedList.h:108
void SetCurrentCell(Cell const &cell)
Definition MapObject.h:49
Definition Map.h:281
std::unordered_set< Corpse * > const * GetCorpsesInCell(uint32 cellId) const
Definition Map.h:499
uint8 GetSpawnMode() const
Definition Map.h:388
bool GameObjectRespawnRelocation(GameObject *go, bool diffGridOnly)
Definition Map.cpp:1623
uint32 GetId() const
Definition Map.cpp:4216
void AddToActive(WorldObject *obj)
Definition Map.cpp:3732
bool CreatureRespawnRelocation(Creature *c, bool diffGridOnly)
Definition Map.cpp:1592
bool ShouldBeSpawnedOnGridLoad(SpawnObjectType type, ObjectGuid::LowType spawnId) const
Definition Map.cpp:3356
Definition NGrid.h:73
uint32 GetGridId(void) const
Definition NGrid.h:93
GridType & GetGridType(const uint32 x, const uint32 y)
Definition NGrid.h:81
void VisitGrid(const uint32 x, const uint32 y, TypeContainerVisitor< T, TypeMapContainer< TT > > &visitor)
Definition NGrid.h:149
void Visit(GridRefManager< T > &)
void Visit(CreatureMapType &m)
void Visit(GameObjectMapType &m)
static void SetObjectCell(MapObject *obj, CellCoord const &cellCoord)
void Visit(CreatureMapType &m)
void Visit(CorpseMapType &)
uint32 LowType
Definition ObjectGuid.h:142
ObjectWorldLoader(ObjectGridLoader &gloader)
void Visit(CorpseMapType &m)
void Visit(GridRefManager< T > &)
bool IsPet() const
Definition Unit.h:884
Map * GetMap() const
Definition Object.h:449
Definition Cell.h:47
CellCoord GetCellCoord() const
Definition Cell.h:77
union Cell::@256 data
struct Cell::@256::@257 Part
uint32 CellX() const
Definition Cell.h:70
uint32 CellY() const
Definition Cell.h:71
uint8 cell_y
Definition Cell.h:92
uint8 cell_x
Definition Cell.h:91
uint32 GetId() const