[2987] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / GridNotifiers.h
blobef647d9eaf09cc9a373dad81238486cb29df4170
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_GRIDNOTIFIERS_H
20 #define MANGOS_GRIDNOTIFIERS_H
22 #include "ObjectGridLoader.h"
23 #include "ByteBuffer.h"
24 #include "UpdateData.h"
25 #include <iostream>
27 #include "Corpse.h"
28 #include "Object.h"
29 #include "DynamicObject.h"
30 #include "GameObject.h"
31 #include "Player.h"
32 #include "Unit.h"
34 class Player;
35 class Map;
37 namespace MaNGOS
40 struct MANGOS_DLL_DECL PlayerNotifier
42 PlayerNotifier(Player &pl) : i_player(pl) {}
43 void Visit(PlayerMapType &);
44 void BuildForMySelf(void);
45 Player &i_player;
48 struct MANGOS_DLL_DECL VisibleNotifier
50 Player &i_player;
51 UpdateData i_data;
52 VisibleNotifier(Player &player) : i_player(player) {}
53 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m);
54 void Visit(std::map<OBJECT_HANDLE, GameObject *> &);
55 void Notify(void);
57 #ifdef WIN32
59 template<> void VisibleNotifier::Visit(std::map<OBJECT_HANDLE, Creature *> &);
60 template<> void VisibleNotifier::Visit(std::map<OBJECT_HANDLE, Player *> &);
61 #endif
64 struct MANGOS_DLL_DECL VisibleChangesNotifier
66 Player &i_player;
67 VisibleChangesNotifier(Player &player) : i_player(player) {}
68 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m);
70 #ifdef WIN32
71 template<> void VisibleChangesNotifier::Visit(std::map<OBJECT_HANDLE, Player *> &);
72 #endif
75 struct MANGOS_DLL_DECL NotVisibleNotifier
77 Player &i_player;
78 UpdateData i_data;
79 NotVisibleNotifier(Player &player) : i_player(player) {}
80 void Notify(void);
81 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m);
82 void Visit(std::map<OBJECT_HANDLE, GameObject *> &);
84 #ifdef WIN32
85 template<> void NotVisibleNotifier::Visit(std::map<OBJECT_HANDLE, Creature *> &);
86 template<> void NotVisibleNotifier::Visit(std::map<OBJECT_HANDLE, Player *> &);
87 #endif
90 struct MANGOS_DLL_DECL ObjectVisibleNotifier
92 Object &i_object;
93 ObjectVisibleNotifier(Object &obj) : i_object(obj) {}
94 void Visit(PlayerMapType &);
97 struct MANGOS_DLL_DECL ObjectNotVisibleNotifier
99 Object &i_object;
100 ObjectNotVisibleNotifier(Object &obj) : i_object(obj) {}
101 void Visit(PlayerMapType &);
104 struct MANGOS_DLL_DECL GridUpdater
106 GridType &i_grid;
107 uint32 i_timeDiff;
108 GridUpdater(GridType &grid, uint32 diff) : i_grid(grid), i_timeDiff(diff) {}
110 template<class T> void updateObjects(std::map<OBJECT_HANDLE, T *> &m)
112 for(typename std::map<OBJECT_HANDLE, T*>::iterator iter=m.begin(); iter != m.end(); ++iter)
113 iter->second->Update(i_timeDiff);
116 void Visit(PlayerMapType &m) { updateObjects<Player>(m); }
117 void Visit(CreatureMapType &m){ updateObjects<Creature>(m); }
118 void Visit(GameObjectMapType &m) { updateObjects<GameObject>(m); }
119 void Visit(DynamicObjectMapType &m) { updateObjects<DynamicObject>(m); }
120 void Visit(CorpseMapType &m) { updateObjects<Corpse>(m); }
123 struct MANGOS_DLL_DECL MessageDeliverer
125 Player &i_player;
126 WorldPacket *i_message;
127 bool i_toSelf;
128 bool i_ownTeamOnly;
129 MessageDeliverer(Player &pl, WorldPacket *msg, bool to_self, bool ownTeamOnly) : i_player(pl), i_message(msg), i_toSelf(to_self), i_ownTeamOnly(ownTeamOnly) {}
130 void Visit(PlayerMapType &m);
133 struct MANGOS_DLL_DECL ObjectMessageDeliverer
135 Object &i_object;
136 WorldPacket *i_message;
137 ObjectMessageDeliverer(Object &obj, WorldPacket *msg) : i_object(obj), i_message(msg) {}
138 void Visit(PlayerMapType &m);
141 struct MANGOS_DLL_DECL CreatureVisibleMovementNotifier
143 Creature &i_creature;
144 CreatureVisibleMovementNotifier(Creature &creature) : i_creature(creature) {}
145 void Visit(PlayerMapType &m);
148 struct MANGOS_DLL_DECL CreatureNotVisibleMovementNotifier
150 Creature &i_creature;
151 CreatureNotVisibleMovementNotifier(Creature &creature) : i_creature(creature) {}
152 void Visit(PlayerMapType &m);
155 struct MANGOS_DLL_DECL ObjectUpdater
157 uint32 i_timeDiff;
158 ObjectUpdater(const uint32 &diff) : i_timeDiff(diff) {}
159 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m);
160 #ifdef WIN32
161 template<> void Visit(std::map<OBJECT_HANDLE, Creature *> &);
162 #endif
165 template<class T>
166 struct MANGOS_DLL_DECL ObjectAccessorNotifier
168 T *& i_object;
169 uint64 i_id;
170 ObjectAccessorNotifier(T * &obj, uint64 id) : i_object(obj), i_id(id)
172 i_object = NULL;
175 void Visit(std::map<OBJECT_HANDLE, T *> &m )
177 if( i_object == NULL )
179 typename std::map<OBJECT_HANDLE, T *>::iterator iter = m.find(i_id);
180 if( iter != m.end() )
182 assert( iter->second != NULL );
183 i_object = iter->second;
188 template<class NOT_INTERESTED> void Visit(std::map<OBJECT_HANDLE, NOT_INTERESTED *> &m) {}
191 template<class Check>
192 struct MANGOS_DLL_DECL GameObjectSearcher
194 GameObject* &i_object;
195 Check const& i_check;
197 GameObjectSearcher(GameObject* & result, Check const& check) : i_object(result),i_check(check) {}
199 void Visit(std::map<OBJECT_HANDLE, GameObject *> &m);
201 template<class NOT_INTERESTED> void Visit(std::map<OBJECT_HANDLE, NOT_INTERESTED *> &m) {}
204 template<class Check>
205 void GameObjectSearcher<Check>::Visit(std::map<OBJECT_HANDLE, GameObject *> &m)
207 // already found
208 if(i_object) return;
210 for(std::map<OBJECT_HANDLE, GameObject *>::iterator itr=m.begin(); itr != m.end(); ++itr)
212 if(i_check(itr->second))
214 i_object = itr->second;
215 return;
220 class GameObjectFocusCheck
222 public:
223 GameObjectFocusCheck(Unit* unit,uint32 focusId) : i_unit(unit), i_focusId(focusId) {}
224 bool operator()(GameObject* go) const
226 if(go->GetGOInfo()->type != GAMEOBJECT_TYPE_SPELL_FOCUS || go->GetGOInfo()->sound0 != i_focusId)
227 return false;
229 float dist = go->GetGOInfo()->sound1;
231 return go->IsWithinDist(i_unit, dist);
233 private:
234 Unit* i_unit;
235 uint32 i_focusId;
239 template<class Check>
240 struct MANGOS_DLL_DECL UnitSearcher
242 Unit* &i_object;
243 Check & i_check;
245 UnitSearcher(Unit* & result, Check & check) : i_object(result),i_check(check) {}
247 void Visit(std::map<OBJECT_HANDLE, Creature *> &m);
248 void Visit(std::map<OBJECT_HANDLE, Player *> &m);
250 template<class NOT_INTERESTED> void Visit(std::map<OBJECT_HANDLE, NOT_INTERESTED *> &m) {}
253 template<class Check>
254 struct MANGOS_DLL_DECL UnitListSearcher
256 std::list<Unit*> &i_objects;
257 Check& i_check;
259 UnitListSearcher(std::list<Unit*> &objects, Check & check) : i_objects(objects),i_check(check) {}
261 void Visit(std::map<OBJECT_HANDLE, Player *> &m)
263 for(std::map<OBJECT_HANDLE, Player *>::iterator itr=m.begin(); itr != m.end(); ++itr)
265 if(i_check(itr->second))
266 i_objects.push_back(itr->second);
270 void Visit(std::map<OBJECT_HANDLE, Creature *> &m)
272 for(std::map<OBJECT_HANDLE, Creature *>::iterator itr=m.begin(); itr != m.end(); ++itr)
274 if(i_check(itr->second))
275 i_objects.push_back(itr->second);
279 template<class NOT_INTERESTED> void Visit(std::map<OBJECT_HANDLE, NOT_INTERESTED *> &m) {}
282 class AnyUnfriendlyUnitInObjectRangeCheck
284 public:
285 AnyUnfriendlyUnitInObjectRangeCheck(Object* obj, Unit* funit, float range) : i_obj(obj), i_funit(funit), i_range(range), i_result(NULL) {}
286 bool operator()(Unit* u)
288 if(!i_funit->IsFriendlyTo(u) && i_obj->IsWithinDist(u, i_range))
290 i_result = u;
291 return true;
293 return false;
295 Unit *GetResult() const { return i_result; }
296 private:
297 Object* const i_obj;
298 Unit* const i_funit;
299 float i_range;
300 Unit* i_result;
303 struct MANGOS_DLL_DECL GridUnitListNotifier
305 std::list<Unit*> &i_data;
306 GridUnitListNotifier(std::list<Unit*> &data) : i_data(data) {}
308 template<class T> inline void Visit(std::map<OBJECT_HANDLE, T *> &m)
310 for(typename std::map<OBJECT_HANDLE, T*>::iterator itr=m.begin(); itr != m.end(); ++itr)
312 i_data.push_back(itr->second);
316 #ifdef WIN32
317 template<> inline void Visit(std::map<OBJECT_HANDLE, Corpse *> &m ) {}
318 template<> inline void Visit(std::map<OBJECT_HANDLE, GameObject *> &m ) {}
319 template<> inline void Visit(std::map<OBJECT_HANDLE, DynamicObject *> &m ) {}
320 #endif
323 struct MANGOS_DLL_DECL PlayerRelocationNotifier
325 Player &i_player;
326 PlayerRelocationNotifier(Player &pl) : i_player(pl) {}
327 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m) {}
328 #ifdef WIN32
329 template<> void Visit(std::map<OBJECT_HANDLE, Corpse *> &);
330 template<> void Visit(std::map<OBJECT_HANDLE, Player *> &);
331 template<> void Visit(std::map<OBJECT_HANDLE, Creature *> &);
332 #endif
335 struct MANGOS_DLL_DECL CreatureRelocationNotifier
337 Creature &i_creature;
338 CreatureRelocationNotifier(Creature &c) : i_creature(c) {}
339 template<class T> void Visit(std::map<OBJECT_HANDLE, T *> &m) {}
340 #ifdef WIN32
341 template<> void Visit(std::map<OBJECT_HANDLE, Player *> &);
342 #endif
345 struct MANGOS_DLL_DECL DynamicObjectUpdater
347 DynamicObject &owner;
348 DynamicObjectUpdater(DynamicObject &owner) : owner(owner) {}
350 template<class T> inline void Visit(std::map<OBJECT_HANDLE, T *> &m) {}
351 #ifdef WIN32
352 template<> inline void Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
353 template<> inline void Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
354 #endif
357 #ifndef WIN32
359 template<> void VisibleNotifier::Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
360 template<> void VisibleNotifier::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
361 template<> void VisibleChangesNotifier::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
362 template<> void NotVisibleNotifier::Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
363 template<> void NotVisibleNotifier::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
364 template<> void ObjectUpdater::Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
365 template<> void PlayerRelocationNotifier::Visit<Corpse>(std::map<OBJECT_HANDLE, Corpse *> &);
366 template<> void PlayerRelocationNotifier::Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
367 template<> void PlayerRelocationNotifier::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
368 template<> void CreatureRelocationNotifier::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
369 template<> inline void DynamicObjectUpdater::Visit<Creature>(std::map<OBJECT_HANDLE, Creature *> &);
370 template<> inline void DynamicObjectUpdater::Visit<Player>(std::map<OBJECT_HANDLE, Player *> &);
371 template<> inline void GridUnitListNotifier::Visit(std::map<OBJECT_HANDLE, Corpse *> &m ) {}
372 template<> inline void GridUnitListNotifier::Visit(std::map<OBJECT_HANDLE, GameObject *> &m ) {}
373 template<> inline void GridUnitListNotifier::Visit(std::map<OBJECT_HANDLE, DynamicObject *> &m ) {}
374 #endif
376 #endif