root/trunk/src/game/AggressorAI.cpp @ 156

Revision 156, 4.9 kB (checked in by yumileroy, 17 years ago)

[svn] Use IsWithinCombatDist? for all AI. This should fix the bug that pet and guard does not attack target. (Note: some scriptedAI may need to update)

Original author: megamage
Date: 2008-11-03 14:11:09-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "AggressorAI.h"
22#include "Errors.h"
23#include "Creature.h"
24#include "Player.h"
25#include "ObjectAccessor.h"
26#include "VMapFactory.h"
27#include "World.h"
28
29#include <list>
30
31int
32AggressorAI::Permissible(const Creature *creature)
33{
34    // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
35    if( !creature->isCivilian() && !creature->IsNeutralToAll() )
36        return PERMIT_BASE_PROACTIVE;
37
38    return PERMIT_BASE_NO;
39}
40
41AggressorAI::AggressorAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
42{
43}
44
45void
46AggressorAI::MoveInLineOfSight(Unit *u)
47{
48    // Ignore Z for flying creatures
49    if( !i_creature.canFly() && i_creature.GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
50        return;
51   
52    if( !i_creature.getVictim() && !i_creature.hasUnitState(UNIT_STAT_STUNNED) && u->isTargetableForAttack() &&
53        ( i_creature.IsHostileTo( u ) /*|| u->getVictim() && i_creature.IsFriendlyTo( u->getVictim() )*/ ) &&
54        u->isInAccessablePlaceFor(&i_creature) )
55    {
56        float attackRadius = i_creature.GetAttackDistance(u);
57        if(i_creature.IsWithinDistInMap(u, attackRadius) && i_creature.IsWithinLOSInMap(u) )
58        {
59            AttackStart(u);
60            u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
61        }
62    }
63}
64
65void AggressorAI::EnterEvadeMode()
66{
67    if( !i_creature.isAlive() )
68    {
69        DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
70        i_victimGuid = 0;
71        i_creature.CombatStop();
72        i_creature.DeleteThreatList();
73        return;
74    }
75
76    Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
77
78    if( !victim  )
79    {
80        DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
81    }
82    else if( !victim->isAlive() )
83    {
84        DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
85    }
86    else if( victim->HasStealthAura() )
87    {
88        DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
89    }
90    else if( victim->isInFlight() )
91    {
92        DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
93    }
94    else
95    {
96        DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
97        //i_state = STATE_LOOK_AT_VICTIM;
98        //i_tracker.Reset(TIME_INTERVAL_LOOK);
99    }
100
101    if(!i_creature.isCharmed())
102    {
103        i_creature.RemoveAllAuras();
104
105        // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
106        if( i_creature.GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
107            i_creature.GetMotionMaster()->MoveTargetedHome();
108    }
109
110    i_creature.DeleteThreatList();
111    i_victimGuid = 0;
112    i_creature.CombatStop();
113    i_creature.SetLootRecipient(NULL);
114}
115
116void
117AggressorAI::UpdateAI(const uint32 /*diff*/)
118{
119    // update i_victimGuid if i_creature.getVictim() !=0 and changed
120    if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
121        return;
122
123    i_victimGuid = i_creature.getVictim()->GetGUID();
124
125    if( i_creature.isAttackReady() )
126    {
127        if( i_creature.IsWithinCombatDist(i_creature.getVictim(), ATTACK_DISTANCE))
128        {
129            i_creature.AttackerStateUpdate(i_creature.getVictim());
130            i_creature.resetAttackTimer();
131        }
132    }
133}
134
135bool
136AggressorAI::IsVisible(Unit *pl) const
137{
138    return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
139        && pl->isVisibleForOrDetect(&i_creature,true);
140}
141
142void
143AggressorAI::AttackStart(Unit *u)
144{
145    if( !u )
146        return;
147
148    if(i_creature.Attack(u,true))
149    {
150        i_creature.SetInCombatWith(u);
151        u->SetInCombatWith(&i_creature);
152
153        i_creature.AddThreat(u, 0.0f);
154        //    DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
155        i_victimGuid = u->GetGUID();
156
157        i_creature.GetMotionMaster()->MoveChase(u);
158    }
159}
Note: See TracBrowser for help on using the browser.