Updated Copyright year to 2013
[getmangos.git] / src / game / AggressorAI.cpp
bloba0406a8fe5abeaa7c83e5a8ca157ed97379cee2b
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 "AggressorAI.h"
20 #include "Errors.h"
21 #include "Creature.h"
22 #include "SharedDefines.h"
23 #include "VMapFactory.h"
24 #include "World.h"
25 #include "DBCStores.h"
26 #include "Map.h"
28 #include <list>
30 int
31 AggressorAI::Permissible(const Creature* creature)
33 // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
34 if (!creature->IsCivilian() && !creature->IsNeutralToAll())
35 return PERMIT_BASE_PROACTIVE;
37 return PERMIT_BASE_NO;
40 AggressorAI::AggressorAI(Creature* c) : CreatureAI(c), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
44 void
45 AggressorAI::MoveInLineOfSight(Unit* u)
47 // Ignore Z for flying creatures
48 if (!m_creature->CanFly() && m_creature->GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE)
49 return;
51 if (m_creature->CanInitiateAttack() && u->isTargetableForAttack() &&
52 m_creature->IsHostileTo(u) && u->isInAccessablePlaceFor(m_creature))
54 float attackRadius = m_creature->GetAttackDistance(u);
55 if (m_creature->IsWithinDistInMap(u, attackRadius) && m_creature->IsWithinLOSInMap(u))
57 if (!m_creature->getVictim())
59 AttackStart(u);
60 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
62 else if (sMapStore.LookupEntry(m_creature->GetMapId())->IsDungeon())
64 m_creature->AddThreat(u);
65 u->SetInCombatWith(m_creature);
71 void AggressorAI::EnterEvadeMode()
73 if (!m_creature->isAlive())
75 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, he is dead [guid=%u]", m_creature->GetGUIDLow());
76 i_victimGuid.Clear();
77 m_creature->CombatStop(true);
78 m_creature->DeleteThreatList();
79 return;
82 Unit* victim = m_creature->GetMap()->GetUnit(i_victimGuid);
84 if (!victim)
86 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, no victim [guid=%u]", m_creature->GetGUIDLow());
88 else if (!victim->isAlive())
90 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is dead [guid=%u]", m_creature->GetGUIDLow());
92 else if (victim->HasStealthAura())
94 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is in stealth [guid=%u]", m_creature->GetGUIDLow());
96 else if (victim->IsTaxiFlying())
98 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim is in flight [guid=%u]", m_creature->GetGUIDLow());
100 else
102 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature stopped attacking, victim out run him [guid=%u]", m_creature->GetGUIDLow());
103 // i_state = STATE_LOOK_AT_VICTIM;
104 // i_tracker.Reset(TIME_INTERVAL_LOOK);
107 if (!m_creature->isCharmed())
109 m_creature->RemoveAllAuras();
111 // Remove ChaseMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
112 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == CHASE_MOTION_TYPE)
113 m_creature->GetMotionMaster()->MoveTargetedHome();
116 m_creature->DeleteThreatList();
117 i_victimGuid.Clear();
118 m_creature->CombatStop(true);
119 m_creature->SetLootRecipient(NULL);
122 void
123 AggressorAI::UpdateAI(const uint32 /*diff*/)
125 // update i_victimGuid if m_creature->getVictim() !=0 and changed
126 if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
127 return;
129 i_victimGuid = m_creature->getVictim()->GetObjectGuid();
131 DoMeleeAttackIfReady();
134 bool
135 AggressorAI::IsVisible(Unit* pl) const
137 return m_creature->IsWithinDist(pl, sWorld.getConfig(CONFIG_FLOAT_SIGHT_MONSTER))
138 && pl->isVisibleForOrDetect(m_creature, m_creature, true);
141 void
142 AggressorAI::AttackStart(Unit* u)
144 if (!u)
145 return;
147 if (m_creature->Attack(u, true))
149 i_victimGuid = u->GetObjectGuid();
151 m_creature->AddThreat(u);
152 m_creature->SetInCombatWith(u);
153 u->SetInCombatWith(m_creature);
155 m_creature->GetMotionMaster()->MoveChase(u);