[7915] Implement more stricted checks and limitations at loading creature addon data.
[getmangos.git] / src / game / ObjectAccessor.cpp
blobb235e5a3c39546f87072b8211ab5e79d2b033f7a
1 /*
2 * Copyright (C) 2005-2009 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 "ObjectAccessor.h"
20 #include "ObjectMgr.h"
21 #include "Policies/SingletonImp.h"
22 #include "Player.h"
23 #include "Creature.h"
24 #include "GameObject.h"
25 #include "DynamicObject.h"
26 #include "Vehicle.h"
27 #include "WorldPacket.h"
28 #include "Item.h"
29 #include "Corpse.h"
30 #include "GridNotifiers.h"
31 #include "MapManager.h"
32 #include "Map.h"
33 #include "CellImpl.h"
34 #include "GridNotifiersImpl.h"
35 #include "Opcodes.h"
36 #include "ObjectDefines.h"
37 #include "MapInstanced.h"
38 #include "World.h"
40 #include <cmath>
42 #define CLASS_LOCK MaNGOS::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex>
43 INSTANTIATE_SINGLETON_2(ObjectAccessor, CLASS_LOCK);
44 INSTANTIATE_CLASS_MUTEX(ObjectAccessor, ACE_Thread_Mutex);
46 ObjectAccessor::ObjectAccessor() {}
47 ObjectAccessor::~ObjectAccessor() {}
49 Creature*
50 ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const &u, uint64 guid)
52 if(Creature *unit = GetPet(guid))
53 return unit;
55 if(Creature *unit = GetVehicle(guid))
56 return unit;
58 return u.GetMap()->GetCreature(guid);
61 Unit*
62 ObjectAccessor::GetUnit(WorldObject const &u, uint64 guid)
64 if(!guid)
65 return NULL;
67 if(IS_PLAYER_GUID(guid))
68 return FindPlayer(guid);
70 return GetCreatureOrPetOrVehicle(u, guid);
73 Corpse*
74 ObjectAccessor::GetCorpse(WorldObject const &u, uint64 guid)
76 Corpse * ret = GetObjectInWorld(guid, (Corpse*)NULL);
77 if(!ret)
78 return NULL;
79 if(ret->GetMapId() != u.GetMapId())
80 return NULL;
81 if(ret->GetInstanceId() != u.GetInstanceId())
82 return NULL;
83 return ret;
86 Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, uint32 typemask)
88 Object *obj = NULL;
90 if(typemask & TYPEMASK_PLAYER)
92 obj = FindPlayer(guid);
93 if(obj) return obj;
96 if(typemask & TYPEMASK_UNIT)
98 obj = GetCreatureOrPetOrVehicle(p,guid);
99 if(obj) return obj;
102 if(typemask & TYPEMASK_GAMEOBJECT)
104 obj = p.GetMap()->GetGameObject(guid);
105 if(obj) return obj;
108 if(typemask & TYPEMASK_DYNAMICOBJECT)
110 obj = p.GetMap()->GetDynamicObject(guid);
111 if(obj) return obj;
114 if(typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
116 obj = ((Player const &)p).GetItemByGuid( guid );
117 if(obj) return obj;
120 return NULL;
123 Player*
124 ObjectAccessor::FindPlayer(uint64 guid)
126 return GetObjectInWorld(guid, (Player*)NULL);
129 Player*
130 ObjectAccessor::FindPlayerByName(const char *name)
132 //TODO: Player Guard
133 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
134 HashMapHolder<Player>::MapType::iterator iter = m.begin();
135 for(; iter != m.end(); ++iter)
136 if( ::strcmp(name, iter->second->GetName()) == 0 )
137 return iter->second;
138 return NULL;
141 void
142 ObjectAccessor::SaveAllPlayers()
144 Guard guard(*HashMapHolder<Player*>::GetLock());
145 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
146 HashMapHolder<Player>::MapType::iterator itr = m.begin();
147 for(; itr != m.end(); ++itr)
148 itr->second->SaveToDB();
151 void
152 ObjectAccessor::UpdateObject(Object* obj, Player* exceptPlayer)
154 UpdateDataMapType update_players;
155 obj->BuildUpdate(update_players);
157 WorldPacket packet;
158 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
160 if(iter->first == exceptPlayer)
161 continue;
163 iter->second.BuildPacket(&packet);
164 iter->first->GetSession()->SendPacket(&packet);
165 packet.clear();
169 void
170 ObjectAccessor::_buildUpdateObject(Object *obj, UpdateDataMapType &update_players)
172 bool build_for_all = true;
173 Player *pl = NULL;
174 if( obj->isType(TYPEMASK_ITEM) )
176 Item *item = static_cast<Item *>(obj);
177 pl = item->GetOwner();
178 build_for_all = false;
181 if( pl != NULL )
182 _buildPacket(pl, obj, update_players);
184 // Capt: okey for all those fools who think its a real fix
185 // THIS IS A TEMP FIX
186 if( build_for_all )
188 WorldObject * temp = dynamic_cast<WorldObject*>(obj);
190 //assert(dynamic_cast<WorldObject*>(obj)!=NULL);
191 if (temp)
192 _buildChangeObjectForPlayer(temp, update_players);
193 else
194 sLog.outDebug("ObjectAccessor: Ln 405 Temp bug fix");
198 void
199 ObjectAccessor::_buildPacket(Player *pl, Object *obj, UpdateDataMapType &update_players)
201 UpdateDataMapType::iterator iter = update_players.find(pl);
203 if( iter == update_players.end() )
205 std::pair<UpdateDataMapType::iterator, bool> p = update_players.insert( UpdateDataValueType(pl, UpdateData()) );
206 assert(p.second);
207 iter = p.first;
210 obj->BuildValuesUpdateBlockForPlayer(&iter->second, iter->first);
213 void
214 ObjectAccessor::_buildChangeObjectForPlayer(WorldObject *obj, UpdateDataMapType &update_players)
216 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
217 Cell cell(p);
218 cell.data.Part.reserved = ALL_DISTRICT;
219 cell.SetNoCreate();
220 WorldObjectChangeAccumulator notifier(*obj, update_players);
221 TypeContainerVisitor<WorldObjectChangeAccumulator, WorldTypeMapContainer > player_notifier(notifier);
222 CellLock<GridReadGuard> cell_lock(cell, p);
223 cell_lock->Visit(cell_lock, player_notifier, *obj->GetMap());
226 Pet*
227 ObjectAccessor::GetPet(uint64 guid)
229 return GetObjectInWorld(guid, (Pet*)NULL);
232 Vehicle*
233 ObjectAccessor::GetVehicle(uint64 guid)
235 return GetObjectInWorld(guid, (Vehicle*)NULL);
238 Corpse*
239 ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
241 Guard guard(i_corpseGuard);
243 Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
244 if( iter == i_player2corpse.end() ) return NULL;
246 assert(iter->second->GetType() != CORPSE_BONES);
248 return iter->second;
251 void
252 ObjectAccessor::RemoveCorpse(Corpse *corpse)
254 assert(corpse && corpse->GetType() != CORPSE_BONES);
256 Guard guard(i_corpseGuard);
257 Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
258 if( iter == i_player2corpse.end() )
259 return;
261 // build mapid*cellid -> guid_set map
262 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
263 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
265 objmgr.DeleteCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID());
266 corpse->RemoveFromWorld();
268 i_player2corpse.erase(iter);
271 void
272 ObjectAccessor::AddCorpse(Corpse *corpse)
274 assert(corpse && corpse->GetType() != CORPSE_BONES);
276 Guard guard(i_corpseGuard);
277 assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
278 i_player2corpse[corpse->GetOwnerGUID()] = corpse;
280 // build mapid*cellid -> guid_set map
281 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
282 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
284 objmgr.AddCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID(),corpse->GetInstanceId());
287 void
288 ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
290 Guard guard(i_corpseGuard);
291 for(Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
292 if(iter->second->GetGrid()==gridpair)
294 // verify, if the corpse in our instance (add only corpses which are)
295 if (map->Instanceable())
297 if (iter->second->GetInstanceId() == map->GetInstanceId())
299 grid.AddWorldObject(iter->second,iter->second->GetGUID());
302 else
304 grid.AddWorldObject(iter->second,iter->second->GetGUID());
309 Corpse*
310 ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
312 Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
313 if(!corpse)
315 //in fact this function is called from several places
316 //even when player doesn't have a corpse, not an error
317 //sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
318 return NULL;
321 DEBUG_LOG("Deleting Corpse and spawning bones.");
323 // remove corpse from player_guid -> corpse map
324 RemoveCorpse(corpse);
326 // remove resurrectble corpse from grid object registry (loaded state checked into call)
327 // do not load the map if it's not loaded
328 Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId());
329 if(map) map->Remove(corpse,false);
331 // remove corpse from DB
332 corpse->DeleteFromDB();
334 Corpse *bones = NULL;
335 // create the bones only if the map and the grid is loaded at the corpse's location
336 // ignore bones creating option in case insignia
337 if (map && (insignia ||
338 (map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_DEATH_BONES_WORLD))) &&
339 !map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
341 // Create bones, don't change Corpse
342 bones = new Corpse;
343 bones->Create(corpse->GetGUIDLow());
345 for (int i = 3; i < CORPSE_END; i++) // don't overwrite guid and object type
346 bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
348 bones->SetGrid(corpse->GetGrid());
349 // bones->m_time = m_time; // don't overwrite time
350 // bones->m_inWorld = m_inWorld; // don't overwrite world state
351 // bones->m_type = m_type; // don't overwrite type
352 bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
353 bones->SetMapId(corpse->GetMapId());
354 bones->SetInstanceId(corpse->GetInstanceId());
355 bones->SetPhaseMask(corpse->GetPhaseMask(),false);
357 bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
358 bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
360 for (int i = 0; i < EQUIPMENT_SLOT_END; i++)
362 if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
363 bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
366 // add bones in grid store if grid loaded where corpse placed
367 map->Add(bones);
370 // all references to the corpse should be removed at this point
371 delete corpse;
373 return bones;
376 void
377 ObjectAccessor::Update(uint32 diff)
379 UpdateDataMapType update_players;
381 Guard guard(i_updateGuard);
382 while(!i_objects.empty())
384 Object* obj = *i_objects.begin();
385 i_objects.erase(i_objects.begin());
386 if (!obj)
387 continue;
388 _buildUpdateObject(obj, update_players);
389 obj->ClearUpdateMask(false);
393 WorldPacket packet; // here we allocate a std::vector with a size of 0x10000
394 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
396 iter->second.BuildPacket(&packet);
397 iter->first->GetSession()->SendPacket(&packet);
398 packet.clear(); // clean the string
402 void
403 ObjectAccessor::UpdatePlayers(uint32 diff)
405 HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer();
406 for(HashMapHolder<Player>::MapType::iterator iter = playerMap.begin(); iter != playerMap.end(); ++iter)
407 if(iter->second->IsInWorld())
408 iter->second->Update(diff);
411 void
412 ObjectAccessor::WorldObjectChangeAccumulator::Visit(PlayerMapType &m)
414 for(PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
415 if(iter->getSource()->HaveAtClient(&i_object))
416 ObjectAccessor::_buildPacket(iter->getSource(), &i_object, i_updateDatas);
419 void
420 ObjectAccessor::UpdateObjectVisibility(WorldObject *obj)
422 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
423 Cell cell(p);
425 obj->GetMap()->UpdateObjectVisibility(obj,cell,p);
428 void ObjectAccessor::UpdateVisibilityForPlayer( Player* player )
430 CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
431 Cell cell(p);
432 Map* m = player->GetMap();
434 m->UpdatePlayerVisibility(player,cell,p);
435 m->UpdateObjectsVisibilityFor(player,cell,p);
438 /// Define the static member of HashMapHolder
440 template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
441 template <class T> ACE_Thread_Mutex HashMapHolder<T>::i_lock;
443 /// Global definitions for the hashmap storage
445 template class HashMapHolder<Player>;
446 template class HashMapHolder<Pet>;
447 template class HashMapHolder<Vehicle>;
448 template class HashMapHolder<GameObject>;
449 template class HashMapHolder<DynamicObject>;
450 template class HashMapHolder<Creature>;
451 template class HashMapHolder<Corpse>;
453 template Player* ObjectAccessor::GetObjectInWorld<Player>(uint32 mapid, float x, float y, uint64 guid, Player* /*fake*/);
454 template Pet* ObjectAccessor::GetObjectInWorld<Pet>(uint32 mapid, float x, float y, uint64 guid, Pet* /*fake*/);
455 template Vehicle* ObjectAccessor::GetObjectInWorld<Vehicle>(uint32 mapid, float x, float y, uint64 guid, Vehicle* /*fake*/);
456 template Creature* ObjectAccessor::GetObjectInWorld<Creature>(uint32 mapid, float x, float y, uint64 guid, Creature* /*fake*/);
457 template Corpse* ObjectAccessor::GetObjectInWorld<Corpse>(uint32 mapid, float x, float y, uint64 guid, Corpse* /*fake*/);
458 template GameObject* ObjectAccessor::GetObjectInWorld<GameObject>(uint32 mapid, float x, float y, uint64 guid, GameObject* /*fake*/);
459 template DynamicObject* ObjectAccessor::GetObjectInWorld<DynamicObject>(uint32 mapid, float x, float y, uint64 guid, DynamicObject* /*fake*/);