2011-05-25 15:55:14 +02:00
/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
2008-10-14 11:57:03 -05:00
*
2011-05-25 15:55:14 +02: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.
2008-10-14 11:57:03 -05:00
*
2011-05-25 15:55:14 +02:00
* 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/>.
*/
2009-10-17 15:51:44 -07:00
2011-05-25 15:55:14 +02:00
#ifndef SCRIPTEDCREATURE_H_
#define SCRIPTEDCREATURE_H_
2009-10-17 15:51:44 -07:00
2009-05-10 16:07:49 -05:00
#include "CreatureAI.h"
2020-09-04 13:38:24 +02:00
#include "Creature.h" // convenience include for scripts, all uses of ScriptedCreature also need Creature (except ScriptedCreature itself doesn't need Creature)
2017-05-28 16:34:44 +02:00
#include "DBCEnums.h"
2015-08-23 14:43:11 +02:00
#include "TaskScheduler.h"
2009-10-17 15:51:44 -07:00
2010-08-08 22:54:58 +06:00
class InstanceScript ;
2020-06-12 01:29:18 +02:00
enum SelectTargetType : uint8 ;
enum SelectEffect : uint8 ;
2009-10-17 15:51:44 -07:00
2016-03-11 16:37:14 +01:00
class TC_GAME_API SummonList
2008-11-03 10:16:06 -06:00
{
2013-03-21 11:06:05 +01:00
public :
2014-09-14 16:14:12 +02:00
typedef GuidList StorageType ;
2013-03-21 11:06:05 +01:00
typedef StorageType :: iterator iterator ;
typedef StorageType :: const_iterator const_iterator ;
typedef StorageType :: size_type size_type ;
typedef StorageType :: value_type value_type ;
explicit SummonList ( Creature * creature )
: me ( creature )
{ }
// And here we see a problem of original inheritance approach. People started
// to exploit presence of std::list members, so I have to provide wrappers
iterator begin ()
{
return storage_ . begin ();
}
const_iterator begin () const
{
return storage_ . begin ();
}
iterator end ()
{
return storage_ . end ();
}
const_iterator end () const
{
return storage_ . end ();
}
iterator erase ( iterator i )
{
return storage_ . erase ( i );
}
bool empty () const
{
return storage_ . empty ();
}
size_type size () const
{
return storage_ . size ();
}
2016-09-26 12:49:32 +02:00
// Clear the underlying storage. This does NOT despawn the creatures - use DespawnAll for that!
void clear ()
{
storage_ . clear ();
}
2017-05-28 16:34:44 +02:00
void Summon ( Creature const * summon );
void Despawn ( Creature const * summon );
2013-03-21 11:06:05 +01:00
void DespawnEntry ( uint32 entry );
void DespawnAll ();
template < typename T >
2020-09-04 13:38:24 +02:00
void DespawnIf ( T const & predicate )
2013-03-21 11:06:05 +01:00
{
storage_ . remove_if ( predicate );
}
template < class Predicate >
2017-03-21 21:48:56 +01:00
void DoAction ( int32 info , Predicate && predicate , uint16 max = 0 )
2013-03-21 11:06:05 +01:00
{
// We need to use a copy of SummonList here, otherwise original SummonList would be modified
StorageType listCopy = storage_ ;
2017-03-23 00:11:58 +01:00
Trinity :: Containers :: RandomResize < StorageType , Predicate > ( listCopy , std :: forward < Predicate > ( predicate ), max );
2017-05-20 00:09:37 +02:00
DoActionImpl ( info , listCopy );
2013-03-21 11:06:05 +01:00
}
2012-04-08 17:40:05 +02:00
2018-08-22 21:51:16 +02:00
void DoZoneInCombat ( uint32 entry = 0 );
2013-03-21 11:06:05 +01:00
void RemoveNotExisting ();
bool HasEntry ( uint32 entry ) const ;
private :
2017-05-20 00:09:37 +02:00
void DoActionImpl ( int32 action , StorageType const & summons );
2013-03-21 11:06:05 +01:00
Creature * me ;
StorageType storage_ ;
2008-11-03 10:16:06 -06:00
};
2009-10-17 15:51:44 -07:00
2016-03-11 16:37:14 +01:00
class TC_GAME_API EntryCheckPredicate
2012-04-08 17:40:05 +02:00
{
public :
2013-10-27 22:27:46 +01:00
EntryCheckPredicate ( uint32 entry ) : _entry ( entry ) { }
2017-03-21 21:48:56 +01:00
bool operator ()( ObjectGuid const & guid ) const { return guid . GetEntry () == _entry ; }
2012-04-08 17:40:05 +02:00
private :
uint32 _entry ;
};
2016-03-11 16:37:14 +01:00
class TC_GAME_API DummyEntryCheckPredicate
2012-04-08 17:40:05 +02:00
{
public :
2017-03-21 21:48:56 +01:00
bool operator ()( ObjectGuid const & ) const { return true ; }
2012-04-08 17:40:05 +02:00
};
2016-03-11 16:37:14 +01:00
struct TC_GAME_API ScriptedAI : public CreatureAI
2008-10-02 16:23:55 -05:00
{
2011-03-19 10:54:16 +01:00
explicit ScriptedAI ( Creature * creature );
2021-10-22 12:51:45 +02:00
explicit ScriptedAI ( Creature * creature , uint32 scriptId );
2013-10-27 22:27:46 +01:00
virtual ~ ScriptedAI () { }
2009-10-17 15:51:44 -07:00
2011-01-16 12:23:17 +01:00
// *************
2008-10-02 16:23:55 -05:00
//CreatureAI Functions
2011-01-16 12:23:17 +01:00
// *************
2009-10-17 15:51:44 -07:00
2011-03-19 10:54:16 +01:00
void AttackStartNoMove ( Unit * target );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Called at World update tick
2014-08-09 20:17:40 +01:00
virtual void UpdateAI ( uint32 diff ) override ;
2009-10-17 15:51:44 -07:00
2011-01-16 12:23:17 +01:00
// *************
2008-10-02 16:23:55 -05:00
// Variables
2011-01-16 12:23:17 +01:00
// *************
2009-10-17 15:51:44 -07:00
2008-10-05 12:10:02 -05:00
//For fleeing
bool IsFleeing ;
2009-10-17 15:51:44 -07:00
2011-01-16 12:23:17 +01:00
// *************
2008-10-02 16:23:55 -05:00
//Pure virtual functions
2011-01-16 12:23:17 +01:00
// *************
2009-10-17 15:51:44 -07:00
2017-12-31 03:23:42 +01:00
// Called before JustEngagedWith even before the creature is in combat.
2014-08-09 20:17:40 +01:00
void AttackStart ( Unit * /*target*/ ) override ;
2013-02-03 14:46:17 -03:00
2011-01-16 12:23:17 +01:00
// *************
2008-10-02 16:23:55 -05:00
//AI Helper Functions
2011-01-16 12:23:17 +01:00
// *************
2009-10-17 15:51:44 -07:00
2008-10-25 11:40:10 -05:00
//Start movement toward victim
2011-03-19 10:54:16 +01:00
void DoStartMovement ( Unit * target , float distance = 0.0f , float angle = 0.0f );
2009-10-17 15:51:44 -07:00
2008-10-25 11:40:10 -05:00
//Start no movement on victim
2011-03-19 10:54:16 +01:00
void DoStartNoMovement ( Unit * target );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Stop attack of current victim
void DoStopAttack ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Cast spell by spell info
2011-07-26 23:09:28 +02:00
void DoCastSpell ( Unit * target , SpellInfo const * spellInfo , bool triggered = false );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Plays a sound to all nearby players
2011-03-19 10:54:16 +01:00
void DoPlaySoundToSet ( WorldObject * source , uint32 soundId );
2009-10-17 15:51:44 -07:00
2017-07-01 20:18:02 +02:00
// Add specified amount of threat directly to victim (ignores redirection effects) - also puts victim in combat and engages them if necessary
void AddThreat ( Unit * victim , float amount , Unit * who = nullptr );
// Adds/removes the specified percentage from the specified victim's threat (to who, or me if not specified)
void ModifyThreatByPercent ( Unit * victim , int32 pct , Unit * who = nullptr );
// Resets the victim's threat level to who (or me if not specified) to zero
void ResetThreat ( Unit * victim , Unit * who = nullptr );
// Resets the specified unit's threat list (me if not specified) - does not delete entries, just sets their threat to zero
void ResetThreatList ( Unit * who = nullptr );
// Returns the threat level of victim towards who (or me if not specified)
float GetThreat ( Unit const * victim , Unit const * who = nullptr );
2009-10-17 15:51:44 -07:00
2011-03-19 10:54:16 +01:00
void DoTeleportTo ( float x , float y , float z , uint32 time = 0 );
void DoTeleportTo ( float const pos [ 4 ]);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Teleports a player without dropping threat (only teleports to same map)
2011-03-19 10:54:16 +01:00
void DoTeleportPlayer ( Unit * unit , float x , float y , float z , float o );
void DoTeleportAll ( float x , float y , float z , float o );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Returns friendly unit with the most amount of hp missing from max hp
2011-03-19 10:54:16 +01:00
Unit * DoSelectLowestHpFriendly ( float range , uint32 minHPDiff = 1 );
2009-10-17 15:51:44 -07:00
2016-11-27 20:28:06 -02:00
//Returns friendly unit with hp pct below specified and with specified entry
Unit * DoSelectBelowHpPctFriendlyWithEntry ( uint32 entry , float range , uint8 hpPct = 1 , bool excludeSelf = true );
2008-10-02 16:23:55 -05:00
//Returns a list of friendly CC'd units within range
2011-03-19 10:54:16 +01:00
std :: list < Creature *> DoFindFriendlyCC ( float range );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Returns a list of all friendly units missing a specific buff within range
2011-03-19 10:54:16 +01:00
std :: list < Creature *> DoFindFriendlyMissingBuff ( float range , uint32 spellId );
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
//Return a player with at least minimumRange from me
2011-03-19 10:54:16 +01:00
Player * GetPlayerAtMinimumRange ( float minRange );
2009-10-17 15:51:44 -07:00
2010-04-14 23:07:41 +02:00
//Spawns a creature relative to me
2011-03-19 10:54:16 +01:00
Creature * DoSpawnCreature ( uint32 entry , float offsetX , float offsetY , float offsetZ , float angle , uint32 type , uint32 despawntime );
2009-10-17 15:51:44 -07:00
2017-05-28 16:34:44 +02:00
bool HealthBelowPct ( uint32 pct ) const ;
bool HealthAbovePct ( uint32 pct ) const ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//Returns spells that meet the specified criteria from the creatures spell list
2015-03-08 13:31:57 +01:00
SpellInfo const * SelectSpell ( Unit * target , uint32 school , uint32 mechanic , SelectTargetType targets , float rangeMin , float rangeMax , SelectEffect effect );
2009-10-17 15:51:44 -07:00
2011-03-19 10:54:16 +01:00
void SetEquipmentSlots ( bool loadDefault , int32 mainHand = EQUIP_NO_CHANGE , int32 offHand = EQUIP_NO_CHANGE , int32 ranged = EQUIP_NO_CHANGE );
2009-10-17 15:51:44 -07:00
2013-02-03 14:46:17 -03:00
// Used to control if MoveChase() is to be used or not in AttackStart(). Some creatures does not chase victims
// NOTE: If you use SetCombatMovement while the creature is in combat, it will do NOTHING - This only affects AttackStart
// You should make the necessary to make it happen so.
// Remember that if you modified _isCombatMovementAllowed (e.g: using SetCombatMovement) it will not be reset at Reset().
// It will keep the last value you set.
2011-03-19 10:54:16 +01:00
void SetCombatMovement ( bool allowMovement );
2012-06-20 21:29:41 +02:00
bool IsCombatMovementAllowed () const { return _isCombatMovementAllowed ; }
2009-10-17 15:51:44 -07:00
2010-01-12 20:51:59 +01:00
// return true for heroic mode. i.e.
2010-04-07 18:09:10 +02:00
// - for dungeon in mode 10-heroic,
// - for raid in mode 10-Heroic
2010-01-12 20:51:59 +01:00
// - for raid in mode 25-heroic
// DO NOT USE to check raid in mode 25-normal.
2012-06-20 21:29:41 +02:00
bool IsHeroic () const { return _isHeroic ; }
2010-01-12 20:51:59 +01:00
// return the dungeon or raid difficulty
2012-06-20 21:29:41 +02:00
Difficulty GetDifficulty () const { return _difficulty ; }
2010-01-12 20:51:59 +01:00
2010-08-21 20:50:38 +02:00
// return true for 25 man or 25 man heroic mode
2015-01-10 01:35:47 +01:00
bool Is25ManRaid () const { return _difficulty == DIFFICULTY_25_N || _difficulty == DIFFICULTY_25_HC ; }
2010-08-21 20:50:38 +02:00
2018-02-10 16:43:01 -03:00
template < class T >
inline T const & DUNGEON_MODE ( T const & normal5 , T const & heroic10 ) const
2010-01-12 20:51:59 +01:00
{
2011-03-19 10:54:16 +01:00
switch ( _difficulty )
2010-01-12 20:51:59 +01:00
{
2014-11-16 14:20:01 +01:00
case DIFFICULTY_NORMAL :
2011-03-19 10:54:16 +01:00
return normal5 ;
2014-11-16 14:20:01 +01:00
case DIFFICULTY_HEROIC :
2011-03-19 10:54:16 +01:00
return heroic10 ;
default :
break ;
2010-01-12 20:51:59 +01:00
}
return heroic10 ;
}
2018-02-10 16:43:01 -03:00
template < class T >
inline T const & RAID_MODE ( T const & normal10 , T const & normal25 ) const
2010-01-12 20:51:59 +01:00
{
2011-03-19 10:54:16 +01:00
switch ( _difficulty )
2010-01-12 20:51:59 +01:00
{
2014-11-16 14:20:01 +01:00
case DIFFICULTY_10_N :
2011-03-19 10:54:16 +01:00
return normal10 ;
2014-11-16 14:20:01 +01:00
case DIFFICULTY_25_N :
2011-03-19 10:54:16 +01:00
return normal25 ;
default :
break ;
2010-01-12 20:51:59 +01:00
}
return normal25 ;
}
2018-02-10 16:43:01 -03:00
template < class T >
inline T const & RAID_MODE ( T const & normal10 , T const & normal25 , T const & heroic10 , T const & heroic25 ) const
2010-01-12 20:51:59 +01:00
{
2011-03-19 10:54:16 +01:00
switch ( _difficulty )
2010-01-12 20:51:59 +01:00
{
2014-11-16 14:20:01 +01:00
case DIFFICULTY_10_N :
2011-03-19 10:54:16 +01:00
return normal10 ;
2014-11-16 14:20:01 +01:00
case DIFFICULTY_25_N :
2011-03-19 10:54:16 +01:00
return normal25 ;
2014-11-16 14:20:01 +01:00
case DIFFICULTY_10_HC :
2011-03-19 10:54:16 +01:00
return heroic10 ;
2014-11-16 14:20:01 +01:00
case DIFFICULTY_25_HC :
2011-03-19 10:54:16 +01:00
return heroic25 ;
default :
break ;
2010-01-12 20:51:59 +01:00
}
return heroic25 ;
}
2009-08-08 21:29:12 +02:00
private :
2011-03-19 10:54:16 +01:00
Difficulty _difficulty ;
bool _isCombatMovementAllowed ;
bool _isHeroic ;
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2016-03-11 16:37:14 +01:00
class TC_GAME_API BossAI : public ScriptedAI
2009-05-10 22:48:22 -05:00
{
2011-03-19 10:54:16 +01:00
public :
BossAI ( Creature * creature , uint32 bossId );
2013-10-27 22:27:46 +01:00
virtual ~ BossAI () { }
2009-10-17 15:51:44 -07:00
2011-03-19 10:54:16 +01:00
InstanceScript * const instance ;
2009-10-17 15:51:44 -07:00
2014-08-09 20:17:40 +01:00
void JustSummoned ( Creature * summon ) override ;
void SummonedCreatureDespawn ( Creature * summon ) override ;
2009-10-17 15:51:44 -07:00
2014-08-09 20:17:40 +01:00
virtual void UpdateAI ( uint32 diff ) override ;
2011-05-25 18:50:44 +02:00
// Hook used to execute events scheduled into EventMap without the need
// to override UpdateAI
// note: You must re-schedule the event within this method if the event
// is supposed to run more than once
2013-02-19 17:35:27 +01:00
virtual void ExecuteEvent ( uint32 /*eventId*/ ) { }
2009-10-17 15:51:44 -07:00
2015-08-23 14:43:11 +02:00
virtual void ScheduleTasks () { }
2014-08-09 20:17:40 +01:00
void Reset () override { _Reset (); }
2017-12-31 03:23:42 +01:00
void JustEngagedWith ( Unit * /*who*/ ) override { _JustEngagedWith (); }
2014-08-09 20:17:40 +01:00
void JustDied ( Unit * /*killer*/ ) override { _JustDied (); }
void JustReachedHome () override { _JustReachedHome (); }
2009-10-17 15:51:44 -07:00
2017-05-28 16:34:44 +02:00
bool CanAIAttack ( Unit const * target ) const override ;
2016-02-07 03:23:36 +01:00
2009-05-17 16:37:03 -05:00
protected :
void _Reset ();
2017-12-31 03:23:42 +01:00
void _JustEngagedWith ();
2009-05-17 16:37:03 -05:00
void _JustDied ();
2017-05-28 16:34:44 +02:00
void _JustReachedHome ();
2020-09-04 13:38:24 +02:00
void _DespawnAtEvade ( Seconds delayToRespawn , Creature * who = nullptr );
2017-05-07 21:48:41 +01:00
void _DespawnAtEvade ( uint32 delayToRespawn = 30 , Creature * who = nullptr ) { _DespawnAtEvade ( Seconds ( delayToRespawn ), who ); }
2009-10-17 15:51:44 -07:00
2009-05-26 19:55:41 -05:00
void TeleportCheaters ();
2011-03-19 10:54:16 +01:00
EventMap events ;
SummonList summons ;
2015-08-23 14:43:11 +02:00
TaskScheduler scheduler ;
2011-03-19 10:54:16 +01:00
private :
2011-05-25 18:50:44 +02:00
uint32 const _bossId ;
2009-05-10 22:48:22 -05:00
};
2009-10-17 15:51:44 -07:00
2016-03-11 16:37:14 +01:00
class TC_GAME_API WorldBossAI : public ScriptedAI
2011-05-28 00:28:04 +02:00
{
public :
WorldBossAI ( Creature * creature );
2013-10-27 22:27:46 +01:00
virtual ~ WorldBossAI () { }
2011-05-28 00:28:04 +02:00
2014-08-09 20:17:40 +01:00
void JustSummoned ( Creature * summon ) override ;
void SummonedCreatureDespawn ( Creature * summon ) override ;
2011-05-28 00:28:04 +02:00
2014-08-09 20:17:40 +01:00
virtual void UpdateAI ( uint32 diff ) override ;
2011-05-28 00:28:04 +02:00
// Hook used to execute events scheduled into EventMap without the need
// to override UpdateAI
// note: You must re-schedule the event within this method if the event
// is supposed to run more than once
2013-02-19 17:35:27 +01:00
virtual void ExecuteEvent ( uint32 /*eventId*/ ) { }
2011-05-28 00:28:04 +02:00
2014-08-09 20:17:40 +01:00
void Reset () override { _Reset (); }
2017-12-31 03:23:42 +01:00
void JustEngagedWith ( Unit * /*who*/ ) override { _JustEngagedWith (); }
2014-08-09 20:17:40 +01:00
void JustDied ( Unit * /*killer*/ ) override { _JustDied (); }
2011-05-28 00:28:04 +02:00
protected :
void _Reset ();
2017-12-31 03:23:42 +01:00
void _JustEngagedWith ();
2011-05-28 00:28:04 +02:00
void _JustDied ();
EventMap events ;
SummonList summons ;
};
2009-11-22 00:46:38 -08:00
// SD2 grid searchers.
2016-10-13 22:44:59 -03:00
inline Creature * GetClosestCreatureWithEntry ( WorldObject * source , uint32 entry , float maxSearchRange , bool alive = true )
{
return source -> FindNearestCreature ( entry , maxSearchRange , alive );
}
inline GameObject * GetClosestGameObjectWithEntry ( WorldObject * source , uint32 entry , float maxSearchRange )
{
return source -> FindNearestGameObject ( entry , maxSearchRange );
}
template < typename Container >
inline void GetCreatureListWithEntryInGrid ( Container & container , WorldObject * source , uint32 entry , float maxSearchRange )
{
source -> GetCreatureListWithEntryInGrid ( container , entry , maxSearchRange );
}
template < typename Container >
inline void GetGameObjectListWithEntryInGrid ( Container & container , WorldObject * source , uint32 entry , float maxSearchRange )
{
source -> GetGameObjectListWithEntryInGrid ( container , entry , maxSearchRange );
}
template < typename Container >
inline void GetPlayerListInGrid ( Container & container , WorldObject * source , float maxSearchRange )
{
source -> GetPlayerListInGrid ( container , maxSearchRange );
}
2009-10-17 15:51:44 -07:00
2011-05-25 15:55:14 +02:00
#endif // SCRIPTEDCREATURE_H_