[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / ObjectAccessor.h
blobfbda81a64d866b96173aa13409866b114004e4fa
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
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 "zthread/FastMutex.h"
26 #include "ByteBuffer.h"
27 #include "UpdateData.h"
29 #include <set>
31 class Creature;
32 class Player;
33 class Corpse;
34 class Unit;
35 class GameObject;
36 class DynamicObject;
37 class Object;
39 class MANGOS_DLL_DECL ObjectAccessor : public MaNGOS::Singleton<ObjectAccessor, MaNGOS::ClassLevelLockable<ObjectAccessor, ZThread::FastMutex> >
42 friend class MaNGOS::OperatorNew<ObjectAccessor>;
43 ObjectAccessor() {}
44 ObjectAccessor(const ObjectAccessor &);
45 ObjectAccessor& operator=(const ObjectAccessor &);
47 public:
49 typedef HM_NAMESPACE::hash_map<uint64, Player* > PlayersMapType;
50 typedef HM_NAMESPACE::hash_map<uint64, Corpse* > Player2CorpsesMapType;
51 typedef HM_NAMESPACE::hash_map<Player*, UpdateData> UpdateDataMapType;
52 typedef HM_NAMESPACE::hash_map<Player*, UpdateData>::value_type UpdateDataValueType;
54 Object* GetObjectByTypeMask(Player const &, uint64, uint32 typemask);
55 Creature* GetCreature(Object const &, uint64);
56 Corpse* GetCorpse(Unit const &u, uint64 guid);
57 Corpse* GetCorpse(float x, float y, uint32 mapid, uint64 guid);
58 Unit* GetUnit(Object const &, uint64);
59 Player* GetPlayer(Unit const &, uint64);
60 GameObject* GetGameObject(Unit const &, uint64);
61 DynamicObject* GetDynamicObject(Unit const &, uint64);
63 Player* FindPlayer(uint64);
64 Player* FindPlayerByName(const char *name) ;
66 PlayersMapType& GetPlayers() { return i_players; }
67 void InsertPlayer(Player *);
68 void RemovePlayer(Player *);
69 void SaveAllPlayers();
71 void AddUpdateObject(Object *obj);
72 void RemoveUpdateObject(Object *obj);
74 void AddObjectToRemoveList(Creature *obj);
75 void AddObjectToRemoveList(GameObject *obj);
76 void AddObjectToRemoveList(Corpse *obj);
77 void AddObjectToRemoveList(DynamicObject *obj);
79 void DoDelayedMovesAndRemoves();
81 void RemoveCreatureCorpseFromPlayerView(Creature *);
82 void RemoveBonesFromPlayerView(Object *);
83 void RemovePlayerFromPlayerView(Player *, Player *);
84 void RemoveInvisiblePlayerFromPlayerView(Player *, Player *);
85 void RemoveCreatureFromPlayerView(Player *pl, Creature *c);
87 void Update(const uint32 &diff);
89 Corpse* GetCorpseForPlayer(Player const &);
90 void RemoveCorpse(Corpse *corpse);
91 void AddCorpse(Corpse *corpse);
93 bool PlayersNearGrid(const uint32 &x, const uint32 &y, const uint32 &) const;
95 private:
96 void RemoveAllObjectsInRemoveList();
98 struct ObjectChangeAccumulator
100 UpdateDataMapType &i_updateDatas;
101 Object &i_object;
102 ObjectAccessor &i_accessor;
103 ObjectChangeAccumulator(Object &obj, UpdateDataMapType &d, ObjectAccessor &a) : i_updateDatas(d), i_object(obj), i_accessor(a) {}
104 void Visit(std::map<OBJECT_HANDLE, Player *> &);
107 friend struct ObjectChangeAccumulator;
108 PlayersMapType i_players;
109 Player2CorpsesMapType i_corpse;
111 typedef ZThread::FastMutex LockType;
112 typedef MaNGOS::GeneralLock<LockType > Guard;
114 void _buildChangeObjectForPlayer(Object *, UpdateDataMapType &);
115 void _buildUpdateObject(Object *, UpdateDataMapType &);
116 void _buildPacket(Player *, Object *, UpdateDataMapType &);
117 void _update(void);
118 std::set<Object *> i_objects;
119 void _AddObjectToRemoveList(Object *obj);
120 std::set<Object *> i_objectsToRemove;
121 LockType i_playerGuard;
122 LockType i_updateGuard;
123 LockType i_removeGuard;
124 LockType i_corpseGuard;
127 namespace MaNGOS
130 struct MANGOS_DLL_DECL BuildUpdateForPlayer
132 Player &i_player;
133 ObjectAccessor::UpdateDataMapType &i_updatePlayers;
134 BuildUpdateForPlayer(Player &player, ObjectAccessor::UpdateDataMapType &data_map) : i_player(player), i_updatePlayers(data_map) {}
135 void Visit(std::map<OBJECT_HANDLE, Player *> &);
138 struct MANGOS_DLL_DECL CreatureCorpseViewRemover
140 Creature &i_creature;
141 CreatureCorpseViewRemover(Creature &c) : i_creature(c) {}
142 void Visit(std::map<OBJECT_HANDLE, Player *> &);
145 struct MANGOS_DLL_DECL BonesViewRemover
147 Object &i_objects;
148 BonesViewRemover(Object &o) : i_objects(o) {}
149 void Visit(std::map<OBJECT_HANDLE, Player *> &);
152 struct MANGOS_DLL_DECL PlayerDeadViewRemover
154 Player &i_player;
155 Player &i_player2;
156 PlayerDeadViewRemover(Player &pl, Player &pl2) : i_player(pl), i_player2(pl2) {}
157 void Visit(std::map<OBJECT_HANDLE, Player *> &);
160 struct MANGOS_DLL_DECL PlayerInvisibilityRemover
162 Player &i_player;
163 Player &i_player2;
164 PlayerInvisibilityRemover(Player &pl, Player &pl2) : i_player(pl), i_player2(pl2) {}
165 void Visit(std::map<OBJECT_HANDLE, Player *> &);
168 struct MANGOS_DLL_DECL CreatureViewRemover
170 Player &i_player;
171 Creature &i_creature;
172 CreatureViewRemover(Player &pl, Creature &c) : i_player(pl), i_creature(c) {}
173 void Visit(std::map<OBJECT_HANDLE, Player *> &);
176 #endif