removed duplicated includes
[getmangos.git] / src / game / ObjectAccessor.cpp
blob6750f023b775dfaffa4210bc3699b24f615c36aa
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 "WorldSession.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, ZThread::FastMutex>
43 INSTANTIATE_SINGLETON_2(ObjectAccessor, CLASS_LOCK);
44 INSTANTIATE_CLASS_MUTEX(ObjectAccessor, ZThread::FastMutex);
46 ObjectAccessor::ObjectAccessor() {}
47 ObjectAccessor::~ObjectAccessor() {}
49 Creature*
50 ObjectAccessor::GetNPCIfCanInteractWith(Player const &player, uint64 guid, uint32 npcflagmask)
52 // unit checks
53 if (!guid)
54 return NULL;
56 // exist
57 Creature *unit = GetCreature(player, guid);
58 if (!unit)
59 return NULL;
61 // player check
62 if(!player.CanInteractWithNPCs(!unit->isSpiritService()))
63 return NULL;
65 // appropriate npc type
66 if(npcflagmask && !unit->HasFlag( UNIT_NPC_FLAGS, npcflagmask ))
67 return NULL;
69 // alive or spirit healer
70 if(!unit->isAlive() && (!unit->isSpiritService() || player.isAlive() ))
71 return NULL;
73 // not allow interaction under control
74 if(unit->GetCharmerOrOwnerGUID())
75 return NULL;
77 // not enemy
78 if( unit->IsHostileTo(&player))
79 return NULL;
81 // not unfriendly
82 FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(unit->getFaction());
83 if(factionTemplate)
85 FactionEntry const* faction = sFactionStore.LookupEntry(factionTemplate->faction);
86 if( faction && faction->reputationListID >= 0 && player.GetReputationRank(faction) <= REP_UNFRIENDLY)
87 return NULL;
90 // not too far
91 if(!unit->IsWithinDistInMap(&player,INTERACTION_DISTANCE))
92 return NULL;
94 return unit;
97 Creature*
98 ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const &u, uint64 guid)
100 if(Creature *unit = GetPet(guid))
101 return unit;
103 if(Creature *unit = GetVehicle(guid))
104 return unit;
106 return GetCreature(u, guid);
109 Creature*
110 ObjectAccessor::GetCreature(WorldObject const &u, uint64 guid)
112 Creature * ret = GetObjectInWorld(guid, (Creature*)NULL);
113 if(!ret)
114 return NULL;
116 if(ret->GetMapId() != u.GetMapId())
117 return NULL;
119 if(ret->GetInstanceId() != u.GetInstanceId())
120 return NULL;
122 return ret;
125 Unit*
126 ObjectAccessor::GetUnit(WorldObject const &u, uint64 guid)
128 if(!guid)
129 return NULL;
131 if(IS_PLAYER_GUID(guid))
132 return FindPlayer(guid);
134 return GetCreatureOrPetOrVehicle(u, guid);
137 Corpse*
138 ObjectAccessor::GetCorpse(WorldObject const &u, uint64 guid)
140 Corpse * ret = GetObjectInWorld(guid, (Corpse*)NULL);
141 if(!ret)
142 return NULL;
143 if(ret->GetMapId() != u.GetMapId())
144 return NULL;
145 if(ret->GetInstanceId() != u.GetInstanceId())
146 return NULL;
147 return ret;
150 Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, uint32 typemask)
152 Object *obj = NULL;
154 if(typemask & TYPEMASK_PLAYER)
156 obj = FindPlayer(guid);
157 if(obj) return obj;
160 if(typemask & TYPEMASK_UNIT)
162 obj = GetCreatureOrPetOrVehicle(p,guid);
163 if(obj) return obj;
166 if(typemask & TYPEMASK_GAMEOBJECT)
168 obj = GetGameObject(p,guid);
169 if(obj) return obj;
172 if(typemask & TYPEMASK_DYNAMICOBJECT)
174 obj = GetDynamicObject(p,guid);
175 if(obj) return obj;
178 if(typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
180 obj = ((Player const &)p).GetItemByGuid( guid );
181 if(obj) return obj;
184 return NULL;
187 GameObject*
188 ObjectAccessor::GetGameObject(WorldObject const &u, uint64 guid)
190 GameObject * ret = GetObjectInWorld(guid, (GameObject*)NULL);
191 if(!ret)
192 return NULL;
193 if(ret->GetMapId() != u.GetMapId())
194 return NULL;
195 if(ret->GetInstanceId() != u.GetInstanceId())
196 return NULL;
197 return ret;
200 DynamicObject*
201 ObjectAccessor::GetDynamicObject(WorldObject const &u, uint64 guid)
203 DynamicObject * ret = GetObjectInWorld(guid, (DynamicObject*)NULL);
204 if(!ret)
205 return NULL;
206 if(ret->GetMapId() != u.GetMapId())
207 return NULL;
208 if(ret->GetInstanceId() != u.GetInstanceId())
209 return NULL;
210 return ret;
213 Player*
214 ObjectAccessor::FindPlayer(uint64 guid)
216 return GetObjectInWorld(guid, (Player*)NULL);
219 Player*
220 ObjectAccessor::FindPlayerByName(const char *name)
222 //TODO: Player Guard
223 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
224 HashMapHolder<Player>::MapType::iterator iter = m.begin();
225 for(; iter != m.end(); ++iter)
226 if( ::strcmp(name, iter->second->GetName()) == 0 )
227 return iter->second;
228 return NULL;
231 void
232 ObjectAccessor::SaveAllPlayers()
234 Guard guard(*HashMapHolder<Player*>::GetLock());
235 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
236 HashMapHolder<Player>::MapType::iterator itr = m.begin();
237 for(; itr != m.end(); ++itr)
238 itr->second->SaveToDB();
241 void
242 ObjectAccessor::UpdateObject(Object* obj, Player* exceptPlayer)
244 UpdateDataMapType update_players;
245 obj->BuildUpdate(update_players);
247 WorldPacket packet;
248 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
250 if(iter->first == exceptPlayer)
251 continue;
253 iter->second.BuildPacket(&packet);
254 iter->first->GetSession()->SendPacket(&packet);
255 packet.clear();
259 void
260 ObjectAccessor::_buildUpdateObject(Object *obj, UpdateDataMapType &update_players)
262 bool build_for_all = true;
263 Player *pl = NULL;
264 if( obj->isType(TYPEMASK_ITEM) )
266 Item *item = static_cast<Item *>(obj);
267 pl = item->GetOwner();
268 build_for_all = false;
271 if( pl != NULL )
272 _buildPacket(pl, obj, update_players);
274 // Capt: okey for all those fools who think its a real fix
275 // THIS IS A TEMP FIX
276 if( build_for_all )
278 WorldObject * temp = dynamic_cast<WorldObject*>(obj);
280 //assert(dynamic_cast<WorldObject*>(obj)!=NULL);
281 if (temp)
282 _buildChangeObjectForPlayer(temp, update_players);
283 else
284 sLog.outDebug("ObjectAccessor: Ln 405 Temp bug fix");
288 void
289 ObjectAccessor::_buildPacket(Player *pl, Object *obj, UpdateDataMapType &update_players)
291 UpdateDataMapType::iterator iter = update_players.find(pl);
293 if( iter == update_players.end() )
295 std::pair<UpdateDataMapType::iterator, bool> p = update_players.insert( UpdateDataValueType(pl, UpdateData()) );
296 assert(p.second);
297 iter = p.first;
300 obj->BuildValuesUpdateBlockForPlayer(&iter->second, iter->first);
303 void
304 ObjectAccessor::_buildChangeObjectForPlayer(WorldObject *obj, UpdateDataMapType &update_players)
306 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
307 Cell cell(p);
308 cell.data.Part.reserved = ALL_DISTRICT;
309 cell.SetNoCreate();
310 WorldObjectChangeAccumulator notifier(*obj, update_players);
311 TypeContainerVisitor<WorldObjectChangeAccumulator, WorldTypeMapContainer > player_notifier(notifier);
312 CellLock<GridReadGuard> cell_lock(cell, p);
313 cell_lock->Visit(cell_lock, player_notifier, *obj->GetMap());
316 Pet*
317 ObjectAccessor::GetPet(uint64 guid)
319 return GetObjectInWorld(guid, (Pet*)NULL);
322 Vehicle*
323 ObjectAccessor::GetVehicle(uint64 guid)
325 return GetObjectInWorld(guid, (Vehicle*)NULL);
328 Corpse*
329 ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
331 Guard guard(i_corpseGuard);
333 Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
334 if( iter == i_player2corpse.end() ) return NULL;
336 assert(iter->second->GetType() != CORPSE_BONES);
338 return iter->second;
341 void
342 ObjectAccessor::RemoveCorpse(Corpse *corpse)
344 assert(corpse && corpse->GetType() != CORPSE_BONES);
346 Guard guard(i_corpseGuard);
347 Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
348 if( iter == i_player2corpse.end() )
349 return;
351 // build mapid*cellid -> guid_set map
352 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
353 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
355 objmgr.DeleteCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID());
356 corpse->RemoveFromWorld();
358 i_player2corpse.erase(iter);
361 void
362 ObjectAccessor::AddCorpse(Corpse *corpse)
364 assert(corpse && corpse->GetType() != CORPSE_BONES);
366 Guard guard(i_corpseGuard);
367 assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
368 i_player2corpse[corpse->GetOwnerGUID()] = corpse;
370 // build mapid*cellid -> guid_set map
371 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
372 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
374 objmgr.AddCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID(),corpse->GetInstanceId());
377 void
378 ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
380 Guard guard(i_corpseGuard);
381 for(Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
382 if(iter->second->GetGrid()==gridpair)
384 // verify, if the corpse in our instance (add only corpses which are)
385 if (map->Instanceable())
387 if (iter->second->GetInstanceId() == map->GetInstanceId())
389 grid.AddWorldObject(iter->second,iter->second->GetGUID());
392 else
394 grid.AddWorldObject(iter->second,iter->second->GetGUID());
399 Corpse*
400 ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
402 Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
403 if(!corpse)
405 //in fact this function is called from several places
406 //even when player doesn't have a corpse, not an error
407 //sLog.outError("ERROR: Try remove corpse that not in map for GUID %ul", player_guid);
408 return NULL;
411 DEBUG_LOG("Deleting Corpse and spawning bones.\n");
413 // remove corpse from player_guid -> corpse map
414 RemoveCorpse(corpse);
416 // remove resurrectble corpse from grid object registry (loaded state checked into call)
417 // do not load the map if it's not loaded
418 Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId());
419 if(map) map->Remove(corpse,false);
421 // remove corpse from DB
422 corpse->DeleteFromDB();
424 Corpse *bones = NULL;
425 // create the bones only if the map and the grid is loaded at the corpse's location
426 // ignore bones creating option in case insignia
427 if (map && (insignia ||
428 (map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_DEATH_BONES_WORLD))) &&
429 !map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
431 // Create bones, don't change Corpse
432 bones = new Corpse;
433 bones->Create(corpse->GetGUIDLow());
435 for (int i = 3; i < CORPSE_END; i++) // don't overwrite guid and object type
436 bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
438 bones->SetGrid(corpse->GetGrid());
439 // bones->m_time = m_time; // don't overwrite time
440 // bones->m_inWorld = m_inWorld; // don't overwrite world state
441 // bones->m_type = m_type; // don't overwrite type
442 bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
443 bones->SetMapId(corpse->GetMapId());
444 bones->SetInstanceId(corpse->GetInstanceId());
445 bones->SetPhaseMask(corpse->GetPhaseMask(),false);
447 bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
448 bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
450 for (int i = 0; i < EQUIPMENT_SLOT_END; i++)
452 if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
453 bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
456 // add bones in grid store if grid loaded where corpse placed
457 map->Add(bones);
460 // all references to the corpse should be removed at this point
461 delete corpse;
463 return bones;
466 void
467 ObjectAccessor::Update(uint32 diff)
469 UpdateDataMapType update_players;
471 Guard guard(i_updateGuard);
472 while(!i_objects.empty())
474 Object* obj = *i_objects.begin();
475 i_objects.erase(i_objects.begin());
476 if (!obj)
477 continue;
478 _buildUpdateObject(obj, update_players);
479 obj->ClearUpdateMask(false);
483 WorldPacket packet; // here we allocate a std::vector with a size of 0x10000
484 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
486 iter->second.BuildPacket(&packet);
487 iter->first->GetSession()->SendPacket(&packet);
488 packet.clear(); // clean the string
492 void
493 ObjectAccessor::UpdatePlayers(uint32 diff)
495 HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer();
496 for(HashMapHolder<Player>::MapType::iterator iter = playerMap.begin(); iter != playerMap.end(); ++iter)
497 if(iter->second->IsInWorld())
498 iter->second->Update(diff);
501 void
502 ObjectAccessor::WorldObjectChangeAccumulator::Visit(PlayerMapType &m)
504 for(PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
505 if(iter->getSource()->HaveAtClient(&i_object))
506 ObjectAccessor::_buildPacket(iter->getSource(), &i_object, i_updateDatas);
509 void
510 ObjectAccessor::UpdateObjectVisibility(WorldObject *obj)
512 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
513 Cell cell(p);
515 obj->GetMap()->UpdateObjectVisibility(obj,cell,p);
518 void ObjectAccessor::UpdateVisibilityForPlayer( Player* player )
520 CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
521 Cell cell(p);
522 Map* m = player->GetMap();
524 m->UpdatePlayerVisibility(player,cell,p);
525 m->UpdateObjectsVisibilityFor(player,cell,p);
528 /// Define the static member of HashMapHolder
530 template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
531 template <class T> ZThread::FastMutex HashMapHolder<T>::i_lock;
533 /// Global definitions for the hashmap storage
535 template class HashMapHolder<Player>;
536 template class HashMapHolder<Pet>;
537 template class HashMapHolder<Vehicle>;
538 template class HashMapHolder<GameObject>;
539 template class HashMapHolder<DynamicObject>;
540 template class HashMapHolder<Creature>;
541 template class HashMapHolder<Corpse>;
543 template Player* ObjectAccessor::GetObjectInWorld<Player>(uint32 mapid, float x, float y, uint64 guid, Player* /*fake*/);
544 template Pet* ObjectAccessor::GetObjectInWorld<Pet>(uint32 mapid, float x, float y, uint64 guid, Pet* /*fake*/);
545 template Vehicle* ObjectAccessor::GetObjectInWorld<Vehicle>(uint32 mapid, float x, float y, uint64 guid, Vehicle* /*fake*/);
546 template Creature* ObjectAccessor::GetObjectInWorld<Creature>(uint32 mapid, float x, float y, uint64 guid, Creature* /*fake*/);
547 template Corpse* ObjectAccessor::GetObjectInWorld<Corpse>(uint32 mapid, float x, float y, uint64 guid, Corpse* /*fake*/);
548 template GameObject* ObjectAccessor::GetObjectInWorld<GameObject>(uint32 mapid, float x, float y, uint64 guid, GameObject* /*fake*/);
549 template DynamicObject* ObjectAccessor::GetObjectInWorld<DynamicObject>(uint32 mapid, float x, float y, uint64 guid, DynamicObject* /*fake*/);