Updated Copyright year to 2013
[getmangos.git] / src / game / GuardAI.cpp
blob62bbc7808c513330372d0bf6f432d228472c15e3
1 /*
2 * Copyright (C) 2005-2013 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 "World.h"
25 int GuardAI::Permissible(const Creature* creature)
27 if (creature->IsGuard())
28 return PERMIT_BASE_SPECIAL;
30 return PERMIT_BASE_NO;
33 GuardAI::GuardAI(Creature* c) : CreatureAI(c), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
37 void GuardAI::MoveInLineOfSight(Unit* u)
39 // Ignore Z for flying creatures
40 if (!m_creature->CanFly() && m_creature->GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE)
41 return;
43 if (!m_creature->getVictim() && u->isTargetableForAttack() &&
44 (u->IsHostileToPlayers() || m_creature->IsHostileTo(u) /*|| u->getVictim() && m_creature->IsFriendlyTo(u->getVictim())*/) &&
45 u->isInAccessablePlaceFor(m_creature))
47 float attackRadius = m_creature->GetAttackDistance(u);
48 if (m_creature->IsWithinDistInMap(u, attackRadius))
50 // Need add code to let guard support player
51 AttackStart(u);
52 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
57 void GuardAI::EnterEvadeMode()
59 if (!m_creature->isAlive())
61 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking because he's dead [guid=%u]", m_creature->GetGUIDLow());
62 m_creature->StopMoving();
63 m_creature->GetMotionMaster()->MoveIdle();
65 i_state = STATE_NORMAL;
67 i_victimGuid.Clear();
68 m_creature->CombatStop(true);
69 m_creature->DeleteThreatList();
70 return;
73 Unit* victim = m_creature->GetMap()->GetUnit(i_victimGuid);
75 if (!victim)
77 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, no victim [guid=%u]", m_creature->GetGUIDLow());
79 else if (!victim->isAlive())
81 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is dead [guid=%u]", m_creature->GetGUIDLow());
83 else if (victim->HasStealthAura())
85 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is in stealth [guid=%u]", m_creature->GetGUIDLow());
87 else if (victim->IsTaxiFlying())
89 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is in flight [guid=%u]", m_creature->GetGUIDLow());
91 else
93 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim out run him [guid=%u]", m_creature->GetGUIDLow());
96 m_creature->RemoveAllAuras();
97 m_creature->DeleteThreatList();
98 i_victimGuid.Clear();
99 m_creature->CombatStop(true);
100 i_state = STATE_NORMAL;
102 // Remove ChaseMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
103 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == CHASE_MOTION_TYPE)
104 m_creature->GetMotionMaster()->MoveTargetedHome();
107 void GuardAI::UpdateAI(const uint32 /*diff*/)
109 // update i_victimGuid if i_creature.getVictim() !=0 and changed
110 if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
111 return;
113 i_victimGuid = m_creature->getVictim()->GetObjectGuid();
115 DoMeleeAttackIfReady();
118 bool GuardAI::IsVisible(Unit* pl) const
120 return m_creature->IsWithinDist(pl, sWorld.getConfig(CONFIG_FLOAT_SIGHT_GUARDER))
121 && pl->isVisibleForOrDetect(m_creature, m_creature, true);
124 void GuardAI::AttackStart(Unit* u)
126 if (!u)
127 return;
129 if (m_creature->Attack(u, true))
131 i_victimGuid = u->GetObjectGuid();
132 m_creature->AddThreat(u);
133 m_creature->SetInCombatWith(u);
134 u->SetInCombatWith(m_creature);
136 m_creature->GetMotionMaster()->MoveChase(u);
140 void GuardAI::JustDied(Unit* killer)
142 if (Player* pkiller = killer->GetCharmerOrOwnerPlayerOrPlayerItself())
143 m_creature->SendZoneUnderAttackMessage(pkiller);