[6982] Implemented gmlevel-based command security
[getmangos.git] / src / game / GridNotifiersImpl.h
blob602d3d247ef82798286cad16594fcf5a277acf1c
1 /*
2 * Copyright (C) 2005-2008 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_GRIDNOTIFIERSIMPL_H
20 #define MANGOS_GRIDNOTIFIERSIMPL_H
22 #include "GridNotifiers.h"
23 #include "WorldPacket.h"
24 #include "Corpse.h"
25 #include "Player.h"
26 #include "UpdateData.h"
27 #include "CreatureAI.h"
28 #include "SpellAuras.h"
30 template<class T>
31 inline void
32 MaNGOS::VisibleNotifier::Visit(GridRefManager<T> &m)
34 for(typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
36 i_player.UpdateVisibilityOf(iter->getSource(),i_data,i_data_updates,i_visibleNow);
37 i_clientGUIDs.erase(iter->getSource()->GetGUID());
41 inline void
42 MaNGOS::ObjectUpdater::Visit(CreatureMapType &m)
44 for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
45 if(!iter->getSource()->isSpiritService())
46 iter->getSource()->Update(i_timeDiff);
49 inline void
50 MaNGOS::PlayerRelocationNotifier::Visit(PlayerMapType &m)
52 for(PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
54 if(&i_player==iter->getSource())
55 continue;
57 // visibility for players updated by ObjectAccessor::UpdateVisibilityFor calls in appropriate places
59 // Cancel Trade
60 if(i_player.GetTrader()==iter->getSource())
61 // iteraction distance
62 if(!i_player.IsWithinDistInMap(iter->getSource(), 5))
63 i_player.GetSession()->SendCancelTrade(); // will clode both side trade windows
67 inline void PlayerCreatureRelocationWorker(Player* pl, Creature* c)
69 // update creature visibility at player/creature move
70 pl->UpdateVisibilityOf(c);
72 // Creature AI reaction
73 if(!c->hasUnitState(UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
75 if( c->AI() && c->AI()->IsVisible(pl) && !c->IsInEvadeMode() )
76 c->AI()->MoveInLineOfSight(pl);
80 inline void CreatureCreatureRelocationWorker(Creature* c1, Creature* c2)
82 if(!c1->hasUnitState(UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
84 if( c1->AI() && c1->AI()->IsVisible(c2) && !c1->IsInEvadeMode() )
85 c1->AI()->MoveInLineOfSight(c2);
88 if(!c2->hasUnitState(UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
90 if( c2->AI() && c2->AI()->IsVisible(c1) && !c2->IsInEvadeMode() )
91 c2->AI()->MoveInLineOfSight(c1);
95 inline void
96 MaNGOS::PlayerRelocationNotifier::Visit(CreatureMapType &m)
98 if(!i_player.isAlive() || i_player.isInFlight())
99 return;
101 for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
102 if( iter->getSource()->isAlive())
103 PlayerCreatureRelocationWorker(&i_player,iter->getSource());
106 template<>
107 inline void
108 MaNGOS::CreatureRelocationNotifier::Visit(PlayerMapType &m)
110 if(!i_creature.isAlive())
111 return;
113 for(PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
114 if( iter->getSource()->isAlive() && !iter->getSource()->isInFlight())
115 PlayerCreatureRelocationWorker(iter->getSource(), &i_creature);
118 template<>
119 inline void
120 MaNGOS::CreatureRelocationNotifier::Visit(CreatureMapType &m)
122 if(!i_creature.isAlive())
123 return;
125 for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
127 Creature* c = iter->getSource();
128 if( c != &i_creature && c->isAlive())
129 CreatureCreatureRelocationWorker(c, &i_creature);
133 inline void MaNGOS::DynamicObjectUpdater::VisitHelper(Unit* target)
135 if(!target->isAlive() || target->isInFlight() )
136 return;
138 if(target->GetTypeId()==TYPEID_UNIT && ((Creature*)target)->isTotem())
139 return;
141 if (!i_dynobject.IsWithinDistInMap(target, i_dynobject.GetRadius()))
142 return;
144 //Check targets for not_selectable unit flag and remove
145 if (target->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE | UNIT_FLAG_NOT_SELECTABLE))
146 return;
148 // Evade target
149 if( target->GetTypeId()==TYPEID_UNIT && ((Creature*)target)->IsInEvadeMode() )
150 return;
152 //Check player targets and remove if in GM mode or GM invisibility (for not self casting case)
153 if( target->GetTypeId()==TYPEID_PLAYER && target != i_check && (((Player*)target)->isGameMaster() || ((Player*)target)->GetVisibility()==VISIBILITY_OFF) )
154 return;
156 if( i_check->GetTypeId()==TYPEID_PLAYER )
158 if (i_check->IsFriendlyTo( target ))
159 return;
161 else
163 if (!i_check->IsHostileTo( target ))
164 return;
167 if (i_dynobject.IsAffecting(target))
168 return;
170 SpellEntry const *spellInfo = sSpellStore.LookupEntry(i_dynobject.GetSpellId());
171 uint32 eff_index = i_dynobject.GetEffIndex();
172 // Check target immune to spell or aura
173 if (target->IsImmunedToSpell(spellInfo) || target->IsImmunedToSpellEffect(spellInfo->Effect[eff_index], spellInfo->EffectMechanic[eff_index]))
174 return;
175 // Apply PersistentAreaAura on target
176 PersistentAreaAura* Aur = new PersistentAreaAura(spellInfo, eff_index, NULL, target, i_dynobject.GetCaster());
177 target->AddAura(Aur);
178 i_dynobject.AddAffected(target);
181 template<>
182 inline void
183 MaNGOS::DynamicObjectUpdater::Visit(CreatureMapType &m)
185 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
186 VisitHelper(itr->getSource());
189 template<>
190 inline void
191 MaNGOS::DynamicObjectUpdater::Visit(PlayerMapType &m)
193 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
194 VisitHelper(itr->getSource());
197 // SEARCHERS & LIST SEARCHERS & WORKERS
199 // WorldObject searchers & workers
201 template<class Check>
202 void MaNGOS::WorldObjectSearcher<Check>::Visit(GameObjectMapType &m)
204 // already found
205 if(i_object)
206 return;
208 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
210 if(i_check(itr->getSource()))
212 i_object = itr->getSource();
213 return;
218 template<class Check>
219 void MaNGOS::WorldObjectSearcher<Check>::Visit(PlayerMapType &m)
221 // already found
222 if(i_object)
223 return;
225 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
227 if(i_check(itr->getSource()))
229 i_object = itr->getSource();
230 return;
235 template<class Check>
236 void MaNGOS::WorldObjectSearcher<Check>::Visit(CreatureMapType &m)
238 // already found
239 if(i_object)
240 return;
242 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
244 if(i_check(itr->getSource()))
246 i_object = itr->getSource();
247 return;
252 template<class Check>
253 void MaNGOS::WorldObjectSearcher<Check>::Visit(CorpseMapType &m)
255 // already found
256 if(i_object)
257 return;
259 for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
261 if(i_check(itr->getSource()))
263 i_object = itr->getSource();
264 return;
269 template<class Check>
270 void MaNGOS::WorldObjectSearcher<Check>::Visit(DynamicObjectMapType &m)
272 // already found
273 if(i_object)
274 return;
276 for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
278 if(i_check(itr->getSource()))
280 i_object = itr->getSource();
281 return;
286 template<class Check>
287 void MaNGOS::WorldObjectListSearcher<Check>::Visit(PlayerMapType &m)
289 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
290 if(i_check(itr->getSource()))
291 i_objects.push_back(itr->getSource());
294 template<class Check>
295 void MaNGOS::WorldObjectListSearcher<Check>::Visit(CreatureMapType &m)
297 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
298 if(i_check(itr->getSource()))
299 i_objects.push_back(itr->getSource());
302 template<class Check>
303 void MaNGOS::WorldObjectListSearcher<Check>::Visit(CorpseMapType &m)
305 for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
306 if(i_check(itr->getSource()))
307 i_objects.push_back(itr->getSource());
310 template<class Check>
311 void MaNGOS::WorldObjectListSearcher<Check>::Visit(GameObjectMapType &m)
313 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
314 if(i_check(itr->getSource()))
315 i_objects.push_back(itr->getSource());
318 template<class Check>
319 void MaNGOS::WorldObjectListSearcher<Check>::Visit(DynamicObjectMapType &m)
321 for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
322 if(i_check(itr->getSource()))
323 i_objects.push_back(itr->getSource());
326 // Gameobject searchers
328 template<class Check>
329 void MaNGOS::GameObjectSearcher<Check>::Visit(GameObjectMapType &m)
331 // already found
332 if(i_object)
333 return;
335 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
337 if(i_check(itr->getSource()))
339 i_object = itr->getSource();
340 return;
345 template<class Check>
346 void MaNGOS::GameObjectLastSearcher<Check>::Visit(GameObjectMapType &m)
348 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
350 if(i_check(itr->getSource()))
351 i_object = itr->getSource();
355 template<class Check>
356 void MaNGOS::GameObjectListSearcher<Check>::Visit(GameObjectMapType &m)
358 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
359 if(i_check(itr->getSource()))
360 i_objects.push_back(itr->getSource());
363 // Unit searchers
365 template<class Check>
366 void MaNGOS::UnitSearcher<Check>::Visit(CreatureMapType &m)
368 // already found
369 if(i_object)
370 return;
372 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
374 if(i_check(itr->getSource()))
376 i_object = itr->getSource();
377 return;
382 template<class Check>
383 void MaNGOS::UnitSearcher<Check>::Visit(PlayerMapType &m)
385 // already found
386 if(i_object)
387 return;
389 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
391 if(i_check(itr->getSource()))
393 i_object = itr->getSource();
394 return;
399 template<class Check>
400 void MaNGOS::UnitLastSearcher<Check>::Visit(CreatureMapType &m)
402 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
404 if(i_check(itr->getSource()))
405 i_object = itr->getSource();
409 template<class Check>
410 void MaNGOS::UnitLastSearcher<Check>::Visit(PlayerMapType &m)
412 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
414 if(i_check(itr->getSource()))
415 i_object = itr->getSource();
419 template<class Check>
420 void MaNGOS::UnitListSearcher<Check>::Visit(PlayerMapType &m)
422 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
423 if(i_check(itr->getSource()))
424 i_objects.push_back(itr->getSource());
427 template<class Check>
428 void MaNGOS::UnitListSearcher<Check>::Visit(CreatureMapType &m)
430 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
431 if(i_check(itr->getSource()))
432 i_objects.push_back(itr->getSource());
435 // Creature searchers
437 template<class Check>
438 void MaNGOS::CreatureSearcher<Check>::Visit(CreatureMapType &m)
440 // already found
441 if(i_object)
442 return;
444 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
446 if(i_check(itr->getSource()))
448 i_object = itr->getSource();
449 return;
454 template<class Check>
455 void MaNGOS::CreatureLastSearcher<Check>::Visit(CreatureMapType &m)
457 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
459 if(i_check(itr->getSource()))
460 i_object = itr->getSource();
464 template<class Check>
465 void MaNGOS::CreatureListSearcher<Check>::Visit(CreatureMapType &m)
467 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
468 if(i_check(itr->getSource()))
469 i_objects.push_back(itr->getSource());
472 template<class Check>
473 void MaNGOS::PlayerSearcher<Check>::Visit(PlayerMapType &m)
475 // already found
476 if(i_object)
477 return;
479 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
481 if(i_check(itr->getSource()))
483 i_object = itr->getSource();
484 return;
489 #endif // MANGOS_GRIDNOTIFIERSIMPL_H