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"
39 class MANGOS_DLL_DECL ObjectAccessor
: public MaNGOS::Singleton
<ObjectAccessor
, MaNGOS::ClassLevelLockable
<ObjectAccessor
, ZThread::FastMutex
> >
42 friend class MaNGOS::OperatorNew
<ObjectAccessor
>;
44 ObjectAccessor(const ObjectAccessor
&);
45 ObjectAccessor
& operator=(const ObjectAccessor
&);
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;
96 void RemoveAllObjectsInRemoveList();
98 struct ObjectChangeAccumulator
100 UpdateDataMapType
&i_updateDatas
;
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
&);
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
;
130 struct MANGOS_DLL_DECL BuildUpdateForPlayer
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
148 BonesViewRemover(Object
&o
) : i_objects(o
) {}
149 void Visit(std::map
<OBJECT_HANDLE
, Player
*> &);
152 struct MANGOS_DLL_DECL PlayerDeadViewRemover
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
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
171 Creature
&i_creature
;
172 CreatureViewRemover(Player
&pl
, Creature
&c
) : i_player(pl
), i_creature(c
) {}
173 void Visit(std::map
<OBJECT_HANDLE
, Player
*> &);