[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / GuardAI.cpp
blobe87f5812be1ca0a2915d1161f03ae8e977dae8a3
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 #include "GuardAI.h"
20 #include "Errors.h"
21 #include "Creature.h"
22 #include "Player.h"
23 #include "ObjectAccessor.h"
24 #include "World.h"
26 int GuardAI::Permissible(const Creature *creature)
28 if( creature->isGuard())
29 return PERMIT_BASE_SPECIAL;
31 return PERMIT_BASE_NO;
34 GuardAI::GuardAI(Creature *c) : CreatureAI(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
38 void GuardAI::MoveInLineOfSight(Unit *u)
40 // Ignore Z for flying creatures
41 if (!m_creature->canFly() && m_creature->GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE)
42 return;
44 if (!m_creature->getVictim() && u->isTargetableForAttack() &&
45 ( u->IsHostileToPlayers() || m_creature->IsHostileTo(u) /*|| u->getVictim() && m_creature->IsFriendlyTo(u->getVictim())*/ ) &&
46 u->isInAccessablePlaceFor(m_creature))
48 float attackRadius = m_creature->GetAttackDistance(u);
49 if (m_creature->IsWithinDistInMap(u,attackRadius))
51 //Need add code to let guard support player
52 AttackStart(u);
53 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
58 void GuardAI::EnterEvadeMode()
60 if (!m_creature->isAlive())
62 DEBUG_LOG("Creature stopped attacking because he's dead [guid=%u]", m_creature->GetGUIDLow());
63 m_creature->StopMoving();
64 m_creature->GetMotionMaster()->MoveIdle();
66 i_state = STATE_NORMAL;
68 i_victimGuid = 0;
69 m_creature->CombatStop(true);
70 m_creature->DeleteThreatList();
71 return;
74 Unit* victim = ObjectAccessor::GetUnit(*m_creature, i_victimGuid );
76 if (!victim)
78 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", m_creature->GetGUIDLow());
80 else if (!victim->isAlive())
82 DEBUG_LOG("Creature stopped attacking because victim is dead [guid=%u]", m_creature->GetGUIDLow());
84 else if (victim->HasStealthAura())
86 DEBUG_LOG("Creature stopped attacking because victim is using stealth [guid=%u]", m_creature->GetGUIDLow());
88 else if (victim->isInFlight())
90 DEBUG_LOG("Creature stopped attacking because victim is flying away [guid=%u]", m_creature->GetGUIDLow());
92 else
94 DEBUG_LOG("Creature stopped attacking because victim outran him [guid=%u]", m_creature->GetGUIDLow());
97 m_creature->RemoveAllAuras();
98 m_creature->DeleteThreatList();
99 i_victimGuid = 0;
100 m_creature->CombatStop(true);
101 i_state = STATE_NORMAL;
103 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
104 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
105 m_creature->GetMotionMaster()->MoveTargetedHome();
108 void GuardAI::UpdateAI(const uint32 /*diff*/)
110 // update i_victimGuid if i_creature.getVictim() !=0 and changed
111 if (!m_creature->SelectHostilTarget() || !m_creature->getVictim())
112 return;
114 i_victimGuid = m_creature->getVictim()->GetGUID();
116 if (m_creature->isAttackReady())
118 if (m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
120 m_creature->AttackerStateUpdate(m_creature->getVictim());
121 m_creature->resetAttackTimer();
126 bool GuardAI::IsVisible(Unit *pl) const
128 return m_creature->IsWithinDist(pl,sWorld.getConfig(CONFIG_SIGHT_GUARDER))
129 && pl->isVisibleForOrDetect(m_creature,m_creature,true);
132 void GuardAI::AttackStart(Unit *u)
134 if( !u )
135 return;
137 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
138 if(m_creature->Attack(u,true))
140 i_victimGuid = u->GetGUID();
141 m_creature->AddThreat(u, 0.0f);
142 m_creature->SetInCombatWith(u);
143 u->SetInCombatWith(m_creature);
145 m_creature->GetMotionMaster()->MoveChase(u);
149 void GuardAI::JustDied(Unit *killer)
151 if(Player* pkiller = killer->GetCharmerOrOwnerPlayerOrPlayerItself())
152 m_creature->SendZoneUnderAttackMessage(pkiller);