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

Revision 2, 4.7 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 "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    if( !i_creature.getVictim() && u->isTargetableForAttack() &&
41        ( u->IsHostileToPlayers() || i_creature.IsHostileTo(u) /*|| u->getVictim() && i_creature.IsFriendlyTo(u->getVictim())*/ ) &&
42        u->isInAccessablePlaceFor(&i_creature))
43    {
44        float attackRadius = i_creature.GetAttackDistance(u);
45        if(i_creature.IsWithinDistInMap(u,attackRadius) && i_creature.GetDistanceZ(u) <= CREATURE_Z_ATTACK_RANGE)
46        {
47            //Need add code to let guard support player
48            AttackStart(u);
49            u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
50        }
51    }
52}
53
54void GuardAI::EnterEvadeMode()
55{
56    if( !i_creature.isAlive() )
57    {
58        DEBUG_LOG("Creature stopped attacking because he's dead [guid=%u]", i_creature.GetGUIDLow());
59        i_creature.StopMoving();
60        i_creature.GetMotionMaster()->MoveIdle();
61
62        i_state = STATE_NORMAL;
63
64        i_victimGuid = 0;
65        i_creature.CombatStop();
66        i_creature.DeleteThreatList();
67        return;
68    }
69
70    Unit* victim = ObjectAccessor::GetUnit(i_creature, i_victimGuid );
71
72    if( !victim  )
73    {
74        DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
75    }
76    else if( !victim ->isAlive() )
77    {
78        DEBUG_LOG("Creature stopped attacking because victim is dead [guid=%u]", i_creature.GetGUIDLow());
79    }
80    else if( victim ->HasStealthAura() )
81    {
82        DEBUG_LOG("Creature stopped attacking because victim is using stealth [guid=%u]", i_creature.GetGUIDLow());
83    }
84    else if( victim ->isInFlight() )
85    {
86        DEBUG_LOG("Creature stopped attacking because victim is flying away [guid=%u]", i_creature.GetGUIDLow());
87    }
88    else
89    {
90        DEBUG_LOG("Creature stopped attacking because victim outran him [guid=%u]", i_creature.GetGUIDLow());
91    }
92
93    i_creature.RemoveAllAuras();
94    i_creature.DeleteThreatList();
95    i_victimGuid = 0;
96    i_creature.CombatStop();
97    i_state = STATE_NORMAL;
98
99    // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
100    if( i_creature.GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE )
101        i_creature.GetMotionMaster()->MoveTargetedHome();
102}
103
104void GuardAI::UpdateAI(const uint32 /*diff*/)
105{
106    // update i_victimGuid if i_creature.getVictim() !=0 and changed
107    if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
108        return;
109
110    i_victimGuid = i_creature.getVictim()->GetGUID();
111
112    if( i_creature.isAttackReady() )
113    {
114        if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
115        {
116            i_creature.AttackerStateUpdate(i_creature.getVictim());
117            i_creature.resetAttackTimer();
118        }
119    }
120}
121
122bool GuardAI::IsVisible(Unit *pl) const
123{
124    return i_creature.GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_GUARDER)
125        && pl->isVisibleForOrDetect(&i_creature,true);
126}
127
128void GuardAI::AttackStart(Unit *u)
129{
130    if( !u )
131        return;
132
133    //    DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
134    if(i_creature.Attack(u,true))
135    {
136        i_creature.SetInCombatWith(u);
137        u->SetInCombatWith(&i_creature);
138
139        i_creature.AddThreat(u, 0.0f);
140        i_victimGuid = u->GetGUID();
141        i_creature.GetMotionMaster()->MoveChase(u);
142    }
143}
144
145void GuardAI::JustDied(Unit *killer)
146{
147    if(Player* pkiller = killer->GetCharmerOrOwnerPlayerOrPlayerItself())
148        i_creature.SendZoneUnderAttackMessage(pkiller);
149}
150
Note: See TracBrowser for help on using the browser.