[4054] added: Line of sight (vmaps) [part 1]
[mangos-git.git] / src / game / AggressorAI.cpp
blob551d56111a99ad0b87353f0f476c75c219f08c70
1 /*
2 * Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/>
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.
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.
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
19 #include "AggressorAI.h"
20 #include "Errors.h"
21 #include "Creature.h"
22 #include "Player.h"
23 #include "TargetedMovementGenerator.h"
24 #include "Database/DBCStores.h"
25 #include "ObjectAccessor.h"
26 #include <list>
27 #include "VMapFactory.h"
29 int
30 AggressorAI::Permissible(const Creature *creature)
32 // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight
33 if( !creature->IsNeutralToAll() && !creature->isCivilian() )
34 return PERMIT_BASE_PROACTIVE;
36 return PERMIT_BASE_NO;
39 AggressorAI::AggressorAI(Creature &c) : i_creature(c), i_victimGuid(0), i_state(STATE_NORMAL), i_tracker(TIME_INTERVAL_LOOK)
43 void
44 AggressorAI::MoveInLineOfSight(Unit *u)
46 if( !i_creature.getVictim() && !i_creature.hasUnitState(UNIT_STAT_STUNDED) && u->isTargetableForAttack() &&
47 ( i_creature.IsHostileTo( u ) /*|| u->getVictim() && i_creature.IsFriendlyTo( u->getVictim() )*/ ) &&
48 u->isInAccessablePlaceFor(&i_creature) )
50 float attackRadius = i_creature.GetAttackDistance(u);
51 if(i_creature.IsWithinDistInMap(u, attackRadius) && i_creature.GetDistanceZ(u) <= CREATURE_Z_ATTACK_RANGE)
53 if(!i_creature.IsWithinLOSInMap(u)) return;
54 AttackStart(u);
55 if(u->HasStealthAura())
56 u->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
61 bool
62 AggressorAI::_needToStop() const
64 if( !i_creature.isAlive() || !i_creature.getVictim())
65 return true;
67 //if(!i_creature.getVictim()->isTargetableForAttack() || !i_creature.getVictim()->isInAccessablePlaceFor(&i_creature))
68 //return true;
69 //no need for this checks because mob changes its victim only when
70 //1) victim is dead (check is in SelectHostilTarget() func)
71 //2) victim is out of threat radius
73 // chaise only at same map
74 if(!i_creature.IsInMap(i_creature.getVictim()))
75 return true;
77 // instance not have threat radius for stop
78 Map* map = MapManager::Instance().GetMap(i_creature.GetMapId(),&i_creature);
79 if(map->Instanceable())
80 return false;
82 float rx,ry,rz;
83 i_creature.GetRespawnCoord(rx, ry, rz);
84 float length = i_creature.getVictim()->GetDistanceSq(rx,ry,rz);
85 return ( length > CREATURE_THREAT_RADIUS );
88 void AggressorAI::_stopAttack()
90 DEBUG_LOG("What we do HERE ?");
91 return;
94 void AggressorAI::EnterEvadeMode()
96 if( !i_creature.isAlive() )
98 DEBUG_LOG("Creature stopped attacking cuz his dead [guid=%u]", i_creature.GetGUIDLow());
99 i_victimGuid = 0;
100 i_creature.CombatStop(true);
101 i_creature.DeleteThreatList();
102 return;
105 Unit* victim = ObjectAccessor::Instance().GetUnit(i_creature, i_victimGuid );
107 if( !victim )
109 DEBUG_LOG("Creature stopped attacking because victim is non exist [guid=%u]", i_creature.GetGUIDLow());
111 else if( !victim->isAlive() )
113 DEBUG_LOG("Creature stopped attacking cuz his victim is dead [guid=%u]", i_creature.GetGUIDLow());
115 else if( victim->HasStealthAura() )
117 DEBUG_LOG("Creature stopped attacking cuz his victim is stealth [guid=%u]", i_creature.GetGUIDLow());
119 else if( victim->isInFlight() )
121 DEBUG_LOG("Creature stopped attacking cuz his victim is fly away [guid=%u]", i_creature.GetGUIDLow());
123 else
125 DEBUG_LOG("Creature stopped attacking due to target out run him [guid=%u]", i_creature.GetGUIDLow());
126 //i_state = STATE_LOOK_AT_VICTIM;
127 //i_tracker.Reset(TIME_INTERVAL_LOOK);
130 i_creature.RemoveAllAuras();
131 i_creature.DeleteThreatList();
132 i_victimGuid = 0;
133 i_creature.CombatStop(true);
135 // Remove TargetedMovementGenerator from MotionMaster stack list, and add HomeMovementGenerator instead
136 if( i_creature->top()->GetMovementGeneratorType() == TARGETED_MOTION_TYPE )
137 i_creature->TargetedHome();
140 void
141 AggressorAI::UpdateAI(const uint32 diff)
143 // update i_victimGuid if i_creature.getVictim() !=0 and changed
144 if(!i_creature.SelectHostilTarget() || !i_creature.getVictim())
145 return;
147 i_victimGuid = i_creature.getVictim()->GetGUID();
149 // i_creature.getVictim() can't be used for check in case stop fighting, i_creature.getVictim() clearóâ at Unit death etc.
150 if( i_victimGuid )
152 if( _needToStop() )
154 DEBUG_LOG("Aggressor AI stoped attacking [guid=%u]", i_creature.GetGUIDLow());
155 EnterEvadeMode(); // i_victimGuid == 0 && i_creature.getVictim() == NULL now
156 return;
159 assert((i_victimGuid != 0) == (i_creature.getVictim() != NULL) && "i_victimGuid and i_creature.getVictim() not synchronized.");
161 if( i_creature.IsWithinDistInMap(i_creature.getVictim(), ATTACK_DISTANCE))
163 if( i_creature.isAttackReady() )
165 i_creature.AttackerStateUpdate(i_creature.getVictim());
166 i_creature.resetAttackTimer();
172 bool
173 AggressorAI::IsVisible(Unit *pl) const
175 return i_creature.GetDistanceSq(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
176 && pl->isVisibleForOrDetect(&i_creature,true);
179 void
180 AggressorAI::AttackStart(Unit *u)
182 if( !u )
183 return;
185 sLog.outDetail("Start to attack");
186 if(i_creature.Attack(u))
188 i_creature.AddThreat(u, 0.0f);
189 // DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", i_creature.GetName(), u->GetGUIDLow());
190 i_victimGuid = u->GetGUID();
192 i_creature.resetAttackTimer();
193 i_creature->Mutate(new TargetedMovementGenerator(*u));
194 if (u->GetTypeId() == TYPEID_PLAYER)
195 i_creature.SetLootRecipient((Player*)u);