[6870] Not output error message at loading empty `db_script_string` table.
[getmangos.git] / src / game / AggressorAI.cpp
blob637410c1fd2b0d0e5b535d97ee59468696e66beb
1 /*
2 * Copyright (C) 2005-2008 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 "Player.h"
23 #include "ObjectAccessor.h"
24 #include "VMapFactory.h"
25 #include "World.h"
27 #include <list>
29 int
30 AggressorAI::Permissible(const Creature *creature)
32 // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
33 if( !creature->isCivilian() && !creature->IsNeutralToAll() )
34 return PERMIT_BASE_PROACTIVE;
36 return PERMIT_BASE_NO;
39 AggressorAI::AggressorAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
43 void
44 AggressorAI::MoveInLineOfSight(Unit *u)
46 // Ignore Z for flying creatures
47 if( !i_creature.canFly() && i_creature.GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
48 return;
50 if( !i_creature.getVictim() && !i_creature.hasUnitState(UNIT_STAT_STUNNED) && u->isTargetableForAttack() &&
51 ( i_creature.IsHostileTo( u ) /*|| u->getVictim() && i_creature.IsFriendlyTo( u->getVictim() )*/ ) &&
52 u->isInAccessablePlaceFor(&i_creature) )
54 float attackRadius = i_creature.GetAttackDistance(u);
55 if(i_creature.IsWithinDistInMap(u, attackRadius) && i_creature.IsWithinLOSInMap(u) )
57 AttackStart(u);
58 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
63 void AggressorAI::EnterEvadeMode()
65 if( !i_creature.isAlive() )
67 DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
68 i_victimGuid = 0;
69 i_creature.CombatStop();
70 i_creature.DeleteThreatList();
71 return;
74 Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
76 if( !victim )
78 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
80 else if( !victim->isAlive() )
82 DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
84 else if( victim->HasStealthAura() )
86 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
88 else if( victim->isInFlight() )
90 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
92 else
94 DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
95 //i_state = STATE_LOOK_AT_VICTIM;
96 //i_tracker.Reset(TIME_INTERVAL_LOOK);
99 if(!i_creature.isCharmed())
101 i_creature.RemoveAllAuras();
103 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
104 if( i_creature.GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
105 i_creature.GetMotionMaster()->MoveTargetedHome();
108 i_creature.DeleteThreatList();
109 i_victimGuid = 0;
110 i_creature.CombatStop();
111 i_creature.SetLootRecipient(NULL);
114 void
115 AggressorAI::UpdateAI(const uint32 /*diff*/)
117 // update i_victimGuid if i_creature.getVictim() !=0 and changed
118 if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
119 return;
121 i_victimGuid = i_creature.getVictim()->GetGUID();
123 if( i_creature.isAttackReady() )
125 if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
127 i_creature.AttackerStateUpdate(i_creature.getVictim());
128 i_creature.resetAttackTimer();
133 bool
134 AggressorAI::IsVisible(Unit *pl) const
136 return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
137 && pl->isVisibleForOrDetect(&i_creature,true);
140 void
141 AggressorAI::AttackStart(Unit *u)
143 if( !u )
144 return;
146 if(i_creature.Attack(u,true))
148 i_creature.SetInCombatWith(u);
149 u->SetInCombatWith(&i_creature);
151 i_creature.AddThreat(u, 0.0f);
152 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
153 i_victimGuid = u->GetGUID();
155 i_creature.GetMotionMaster()->MoveChase(u);