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

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

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

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