root/trunk/src/game/GuardAI.cpp @ 37

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

[svn] * svn:eol-style native set on all files that need it

Original author: Neo2003
Date: 2008-10-11 14:16:25-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 "GuardAI.h"
20#include "Errors.h"
21#include "Creature.h"
22#include "Player.h"
23#include "ObjectAccessor.h"
24#include "World.h"
25
26int GuardAI::Permissible(const Creature *creature)
27{
28    if( creature->isGuard())
29        return PERMIT_BASE_SPECIAL;
30
31    return PERMIT_BASE_NO;
32}
33
34GuardAI::GuardAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
35{
36}
37
38void GuardAI::MoveInLineOfSight(Unit *u)
39{
40    // Ignore Z for flying creatures
41    if ( !i_creature.canFly() && i_creature.GetDistanceZ(u) > CREATURE_Z_ATTACK_RANGE )
42        return;
43
44    if( !i_creature.getVictim() && u->isTargetableForAttack() &&
45        ( u->IsHostileToPlayers() || i_creature.IsHostileTo(u) /*|| u->getVictim() && i_creature.IsFriendlyTo(u->getVictim())*/ ) &&
46        u->isInAccessablePlaceFor(&i_creature))
47    {
48        float attackRadius = i_creature.GetAttackDistance(u);
49        if(i_creature.IsWithinDistInMap(u,attackRadius))
50        {
51            //Need add code to let guard support player
52            AttackStart(u);
53            u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
54        }
55    }
56}
57
58void GuardAI::EnterEvadeMode()
59{
60    if( !i_creature.isAlive() )
61    {
62        DEBUG_LOG("Creature stopped attacking because he's dead [guid=%u]", i_creature.GetGUIDLow());
63        i_creature.StopMoving();
64        i_creature.GetMotionMaster()->MoveIdle();
65
66        i_state = STATE_NORMAL;
67
68        i_victimGuid = 0;
69        i_creature.CombatStop();
70        i_creature.DeleteThreatList();
71        return;
72    }
73
74    Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
75
76    if( !victim  )
77    {
78        DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
79    }
80    else if( !victim ->isAlive() )
81    {
82        DEBUG_LOG("Creature stopped attacking because victim is dead [guid=%u]", i_creature.GetGUIDLow());
83    }
84    else if( victim ->HasStealthAura() )
85    {
86        DEBUG_LOG("Creature stopped attacking because victim is using stealth [guid=%u]", i_creature.GetGUIDLow());
87    }
88    else if( victim ->isInFlight() )
89    {
90        DEBUG_LOG("Creature stopped attacking because victim is flying away [guid=%u]", i_creature.GetGUIDLow());
91    }
92    else
93    {
94        DEBUG_LOG("Creature stopped attacking because victim outran him [guid=%u]", i_creature.GetGUIDLow());
95    }
96
97    i_creature.RemoveAllAuras();
98    i_creature.DeleteThreatList();
99    i_victimGuid = 0;
100    i_creature.CombatStop();
101    i_state = STATE_NORMAL;
102
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();
106}
107
108void GuardAI::UpdateAI(const uint32 /*diff*/)
109{
110    // update i_victimGuid if i_creature.getVictim() !=0 and changed
111    if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
112        return;
113
114    i_victimGuid = i_creature.getVictim()->GetGUID();
115
116    if( i_creature.isAttackReady() )
117    {
118        if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
119        {
120            i_creature.AttackerStateUpdate(i_creature.getVictim());
121            i_creature.resetAttackTimer();
122        }
123    }
124}
125
126bool GuardAI::IsVisible(Unit *pl) const
127{
128    return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_GUARDER)
129        && pl->isVisibleForOrDetect(&i_creature,true);
130}
131
132void GuardAI::AttackStart(Unit *u)
133{
134    if( !u )
135        return;
136
137    //    DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
138    if(i_creature.Attack(u,true))
139    {
140        i_creature.SetInCombatWith(u);
141        u->SetInCombatWith(&i_creature);
142
143        i_creature.AddThreat(u, 0.0f);
144        i_victimGuid = u->GetGUID();
145        i_creature.GetMotionMaster()->MoveChase(u);
146    }
147}
148
149void GuardAI::JustDied(Unit *killer)
150{
151    if(Player* pkiller = killer->GetCharmerOrOwnerPlayerOrPlayerItself())
152        i_creature.SendZoneUnderAttackMessage(pkiller);
153}
154
Note: See TracBrowser for help on using the browser.