[7607] Improvements in support some generic achievement classes
[AHbot.git] / src / game / AggressorAI.cpp
blobc792d0261ac98855e248fd7f8fc7c16cc23b4c5a
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) : i_creature(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( !i_creature.canFly() && i_creature.GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
47 return;
49 if( !i_creature.hasUnitState(UNIT_STAT_STUNNED) && u->isTargetableForAttack() &&
50 ( i_creature.IsHostileTo( u ) /*|| u->getVictim() && i_creature.IsFriendlyTo( u->getVictim() )*/ ) &&
51 u->isInAccessablePlaceFor(&i_creature) )
53 float attackRadius = i_creature.GetAttackDistance(u);
54 if(i_creature.IsWithinDistInMap(u, attackRadius) && i_creature.IsWithinLOSInMap(u) )
56 if(!i_creature.getVictim())
58 AttackStart(u);
59 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
61 else if(sMapStore.LookupEntry(i_creature.GetMapId())->IsDungeon())
63 u->SetInCombatWith(&i_creature);
64 i_creature.AddThreat(u, 0.0f);
70 void AggressorAI::EnterEvadeMode()
72 if( !i_creature.isAlive() )
74 DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
75 i_victimGuid = 0;
76 i_creature.CombatStop();
77 i_creature.DeleteThreatList();
78 return;
81 Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
83 if( !victim )
85 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
87 else if( !victim->isAlive() )
89 DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
91 else if( victim->HasStealthAura() )
93 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
95 else if( victim->isInFlight() )
97 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
99 else
101 DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
102 //i_state = STATE_LOOK_AT_VICTIM;
103 //i_tracker.Reset(TIME_INTERVAL_LOOK);
106 if(!i_creature.isCharmed())
108 i_creature.RemoveAllAuras();
110 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
111 if( i_creature.GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
112 i_creature.GetMotionMaster()->MoveTargetedHome();
115 i_creature.DeleteThreatList();
116 i_victimGuid = 0;
117 i_creature.CombatStop();
118 i_creature.SetLootRecipient(NULL);
121 void
122 AggressorAI::UpdateAI(const uint32 /*diff*/)
124 // update i_victimGuid if i_creature.getVictim() !=0 and changed
125 if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
126 return;
128 i_victimGuid = i_creature.getVictim()->GetGUID();
130 if( i_creature.isAttackReady() )
132 if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
134 i_creature.AttackerStateUpdate(i_creature.getVictim());
135 i_creature.resetAttackTimer();
140 bool
141 AggressorAI::IsVisible(Unit *pl) const
143 return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
144 && pl->isVisibleForOrDetect(&i_creature,true);
147 void
148 AggressorAI::AttackStart(Unit *u)
150 if( !u )
151 return;
153 if(i_creature.Attack(u,true))
155 i_creature.SetInCombatWith(u);
156 u->SetInCombatWith(&i_creature);
158 i_creature.AddThreat(u, 0.0f);
159 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
160 i_victimGuid = u->GetGUID();
162 i_creature.GetMotionMaster()->MoveChase(u);