root/trunk/src/game/GridNotifiersImpl.h @ 22

Revision 2, 13.1 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef MANGOS_GRIDNOTIFIERSIMPL_H
20#define MANGOS_GRIDNOTIFIERSIMPL_H
21
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"
29
30template<class T>
31inline void
32MaNGOS::VisibleNotifier::Visit(GridRefManager<T> &m)
33{
34    for(typename GridRefManager<T>::iterator iter = m.begin(); iter != m.end(); ++iter)
35    {
36        i_player.UpdateVisibilityOf(iter->getSource(),i_data,i_data_updates,i_visibleNow);
37        i_clientGUIDs.erase(iter->getSource()->GetGUID());
38    }
39}
40
41inline void
42MaNGOS::ObjectUpdater::Visit(CreatureMapType &m)
43{
44    for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
45        if(!iter->getSource()->isSpiritService())
46            iter->getSource()->Update(i_timeDiff);
47}
48
49inline void
50MaNGOS::PlayerRelocationNotifier::Visit(PlayerMapType &m)
51{
52    for(PlayerMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
53    {
54        if(&i_player==iter->getSource())
55            continue;
56
57        // visibility for players updated by ObjectAccessor::UpdateVisibilityFor calls in appropriate places
58
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
64    }
65}
66
67inline void PlayerCreatureRelocationWorker(Player* pl, Creature* c)
68{
69    // update creature visibility at player/creature move
70    pl->UpdateVisibilityOf(c);
71
72    // Creature AI reaction
73    if(!c->hasUnitState(UNIT_STAT_CHASE | UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
74    {
75        if( c->AI() && c->AI()->IsVisible(pl) && !c->IsInEvadeMode() )
76            c->AI()->MoveInLineOfSight(pl);
77    }
78}
79
80inline void CreatureCreatureRelocationWorker(Creature* c1, Creature* c2)
81{
82    if(!c1->hasUnitState(UNIT_STAT_CHASE | UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
83    {
84        if( c1->AI() && c1->AI()->IsVisible(c2) && !c1->IsInEvadeMode() )
85            c1->AI()->MoveInLineOfSight(c2);
86    }
87
88    if(!c2->hasUnitState(UNIT_STAT_CHASE | UNIT_STAT_SEARCHING | UNIT_STAT_FLEEING))
89    {
90        if( c2->AI() && c2->AI()->IsVisible(c1) && !c2->IsInEvadeMode() )
91            c2->AI()->MoveInLineOfSight(c1);
92    }
93}
94
95inline void
96MaNGOS::PlayerRelocationNotifier::Visit(CreatureMapType &m)
97{
98    if(!i_player.isAlive() || i_player.isInFlight())
99        return;
100
101    for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
102        if( iter->getSource()->isAlive())
103            PlayerCreatureRelocationWorker(&i_player,iter->getSource());
104}
105
106template<>
107inline void
108MaNGOS::CreatureRelocationNotifier::Visit(PlayerMapType &m)
109{
110    if(!i_creature.isAlive())
111        return;
112
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);
116}
117
118template<>
119inline void
120MaNGOS::CreatureRelocationNotifier::Visit(CreatureMapType &m)
121{
122    if(!i_creature.isAlive())
123        return;
124
125    for(CreatureMapType::iterator iter=m.begin(); iter != m.end(); ++iter)
126    {
127        Creature* c = iter->getSource();
128        if( c != &i_creature && c->isAlive())
129            CreatureCreatureRelocationWorker(c, &i_creature);
130    }
131}
132
133inline void MaNGOS::DynamicObjectUpdater::VisitHelper(Unit* target)
134{
135    if(!target->isAlive() || target->isInFlight() )
136        return;
137
138    if(target->GetTypeId()==TYPEID_UNIT && ((Creature*)target)->isTotem())
139        return;
140
141    if (!i_dynobject.IsWithinDistInMap(target, i_dynobject.GetRadius()))
142        return;
143
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;
147
148    // Evade target
149    if( target->GetTypeId()==TYPEID_UNIT && ((Creature*)target)->IsInEvadeMode() )
150        return;
151
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;
155
156    if( i_check->GetTypeId()==TYPEID_PLAYER )
157    {
158        if (i_check->IsFriendlyTo( target ))
159            return;
160    }
161    else
162    {
163        if (!i_check->IsHostileTo( target ))
164            return;
165    }
166
167    if (i_dynobject.IsAffecting(target))
168        return;
169
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);
179}
180
181template<>
182inline void
183MaNGOS::DynamicObjectUpdater::Visit(CreatureMapType  &m)
184{
185    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
186        VisitHelper(itr->getSource());
187}
188
189template<>
190inline void
191MaNGOS::DynamicObjectUpdater::Visit(PlayerMapType  &m)
192{
193    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
194        VisitHelper(itr->getSource());
195}
196
197// SEARCHERS & LIST SEARCHERS & WORKERS
198
199// WorldObject searchers & workers
200
201template<class Check>
202void MaNGOS::WorldObjectSearcher<Check>::Visit(GameObjectMapType &m)
203{
204    // already found
205    if(i_object)
206        return;
207
208    for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
209    {
210        if(i_check(itr->getSource()))
211        {
212            i_object = itr->getSource();
213            return;
214        }
215    }
216}
217
218template<class Check>
219void MaNGOS::WorldObjectSearcher<Check>::Visit(PlayerMapType &m)
220{
221    // already found
222    if(i_object)
223        return;
224
225    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
226    {
227        if(i_check(itr->getSource()))
228        {
229            i_object = itr->getSource();
230            return;
231        }
232    }
233}
234
235template<class Check>
236void MaNGOS::WorldObjectSearcher<Check>::Visit(CreatureMapType &m)
237{
238    // already found
239    if(i_object)
240        return;
241
242    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
243    {
244        if(i_check(itr->getSource()))
245        {
246            i_object = itr->getSource();
247            return;
248        }
249    }
250}
251
252template<class Check>
253void MaNGOS::WorldObjectSearcher<Check>::Visit(CorpseMapType &m)
254{
255    // already found
256    if(i_object)
257        return;
258
259    for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
260    {
261        if(i_check(itr->getSource()))
262        {
263            i_object = itr->getSource();
264            return;
265        }
266    }
267}
268
269template<class Check>
270void MaNGOS::WorldObjectSearcher<Check>::Visit(DynamicObjectMapType &m)
271{
272    // already found
273    if(i_object)
274        return;
275
276    for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
277    {
278        if(i_check(itr->getSource()))
279        {
280            i_object = itr->getSource();
281            return;
282        }
283    }
284}
285
286template<class Check>
287void MaNGOS::WorldObjectListSearcher<Check>::Visit(PlayerMapType &m)
288{
289    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
290        if(i_check(itr->getSource()))
291            i_objects.push_back(itr->getSource());
292}
293
294template<class Check>
295void MaNGOS::WorldObjectListSearcher<Check>::Visit(CreatureMapType &m)
296{
297    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
298        if(i_check(itr->getSource()))
299            i_objects.push_back(itr->getSource());
300}
301
302template<class Check>
303void MaNGOS::WorldObjectListSearcher<Check>::Visit(CorpseMapType &m)
304{
305    for(CorpseMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
306        if(i_check(itr->getSource()))
307            i_objects.push_back(itr->getSource());
308}
309
310template<class Check>
311void MaNGOS::WorldObjectListSearcher<Check>::Visit(GameObjectMapType &m)
312{
313    for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
314        if(i_check(itr->getSource()))
315            i_objects.push_back(itr->getSource());
316}
317
318template<class Check>
319void MaNGOS::WorldObjectListSearcher<Check>::Visit(DynamicObjectMapType &m)
320{
321    for(DynamicObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
322        if(i_check(itr->getSource()))
323            i_objects.push_back(itr->getSource());
324}
325
326// Gameobject searchers
327
328template<class Check>
329void MaNGOS::GameObjectSearcher<Check>::Visit(GameObjectMapType &m)
330{
331    // already found
332    if(i_object)
333        return;
334
335    for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
336    {
337        if(i_check(itr->getSource()))
338        {
339            i_object = itr->getSource();
340            return;
341        }
342    }
343}
344
345template<class Check>
346void MaNGOS::GameObjectLastSearcher<Check>::Visit(GameObjectMapType &m)
347{
348    for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
349    {
350        if(i_check(itr->getSource()))
351            i_object = itr->getSource();
352    }
353}
354
355template<class Check>
356void MaNGOS::GameObjectListSearcher<Check>::Visit(GameObjectMapType &m)
357{
358    for(GameObjectMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
359        if(i_check(itr->getSource()))
360            i_objects.push_back(itr->getSource());
361}
362
363// Unit searchers
364
365template<class Check>
366void MaNGOS::UnitSearcher<Check>::Visit(CreatureMapType &m)
367{
368    // already found
369    if(i_object)
370        return;
371
372    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
373    {
374        if(i_check(itr->getSource()))
375        {
376            i_object = itr->getSource();
377            return;
378        }
379    }
380}
381
382template<class Check>
383void MaNGOS::UnitSearcher<Check>::Visit(PlayerMapType &m)
384{
385    // already found
386    if(i_object)
387        return;
388
389    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
390    {
391        if(i_check(itr->getSource()))
392        {
393            i_object = itr->getSource();
394            return;
395        }
396    }
397}
398
399template<class Check>
400void MaNGOS::UnitLastSearcher<Check>::Visit(CreatureMapType &m)
401{
402    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
403    {
404        if(i_check(itr->getSource()))
405            i_object = itr->getSource();
406    }
407}
408
409template<class Check>
410void MaNGOS::UnitLastSearcher<Check>::Visit(PlayerMapType &m)
411{
412    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
413    {
414        if(i_check(itr->getSource()))
415            i_object = itr->getSource();
416    }
417}
418
419template<class Check>
420void MaNGOS::UnitListSearcher<Check>::Visit(PlayerMapType &m)
421{
422    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
423        if(i_check(itr->getSource()))
424            i_objects.push_back(itr->getSource());
425}
426
427template<class Check>
428void MaNGOS::UnitListSearcher<Check>::Visit(CreatureMapType &m)
429{
430    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
431        if(i_check(itr->getSource()))
432            i_objects.push_back(itr->getSource());
433}
434
435// Creature searchers
436
437template<class Check>
438void MaNGOS::CreatureSearcher<Check>::Visit(CreatureMapType &m)
439{
440    // already found
441    if(i_object)
442        return;
443
444    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
445    {
446        if(i_check(itr->getSource()))
447        {
448            i_object = itr->getSource();
449            return;
450        }
451    }
452}
453
454template<class Check>
455void MaNGOS::CreatureLastSearcher<Check>::Visit(CreatureMapType &m)
456{
457    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
458    {
459        if(i_check(itr->getSource()))
460            i_object = itr->getSource();
461    }
462}
463
464template<class Check>
465void MaNGOS::CreatureListSearcher<Check>::Visit(CreatureMapType &m)
466{
467    for(CreatureMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
468        if(i_check(itr->getSource()))
469            i_objects.push_back(itr->getSource());
470}
471
472template<class Check>
473void MaNGOS::PlayerSearcher<Check>::Visit(PlayerMapType &m)
474{
475    // already found
476    if(i_object)
477        return;
478
479    for(PlayerMapType::iterator itr=m.begin(); itr != m.end(); ++itr)
480    {
481        if(i_check(itr->getSource()))
482        {
483            i_object = itr->getSource();
484            return;
485        }
486    }
487}
488
489#endif                                                      // MANGOS_GRIDNOTIFIERSIMPL_H
Note: See TracBrowser for help on using the browser.