OregonCore  revision fb2a440-git
Your Favourite TBC server
ObjectAccessor.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef OREGON_OBJECTACCESSOR_H
19 #define OREGON_OBJECTACCESSOR_H
20 
21 #include "Platform/Define.h"
22 #include "Policies/Singleton.h"
23 #include <ace/Thread_Mutex.h>
24 #include "Utilities/UnorderedMap.h"
26 
27 #include "ByteBuffer.h"
28 #include "UpdateData.h"
29 
30 #include "GridDefines.h"
31 #include "Object.h"
32 #include "Player.h"
33 
34 #include <set>
35 
36 class Creature;
37 class Corpse;
38 class Unit;
39 class GameObject;
40 class DynamicObject;
41 class WorldObject;
42 class Map;
43 
44 template <class T>
46 {
47  public:
48 
49  typedef UNORDERED_MAP<uint64, T*> MapType;
50  typedef ACE_Thread_Mutex LockType;
52 
53  static void Insert(T* o)
54  {
55  Guard guard(i_lock);
56  m_objectMap[o->GetGUID()] = o;
57  }
58 
59  static void Remove(T* o)
60  {
61  Guard guard(i_lock);
62  m_objectMap.erase(o->GetGUID());
63  }
64 
65  static T* Find(uint64 guid)
66  {
67  Guard guard(i_lock);
68  typename MapType::iterator itr = m_objectMap.find(guid);
69  return (itr != m_objectMap.end()) ? itr->second : NULL;
70  }
71 
72  static MapType& GetContainer()
73  {
74  return m_objectMap;
75  }
76 
77  static LockType* GetLock()
78  {
79  return &i_lock;
80  }
81  private:
82 
83  //Non instanceable only static
85 
86  static LockType i_lock;
87  static MapType m_objectMap;
88 };
89 
90 class ObjectAccessor : public Oregon::Singleton<ObjectAccessor, Oregon::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex> >
91 {
94  ~ObjectAccessor();
96  ObjectAccessor& operator=(const ObjectAccessor&);
97 
98  public:
99 
100  typedef UNORDERED_MAP<uint64, Corpse*> Player2CorpsesMapType;
101  typedef UNORDERED_MAP<Player*, UpdateData>::value_type UpdateDataValueType;
102 
103  // returns object if is in world
104  template<class T> static T* GetObjectInWorld(uint64 guid, T* /*typeSpecifier*/)
105  {
106  return HashMapHolder<T>::Find(guid);
107  }
108 
109  // Player may be not in world while in ObjectAccessor
110  static Player* GetObjectInWorld(uint64 guid, Player* /*typeSpecifier*/)
111  {
112  Player* player = HashMapHolder<Player>::Find(guid);
113  if (player && player->IsInWorld())
114  return player;
115  return NULL;
116  }
117 
118  static Unit* GetObjectInWorld(uint64 guid, Unit* /*typeSpecifier*/)
119  {
120  if (IS_PLAYER_GUID(guid))
121  return (Unit*)GetObjectInWorld(guid, (Player*)NULL);
122 
123  if (IS_PET_GUID(guid))
124  return (Unit*)GetObjectInWorld(guid, (Pet*)NULL);
125 
126  return (Unit*)GetObjectInWorld(guid, (Creature*)NULL);
127  }
128 
129  // returns object if is in map
130  template<class T> static T* GetObjectInMap(uint64 guid, Map* map, T* /*typeSpecifier*/)
131  {
132  assert(map);
133  if (T* obj = GetObjectInWorld(guid, (T*)NULL))
134  if (obj->GetMap() == map)
135  return obj;
136  return NULL;
137  }
138 
139  template<class T> static T* GetObjectInWorld(uint32 mapid, float x, float y, uint64 guid, T* /*fake*/)
140  {
141  T* obj = HashMapHolder<T>::Find(guid);
142  if (!obj || obj->GetMapId() != mapid)
143  return NULL;
144 
147  {
148  sLog.outError("ObjectAccessor::GetObjectInWorld: invalid coordinates supplied X:%f Y:%f grid cell [%u:%u]", x, y, p.x_coord, p.y_coord);
149  return NULL;
150  }
151 
152  CellCoord q = Oregon::ComputeCellCoord(obj->GetPositionX(), obj->GetPositionY());
154  {
155  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);
156  return NULL;
157  }
158 
159  int32 dx = int32(p.x_coord) - int32(q.x_coord);
160  int32 dy = int32(p.y_coord) - int32(q.y_coord);
161 
162  if (dx > -2 && dx < 2 && dy > -2 && dy < 2)
163  return obj;
164  else
165  return NULL;
166  }
167 
168  // these functions return objects only if in map of specified object
169  static WorldObject* GetWorldObject(WorldObject const&, uint64);
170  static Object* GetObjectByTypeMask(WorldObject const&, uint64, uint32 typemask);
171  static Corpse* GetCorpse(WorldObject const& u, uint64 guid);
172  static GameObject* GetGameObject(WorldObject const& u, uint64 guid);
173  static DynamicObject* GetDynamicObject(WorldObject const& u, uint64 guid);
174  static Unit* GetUnit(WorldObject const&, uint64 guid);
175  static Creature* GetCreature(WorldObject const& u, uint64 guid);
176  static Pet* GetPet(WorldObject const&, uint64 guid);
177  static Player* GetPlayer(WorldObject const&, uint64 guid);
178  static Creature* GetCreatureOrPet(WorldObject const&, uint64);
179 
180  // these functions return objects if found in whole world
181  // ACCESS LIKE THAT IS NOT THREAD SAFE
182  static Pet* FindPet(uint64);
183  static Player* FindPlayer(uint64, bool force = false);
184  static Unit* FindUnit(uint64);
185  Player* FindPlayerByName(const char* name, bool force = false);
186  Player* FindPlayerByAccountId(uint64 Id, bool force = false);
187 
188  // when using this, you must use the hashmapholder's lock
190  {
192  }
193 
194  // when using this, you must use the hashmapholder's lock
196  {
198  }
199 
200  // when using this, you must use the hashmapholder's lock
202  {
204  }
205 
206  template<class T> void AddObject(T* object)
207  {
208  HashMapHolder<T>::Insert(object);
209  }
210 
211  template<class T> void RemoveObject(T* object)
212  {
213  HashMapHolder<T>::Remove(object);
214  }
215 
217  {
219  RemoveUpdateObject((Object*)pl);
220  }
221 
222  void SaveAllPlayers();
223 
225  {
226  Guard guard(i_updateGuard);
227  i_objects.insert(obj);
228  }
229 
231  {
232  Guard guard(i_updateGuard);
233  i_objects.erase(obj);
234  }
235 
236  void Update(uint32 diff);
237 
238  Corpse* GetCorpseForPlayerGUID(uint64 guid);
239  void RemoveCorpse(Corpse* corpse);
240  void AddCorpse(Corpse* corpse);
241  void AddCorpsesToGrid(GridCoord const& gridpair, GridType& grid, Map* map);
242  Corpse* ConvertCorpseForPlayer(uint64 player_guid, bool insignia = false);
243  void RemoveOldCorpses();
244 
245  typedef ACE_Thread_Mutex LockType;
247 
248  private:
249 
250  Player2CorpsesMapType i_player2corpse;
251 
252  static void _buildChangeObjectForPlayer(WorldObject*, UpdateDataMapType&);
253  static void _buildPacket(Player*, Object*, UpdateDataMapType&);
254  void _update();
255 
256  std::set<Object*> i_objects;
257 
258  LockType i_updateGuard;
259  LockType i_corpseGuard;
260 };
261 #endif
262 
Definition: Object.h:130
static HashMapHolder< Creature >::MapType const & GetCreatures()
void RemoveUpdateObject(Object *obj)
Definition: Corpse.h:48
static void Insert(T *o)
static MapType & GetContainer()
std::set< Object * > i_objects
static Unit * GetObjectInWorld(uint64 guid, Unit *)
HashMapHolder< Player >::MapType & GetPlayers()
static LockType i_lock
static LockType * GetLock()
void AddObject(T *object)
#define sLog
Log class singleton.
Definition: Log.h:187
ACE_INT32 int32
Definition: Define.h:67
static T * Find(uint64 guid)
Definition: Grid.h:45
UNORDERED_MAP< Player *, UpdateData >::value_type UpdateDataValueType
uint32 y_coord
Definition: GridDefines.h:142
UNORDERED_MAP< uint64, T * > MapType
uint32 x_coord
Definition: GridDefines.h:141
void RemoveObject(Player *pl)
#define TOTAL_NUMBER_OF_CELLS_PER_MAP
Definition: GridDefines.h:49
#define IS_PET_GUID(Guid)
Definition: ObjectGuid.h:67
static MapType m_objectMap
Player2CorpsesMapType i_player2corpse
#define IS_PLAYER_GUID(Guid)
Definition: ObjectGuid.h:69
static T * GetObjectInWorld(uint64 guid, T *)
const bool & IsInWorld() const
Definition: Object.h:135
UNORDERED_MAP< uint64, Corpse * > Player2CorpsesMapType
void AddUpdateObject(Object *obj)
UNORDERED_MAP< Player *, UpdateData > UpdateDataMapType
Definition: Object.h:126
Definition: Map.h:266
CellCoord ComputeCellCoord(float x, float y)
Definition: GridDefines.h:167
ACE_Thread_Mutex LockType
LockType i_corpseGuard
void RemoveObject(T *object)
static Player * GetObjectInWorld(uint64 guid, Player *)
ACE_UINT64 uint64
Definition: Define.h:70
Oregon::GeneralLock< LockType > Guard
Oregon::GeneralLock< LockType > Guard
LockType i_updateGuard
static void Remove(T *o)
static HashMapHolder< GameObject >::MapType const & GetGameObjects()
ACE_Thread_Mutex LockType
static T * GetObjectInMap(uint64 guid, Map *map, T *)
ACE_UINT32 uint32
Definition: Define.h:71
Definition: Unit.h:908
Definition: Player.h:922
static T * GetObjectInWorld(uint32 mapid, float x, float y, uint64 guid, T *)
Definition: Pet.h:146