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

Revision 2, 4.8 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
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"
26
27#include <list>
28
29int
30AggressorAI::Permissible(const Creature *creature)
31{
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;
35
36    return PERMIT_BASE_NO;
37}
38
39AggressorAI::AggressorAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
40{
41}
42
43void
44AggressorAI::MoveInLineOfSight(Unit *u)
45{
46    if( i_creature.GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
47        return;
48    if( !i_creature.getVictim() && !i_creature.hasUnitState(UNIT_STAT_STUNDED) && u->isTargetableForAttack() &&
49        ( i_creature.IsHostileTo( u ) /*|| u->getVictim() && i_creature.IsFriendlyTo( u->getVictim() )*/ ) &&
50        u->isInAccessablePlaceFor(&i_creature) )
51    {
52        float attackRadius = i_creature.GetAttackDistance(u);
53        if(i_creature.IsWithinDistInMap(u, attackRadius) && i_creature.IsWithinLOSInMap(u) )
54        {
55            AttackStart(u);
56            u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
57        }
58    }
59}
60
61void AggressorAI::EnterEvadeMode()
62{
63    if( !i_creature.isAlive() )
64    {
65        DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
66        i_victimGuid = 0;
67        i_creature.CombatStop();
68        i_creature.DeleteThreatList();
69        return;
70    }
71
72    Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
73
74    if( !victim  )
75    {
76        DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
77    }
78    else if( !victim->isAlive() )
79    {
80        DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
81    }
82    else if( victim->HasStealthAura() )
83    {
84        DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
85    }
86    else if( victim->isInFlight() )
87    {
88        DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
89    }
90    else
91    {
92        DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
93        //i_state = STATE_LOOK_AT_VICTIM;
94        //i_tracker.Reset(TIME_INTERVAL_LOOK);
95    }
96
97    if(!i_creature.isCharmed())
98    {
99        i_creature.RemoveAllAuras();
100
101        // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
102        if( i_creature.GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
103            i_creature.GetMotionMaster()->MoveTargetedHome();
104    }
105
106    i_creature.DeleteThreatList();
107    i_victimGuid = 0;
108    i_creature.CombatStop();
109    i_creature.SetLootRecipient(NULL);
110}
111
112void
113AggressorAI::UpdateAI(const uint32 /*diff*/)
114{
115    // update i_victimGuid if i_creature.getVictim() !=0 and changed
116    if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
117        return;
118
119    i_victimGuid = i_creature.getVictim()->GetGUID();
120
121    if( i_creature.isAttackReady() )
122    {
123        if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
124        {
125            i_creature.AttackerStateUpdate(i_creature.getVictim());
126            i_creature.resetAttackTimer();
127        }
128    }
129}
130
131bool
132AggressorAI::IsVisible(Unit *pl) const
133{
134    return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
135        && pl->isVisibleForOrDetect(&i_creature,true);
136}
137
138void
139AggressorAI::AttackStart(Unit *u)
140{
141    if( !u )
142        return;
143
144    if(i_creature.Attack(u,true))
145    {
146        i_creature.SetInCombatWith(u);
147        u->SetInCombatWith(&i_creature);
148
149        i_creature.AddThreat(u, 0.0f);
150        //    DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
151        i_victimGuid = u->GetGUID();
152
153        i_creature.GetMotionMaster()->MoveChase(u);
154    }
155}
Note: See TracBrowser for help on using the browser.