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-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"
2008-10-02 16:23:55 -05:00
2009-03-05 18:48:56 -06:00
//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-05-20 11:44:38 -05:00
AISpellInfoType * UnitAI :: AISpellInfo ;
2009-05-17 23:27:51 -05:00
TRINITY_DLL_SPEC AISpellInfoType * GetAISpellInfo ( uint32 i ) { return & CreatureAI :: AISpellInfo [ i ]; }
2009-05-10 14:29:28 -05: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-05-13 10:54:11 -05:00
if ( ! creature -> CanHaveThreatList ())
return ;
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-05-13 10:54:11 -05:00
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> getVictim ())
2009-04-30 22:26:51 -05:00
{
2009-05-16 14:49:30 -05:00
if ( Unit * target = creature -> SelectNearestTarget ( 50 ))
2009-05-09 14:08:11 -05:00
creature -> AI () -> AttackStart ( target );
2009-04-30 22:26:51 -05:00
else if ( creature -> isSummon ())
{
if ( Unit * summoner = (( TempSummon * ) creature ) -> GetSummoner ())
{
2009-05-13 10:54:11 -05:00
Unit * target = summoner -> getAttackerForHelper ();
if ( ! target && summoner -> CanHaveThreatList () && ! summoner -> getThreatManager (). isThreatListEmpty ())
target = summoner -> getThreatManager (). getHostilTarget ();
if ( target && ( creature -> IsFriendlyTo ( summoner ) || creature -> IsHostileTo ( target )))
creature -> AI () -> AttackStart ( target );
2009-04-30 22:26:51 -05:00
}
}
}
2009-04-29 00:15:18 -05:00
2009-05-13 10:54:11 -05:00
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> getVictim ())
2009-04-07 19:38:09 -05:00
{
2009-05-13 10:54:11 -05:00
sLog . outError ( "DoZoneInCombat called for creature that has empty threat list (creature entry = %u)" , creature -> GetEntry ());
2009-04-07 19:38:09 -05:00
return ;
}
2009-05-27 21:30:03 +02:00
Map :: PlayerList const & PlList = map -> GetPlayers ();
if ( PlList . isEmpty ())
return ;
for ( Map :: PlayerList :: const_iterator i = PlList . begin (); i != PlList . end (); ++ i )
2009-04-07 19:38:09 -05:00
{
2009-05-27 21:30:03 +02:00
if ( Player * pPlayer = i -> getSource ())
2009-05-13 10:54:11 -05:00
{
2009-05-27 21:30:03 +02:00
if ( pPlayer -> isGameMaster ())
continue ;
if ( pPlayer -> isAlive ())
{
creature -> SetInCombatWith ( pPlayer );
pPlayer -> SetInCombatWith ( creature );
creature -> AddThreat ( pPlayer , 0.0f );
}
2009-05-13 10:54:11 -05:00
}
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-05-20 11:44:38 -05:00
void CreatureAI :: SelectNearestTarget ( Unit * who )
{
if ( me -> getVictim () && me -> GetDistanceOrder ( who , me -> getVictim ()) && me -> canAttack ( who ))
{
me -> getThreatManager (). modifyThreatPercent ( me -> getVictim (), - 100 );
me -> AddThreat ( who , 1000000.0f );
}
}
2009-05-16 16:52:57 -05:00
void CreatureAI :: SetGazeOn ( Unit * target )
{
if ( me -> canAttack ( target ))
{
AttackStart ( target );
me -> SetReactState ( REACT_PASSIVE );
}
}
bool CreatureAI :: UpdateVictimWithGaze ()
{
if ( ! me -> isInCombat ())
return false ;
if ( me -> HasReactState ( REACT_PASSIVE ))
{
if ( me -> getVictim ())
return true ;
else
me -> SetReactState ( REACT_AGGRESSIVE );
}
if ( Unit * victim = me -> SelectVictim ())
AttackStart ( victim );
return me -> getVictim ();
}
2009-05-21 17:19:10 -05:00
bool CreatureAI :: UpdateVictim ()
2009-05-16 14:49:30 -05:00
{
if ( ! me -> isInCombat ())
return false ;
2009-05-21 17:19:10 -05:00
if ( ! me -> HasReactState ( REACT_PASSIVE ))
2009-05-16 14:49:30 -05:00
{
if ( Unit * victim = me -> SelectVictim ())
AttackStart ( victim );
return me -> getVictim ();
}
else if ( me -> getThreatManager (). isThreatListEmpty ())
{
EnterEvadeMode ();
return false ;
}
return true ;
}
2009-05-13 10:54:11 -05:00
bool CreatureAI :: _EnterEvadeMode ()
2009-01-30 18:10:05 -06:00
{
2009-05-13 10:54:11 -05:00
if ( me -> IsInEvadeMode () || ! me -> isAlive ())
return false ;
2009-05-12 10:11:54 -05:00
2009-01-28 20:16:11 -06:00
me -> RemoveAllAuras ();
me -> DeleteThreatList ();
2009-05-12 10:11:54 -05:00
me -> CombatStop ( true );
2009-01-28 20:16:11 -06:00
me -> LoadCreaturesAddon ();
me -> SetLootRecipient ( NULL );
2009-05-13 10:34:55 +02:00
me -> ResetDamageByPlayers ();
2009-01-28 20:16:11 -06:00
2009-05-13 10:54:11 -05:00
return true ;
}
void CreatureAI :: EnterEvadeMode ()
{
if ( ! _EnterEvadeMode ())
return ;
if ( Unit * owner = me -> GetCharmerOrOwner ())
me -> GetMotionMaster () -> MoveFollow ( owner , PET_FOLLOW_DIST , PET_FOLLOW_ANGLE , MOTION_SLOT_IDLE );
else
me -> GetMotionMaster () -> MoveTargetedHome ();
2009-05-11 11:46:48 -05:00
Reset ();
2009-01-08 21:48:38 -06:00
}
2009-04-14 21:07:52 -05:00
/*void CreatureAI::AttackedBy( Unit* attacker )
{
if(!m_creature->getVictim())
AttackStart(attacker);
}*/