[7607] Improvements in support some generic achievement classes
[AHbot.git] / src / game / ObjectAccessor.cpp
blob68807c6ba2ab44acd3a9c299dd2660620db03d01
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, 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 if(FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(unit->getFaction()))
83 if(factionTemplate->faction)
84 if(FactionEntry const* faction = sFactionStore.LookupEntry(factionTemplate->faction))
85 if(faction->reputationListID >= 0 && player.GetReputationMgr().GetRank(faction) <= REP_UNFRIENDLY)
86 return NULL;
88 // not too far
89 if(!unit->IsWithinDistInMap(&player,INTERACTION_DISTANCE))
90 return NULL;
92 return unit;
95 Creature*
96 ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const &u, uint64 guid)
98 if(Creature *unit = GetPet(guid))
99 return unit;
101 if(Creature *unit = GetVehicle(guid))
102 return unit;
104 return GetCreature(u, guid);
107 Creature*
108 ObjectAccessor::GetCreature(WorldObject const &u, uint64 guid)
110 Creature * ret = GetObjectInWorld(guid, (Creature*)NULL);
111 if(!ret)
112 return NULL;
114 if(ret->GetMapId() != u.GetMapId())
115 return NULL;
117 if(ret->GetInstanceId() != u.GetInstanceId())
118 return NULL;
120 return ret;
123 Unit*
124 ObjectAccessor::GetUnit(WorldObject const &u, uint64 guid)
126 if(!guid)
127 return NULL;
129 if(IS_PLAYER_GUID(guid))
130 return FindPlayer(guid);
132 return GetCreatureOrPetOrVehicle(u, guid);
135 Corpse*
136 ObjectAccessor::GetCorpse(WorldObject const &u, uint64 guid)
138 Corpse * ret = GetObjectInWorld(guid, (Corpse*)NULL);
139 if(!ret)
140 return NULL;
141 if(ret->GetMapId() != u.GetMapId())
142 return NULL;
143 if(ret->GetInstanceId() != u.GetInstanceId())
144 return NULL;
145 return ret;
148 Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, uint32 typemask)
150 Object *obj = NULL;
152 if(typemask & TYPEMASK_PLAYER)
154 obj = FindPlayer(guid);
155 if(obj) return obj;
158 if(typemask & TYPEMASK_UNIT)
160 obj = GetCreatureOrPetOrVehicle(p,guid);
161 if(obj) return obj;
164 if(typemask & TYPEMASK_GAMEOBJECT)
166 obj = GetGameObject(p,guid);
167 if(obj) return obj;
170 if(typemask & TYPEMASK_DYNAMICOBJECT)
172 obj = GetDynamicObject(p,guid);
173 if(obj) return obj;
176 if(typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
178 obj = ((Player const &)p).GetItemByGuid( guid );
179 if(obj) return obj;
182 return NULL;
185 GameObject*
186 ObjectAccessor::GetGameObject(WorldObject const &u, uint64 guid)
188 GameObject * ret = GetObjectInWorld(guid, (GameObject*)NULL);
189 if(!ret)
190 return NULL;
191 if(ret->GetMapId() != u.GetMapId())
192 return NULL;
193 if(ret->GetInstanceId() != u.GetInstanceId())
194 return NULL;
195 return ret;
198 DynamicObject*
199 ObjectAccessor::GetDynamicObject(WorldObject const &u, uint64 guid)
201 DynamicObject * ret = GetObjectInWorld(guid, (DynamicObject*)NULL);
202 if(!ret)
203 return NULL;
204 if(ret->GetMapId() != u.GetMapId())
205 return NULL;
206 if(ret->GetInstanceId() != u.GetInstanceId())
207 return NULL;
208 return ret;
211 Player*
212 ObjectAccessor::FindPlayer(uint64 guid)
214 return GetObjectInWorld(guid, (Player*)NULL);
217 Player*
218 ObjectAccessor::FindPlayerByName(const char *name)
220 //TODO: Player Guard
221 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
222 HashMapHolder<Player>::MapType::iterator iter = m.begin();
223 for(; iter != m.end(); ++iter)
224 if( ::strcmp(name, iter->second->GetName()) == 0 )
225 return iter->second;
226 return NULL;
229 void
230 ObjectAccessor::SaveAllPlayers()
232 Guard guard(*HashMapHolder<Player*>::GetLock());
233 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
234 HashMapHolder<Player>::MapType::iterator itr = m.begin();
235 for(; itr != m.end(); ++itr)
236 itr->second->SaveToDB();
239 void
240 ObjectAccessor::UpdateObject(Object* obj, Player* exceptPlayer)
242 UpdateDataMapType update_players;
243 obj->BuildUpdate(update_players);
245 WorldPacket packet;
246 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
248 if(iter->first == exceptPlayer)
249 continue;
251 iter->second.BuildPacket(&packet);
252 iter->first->GetSession()->SendPacket(&packet);
253 packet.clear();
257 void
258 ObjectAccessor::_buildUpdateObject(Object *obj, UpdateDataMapType &update_players)
260 bool build_for_all = true;
261 Player *pl = NULL;
262 if( obj->isType(TYPEMASK_ITEM) )
264 Item *item = static_cast<Item *>(obj);
265 pl = item->GetOwner();
266 build_for_all = false;
269 if( pl != NULL )
270 _buildPacket(pl, obj, update_players);
272 // Capt: okey for all those fools who think its a real fix
273 // THIS IS A TEMP FIX
274 if( build_for_all )
276 WorldObject * temp = dynamic_cast<WorldObject*>(obj);
278 //assert(dynamic_cast<WorldObject*>(obj)!=NULL);
279 if (temp)
280 _buildChangeObjectForPlayer(temp, update_players);
281 else
282 sLog.outDebug("ObjectAccessor: Ln 405 Temp bug fix");
286 void
287 ObjectAccessor::_buildPacket(Player *pl, Object *obj, UpdateDataMapType &update_players)
289 UpdateDataMapType::iterator iter = update_players.find(pl);
291 if( iter == update_players.end() )
293 std::pair<UpdateDataMapType::iterator, bool> p = update_players.insert( UpdateDataValueType(pl, UpdateData()) );
294 assert(p.second);
295 iter = p.first;
298 obj->BuildValuesUpdateBlockForPlayer(&iter->second, iter->first);
301 void
302 ObjectAccessor::_buildChangeObjectForPlayer(WorldObject *obj, UpdateDataMapType &update_players)
304 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
305 Cell cell(p);
306 cell.data.Part.reserved = ALL_DISTRICT;
307 cell.SetNoCreate();
308 WorldObjectChangeAccumulator notifier(*obj, update_players);
309 TypeContainerVisitor<WorldObjectChangeAccumulator, WorldTypeMapContainer > player_notifier(notifier);
310 CellLock<GridReadGuard> cell_lock(cell, p);
311 cell_lock->Visit(cell_lock, player_notifier, *obj->GetMap());
314 Pet*
315 ObjectAccessor::GetPet(uint64 guid)
317 return GetObjectInWorld(guid, (Pet*)NULL);
320 Vehicle*
321 ObjectAccessor::GetVehicle(uint64 guid)
323 return GetObjectInWorld(guid, (Vehicle*)NULL);
326 Corpse*
327 ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
329 Guard guard(i_corpseGuard);
331 Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
332 if( iter == i_player2corpse.end() ) return NULL;
334 assert(iter->second->GetType() != CORPSE_BONES);
336 return iter->second;
339 void
340 ObjectAccessor::RemoveCorpse(Corpse *corpse)
342 assert(corpse && corpse->GetType() != CORPSE_BONES);
344 Guard guard(i_corpseGuard);
345 Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
346 if( iter == i_player2corpse.end() )
347 return;
349 // build mapid*cellid -> guid_set map
350 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
351 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
353 objmgr.DeleteCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID());
354 corpse->RemoveFromWorld();
356 i_player2corpse.erase(iter);
359 void
360 ObjectAccessor::AddCorpse(Corpse *corpse)
362 assert(corpse && corpse->GetType() != CORPSE_BONES);
364 Guard guard(i_corpseGuard);
365 assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
366 i_player2corpse[corpse->GetOwnerGUID()] = corpse;
368 // build mapid*cellid -> guid_set map
369 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
370 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
372 objmgr.AddCorpseCellData(corpse->GetMapId(),cell_id,corpse->GetOwnerGUID(),corpse->GetInstanceId());
375 void
376 ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
378 Guard guard(i_corpseGuard);
379 for(Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
380 if(iter->second->GetGrid()==gridpair)
382 // verify, if the corpse in our instance (add only corpses which are)
383 if (map->Instanceable())
385 if (iter->second->GetInstanceId() == map->GetInstanceId())
387 grid.AddWorldObject(iter->second,iter->second->GetGUID());
390 else
392 grid.AddWorldObject(iter->second,iter->second->GetGUID());
397 Corpse*
398 ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
400 Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
401 if(!corpse)
403 //in fact this function is called from several places
404 //even when player doesn't have a corpse, not an error
405 //sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
406 return NULL;
409 DEBUG_LOG("Deleting Corpse and spawning bones.");
411 // remove corpse from player_guid -> corpse map
412 RemoveCorpse(corpse);
414 // remove resurrectble corpse from grid object registry (loaded state checked into call)
415 // do not load the map if it's not loaded
416 Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId());
417 if(map) map->Remove(corpse,false);
419 // remove corpse from DB
420 corpse->DeleteFromDB();
422 Corpse *bones = NULL;
423 // create the bones only if the map and the grid is loaded at the corpse's location
424 // ignore bones creating option in case insignia
425 if (map && (insignia ||
426 (map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_DEATH_BONES_WORLD))) &&
427 !map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
429 // Create bones, don't change Corpse
430 bones = new Corpse;
431 bones->Create(corpse->GetGUIDLow());
433 for (int i = 3; i < CORPSE_END; i++) // don't overwrite guid and object type
434 bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
436 bones->SetGrid(corpse->GetGrid());
437 // bones->m_time = m_time; // don't overwrite time
438 // bones->m_inWorld = m_inWorld; // don't overwrite world state
439 // bones->m_type = m_type; // don't overwrite type
440 bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
441 bones->SetMapId(corpse->GetMapId());
442 bones->SetInstanceId(corpse->GetInstanceId());
443 bones->SetPhaseMask(corpse->GetPhaseMask(),false);
445 bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
446 bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
448 for (int i = 0; i < EQUIPMENT_SLOT_END; i++)
450 if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
451 bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
454 // add bones in grid store if grid loaded where corpse placed
455 map->Add(bones);
458 // all references to the corpse should be removed at this point
459 delete corpse;
461 return bones;
464 void
465 ObjectAccessor::Update(uint32 diff)
467 UpdateDataMapType update_players;
469 Guard guard(i_updateGuard);
470 while(!i_objects.empty())
472 Object* obj = *i_objects.begin();
473 i_objects.erase(i_objects.begin());
474 if (!obj)
475 continue;
476 _buildUpdateObject(obj, update_players);
477 obj->ClearUpdateMask(false);
481 WorldPacket packet; // here we allocate a std::vector with a size of 0x10000
482 for(UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
484 iter->second.BuildPacket(&packet);
485 iter->first->GetSession()->SendPacket(&packet);
486 packet.clear(); // clean the string
490 void
491 ObjectAccessor::UpdatePlayers(uint32 diff)
493 HashMapHolder<Player>::MapType& playerMap = HashMapHolder<Player>::GetContainer();
494 for(HashMapHolder<Player>::MapType::iterator iter = playerMap.begin(); iter != playerMap.end(); ++iter)
495 if(iter->second->IsInWorld())
496 iter->second->Update(diff);
499 void
500 ObjectAccessor::WorldObjectChangeAccumulator::Visit(PlayerMapType &m)
502 for(PlayerMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
503 if(iter->getSource()->HaveAtClient(&i_object))
504 ObjectAccessor::_buildPacket(iter->getSource(), &i_object, i_updateDatas);
507 void
508 ObjectAccessor::UpdateObjectVisibility(WorldObject *obj)
510 CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
511 Cell cell(p);
513 obj->GetMap()->UpdateObjectVisibility(obj,cell,p);
516 void ObjectAccessor::UpdateVisibilityForPlayer( Player* player )
518 CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
519 Cell cell(p);
520 Map* m = player->GetMap();
522 m->UpdatePlayerVisibility(player,cell,p);
523 m->UpdateObjectsVisibilityFor(player,cell,p);
526 /// Define the static member of HashMapHolder
528 template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
529 template <class T> ZThread::FastMutex HashMapHolder<T>::i_lock;
531 /// Global definitions for the hashmap storage
533 template class HashMapHolder<Player>;
534 template class HashMapHolder<Pet>;
535 template class HashMapHolder<Vehicle>;
536 template class HashMapHolder<GameObject>;
537 template class HashMapHolder<DynamicObject>;
538 template class HashMapHolder<Creature>;
539 template class HashMapHolder<Corpse>;
541 template Player* ObjectAccessor::GetObjectInWorld<Player>(uint32 mapid, float x, float y, uint64 guid, Player* /*fake*/);
542 template Pet* ObjectAccessor::GetObjectInWorld<Pet>(uint32 mapid, float x, float y, uint64 guid, Pet* /*fake*/);
543 template Vehicle* ObjectAccessor::GetObjectInWorld<Vehicle>(uint32 mapid, float x, float y, uint64 guid, Vehicle* /*fake*/);
544 template Creature* ObjectAccessor::GetObjectInWorld<Creature>(uint32 mapid, float x, float y, uint64 guid, Creature* /*fake*/);
545 template Corpse* ObjectAccessor::GetObjectInWorld<Corpse>(uint32 mapid, float x, float y, uint64 guid, Corpse* /*fake*/);
546 template GameObject* ObjectAccessor::GetObjectInWorld<GameObject>(uint32 mapid, float x, float y, uint64 guid, GameObject* /*fake*/);
547 template DynamicObject* ObjectAccessor::GetObjectInWorld<DynamicObject>(uint32 mapid, float x, float y, uint64 guid, DynamicObject* /*fake*/);