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 | #include "TotemAI.h" |
---|
20 | #include "Totem.h" |
---|
21 | #include "Creature.h" |
---|
22 | #include "Player.h" |
---|
23 | #include "Database/DBCStores.h" |
---|
24 | #include "MapManager.h" |
---|
25 | #include "ObjectAccessor.h" |
---|
26 | #include "SpellMgr.h" |
---|
27 | |
---|
28 | #include "GridNotifiers.h" |
---|
29 | #include "GridNotifiersImpl.h" |
---|
30 | #include "CellImpl.h" |
---|
31 | |
---|
32 | int |
---|
33 | TotemAI::Permissible(const Creature *creature) |
---|
34 | { |
---|
35 | if( creature->isTotem() ) |
---|
36 | return PERMIT_BASE_PROACTIVE; |
---|
37 | |
---|
38 | return PERMIT_BASE_NO; |
---|
39 | } |
---|
40 | |
---|
41 | TotemAI::TotemAI(Creature &c) : i_totem(static_cast<Totem&>(c)), i_victimGuid(0) |
---|
42 | { |
---|
43 | } |
---|
44 | |
---|
45 | void |
---|
46 | TotemAI::MoveInLineOfSight(Unit *) |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | void TotemAI::EnterEvadeMode() |
---|
51 | { |
---|
52 | i_totem.CombatStop(); |
---|
53 | } |
---|
54 | |
---|
55 | void |
---|
56 | TotemAI::UpdateAI(const uint32 /*diff*/) |
---|
57 | { |
---|
58 | if (i_totem.GetTotemType() != TOTEM_ACTIVE) |
---|
59 | return; |
---|
60 | |
---|
61 | if (!i_totem.isAlive() || i_totem.IsNonMeleeSpellCasted(false)) |
---|
62 | return; |
---|
63 | |
---|
64 | // Search spell |
---|
65 | SpellEntry const *spellInfo = sSpellStore.LookupEntry(i_totem.GetSpell()); |
---|
66 | if (!spellInfo) |
---|
67 | return; |
---|
68 | |
---|
69 | // Get spell rangy |
---|
70 | SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex); |
---|
71 | float max_range = GetSpellMaxRange(srange); |
---|
72 | |
---|
73 | // SPELLMOD_RANGE not applied in this place just because not existence range mods for attacking totems |
---|
74 | |
---|
75 | // pointer to appropriate target if found any |
---|
76 | Unit* victim = i_victimGuid ? ObjectAccessor::GetUnit(i_totem, i_victimGuid) : NULL; |
---|
77 | |
---|
78 | // Search victim if no, not attackable, or out of range, or friendly (possible in case duel end) |
---|
79 | if( !victim || |
---|
80 | !victim->isTargetableForAttack() || !i_totem.IsWithinDistInMap(victim, max_range) || |
---|
81 | i_totem.IsFriendlyTo(victim) || !victim->isVisibleForOrDetect(&i_totem,false) ) |
---|
82 | { |
---|
83 | CellPair p(MaNGOS::ComputeCellPair(i_totem.GetPositionX(),i_totem.GetPositionY())); |
---|
84 | Cell cell(p); |
---|
85 | cell.data.Part.reserved = ALL_DISTRICT; |
---|
86 | |
---|
87 | victim = NULL; |
---|
88 | |
---|
89 | MaNGOS::NearestAttackableUnitInObjectRangeCheck u_check(&i_totem, &i_totem, max_range); |
---|
90 | MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck> checker(victim, u_check); |
---|
91 | |
---|
92 | TypeContainerVisitor<MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker); |
---|
93 | TypeContainerVisitor<MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker); |
---|
94 | |
---|
95 | CellLock<GridReadGuard> cell_lock(cell, p); |
---|
96 | cell_lock->Visit(cell_lock, grid_object_checker, *MapManager::Instance().GetMap(i_totem.GetMapId(), &i_totem)); |
---|
97 | cell_lock->Visit(cell_lock, world_object_checker, *MapManager::Instance().GetMap(i_totem.GetMapId(), &i_totem)); |
---|
98 | } |
---|
99 | |
---|
100 | // If have target |
---|
101 | if (victim) |
---|
102 | { |
---|
103 | // remember |
---|
104 | i_victimGuid = victim->GetGUID(); |
---|
105 | |
---|
106 | // attack |
---|
107 | i_totem.SetInFront(victim); // client change orientation by self |
---|
108 | i_totem.CastSpell(victim, i_totem.GetSpell(), false); |
---|
109 | } |
---|
110 | else |
---|
111 | i_victimGuid = 0; |
---|
112 | } |
---|
113 | |
---|
114 | bool |
---|
115 | TotemAI::IsVisible(Unit *) const |
---|
116 | { |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | void |
---|
121 | TotemAI::AttackStart(Unit *) |
---|
122 | { |
---|
123 | } |
---|