[9529] Make Player::IsValidPos const
[getmangos.git] / src / game / ObjectGridLoader.cpp
blob9935ce23caac259da3a877f4984703ad784cd255
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "ObjectGridLoader.h"
20 #include "ObjectAccessor.h"
21 #include "ObjectMgr.h"
22 #include "Creature.h"
23 #include "GameObject.h"
24 #include "DynamicObject.h"
25 #include "Corpse.h"
26 #include "World.h"
27 #include "CellImpl.h"
29 class MANGOS_DLL_DECL ObjectGridRespawnMover
31 public:
32 ObjectGridRespawnMover() {}
34 void Move(GridType &grid);
36 template<class T> void Visit(GridRefManager<T> &) {}
37 void Visit(CreatureMapType &m);
40 void
41 ObjectGridRespawnMover::Move(GridType &grid)
43 TypeContainerVisitor<ObjectGridRespawnMover, GridTypeMapContainer > mover(*this);
44 grid.Visit(mover);
47 void
48 ObjectGridRespawnMover::Visit(CreatureMapType &m)
50 // creature 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(CreatureMapType::iterator iter=m.begin(), next; iter != m.end(); iter = next)
55 next = iter; ++next;
57 Creature * c = iter->getSource();
59 assert((!c->isPet() || !c->isVehicle()) && "ObjectGridRespawnMover don't must be called for pets");
61 Cell const& cur_cell = c->GetCurrentCell();
63 float resp_x, resp_y, resp_z;
64 c->GetRespawnCoord(resp_x, resp_y, resp_z);
65 CellPair resp_val = MaNGOS::ComputeCellPair(resp_x, resp_y);
66 Cell resp_cell(resp_val);
68 if(cur_cell.DiffGrid(resp_cell))
70 c->GetMap()->CreatureRespawnRelocation(c);
71 // false result ignored: will be unload with other creatures at grid
76 // for loading world object at grid loading (Corpses)
77 class ObjectWorldLoader
79 public:
80 explicit ObjectWorldLoader(ObjectGridLoader& gloader)
81 : i_cell(gloader.i_cell), i_grid(gloader.i_grid), i_map(gloader.i_map), i_corpses (0)
84 void Visit(CorpseMapType &m);
86 template<class T> void Visit(GridRefManager<T>&) { }
88 private:
89 Cell i_cell;
90 NGridType &i_grid;
91 Map* i_map;
92 public:
93 uint32 i_corpses;
96 template<class T> void addUnitState(T* /*obj*/, CellPair const& /*cell_pair*/)
100 template<> void addUnitState(Creature *obj, CellPair const& cell_pair)
102 Cell cell(cell_pair);
104 obj->SetCurrentCell(cell);
107 template <class T>
108 void LoadHelper(CellGuidSet const& guid_set, CellPair &cell, GridRefManager<T> &m, uint32 &count, Map* map)
110 BattleGround* bg = map->IsBattleGroundOrArena() ? ((BattleGroundMap*)map)->GetBG() : NULL;
112 for(CellGuidSet::const_iterator i_guid = guid_set.begin(); i_guid != guid_set.end(); ++i_guid)
114 uint32 guid = *i_guid;
116 T* obj = new T;
117 //sLog.outString("DEBUG: LoadHelper from table: %s for (guid: %u) Loading",table,guid);
118 if(!obj->LoadFromDB(guid, map))
120 delete obj;
121 continue;
124 obj->GetGridRef().link(&m, obj);
126 addUnitState(obj,cell);
127 obj->SetMap(map);
128 obj->AddToWorld();
129 if(obj->isActiveObject())
130 map->AddToActive(obj);
131 if (bg)
132 bg->OnObjectDBLoad(obj);
134 ++count;
138 void LoadHelper(CellCorpseSet const& cell_corpses, CellPair &cell, CorpseMapType &m, uint32 &count, Map* map)
140 if(cell_corpses.empty())
141 return;
143 for(CellCorpseSet::const_iterator itr = cell_corpses.begin(); itr != cell_corpses.end(); ++itr)
145 if(itr->second != map->GetInstanceId())
146 continue;
148 uint32 player_guid = itr->first;
150 Corpse *obj = sObjectAccessor.GetCorpseForPlayerGUID(player_guid);
151 if(!obj)
152 continue;
154 obj->GetGridRef().link(&m, obj);
156 addUnitState(obj,cell);
157 obj->SetMap(map);
158 obj->AddToWorld();
159 if(obj->isActiveObject())
160 map->AddToActive(obj);
162 ++count;
166 void
167 ObjectGridLoader::Visit(GameObjectMapType &m)
169 uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
170 uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
171 CellPair cell_pair(x,y);
172 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
174 CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cell_id);
176 LoadHelper(cell_guids.gameobjects, cell_pair, m, i_gameObjects, i_map);
179 void
180 ObjectGridLoader::Visit(CreatureMapType &m)
182 uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
183 uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
184 CellPair cell_pair(x,y);
185 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
187 CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), i_map->GetSpawnMode(), cell_id);
189 LoadHelper(cell_guids.creatures, cell_pair, m, i_creatures, i_map);
192 void
193 ObjectWorldLoader::Visit(CorpseMapType &m)
195 uint32 x = (i_cell.GridX()*MAX_NUMBER_OF_CELLS) + i_cell.CellX();
196 uint32 y = (i_cell.GridY()*MAX_NUMBER_OF_CELLS) + i_cell.CellY();
197 CellPair cell_pair(x,y);
198 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
200 // corpses are always added to spawn mode 0 and they are spawned by their instance id
201 CellObjectGuids const& cell_guids = sObjectMgr.GetCellObjectGuids(i_map->GetId(), 0, cell_id);
202 LoadHelper(cell_guids.corpses, cell_pair, m, i_corpses, i_map);
205 void
206 ObjectGridLoader::Load(GridType &grid)
209 TypeContainerVisitor<ObjectGridLoader, GridTypeMapContainer > loader(*this);
210 grid.Visit(loader);
214 ObjectWorldLoader wloader(*this);
215 TypeContainerVisitor<ObjectWorldLoader, WorldTypeMapContainer > loader(wloader);
216 grid.Visit(loader);
217 i_corpses = wloader.i_corpses;
221 void ObjectGridLoader::LoadN(void)
223 i_gameObjects = 0; i_creatures = 0; i_corpses = 0;
224 i_cell.data.Part.cell_y = 0;
225 for(unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
227 i_cell.data.Part.cell_x = x;
228 for(unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
230 i_cell.data.Part.cell_y = y;
231 GridLoader<Player, AllWorldObjectTypes, AllGridObjectTypes> loader;
232 loader.Load(i_grid(x, y), *this);
235 sLog.outDebug("%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());
238 void ObjectGridUnloader::MoveToRespawnN()
240 for(unsigned int x=0; x < MAX_NUMBER_OF_CELLS; ++x)
242 for(unsigned int y=0; y < MAX_NUMBER_OF_CELLS; ++y)
244 ObjectGridRespawnMover mover;
245 mover.Move(i_grid(x, y));
250 void
251 ObjectGridUnloader::Unload(GridType &grid)
253 TypeContainerVisitor<ObjectGridUnloader, GridTypeMapContainer > unloader(*this);
254 grid.Visit(unloader);
257 template<class T>
258 void
259 ObjectGridUnloader::Visit(GridRefManager<T> &m)
261 // remove all cross-reference before deleting
262 for(typename GridRefManager<T>::iterator iter=m.begin(); iter != m.end(); ++iter)
263 iter->getSource()->CleanupsBeforeDelete();
265 while(!m.isEmpty())
267 T *obj = m.getFirst()->getSource();
268 // if option set then object already saved at this moment
269 if(!sWorld.getConfig(CONFIG_BOOL_SAVE_RESPAWN_TIME_IMMEDIATLY))
270 obj->SaveRespawnTime();
271 ///- object must be out of world before delete
272 obj->RemoveFromWorld();
273 ///- object will get delinked from the manager when deleted
274 delete obj;
278 void
279 ObjectGridStoper::Stop(GridType &grid)
281 TypeContainerVisitor<ObjectGridStoper, GridTypeMapContainer > stoper(*this);
282 grid.Visit(stoper);
285 void
286 ObjectGridStoper::Visit(CreatureMapType &m)
288 // stop any fights at grid de-activation and remove dynobjects created at cast by creatures
289 for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
291 iter->getSource()->CombatStop();
292 iter->getSource()->DeleteThreatList();
293 iter->getSource()->RemoveAllDynObjects();
297 template void ObjectGridUnloader::Visit(GameObjectMapType &);
298 template void ObjectGridUnloader::Visit(DynamicObjectMapType &);