2010-10-07 15:35:36 +02:00
/*
2017-01-01 16:21:30 +01:00
* Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "CreatureAI.h"
2009-05-10 16:07:49 -05:00
#include "CreatureAIImpl.h"
2009-01-08 21:48:38 -06:00
#include "Creature.h"
2009-04-13 18:03:28 -05:00
#include "World.h"
2009-05-10 22:48:22 -05:00
#include "SpellMgr.h"
2009-08-22 21:25:35 -05:00
#include "Vehicle.h"
2011-08-03 08:18:24 +02:00
#include "Log.h"
#include "MapReference.h"
#include "Player.h"
2012-03-01 23:45:07 +01:00
#include "CreatureTextMgr.h"
2016-01-13 15:33:17 +01:00
#include "Language.h"
2009-10-17 15:51:44 -07:00
2009-03-05 18:48:56 -06:00
//Disable CreatureAI when charmed
2015-12-16 20:33:09 +01:00
void CreatureAI :: OnCharmed ( bool apply )
2009-03-13 17:48:57 -06:00
{
2015-12-16 20:33:09 +01:00
if ( apply )
{
me -> NeedChangeAI = true ;
me -> IsAIEnabled = false ;
}
2009-03-13 17:48:57 -06:00
}
2009-10-17 15:51:44 -07:00
2011-08-18 14:55:27 -03:00
AISpellInfoType * UnitAI :: AISpellInfo ;
AISpellInfoType * GetAISpellInfo ( uint32 i ) { return & CreatureAI :: AISpellInfo [ i ]; }
2009-10-17 15:51:44 -07:00
2017-05-18 23:52:58 +02:00
CreatureAI :: CreatureAI ( Creature * creature ) : UnitAI ( creature ), me ( creature ), _boundary ( nullptr ), m_MoveInLineOfSight_locked ( false )
{
}
CreatureAI ::~ CreatureAI ()
{
}
2014-09-18 23:33:59 +02:00
void CreatureAI :: Talk ( uint8 id , WorldObject const * whisperTarget /*= nullptr*/ )
2010-11-16 19:21:48 +01:00
{
2013-12-23 14:23:49 +01:00
sCreatureTextMgr -> SendChat ( me , id , whisperTarget );
2010-11-16 19:21:48 +01:00
}
2016-05-11 23:24:26 +02:00
void CreatureAI :: DoZoneInCombat ( Creature * creature /*= nullptr*/ , float maxRangeToNearestTarget /* = 250.0f*/ )
2009-04-07 19:38:09 -05:00
{
2009-04-30 22:26:51 -05:00
if ( ! creature )
creature = me ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! creature -> CanHaveThreatList ())
2009-05-13 10:54:11 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-08-18 14:55:27 -03:00
Map * map = creature -> GetMap ();
2009-04-07 19:38:09 -05:00
if ( ! map -> IsDungeon ()) //use IsDungeon instead of Instanceable, in case battlegrounds will be instantiated
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "misc" , "DoZoneInCombat call for map that isn't an instance (creature entry = %d)" , creature -> GetTypeId () == TYPEID_UNIT ? creature -> ToCreature () -> GetEntry () : 0 );
2009-04-07 19:38:09 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> GetVictim ())
2009-04-30 22:26:51 -05:00
{
2011-11-23 17:55:16 +01:00
if ( Unit * nearTarget = creature -> SelectNearestTarget ( maxRangeToNearestTarget ))
creature -> AI () -> AttackStart ( nearTarget );
2013-06-11 19:54:27 -02:30
else if ( creature -> IsSummon ())
2009-04-30 22:26:51 -05:00
{
2011-08-18 14:55:27 -03:00
if ( Unit * summoner = creature -> ToTempSummon () -> GetSummoner ())
2009-04-30 22:26:51 -05:00
{
2011-06-12 01:47:45 +02:00
Unit * target = summoner -> getAttackerForHelper ();
2010-04-07 19:14:10 +02:00
if ( ! target && summoner -> CanHaveThreatList () && ! summoner -> getThreatManager (). isThreatListEmpty ())
2009-05-13 10:54:11 -05:00
target = summoner -> getThreatManager (). getHostilTarget ();
2010-04-07 19:14:10 +02:00
if ( target && ( creature -> IsFriendlyTo ( summoner ) || creature -> IsHostileTo ( target )))
2009-05-13 10:54:11 -05:00
creature -> AI () -> AttackStart ( target );
2009-04-30 22:26:51 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
2013-09-03 15:09:37 -05:00
// Intended duplicated check, the code above this should select a victim
// If it can't find a suitable attack target then we should error out.
2013-06-11 19:54:27 -02:30
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> GetVictim ())
2009-04-07 19:38:09 -05:00
{
2016-08-05 21:22:12 +02:00
TC_LOG_ERROR ( "misc.dozoneincombat" , "DoZoneInCombat called for creature that has empty threat list (creature entry = %u)" , creature -> GetEntry ());
2009-04-07 19:38:09 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-08-18 14:55:27 -03:00
Map :: PlayerList const & playerList = map -> GetPlayers ();
2009-10-17 15:51:44 -07:00
2011-08-18 14:55:27 -03:00
if ( playerList . isEmpty ())
2009-05-27 21:30:03 +02:00
return ;
2009-10-17 15:51:44 -07:00
2011-08-18 14:55:27 -03:00
for ( Map :: PlayerList :: const_iterator itr = playerList . begin (); itr != playerList . end (); ++ itr )
2009-04-07 19:38:09 -05:00
{
2013-06-11 21:25:12 -02:30
if ( Player * player = itr -> GetSource ())
2009-05-13 10:54:11 -05:00
{
2013-06-11 21:25:12 -02:30
if ( player -> IsGameMaster ())
2009-05-27 21:30:03 +02:00
continue ;
2009-10-17 15:51:44 -07:00
2013-06-11 19:54:27 -02:30
if ( player -> IsAlive ())
2009-05-27 21:30:03 +02:00
{
2011-08-18 14:55:27 -03:00
creature -> SetInCombatWith ( player );
player -> SetInCombatWith ( creature );
creature -> AddThreat ( player , 0.0f );
2009-05-27 21:30:03 +02:00
}
2009-10-17 15:51:44 -07:00
2009-10-06 13:16:10 -07:00
/* Causes certain things to never leave the threat list (Priest Lightwell, etc):
2011-10-07 11:08:09 -05:00
for (Unit::ControlList::const_iterator itr = player->m_Controlled.begin(); itr != player->m_Controlled.end(); ++itr)
2009-08-08 11:56:54 -05:00
{
creature->SetInCombatWith(*itr);
(*itr)->SetInCombatWith(creature);
creature->AddThreat(*itr, 0.0f);
2009-10-06 13:16:10 -07:00
}*/
2009-05-13 10:54:11 -05:00
}
2009-04-07 19:38:09 -05:00
}
}
2009-10-17 15:51:44 -07:00
2010-03-06 18:18:32 +02:00
// scripts does not take care about MoveInLineOfSight loops
// MoveInLineOfSight can be called inside another MoveInLineOfSight and cause stack overflow
2011-08-18 14:55:27 -03:00
void CreatureAI :: MoveInLineOfSight_Safe ( Unit * who )
2010-04-07 18:09:10 +02:00
{
2010-04-07 19:14:10 +02:00
if ( m_MoveInLineOfSight_locked == true )
2010-03-06 18:18:32 +02:00
return ;
m_MoveInLineOfSight_locked = true ;
MoveInLineOfSight ( who );
m_MoveInLineOfSight_locked = false ;
}
2011-08-18 14:55:27 -03:00
void CreatureAI :: MoveInLineOfSight ( Unit * who )
2009-01-08 21:48:38 -06:00
{
2013-06-11 19:54:27 -02:30
if ( me -> GetVictim ())
2009-04-13 18:03:28 -05:00
return ;
2009-10-17 16:20:24 -07:00
2009-12-17 06:58:19 +01:00
if ( me -> GetCreatureType () == CREATURE_TYPE_NON_COMBAT_PET ) // non-combat pets should just stand there and look good;)
2009-10-09 10:55:50 +01:00
return ;
2009-10-17 15:51:44 -07:00
2016-02-11 18:33:00 +01:00
if ( me -> HasReactState ( REACT_AGGRESSIVE ) && me -> CanStartAttack ( who , false ))
2009-04-13 18:03:28 -05:00
AttackStart ( who );
2013-06-11 19:54:27 -02:30
//else if (who->GetVictim() && me->IsFriendlyTo(who)
2010-12-23 23:25:44 +01:00
// && me->IsWithinDistInMap(who, sWorld->getIntConfig(CONFIG_CREATURE_FAMILY_ASSISTANCE_RADIUS))
2013-06-11 21:25:12 -02:30
// && me->CanStartAttack(who->GetVictim(), true)) /// @todo if we use true, it will not attack it when it arrives
2013-06-11 19:54:27 -02:30
// me->GetMotionMaster()->MoveChase(who->GetVictim());
2009-01-30 18:10:05 -06:00
}
2009-10-17 15:51:44 -07:00
2015-07-15 18:43:19 +02:00
// Distract creature, if player gets too close while stealthed/prowling
void CreatureAI :: TriggerAlert ( Unit const * who ) const
{
// If there's no target, or target isn't a player do nothing
if ( ! who || who -> GetTypeId () != TYPEID_PLAYER )
return ;
// If this unit isn't an NPC, is already distracted, is in combat, is confused, stunned or fleeing, do nothing
if ( me -> GetTypeId () != TYPEID_UNIT || me -> IsInCombat () || me -> HasUnitState ( UNIT_STATE_CONFUSED | UNIT_STATE_STUNNED | UNIT_STATE_FLEEING | UNIT_STATE_DISTRACTED ))
return ;
// Only alert for hostiles!
if ( me -> IsCivilian () || me -> HasReactState ( REACT_PASSIVE ) || ! me -> IsHostileTo ( who ) || ! me -> _IsTargetAcceptable ( who ))
return ;
// Send alert sound (if any) for this creature
me -> SendAIReaction ( AI_REACTION_ALERT );
// Face the unit (stealthed player) and set distracted state for 5 seconds
2016-09-20 20:19:15 +02:00
me -> SetFacingTo ( me -> GetAngle ( who -> GetPositionX (), who -> GetPositionY ()), true );
2015-07-15 18:43:19 +02:00
me -> StopMoving ();
me -> GetMotionMaster () -> MoveDistract ( 5 * IN_MILLISECONDS );
}
2016-01-13 15:33:17 +01:00
void CreatureAI :: EnterEvadeMode ( EvadeReason why )
2009-05-13 10:54:11 -05:00
{
2016-01-13 15:33:17 +01:00
if ( ! _EnterEvadeMode ( why ))
2009-05-13 10:54:11 -05:00
return ;
2009-10-17 15:51:44 -07:00
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "entities.unit" , "Creature %u enters evade mode." , me -> GetEntry ());
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! me -> GetVehicle ()) // otherwise me will be in evade mode forever
2009-07-22 17:10:30 +02:00
{
2011-08-18 14:55:27 -03:00
if ( Unit * owner = me -> GetCharmerOrOwner ())
2009-08-08 11:57:51 -05:00
{
me -> GetMotionMaster () -> Clear ( false );
2010-04-14 23:07:41 +02:00
me -> GetMotionMaster () -> MoveFollow ( owner , PET_FOLLOW_DIST , me -> GetFollowAngle (), MOTION_SLOT_ACTIVE );
2009-08-08 11:57:51 -05:00
}
else
2012-09-22 02:11:31 +01:00
{
// Required to prevent attacking creatures that are evading and cause them to reenter combat
// Does not apply to MoveFollow
me -> AddUnitState ( UNIT_STATE_EVADE );
2009-08-08 11:57:51 -05:00
me -> GetMotionMaster () -> MoveTargetedHome ();
2012-09-22 02:11:31 +01:00
}
2009-07-22 17:10:30 +02:00
}
2009-10-17 15:51:44 -07:00
2009-05-11 11:46:48 -05:00
Reset ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( me -> IsVehicle ()) // use the same sequence of addtoworld, aireset may remove all summons!
2011-04-22 21:59:37 +02:00
me -> GetVehicleKit () -> Reset ( true );
2009-01-08 21:48:38 -06:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
/*void CreatureAI::AttackedBy(Unit* attacker)
2009-04-14 21:07:52 -05:00
{
2013-06-11 19:54:27 -02:30
if (!me->GetVictim())
2009-04-14 21:07:52 -05:00
AttackStart(attacker);
}*/
2013-01-02 14:20:20 +01:00
void CreatureAI :: SetGazeOn ( Unit * target )
{
if ( me -> IsValidAttackTarget ( target ))
{
2016-01-13 18:38:54 +01:00
if ( ! me -> IsFocusing ( nullptr , true ))
AttackStart ( target );
2013-01-02 14:20:20 +01:00
me -> SetReactState ( REACT_PASSIVE );
}
}
bool CreatureAI :: UpdateVictimWithGaze ()
{
2013-06-11 19:54:27 -02:30
if ( ! me -> IsInCombat ())
2013-01-02 14:20:20 +01:00
return false ;
if ( me -> HasReactState ( REACT_PASSIVE ))
{
2013-06-11 19:54:27 -02:30
if ( me -> GetVictim ())
2013-01-02 14:20:20 +01:00
return true ;
else
me -> SetReactState ( REACT_AGGRESSIVE );
}
if ( Unit * victim = me -> SelectVictim ())
2016-01-13 18:38:54 +01:00
if ( ! me -> IsFocusing ( nullptr , true ))
AttackStart ( victim );
2014-07-17 15:42:57 +02:00
return me -> GetVictim () != nullptr ;
2013-01-02 14:20:20 +01:00
}
bool CreatureAI :: UpdateVictim ()
{
2013-06-11 19:54:27 -02:30
if ( ! me -> IsInCombat ())
2013-01-02 14:20:20 +01:00
return false ;
if ( ! me -> HasReactState ( REACT_PASSIVE ))
{
if ( Unit * victim = me -> SelectVictim ())
2016-01-13 18:38:54 +01:00
if ( ! me -> IsFocusing ( nullptr , true ))
AttackStart ( victim );
2014-07-17 15:42:57 +02:00
return me -> GetVictim () != nullptr ;
2013-01-02 14:20:20 +01:00
}
else if ( me -> getThreatManager (). isThreatListEmpty ())
{
2016-01-13 15:33:17 +01:00
EnterEvadeMode ( EVADE_REASON_NO_HOSTILES );
2013-01-02 14:20:20 +01:00
return false ;
}
return true ;
}
2016-01-13 15:33:17 +01:00
bool CreatureAI :: _EnterEvadeMode ( EvadeReason /*why*/ )
2013-01-02 14:20:20 +01:00
{
2013-06-11 19:54:27 -02:30
if ( ! me -> IsAlive ())
2013-01-02 14:20:20 +01:00
return false ;
2016-04-25 11:46:00 +02:00
me -> RemoveAurasOnEvade ();
2013-01-02 14:20:20 +01:00
// sometimes bosses stuck in combat?
me -> DeleteThreatList ();
me -> CombatStop ( true );
2016-05-11 23:24:26 +02:00
me -> SetLootRecipient ( nullptr );
2013-01-02 14:20:20 +01:00
me -> ResetPlayerDamageReq ();
2013-08-02 19:56:48 +02:00
me -> SetLastDamagedTime ( 0 );
2016-03-24 16:12:53 +01:00
me -> SetCannotReachTarget ( false );
2013-01-02 14:20:20 +01:00
if ( me -> IsInEvadeMode ())
return false ;
return true ;
}
2016-03-24 16:12:53 +01:00
const uint32 BOUNDARY_VISUALIZE_CREATURE = 15425 ;
const float BOUNDARY_VISUALIZE_CREATURE_SCALE = 0.25f ;
const int8 BOUNDARY_VISUALIZE_STEP_SIZE = 1 ;
const int32 BOUNDARY_VISUALIZE_FAILSAFE_LIMIT = 750 ;
const float BOUNDARY_VISUALIZE_SPAWN_HEIGHT = 5.0f ;
2016-01-13 15:33:17 +01:00
int32 CreatureAI :: VisualizeBoundary ( uint32 duration , Unit * owner , bool fill ) const
{
typedef std :: pair < int32 , int32 > coordinate ;
2016-03-16 21:38:01 +01:00
2016-01-13 15:33:17 +01:00
if ( ! owner )
return - 1 ;
if ( ! _boundary || _boundary -> empty ())
return LANG_CREATURE_MOVEMENT_NOT_BOUNDED ;
std :: queue < coordinate > Q ;
std :: unordered_set < coordinate > alreadyChecked ;
std :: unordered_set < coordinate > outOfBounds ;
2016-03-16 21:38:01 +01:00
2016-01-13 15:33:17 +01:00
Position startPosition = owner -> GetPosition ();
2016-03-24 16:12:53 +01:00
if ( ! CheckBoundary ( & startPosition ))
{ // fall back to creature position
2016-01-13 15:33:17 +01:00
startPosition = me -> GetPosition ();
if ( ! CheckBoundary ( & startPosition ))
2016-03-24 16:12:53 +01:00
{ // fall back to creature home position
2016-01-13 15:33:17 +01:00
startPosition = me -> GetHomePosition ();
2016-03-24 16:12:53 +01:00
if ( ! CheckBoundary ( & startPosition ))
2016-01-13 15:33:17 +01:00
return LANG_CREATURE_NO_INTERIOR_POINT_FOUND ;
}
}
float spawnZ = startPosition . GetPositionZ () + BOUNDARY_VISUALIZE_SPAWN_HEIGHT ;
bool boundsWarning = false ;
Q . push ({ 0 , 0 });
while ( ! Q . empty ())
{
coordinate front = Q . front ();
bool hasOutOfBoundsNeighbor = false ;
for ( coordinate off : std :: initializer_list < coordinate > {{ 1 , 0 }, { 0 , 1 }, { - 1 , 0 }, { 0 , - 1 }})
{
coordinate next ( front . first + off . first , front . second + off . second );
if ( next . first > BOUNDARY_VISUALIZE_FAILSAFE_LIMIT || next . first < - BOUNDARY_VISUALIZE_FAILSAFE_LIMIT || next . second > BOUNDARY_VISUALIZE_FAILSAFE_LIMIT || next . second < - BOUNDARY_VISUALIZE_FAILSAFE_LIMIT )
{
boundsWarning = true ;
continue ;
}
if ( alreadyChecked . find ( next ) == alreadyChecked . end ()) // never check a coordinate twice
{
Position nextPos ( startPosition . GetPositionX () + next . first * BOUNDARY_VISUALIZE_STEP_SIZE , startPosition . GetPositionY () + next . second * BOUNDARY_VISUALIZE_STEP_SIZE , startPosition . GetPositionZ ());
if ( CheckBoundary ( & nextPos ))
Q . push ( next );
else
{
outOfBounds . insert ( next );
hasOutOfBoundsNeighbor = true ;
}
alreadyChecked . insert ( next );
}
else
if ( outOfBounds . find ( next ) != outOfBounds . end ())
hasOutOfBoundsNeighbor = true ;
}
if ( fill || hasOutOfBoundsNeighbor )
if ( TempSummon * point = owner -> SummonCreature ( BOUNDARY_VISUALIZE_CREATURE , Position ( startPosition . GetPositionX () + front . first * BOUNDARY_VISUALIZE_STEP_SIZE , startPosition . GetPositionY () + front . second * BOUNDARY_VISUALIZE_STEP_SIZE , spawnZ ), TEMPSUMMON_TIMED_DESPAWN , duration * IN_MILLISECONDS ))
{
point -> SetObjectScale ( BOUNDARY_VISUALIZE_CREATURE_SCALE );
point -> SetFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_IMMUNE_TO_PC | UNIT_FLAG_STUNNED | UNIT_FLAG_IMMUNE_TO_NPC );
if ( ! hasOutOfBoundsNeighbor )
point -> SetFlag ( UNIT_FIELD_FLAGS , UNIT_FLAG_NOT_SELECTABLE );
}
Q . pop ();
}
return boundsWarning ? LANG_CREATURE_MOVEMENT_MAYBE_UNBOUNDED : 0 ;
}
2016-02-07 03:23:36 +01:00
bool CreatureAI :: CheckBoundary ( Position const * who ) const
2016-01-13 15:33:17 +01:00
{
if ( ! who )
who = me ;
if ( _boundary )
for ( CreatureBoundary :: const_iterator it = _boundary -> begin (); it != _boundary -> end (); ++ it )
if ( ! ( * it ) -> IsWithinBoundary ( who ))
return false ;
return true ;
}
2017-05-18 23:52:58 +02:00
void CreatureAI :: SetBoundary ( CreatureBoundary const * boundary )
{
_boundary = boundary ;
me -> DoImmediateBoundaryCheck ();
}
2016-01-13 15:33:17 +01:00
bool CreatureAI :: CheckInRoom ()
{
if ( CheckBoundary ())
return true ;
else
{
EnterEvadeMode ( EVADE_REASON_BOUNDARY );
return false ;
}
}
2013-01-02 14:20:20 +01:00
Creature * CreatureAI :: DoSummon ( uint32 entry , const Position & pos , uint32 despawnTime , TempSummonType summonType )
{
return me -> SummonCreature ( entry , pos , summonType , despawnTime );
}
Creature * CreatureAI :: DoSummon ( uint32 entry , WorldObject * obj , float radius , uint32 despawnTime , TempSummonType summonType )
{
2014-04-25 21:19:30 +02:00
Position pos = obj -> GetRandomNearPosition ( radius );
2013-01-02 14:20:20 +01:00
return me -> SummonCreature ( entry , pos , summonType , despawnTime );
}
Creature * CreatureAI :: DoSummonFlyer ( uint32 entry , WorldObject * obj , float flightZ , float radius , uint32 despawnTime , TempSummonType summonType )
{
2014-04-25 21:19:30 +02:00
Position pos = obj -> GetRandomNearPosition ( radius );
2013-01-02 14:20:20 +01:00
pos . m_positionZ += flightZ ;
return me -> SummonCreature ( entry , pos , summonType , despawnTime );
}