2010-10-07 15:35:36 +02:00
/*
2011-01-01 15:01:13 +01:00
* Copyright (C) 2008-2011 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"
2009-10-17 15:51:44 -07:00
2009-03-05 18:48:56 -06:00
//Disable CreatureAI when charmed
2010-04-19 09:26:37 +02:00
void CreatureAI :: OnCharmed ( bool /*apply*/ )
2009-03-13 17:48:57 -06:00
{
//me->IsAIEnabled = !apply;*/
me -> NeedChangeAI = true ;
me -> IsAIEnabled = false ;
}
2009-10-17 15:51:44 -07:00
2009-05-20 11:44:38 -05:00
AISpellInfoType * UnitAI :: AISpellInfo ;
2010-02-05 00:31:28 +01:00
AISpellInfoType * GetAISpellInfo ( uint32 i ) { return & CreatureAI :: AISpellInfo [ i ]; }
2009-10-17 15:51:44 -07:00
2010-11-16 19:21:48 +01:00
void CreatureAI :: Talk ( uint8 id , uint64 WhisperGuid )
{
2010-12-22 21:25:23 +01:00
sCreatureTextMgr -> SendChat ( me , id , WhisperGuid );
2010-11-16 19:21:48 +01:00
}
2011-03-19 19:41:42 +01:00
void CreatureAI :: DoZoneInCombat ( Creature * creature /*= NULL*/ )
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
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
{
2010-12-23 23:25:44 +01:00
sLog -> outError ( "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
2010-04-07 19:14:10 +02:00
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> getVictim ())
2009-04-30 22:26:51 -05:00
{
2011-06-12 01:47:45 +02:00
if ( Unit * target = creature -> SelectNearestTarget ( 50 ))
2009-05-09 14:08:11 -05:00
creature -> AI () -> AttackStart ( target );
2010-04-07 19:14:10 +02:00
else if ( creature -> isSummon ())
2009-04-30 22:26:51 -05:00
{
2010-04-07 19:14:10 +02: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
2010-04-07 19:14:10 +02:00
if ( ! creature -> HasReactState ( REACT_PASSIVE ) && ! creature -> getVictim ())
2009-04-07 19:38:09 -05:00
{
2010-12-23 23:25:44 +01: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-10-17 15:51:44 -07:00
2009-05-27 21:30:03 +02:00
Map :: PlayerList const & PlList = map -> GetPlayers ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( PlList . isEmpty ())
2009-05-27 21:30:03 +02:00
return ;
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for ( Map :: PlayerList :: const_iterator i = PlList . begin (); i != PlList . end (); ++ i )
2009-04-07 19:38:09 -05:00
{
2010-04-07 19:14:10 +02:00
if ( Player * pPlayer = i -> getSource ())
2009-05-13 10:54:11 -05:00
{
2010-04-07 19:14:10 +02:00
if ( pPlayer -> isGameMaster ())
2009-05-27 21:30:03 +02:00
continue ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( pPlayer -> isAlive ())
2009-05-27 21:30:03 +02:00
{
creature -> SetInCombatWith ( pPlayer );
pPlayer -> SetInCombatWith ( creature );
creature -> AddThreat ( pPlayer , 0.0f );
}
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):
2009-10-17 16:20:24 -07:00
for (Unit::ControlList::const_iterator itr = pPlayer->m_Controlled.begin(); itr != pPlayer->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
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 ;
}
2009-01-30 18:10:05 -06:00
void CreatureAI :: MoveInLineOfSight ( Unit * who )
2009-01-08 21:48:38 -06:00
{
2010-04-07 19:14:10 +02:00
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
2010-04-07 19:14:10 +02:00
if ( me -> canStartAttack ( who , false ))
2009-04-13 18:03:28 -05:00
AttackStart ( who );
2010-04-07 19:14:10 +02:00
//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))
2009-08-27 13:54:57 -05:00
// && me->canStartAttack(who->getVictim(), true)) // TODO: if we use true, it will not attack it when it arrives
// me->GetMotionMaster()->MoveChase(who->getVictim());
2009-01-30 18:10:05 -06:00
}
2009-10-17 15:51:44 -07:00
2009-05-13 10:54:11 -05:00
void CreatureAI :: EnterEvadeMode ()
{
2010-04-07 19:14:10 +02:00
if ( ! _EnterEvadeMode ())
2009-05-13 10:54:11 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-02-20 20:16:34 +01:00
sLog -> outDebug ( LOG_FILTER_UNITS , "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
{
2010-04-07 19:14:10 +02: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
me -> GetMotionMaster () -> MoveTargetedHome ();
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
{
2010-04-14 23:07:41 +02:00
if (!me->getVictim())
2009-04-14 21:07:52 -05:00
AttackStart(attacker);
}*/