2008-10-02 16:23:55 -05:00
/*
2009-02-04 12:42:26 +01:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2009-02-04 12:04:12 +01:00
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-11-20 16:16:57 -06:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* 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, write to the Free Software
2008-11-20 16:16:57 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
#include "CreatureAI.h"
2009-01-08 21:48:38 -06:00
#include "Creature.h"
2009-01-30 18:10:05 -06:00
#include "Player.h"
2009-01-08 21:48:38 -06:00
#include "Pet.h"
#include "SpellAuras.h"
2009-04-13 18:03:28 -05:00
#include "World.h"
2008-10-02 16:23:55 -05:00
2009-01-30 18:10:05 -06:00
void UnitAI :: AttackStart ( Unit * victim )
2008-10-02 16:23:55 -05:00
{
2009-01-30 18:10:05 -06:00
if ( ! victim )
return ;
if ( me -> Attack ( victim , true ))
{
//DEBUG_LOG("Creature %s tagged a victim to kill [guid=%u]", me->GetName(), victim->GetGUIDLow());
me -> GetMotionMaster () -> MoveChase ( victim );
}
2008-10-02 16:23:55 -05:00
}
2009-01-08 21:48:38 -06:00
2009-03-12 14:15:44 -06:00
void UnitAI :: DoMeleeAttackIfReady ()
{
//Make sure our attack is ready and we aren't currently casting before checking distance
if ( me -> isAttackReady () && ! me -> hasUnitState ( UNIT_STAT_CASTING ))
{
//If we are within range melee the target
if ( me -> IsWithinMeleeRange ( me -> getVictim ()))
{
me -> AttackerStateUpdate ( me -> getVictim ());
me -> resetAttackTimer ();
}
}
if ( me -> haveOffhandWeapon () && me -> isAttackReady ( OFF_ATTACK ) && ! me -> hasUnitState ( UNIT_STAT_CASTING ))
{
//If we are within range melee the target
if ( me -> IsWithinMeleeRange ( me -> getVictim ()))
{
me -> AttackerStateUpdate ( me -> getVictim (), OFF_ATTACK );
me -> resetAttackTimer ( OFF_ATTACK );
}
}
}
2009-03-05 18:48:56 -06:00
//Enable PlayerAI when charmed
void PlayerAI :: OnCharmed ( bool apply ) { me -> IsAIEnabled = apply ; }
//Disable CreatureAI when charmed
2009-03-13 17:48:57 -06:00
void CreatureAI :: OnCharmed ( bool apply )
{
//me->IsAIEnabled = !apply;*/
me -> NeedChangeAI = true ;
me -> IsAIEnabled = false ;
}
2009-03-05 18:48:56 -06:00
2009-04-30 22:26:51 -05:00
void CreatureAI :: DoZoneInCombat ( Creature * creature )
2009-04-07 19:38:09 -05:00
{
2009-04-30 22:26:51 -05:00
if ( ! creature )
creature = me ;
2009-04-07 19:38:09 -05:00
2009-04-30 22:26:51 -05: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
{
2009-04-30 22:26:51 -05:00
sLog . outError ( "DoZoneInCombat call for map that isn't an instance (creature entry = %d)" , creature -> GetTypeId () == TYPEID_UNIT ? (( Creature * ) creature ) -> GetEntry () : 0 );
2009-04-07 19:38:09 -05:00
return ;
}
2009-04-30 22:26:51 -05:00
if ( ! creature -> getVictim ())
{
if ( Unit * target = creature -> SelectNearestTarget ())
2009-04-29 00:15:18 -05:00
AttackStart ( target );
2009-04-30 22:26:51 -05:00
else if ( creature -> isSummon ())
{
if ( Unit * summoner = (( TempSummon * ) creature ) -> GetSummoner ())
{
if ( summoner -> getVictim ()
&& ( creature -> IsFriendlyTo ( summoner ) || creature -> IsHostileTo ( summoner -> getVictim ())))
AttackStart ( summoner -> getVictim ());
}
}
}
2009-04-29 00:15:18 -05:00
2009-04-30 22:26:51 -05:00
if ( ! creature -> CanHaveThreatList () || creature -> getThreatManager (). isThreatListEmpty ())
2009-04-07 19:38:09 -05:00
{
2009-04-30 22:26:51 -05:00
sLog . outError ( "DoZoneInCombat called for creature that either cannot have threat list or has empty threat list (creature entry = %d)" , creature -> GetTypeId () == TYPEID_UNIT ? (( Creature * ) creature ) -> GetEntry () : 0 );
2009-04-07 19:38:09 -05:00
return ;
}
Map :: PlayerList const & PlayerList = map -> GetPlayers ();
for ( Map :: PlayerList :: const_iterator i = PlayerList . begin (); i != PlayerList . end (); ++ i )
{
if ( Player * i_pl = i -> getSource ())
if ( i_pl -> isAlive ())
{
2009-04-30 22:26:51 -05:00
creature -> SetInCombatWith ( i_pl );
i_pl -> SetInCombatWith ( creature );
creature -> AddThreat ( i_pl , 0.0f );
2009-04-07 19:38:09 -05:00
}
}
}
2009-01-30 18:10:05 -06:00
void CreatureAI :: MoveInLineOfSight ( Unit * who )
2009-01-08 21:48:38 -06:00
{
2009-04-13 18:03:28 -05:00
if ( me -> getVictim ())
return ;
2009-02-09 08:06:16 -05:00
2009-04-13 18:03:28 -05:00
if ( me -> canStartAttack ( who ))
AttackStart ( who );
else if ( who -> getVictim () && me -> IsFriendlyTo ( who )
&& me -> IsWithinDistInMap ( who , sWorld . getConfig ( CONFIG_CREATURE_FAMILY_ASSISTANCE_RADIUS ))
&& me -> canAttack ( who -> getVictim ()))
AttackStart ( who -> getVictim ());
2009-01-30 18:10:05 -06:00
}
2009-01-28 20:16:11 -06:00
2009-01-30 18:10:05 -06:00
bool CreatureAI :: UpdateVictim ()
{
if ( ! me -> isInCombat ())
return false ;
if ( Unit * victim = me -> SelectVictim ())
AttackStart ( victim );
return me -> getVictim ();
}
void CreatureAI :: EnterEvadeMode ()
{
2009-01-28 20:16:11 -06:00
me -> RemoveAllAuras ();
me -> DeleteThreatList ();
me -> CombatStop ();
me -> LoadCreaturesAddon ();
me -> SetLootRecipient ( NULL );
if ( me -> isAlive ())
me -> GetMotionMaster () -> MoveTargetedHome ();
2009-01-08 21:48:38 -06:00
}
void SimpleCharmedAI :: UpdateAI ( const uint32 /*diff*/ )
{
2009-01-28 20:16:11 -06:00
Creature * charmer = ( Creature * ) me -> GetCharmer ();
2009-01-08 21:48:38 -06:00
//kill self if charm aura has infinite duration
if ( charmer -> IsInEvadeMode ())
{
2009-04-06 13:31:14 +02:00
Unit :: AuraEffectList const & auras = me -> GetAurasByType ( SPELL_AURA_MOD_CHARM );
for ( Unit :: AuraEffectList :: const_iterator iter = auras . begin (); iter != auras . end (); ++ iter )
if (( * iter ) -> GetCasterGUID () == charmer -> GetGUID () && ( * iter ) -> GetParentAura () -> IsPermanent ())
2009-01-08 21:48:38 -06:00
{
2009-01-28 20:16:11 -06:00
charmer -> Kill ( me );
2009-01-08 21:48:38 -06:00
return ;
}
}
if ( ! charmer -> isInCombat ())
2009-01-28 20:16:11 -06:00
me -> GetMotionMaster () -> MoveFollow ( charmer , PET_FOLLOW_DIST , PET_FOLLOW_ANGLE );
2009-01-08 21:48:38 -06:00
2009-01-28 20:16:11 -06:00
Unit * target = me -> getVictim ();
2009-01-08 21:48:38 -06:00
if ( ! target || ! charmer -> canAttack ( target ))
2009-01-30 18:10:05 -06:00
AttackStart ( charmer -> SelectNearestTarget ());
2009-01-08 21:48:38 -06:00
}
2009-02-17 20:07:49 -06:00
2009-04-14 21:07:52 -05:00
/*void CreatureAI::AttackedBy( Unit* attacker )
{
if(!m_creature->getVictim())
AttackStart(attacker);
}*/