[6982] Implemented gmlevel-based command security
[getmangos.git] / src / game / TotemAI.cpp
blob572a8b04d21d4033c191a9abdb41e3662c0ab529
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 #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"
28 #include "GridNotifiers.h"
29 #include "GridNotifiersImpl.h"
30 #include "CellImpl.h"
32 int
33 TotemAI::Permissible(const Creature *creature)
35 if( creature->isTotem() )
36 return PERMIT_BASE_PROACTIVE;
38 return PERMIT_BASE_NO;
41 TotemAI::TotemAI(Creature &c) : i_totem(static_cast<Totem&>(c)), i_victimGuid(0)
45 void
46 TotemAI::MoveInLineOfSight(Unit *)
50 void TotemAI::EnterEvadeMode()
52 i_totem.CombatStop();
55 void
56 TotemAI::UpdateAI(const uint32 /*diff*/)
58 if (i_totem.GetTotemType() != TOTEM_ACTIVE)
59 return;
61 if (!i_totem.isAlive() || i_totem.IsNonMeleeSpellCasted(false))
62 return;
64 // Search spell
65 SpellEntry const *spellInfo = sSpellStore.LookupEntry(i_totem.GetSpell());
66 if (!spellInfo)
67 return;
69 // Get spell rangy
70 SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex);
71 float max_range = GetSpellMaxRange(srange);
73 // SPELLMOD_RANGE not applied in this place just because not existence range mods for attacking totems
75 // pointer to appropriate target if found any
76 Unit* victim = i_victimGuid ? ObjectAccessor::GetUnit(i_totem, i_victimGuid) : NULL;
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) )
83 CellPair p(MaNGOS::ComputeCellPair(i_totem.GetPositionX(),i_totem.GetPositionY()));
84 Cell cell(p);
85 cell.data.Part.reserved = ALL_DISTRICT;
87 victim = NULL;
89 MaNGOS::NearestAttackableUnitInObjectRangeCheck u_check(&i_totem, &i_totem, max_range);
90 MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck> checker(victim, u_check);
92 TypeContainerVisitor<MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck>, GridTypeMapContainer > grid_object_checker(checker);
93 TypeContainerVisitor<MaNGOS::UnitLastSearcher<MaNGOS::NearestAttackableUnitInObjectRangeCheck>, WorldTypeMapContainer > world_object_checker(checker);
95 CellLock<GridReadGuard> cell_lock(cell, p);
96 cell_lock->Visit(cell_lock, grid_object_checker, *i_totem.GetMap());
97 cell_lock->Visit(cell_lock, world_object_checker, *i_totem.GetMap());
100 // If have target
101 if (victim)
103 // remember
104 i_victimGuid = victim->GetGUID();
106 // attack
107 i_totem.SetInFront(victim); // client change orientation by self
108 i_totem.CastSpell(victim, i_totem.GetSpell(), false);
110 else
111 i_victimGuid = 0;
114 bool
115 TotemAI::IsVisible(Unit *) const
117 return false;
120 void
121 TotemAI::AttackStart(Unit *)