[8483] Implement glyph 43361.
[getmangos.git] / src / game / AggressorAI.cpp
blob4d1b9c427a7339c23beabaad11a355365c5a57cf
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 "AggressorAI.h"
20 #include "Errors.h"
21 #include "Creature.h"
22 #include "ObjectAccessor.h"
23 #include "VMapFactory.h"
24 #include "World.h"
26 #include <list>
28 int
29 AggressorAI::Permissible(const Creature *creature)
31 // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
32 if( !creature->isCivilian() && !creature->IsNeutralToAll() )
33 return PERMIT_BASE_PROACTIVE;
35 return PERMIT_BASE_NO;
38 AggressorAI::AggressorAI(Creature *c) : CreatureAI(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
42 void
43 AggressorAI::MoveInLineOfSight(Unit *u)
45 // Ignore Z for flying creatures
46 if( !m_creature->canFly() && m_creature->GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
47 return;
49 if( !m_creature->hasUnitState(UNIT_STAT_STUNNED) && u->isTargetableForAttack() &&
50 ( m_creature->IsHostileTo( u ) /*|| u->getVictim() && m_creature->IsFriendlyTo( u->getVictim() )*/ ) &&
51 u->isInAccessablePlaceFor(m_creature) )
53 float attackRadius = m_creature->GetAttackDistance(u);
54 if(m_creature->IsWithinDistInMap(u, attackRadius) && m_creature->IsWithinLOSInMap(u) )
56 if(!m_creature->getVictim())
58 AttackStart(u);
59 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
61 else if(sMapStore.LookupEntry(m_creature->GetMapId())->IsDungeon())
63 m_creature->AddThreat(u, 0.0f);
64 u->SetInCombatWith(m_creature);
70 void AggressorAI::EnterEvadeMode()
72 if( !m_creature->isAlive() )
74 DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", m_creature->GetGUIDLow());
75 i_victimGuid = 0;
76 m_creature->CombatStop(true);
77 m_creature->DeleteThreatList();
78 return;
81 Unit* victim = ObjectAccessor::GetUnit(*m_creature, i_victimGuid );
83 if( !victim )
85 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", m_creature->GetGUIDLow());
87 else if( !victim->isAlive() )
89 DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", m_creature->GetGUIDLow());
91 else if( victim->HasStealthAura() )
93 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", m_creature->GetGUIDLow());
95 else if( victim->isInFlight() )
97 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", m_creature->GetGUIDLow());
99 else
101 DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", m_creature->GetGUIDLow());
102 //i_state = STATE_LOOK_AT_VICTIM;
103 //i_tracker.Reset(TIME_INTERVAL_LOOK);
106 if(!m_creature->isCharmed())
108 m_creature->RemoveAllAuras();
110 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
111 if( m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
112 m_creature->GetMotionMaster()->MoveTargetedHome();
115 m_creature->DeleteThreatList();
116 i_victimGuid = 0;
117 m_creature->CombatStop(true);
118 m_creature->SetLootRecipient(NULL);
121 void
122 AggressorAI::UpdateAI(const uint32 /*diff*/)
124 // update i_victimGuid if m_creature->getVictim() !=0 and changed
125 if(!m_creature->SelectHostilTarget() || !m_creature->getVictim())
126 return;
128 i_victimGuid = m_creature->getVictim()->GetGUID();
130 if( m_creature->isAttackReady() )
132 if( m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
134 m_creature->AttackerStateUpdate(m_creature->getVictim());
135 m_creature->resetAttackTimer();
140 bool
141 AggressorAI::IsVisible(Unit *pl) const
143 return m_creature->IsWithinDist(pl,sWorld.getConfig(CONFIG_SIGHT_MONSTER))
144 && pl->isVisibleForOrDetect(m_creature,m_creature,true);
147 void
148 AggressorAI::AttackStart(Unit *u)
150 if( !u )
151 return;
153 if(m_creature->Attack(u,true))
155 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", m_creature->GetName(), u->GetGUIDLow());
156 i_victimGuid = u->GetGUID();
158 m_creature->AddThreat(u, 0.0f);
159 m_creature->SetInCombatWith(u);
160 u->SetInCombatWith(m_creature);
162 m_creature->GetMotionMaster()->MoveChase(u);