[8483] Implement glyph 43361.
[getmangos.git] / src / game / ObjectAccessor.h
blobcf223ebc9e3f3953ad8f1327da0f3c673dab7fee
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 #ifndef MANGOS_OBJECTACCESSOR_H
20 #define MANGOS_OBJECTACCESSOR_H
22 #include "Platform/Define.h"
23 #include "Policies/Singleton.h"
24 #include <ace/Thread_Mutex.h>
25 #include "Utilities/UnorderedMap.h"
26 #include "Policies/ThreadingModel.h"
28 #include "ByteBuffer.h"
29 #include "UpdateData.h"
31 #include "GridDefines.h"
32 #include "Object.h"
33 #include "Player.h"
35 #include <set>
37 class Creature;
38 class Corpse;
39 class Unit;
40 class GameObject;
41 class DynamicObject;
42 class Vehicle;
43 class WorldObject;
44 class Map;
46 template <class T>
47 class HashMapHolder
49 public:
51 typedef UNORDERED_MAP< uint64, T* > MapType;
52 typedef ACE_Thread_Mutex LockType;
53 typedef MaNGOS::GeneralLock<LockType > Guard;
55 static void Insert(T* o) { m_objectMap[o->GetGUID()] = o; }
57 static void Remove(T* o)
59 Guard guard(i_lock);
60 m_objectMap.erase(o->GetGUID());
63 static T* Find(uint64 guid)
65 typename MapType::iterator itr = m_objectMap.find(guid);
66 return (itr != m_objectMap.end()) ? itr->second : NULL;
69 static MapType& GetContainer() { return m_objectMap; }
71 static LockType* GetLock() { return &i_lock; }
72 private:
74 //Non instanceable only static
75 HashMapHolder() {}
77 static LockType i_lock;
78 static MapType m_objectMap;
81 class MANGOS_DLL_DECL ObjectAccessor : public MaNGOS::Singleton<ObjectAccessor, MaNGOS::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex> >
84 friend class MaNGOS::OperatorNew<ObjectAccessor>;
85 ObjectAccessor();
86 ~ObjectAccessor();
87 ObjectAccessor(const ObjectAccessor &);
88 ObjectAccessor& operator=(const ObjectAccessor &);
90 public:
91 typedef UNORDERED_MAP<uint64, Corpse* > Player2CorpsesMapType;
92 typedef UNORDERED_MAP<Player*, UpdateData>::value_type UpdateDataValueType;
94 template<class T> static T* GetObjectInWorld(uint64 guid, T* /*fake*/)
96 return HashMapHolder<T>::Find(guid);
99 static Unit* GetObjectInWorld(uint64 guid, Unit* /*fake*/)
101 if(!guid)
102 return NULL;
104 if (IS_PLAYER_GUID(guid))
106 Unit * u = (Unit*)HashMapHolder<Player>::Find(guid);
107 if(!u || !u->IsInWorld())
108 return NULL;
110 return u;
113 if (IS_PET_GUID(guid))
114 return (Unit*)HashMapHolder<Pet>::Find(guid);
116 return (Unit*)HashMapHolder<Creature>::Find(guid);
119 template<class T> static T* GetObjectInWorld(uint32 mapid, float x, float y, uint64 guid, T* /*fake*/)
121 T* obj = HashMapHolder<T>::Find(guid);
122 if(!obj || obj->GetMapId() != mapid) return NULL;
124 CellPair p = MaNGOS::ComputeCellPair(x,y);
125 if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
127 sLog.outError("ObjectAccessor::GetObjectInWorld: invalid coordinates supplied X:%f Y:%f grid cell [%u:%u]", x, y, p.x_coord, p.y_coord);
128 return NULL;
131 CellPair q = MaNGOS::ComputeCellPair(obj->GetPositionX(),obj->GetPositionY());
132 if(q.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || q.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
134 sLog.outError("ObjectAccessor::GetObjecInWorld: object (GUID: %u TypeId: %u) has invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), q.x_coord, q.y_coord);
135 return NULL;
138 int32 dx = int32(p.x_coord) - int32(q.x_coord);
139 int32 dy = int32(p.y_coord) - int32(q.y_coord);
141 if (dx > -2 && dx < 2 && dy > -2 && dy < 2) return obj;
142 else return NULL;
145 static WorldObject* GetWorldObject(WorldObject const &, uint64);
146 static Object* GetObjectByTypeMask(WorldObject const &, uint64, uint32 typemask);
147 static Creature* GetCreatureOrPetOrVehicle(WorldObject const &, uint64);
148 static Unit* GetUnit(WorldObject const &, uint64);
149 static Pet* GetPet(Unit const &, uint64 guid) { return GetPet(guid); }
150 static Player* GetPlayer(Unit const &, uint64 guid) { return FindPlayer(guid); }
151 static Corpse* GetCorpse(WorldObject const &u, uint64 guid);
152 static Pet* GetPet(uint64 guid);
153 static Vehicle* GetVehicle(uint64 guid);
154 static Player* FindPlayer(uint64);
156 Player* FindPlayerByName(const char *name) ;
158 HashMapHolder<Player>::MapType& GetPlayers()
160 return HashMapHolder<Player>::GetContainer();
163 template<class T> void AddObject(T *object)
165 HashMapHolder<T>::Insert(object);
168 template<class T> void RemoveObject(T *object)
170 HashMapHolder<T>::Remove(object);
173 void RemoveObject(Player *pl)
175 HashMapHolder<Player>::Remove(pl);
177 Guard guard(i_updateGuard);
178 i_objects.erase((Object *)pl);
181 void SaveAllPlayers();
183 void AddUpdateObject(Object *obj)
185 Guard guard(i_updateGuard);
186 i_objects.insert(obj);
189 void RemoveUpdateObject(Object *obj)
191 Guard guard(i_updateGuard);
192 i_objects.erase( obj );
195 void Update(uint32 diff);
197 Corpse* GetCorpseForPlayerGUID(uint64 guid);
198 void RemoveCorpse(Corpse *corpse);
199 void AddCorpse(Corpse* corpse);
200 void AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map);
201 Corpse* ConvertCorpseForPlayer(uint64 player_guid, bool insignia = false);
203 static void UpdateObject(Object* obj, Player* exceptPlayer);
204 static void _buildUpdateObject(Object* obj, UpdateDataMapType &);
206 static void UpdateObjectVisibility(WorldObject* obj);
207 static void UpdateVisibilityForPlayer(Player* player);
208 private:
209 struct WorldObjectChangeAccumulator
211 UpdateDataMapType &i_updateDatas;
212 WorldObject &i_object;
213 WorldObjectChangeAccumulator(WorldObject &obj, UpdateDataMapType &d) : i_updateDatas(d), i_object(obj) {}
214 void Visit(PlayerMapType &);
215 template<class SKIP> void Visit(GridRefManager<SKIP> &) {}
218 friend struct WorldObjectChangeAccumulator;
219 Player2CorpsesMapType i_player2corpse;
221 typedef ACE_Thread_Mutex LockType;
222 typedef MaNGOS::GeneralLock<LockType > Guard;
224 static void _buildChangeObjectForPlayer(WorldObject *, UpdateDataMapType &);
225 static void _buildPacket(Player *, Object *, UpdateDataMapType &);
226 std::set<Object *> i_objects;
227 LockType i_playerGuard;
228 LockType i_updateGuard;
229 LockType i_corpseGuard;
230 LockType i_petGuard;
232 #endif