[9529] Make Player::IsValidPos const
[getmangos.git] / src / game / ObjectAccessor.h
blobd704045b3e9cb23fe716b99c73a387539f3e4b97
1 /*
2 * Copyright (C) 2005-2010 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 "UpdateData.h"
30 #include "GridDefines.h"
31 #include "Object.h"
32 #include "Player.h"
33 #include "Corpse.h"
35 #include <set>
36 #include <list>
38 class Creature;
39 class Unit;
40 class GameObject;
41 class WorldObject;
42 class Map;
44 template <class T>
45 class HashMapHolder
47 public:
49 typedef UNORDERED_MAP< uint64, T* > MapType;
50 typedef ACE_Thread_Mutex LockType;
51 typedef MaNGOS::GeneralLock<LockType > Guard;
53 static void Insert(T* o) { m_objectMap[o->GetGUID()] = o; }
55 static void Remove(T* o)
57 Guard guard(i_lock);
58 m_objectMap.erase(o->GetGUID());
61 static T* Find(uint64 guid)
63 typename MapType::iterator itr = m_objectMap.find(guid);
64 return (itr != m_objectMap.end()) ? itr->second : NULL;
67 static MapType& GetContainer() { return m_objectMap; }
69 static LockType* GetLock() { return &i_lock; }
70 private:
72 //Non instanceable only static
73 HashMapHolder() {}
75 static LockType i_lock;
76 static MapType m_objectMap;
79 class MANGOS_DLL_DECL ObjectAccessor : public MaNGOS::Singleton<ObjectAccessor, MaNGOS::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex> >
82 friend class MaNGOS::OperatorNew<ObjectAccessor>;
83 ObjectAccessor();
84 ~ObjectAccessor();
85 ObjectAccessor(const ObjectAccessor &);
86 ObjectAccessor& operator=(const ObjectAccessor &);
88 public:
89 typedef UNORDERED_MAP<uint64, Corpse* > Player2CorpsesMapType;
91 // global (obj used for map only location local guid objects (pets currently)
92 static Unit* GetUnitInWorld(WorldObject const& obj, uint64 guid);
94 // FIXME: map local object with global search
95 static Creature* GetCreatureInWorld(uint64 guid) { return FindHelper<Creature>(guid); }
96 static GameObject* GetGameObjectInWorld(uint64 guid) { return FindHelper<GameObject>(guid); }
98 // possible local search for specific object map
99 static Object* GetObjectByTypeMask(WorldObject const &, uint64, uint32 typemask);
100 static Unit* GetUnit(WorldObject const &, uint64);
102 // Player access
103 static Player* FindPlayer(uint64 guid);
104 static Player* FindPlayerByName(const char *name);
105 static void KickPlayer(uint64 guid);
107 HashMapHolder<Player>::MapType& GetPlayers()
109 return HashMapHolder<Player>::GetContainer();
112 void SaveAllPlayers();
114 // Corpse access
115 Corpse* GetCorpseForPlayerGUID(uint64 guid);
116 static Corpse* GetCorpseInMap(uint64 guid, uint32 mapid);
117 void RemoveCorpse(Corpse *corpse);
118 void AddCorpse(Corpse* corpse);
119 void AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map);
120 Corpse* ConvertCorpseForPlayer(uint64 player_guid, bool insignia = false);
122 // For call from Player/Corpse AddToWorld/RemoveFromWorld only
123 void AddObject(Corpse *object) { HashMapHolder<Corpse>::Insert(object); }
124 void AddObject(Player *object) { HashMapHolder<Player>::Insert(object); }
125 void RemoveObject(Corpse *object) { HashMapHolder<Corpse>::Remove(object); }
126 void RemoveObject(Player *object) { HashMapHolder<Player>::Remove(object); }
128 // TODO: This methods will need lock in MT environment
129 static void LinkMap(Map* map) { i_mapList.push_back(map); }
130 static void DelinkMap(Map* map) { i_mapList.remove(map); }
131 private:
132 // TODO: This methods will need lock in MT environment
133 // Theoreticaly multiple threads can enter and search in this method but
134 // in that case linking/delinking other map should be guarded
135 template <class OBJECT> static OBJECT* FindHelper(uint64 guid)
137 for (std::list<Map*>::const_iterator i = i_mapList.begin() ; i != i_mapList.end(); ++i)
139 if (OBJECT* ret = (*i)->GetObjectsStore().find(guid, (OBJECT*)NULL))
140 return ret;
143 return NULL;
146 static std::list<Map*> i_mapList;
148 Player2CorpsesMapType i_player2corpse;
150 typedef ACE_Thread_Mutex LockType;
151 typedef MaNGOS::GeneralLock<LockType > Guard;
153 LockType i_playerGuard;
154 LockType i_corpseGuard;
157 inline Unit* ObjectAccessor::GetUnitInWorld(WorldObject const& obj, uint64 guid)
159 if(!guid)
160 return NULL;
162 if (IS_PLAYER_GUID(guid))
163 return FindPlayer(guid);
165 if (IS_PET_GUID(guid))
166 return obj.IsInWorld() ? obj.GetMap()->GetPet(guid) : NULL;
168 return GetCreatureInWorld(guid);
171 #define sObjectAccessor ObjectAccessor::Instance()
173 #endif