[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / ObjectAccessor.h
blobe704f10fe39c2fecfd0e56ad9b9f8a7d8f0c7870
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(ObjectGuid guid)
63 typename MapType::iterator itr = m_objectMap.find(guid.GetRawValue());
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, ObjectGuid guid);
94 // FIXME: map local object with global search
95 static Creature* GetCreatureInWorld(ObjectGuid guid) { return FindHelper<Creature>(guid); }
96 static GameObject* GetGameObjectInWorld(ObjectGuid guid) { return FindHelper<GameObject>(guid); }
98 // possible local search for specific object map
99 static Unit* GetUnit(WorldObject const &, ObjectGuid guid);
101 // Player access
102 static Player* FindPlayer(ObjectGuid guid);
103 static Player* FindPlayerByName(const char *name);
104 static void KickPlayer(uint64 guid);
106 HashMapHolder<Player>::MapType& GetPlayers()
108 return HashMapHolder<Player>::GetContainer();
111 void SaveAllPlayers();
113 // Corpse access
114 Corpse* GetCorpseForPlayerGUID(ObjectGuid guid);
115 static Corpse* GetCorpseInMap(ObjectGuid guid, uint32 mapid);
116 void RemoveCorpse(Corpse *corpse);
117 void AddCorpse(Corpse* corpse);
118 void AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map);
119 Corpse* ConvertCorpseForPlayer(ObjectGuid player_guid, bool insignia = false);
121 // For call from Player/Corpse AddToWorld/RemoveFromWorld only
122 void AddObject(Corpse *object) { HashMapHolder<Corpse>::Insert(object); }
123 void AddObject(Player *object) { HashMapHolder<Player>::Insert(object); }
124 void RemoveObject(Corpse *object) { HashMapHolder<Corpse>::Remove(object); }
125 void RemoveObject(Player *object) { HashMapHolder<Player>::Remove(object); }
127 // TODO: This methods will need lock in MT environment
128 static void LinkMap(Map* map) { i_mapList.push_back(map); }
129 static void DelinkMap(Map* map) { i_mapList.remove(map); }
130 private:
131 // TODO: This methods will need lock in MT environment
132 // Theoreticaly multiple threads can enter and search in this method but
133 // in that case linking/delinking other map should be guarded
134 template <class OBJECT> static OBJECT* FindHelper(ObjectGuid guid)
136 for (std::list<Map*>::const_iterator i = i_mapList.begin() ; i != i_mapList.end(); ++i)
138 if (OBJECT* ret = (*i)->GetObjectsStore().find(guid.GetRawValue(), (OBJECT*)NULL))
139 return ret;
142 return NULL;
145 static std::list<Map*> i_mapList;
147 Player2CorpsesMapType i_player2corpse;
149 typedef ACE_Thread_Mutex LockType;
150 typedef MaNGOS::GeneralLock<LockType > Guard;
152 LockType i_playerGuard;
153 LockType i_corpseGuard;
156 inline Unit* ObjectAccessor::GetUnitInWorld(WorldObject const& obj, ObjectGuid guid)
158 if(guid.IsEmpty())
159 return NULL;
161 if (guid.IsPlayer())
162 return FindPlayer(guid);
164 if (guid.IsPet())
165 return obj.IsInWorld() ? obj.GetMap()->GetPet(guid) : NULL;
167 return GetCreatureInWorld(guid);
170 #define sObjectAccessor ObjectAccessor::Instance()
172 #endif