[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / GridNotifiersImpl.h
blobd0815bc35a6449dec7decd31bd528845c69eeb1a
1 /*
2 * Copyright (C) 2005-2009 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, 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(!itr->getSource()->InSamePhase(i_phaseMask))
211 continue;
213 if (i_check(itr->getSource()))
215 i_object = itr->getSource();
216 return;
221 template<class Check>
222 void MaNGOS::WorldObjectSearcher<Check>::Visit(PlayerMapType &m)
224 // already found
225 if(i_object)
226 return;
228 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
230 if(!itr->getSource()->InSamePhase(i_phaseMask))
231 continue;
233 if(i_check(itr->getSource()))
235 i_object = itr->getSource();
236 return;
241 template<class Check>
242 void MaNGOS::WorldObjectSearcher<Check>::Visit(CreatureMapType &m)
244 // already found
245 if(i_object)
246 return;
248 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
250 if(!itr->getSource()->InSamePhase(i_phaseMask))
251 continue;
253 if(i_check(itr->getSource()))
255 i_object = itr->getSource();
256 return;
261 template<class Check>
262 void MaNGOS::WorldObjectSearcher<Check>::Visit(CorpseMapType &m)
264 // already found
265 if(i_object)
266 return;
268 for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
270 if(!itr->getSource()->InSamePhase(i_phaseMask))
271 continue;
273 if(i_check(itr->getSource()))
275 i_object = itr->getSource();
276 return;
281 template<class Check>
282 void MaNGOS::WorldObjectSearcher<Check>::Visit(DynamicObjectMapType &m)
284 // already found
285 if(i_object)
286 return;
288 for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
290 if(!itr->getSource()->InSamePhase(i_phaseMask))
291 continue;
293 if(i_check(itr->getSource()))
295 i_object = itr->getSource();
296 return;
301 template<class Check>
302 void MaNGOS::WorldObjectListSearcher<Check>::Visit(PlayerMapType &m)
304 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
305 if(itr->getSource()->InSamePhase(i_phaseMask))
306 if(i_check(itr->getSource()))
307 i_objects.push_back(itr->getSource());
310 template<class Check>
311 void MaNGOS::WorldObjectListSearcher<Check>::Visit(CreatureMapType &m)
313 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
314 if(itr->getSource()->InSamePhase(i_phaseMask))
315 if(i_check(itr->getSource()))
316 i_objects.push_back(itr->getSource());
319 template<class Check>
320 void MaNGOS::WorldObjectListSearcher<Check>::Visit(CorpseMapType &m)
322 for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
323 if(itr->getSource()->InSamePhase(i_phaseMask))
324 if(i_check(itr->getSource()))
325 i_objects.push_back(itr->getSource());
328 template<class Check>
329 void MaNGOS::WorldObjectListSearcher<Check>::Visit(GameObjectMapType &m)
331 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
332 if(itr->getSource()->InSamePhase(i_phaseMask))
333 if(i_check(itr->getSource()))
334 i_objects.push_back(itr->getSource());
337 template<class Check>
338 void MaNGOS::WorldObjectListSearcher<Check>::Visit(DynamicObjectMapType &m)
340 for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
341 if(itr->getSource()->InSamePhase(i_phaseMask))
342 if(i_check(itr->getSource()))
343 i_objects.push_back(itr->getSource());
346 // Gameobject searchers
348 template<class Check>
349 void MaNGOS::GameObjectSearcher<Check>::Visit(GameObjectMapType &m)
351 // already found
352 if(i_object)
353 return;
355 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
357 if(!itr->getSource()->InSamePhase(i_phaseMask))
358 continue;
360 if(i_check(itr->getSource()))
362 i_object = itr->getSource();
363 return;
368 template<class Check>
369 void MaNGOS::GameObjectLastSearcher<Check>::Visit(GameObjectMapType &m)
371 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
373 if(!itr->getSource()->InSamePhase(i_phaseMask))
374 continue;
376 if(i_check(itr->getSource()))
377 i_object = itr->getSource();
381 template<class Check>
382 void MaNGOS::GameObjectListSearcher<Check>::Visit(GameObjectMapType &m)
384 for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
385 if(itr->getSource()->InSamePhase(i_phaseMask))
386 if(i_check(itr->getSource()))
387 i_objects.push_back(itr->getSource());
390 // Unit searchers
392 template<class Check>
393 void MaNGOS::UnitSearcher<Check>::Visit(CreatureMapType &m)
395 // already found
396 if(i_object)
397 return;
399 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
401 if(!itr->getSource()->InSamePhase(i_phaseMask))
402 continue;
404 if(i_check(itr->getSource()))
406 i_object = itr->getSource();
407 return;
412 template<class Check>
413 void MaNGOS::UnitSearcher<Check>::Visit(PlayerMapType &m)
415 // already found
416 if(i_object)
417 return;
419 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
421 if(!itr->getSource()->InSamePhase(i_phaseMask))
422 continue;
424 if(i_check(itr->getSource()))
426 i_object = itr->getSource();
427 return;
432 template<class Check>
433 void MaNGOS::UnitLastSearcher<Check>::Visit(CreatureMapType &m)
435 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
437 if(!itr->getSource()->InSamePhase(i_phaseMask))
438 continue;
440 if(i_check(itr->getSource()))
441 i_object = itr->getSource();
445 template<class Check>
446 void MaNGOS::UnitLastSearcher<Check>::Visit(PlayerMapType &m)
448 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
450 if(!itr->getSource()->InSamePhase(i_phaseMask))
451 continue;
453 if(i_check(itr->getSource()))
454 i_object = itr->getSource();
458 template<class Check>
459 void MaNGOS::UnitListSearcher<Check>::Visit(PlayerMapType &m)
461 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
462 if(itr->getSource()->InSamePhase(i_phaseMask))
463 if(i_check(itr->getSource()))
464 i_objects.push_back(itr->getSource());
467 template<class Check>
468 void MaNGOS::UnitListSearcher<Check>::Visit(CreatureMapType &m)
470 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
471 if(itr->getSource()->InSamePhase(i_phaseMask))
472 if(i_check(itr->getSource()))
473 i_objects.push_back(itr->getSource());
476 // Creature searchers
478 template<class Check>
479 void MaNGOS::CreatureSearcher<Check>::Visit(CreatureMapType &m)
481 // already found
482 if(i_object)
483 return;
485 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
487 if(!itr->getSource()->InSamePhase(i_phaseMask))
488 continue;
490 if(i_check(itr->getSource()))
492 i_object = itr->getSource();
493 return;
498 template<class Check>
499 void MaNGOS::CreatureLastSearcher<Check>::Visit(CreatureMapType &m)
501 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
503 if(!itr->getSource()->InSamePhase(i_phaseMask))
504 continue;
506 if(i_check(itr->getSource()))
507 i_object = itr->getSource();
511 template<class Check>
512 void MaNGOS::CreatureListSearcher<Check>::Visit(CreatureMapType &m)
514 for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
515 if(itr->getSource()->InSamePhase(i_phaseMask))
516 if( i_check(itr->getSource()))
517 i_objects.push_back(itr->getSource());
520 template<class Check>
521 void MaNGOS::PlayerSearcher<Check>::Visit(PlayerMapType &m)
523 // already found
524 if(i_object)
525 return;
527 for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
529 if(!itr->getSource()->InSamePhase(i_phaseMask))
530 continue;
532 if(i_check(itr->getSource()))
534 i_object = itr->getSource();
535 return;
540 #endif // MANGOS_GRIDNOTIFIERSIMPL_H