root/trunk/src/game/ObjectAccessor.h @ 13

Revision 2, 7.8 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_OBJECTACCESSOR_H
20#define MANGOS_OBJECTACCESSOR_H
21
22#include "Platform/Define.h"
23#include "Policies/Singleton.h"
24#include "zthread/FastMutex.h"
25#include "Utilities/HashMap.h"
26#include "Policies/ThreadingModel.h"
27
28#include "ByteBuffer.h"
29#include "UpdateData.h"
30
31#include "GridDefines.h"
32#include "Object.h"
33#include "Player.h"
34
35#include <set>
36
37class Creature;
38class Corpse;
39class Unit;
40class GameObject;
41class DynamicObject;
42class WorldObject;
43class Map;
44
45template <class T>
46class HashMapHolder
47{
48    public:
49
50        typedef HM_NAMESPACE::hash_map< uint64, T* >   MapType;
51        typedef ZThread::FastMutex LockType;
52        typedef MaNGOS::GeneralLock<LockType > Guard;
53
54        static void Insert(T* o) { m_objectMap[o->GetGUID()] = o; }
55
56        static void Remove(T* o)
57        {
58            Guard guard(i_lock);
59            typename MapType::iterator itr = m_objectMap.find(o->GetGUID());
60            if (itr != m_objectMap.end())
61                m_objectMap.erase(itr);
62        }
63
64        static T* Find(uint64 guid)
65        {
66            typename MapType::iterator itr = m_objectMap.find(guid);
67            return (itr != m_objectMap.end()) ? itr->second : NULL;
68        }
69
70        static MapType& GetContainer() { return m_objectMap; }
71
72        static LockType* GetLock() { return &i_lock; }
73    private:
74
75        //Non instanciable only static
76        HashMapHolder() {}
77
78        static LockType i_lock;
79        static MapType  m_objectMap;
80};
81
82class MANGOS_DLL_DECL ObjectAccessor : public MaNGOS::Singleton<ObjectAccessor, MaNGOS::ClassLevelLockable<ObjectAccessor, ZThread::FastMutex> >
83{
84
85    friend class MaNGOS::OperatorNew<ObjectAccessor>;
86    ObjectAccessor();
87    ~ObjectAccessor();
88    ObjectAccessor(const ObjectAccessor &);
89    ObjectAccessor& operator=(const ObjectAccessor &);
90
91    public:
92        typedef HM_NAMESPACE::hash_map<uint64, Corpse* >      Player2CorpsesMapType;
93        typedef HM_NAMESPACE::hash_map<Player*, UpdateData>::value_type UpdateDataValueType;
94
95        template<class T> static T* GetObjectInWorld(uint64 guid, T* /*fake*/)
96        {
97            return HashMapHolder<T>::Find(guid);
98        }
99
100        static Unit* GetObjectInWorld(uint64 guid, Unit* /*fake*/)
101        {
102            if(!guid)
103                return NULL;
104
105            if (IS_PLAYER_GUID(guid))
106                return (Unit*)HashMapHolder<Player>::Find(guid);
107
108            if (Unit* u = (Unit*)HashMapHolder<Pet>::Find(guid))
109                return u;
110
111            return (Unit*)HashMapHolder<Creature>::Find(guid);
112        }
113
114        template<class T> static T* GetObjectInWorld(uint32 mapid, float x, float y, uint64 guid, T* /*fake*/)
115        {
116            T* obj = HashMapHolder<T>::Find(guid);
117            if(!obj || obj->GetMapId() != mapid) return NULL;
118
119            CellPair p = MaNGOS::ComputeCellPair(x,y);
120            if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
121            {
122                sLog.outError("ObjectAccessor::GetObjectInWorld: invalid coordinates supplied X:%f Y:%f grid cell [%u:%u]", x, y, p.x_coord, p.y_coord);
123                return NULL;
124            }
125
126            CellPair q = MaNGOS::ComputeCellPair(obj->GetPositionX(),obj->GetPositionY());
127            if(q.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || q.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
128            {
129                sLog.outError("ObjectAccessor::GetObjecInWorld: object "I64FMTD" has invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUID(), obj->GetPositionX(), obj->GetPositionY(), q.x_coord, q.y_coord);
130                return NULL;
131            }
132
133            int32 dx = int32(p.x_coord) - int32(q.x_coord);
134            int32 dy = int32(p.y_coord) - int32(q.y_coord);
135
136            if (dx > -2 && dx < 2 && dy > -2 && dy < 2) return obj;
137            else return NULL;
138        }
139
140        static Object*   GetObjectByTypeMask(Player const &, uint64, uint32 typemask);
141        static Creature* GetNPCIfCanInteractWith(Player const &player, uint64 guid, uint32 npcflagmask);
142        static Creature* GetCreature(WorldObject const &, uint64);
143        static Creature* GetCreatureOrPet(WorldObject const &, uint64);
144        static Unit* GetUnit(WorldObject const &, uint64);
145        static Pet* GetPet(Unit const &, uint64 guid) { return GetPet(guid); }
146        static Player* GetPlayer(Unit const &, uint64 guid) { return FindPlayer(guid); }
147        static GameObject* GetGameObject(WorldObject const &, uint64);
148        static DynamicObject* GetDynamicObject(Unit const &, uint64);
149        static Corpse* GetCorpse(WorldObject const &u, uint64 guid);
150        static Pet* GetPet(uint64 guid);
151        static Player* FindPlayer(uint64);
152
153        Player* FindPlayerByName(const char *name) ;
154
155        HashMapHolder<Player>::MapType& GetPlayers()
156        {
157            return HashMapHolder<Player>::GetContainer();
158        }
159
160        template<class T> void AddObject(T *object)
161        {
162            HashMapHolder<T>::Insert(object);
163        }
164
165        template<class T> void RemoveObject(T *object)
166        {
167            HashMapHolder<T>::Remove(object);
168        }
169
170        void RemoveObject(Player *pl)
171        {
172            HashMapHolder<Player>::Remove(pl);
173
174            Guard guard(i_updateGuard);
175
176            std::set<Object *>::iterator iter2 = std::find(i_objects.begin(), i_objects.end(), (Object *)pl);
177            if( iter2 != i_objects.end() )
178                i_objects.erase(iter2);
179        }
180
181        void SaveAllPlayers();
182
183        void AddUpdateObject(Object *obj);
184        void RemoveUpdateObject(Object *obj);
185
186        void Update(uint32 diff);
187
188        Corpse* GetCorpseForPlayerGUID(uint64 guid);
189        void RemoveCorpse(Corpse *corpse);
190        void AddCorpse(Corpse* corpse);
191        void AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map);
192        Corpse* ConvertCorpseForPlayer(uint64 player_guid);
193
194        bool PlayersNearGrid(uint32 x,uint32 y,uint32 m_id,uint32 i_id) const;
195
196        static void UpdateObject(Object* obj, Player* exceptPlayer);
197        static void _buildUpdateObject(Object* obj, UpdateDataMapType &);
198
199        static void UpdateObjectVisibility(WorldObject* obj);
200        static void UpdateVisibilityForPlayer(Player* player);
201    private:
202        struct WorldObjectChangeAccumulator
203        {
204            UpdateDataMapType &i_updateDatas;
205            WorldObject &i_object;
206            WorldObjectChangeAccumulator(WorldObject &obj, UpdateDataMapType &d) : i_updateDatas(d), i_object(obj) {}
207            void Visit(PlayerMapType &);
208            template<class SKIP> void Visit(GridRefManager<SKIP> &) {}
209        };
210
211        friend struct WorldObjectChangeAccumulator;
212        Player2CorpsesMapType   i_player2corpse;
213
214        typedef ZThread::FastMutex LockType;
215        typedef MaNGOS::GeneralLock<LockType > Guard;
216
217        static void _buildChangeObjectForPlayer(WorldObject *, UpdateDataMapType &);
218        static void _buildPacket(Player *, Object *, UpdateDataMapType &);
219        void _update(void);
220        std::set<Object *> i_objects;
221        LockType i_playerGuard;
222        LockType i_updateGuard;
223        LockType i_corpseGuard;
224        LockType i_petGuard;
225};
226#endif
Note: See TracBrowser for help on using the browser.