2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_CREATUREAI_H
20 #define MANGOS_CREATUREAI_H
23 #include "Platform/Define.h"
24 #include "Policies/Singleton.h"
25 #include "Dynamic/ObjectRegistry.h"
26 #include "Dynamic/FactoryHolder.h"
34 #define TIME_INTERVAL_LOOK 5000
35 #define VISIBILITY_RANGE 10000
40 CAST_FAIL_IS_CASTING
= 1,
42 CAST_FAIL_TOO_FAR
= 3,
43 CAST_FAIL_TOO_CLOSE
= 4,
46 CAST_FAIL_TARGET_AURA
= 7
51 CAST_INTURRUPT_PREVIOUS
= 0x01, //Interrupt any spell casting
52 CAST_TRIGGERED
= 0x02, //Triggered (this makes spell cost zero mana and have no cast time)
53 CAST_FORCE_CAST
= 0x04, //Forces cast even if creature is out of mana or out of range
54 CAST_NO_MELEE_IF_OOM
= 0x08, //Prevents creature from entering melee if out of mana or out of range
55 CAST_FORCE_TARGET_SELF
= 0x10, //Forces the target to cast this spell on itself
56 CAST_AURA_NOT_PRESENT
= 0x20, //Only casts the spell if the target does not have an aura from the spell
59 class MANGOS_DLL_SPEC CreatureAI
62 explicit CreatureAI(Creature
* creature
) : m_creature(creature
) {}
64 virtual ~CreatureAI();
66 ///== Reactions At =================================
68 // Called if IsVisible(Unit *who) is true at each *who move, reaction at visibility zone enter
69 virtual void MoveInLineOfSight(Unit
*) {}
71 // Called for reaction at enter to combat if not in combat yet (enemy can be NULL)
72 virtual void EnterCombat(Unit
* /*enemy*/) {}
74 // Called for reaction at stopping attack at no attackers or targets
75 virtual void EnterEvadeMode() {}
77 // Called at reaching home after evade
78 virtual void JustReachedHome() {}
80 // Called at any heal cast/item used (call non implemented)
81 virtual void HealBy(Unit
* /*healer*/, uint32
/*amount_healed*/) {}
83 // Helper functions for cast spell
84 CanCastResult
DoCastSpellIfCan(Unit
* pTarget
, uint32 uiSpell
, uint32 uiCastFlags
= 0, uint64 uiOriginalCasterGUID
= 0);
85 virtual CanCastResult
CanCastSpell(Unit
* pTarget
, const SpellEntry
*pSpell
, bool isTriggered
);
87 // Called at any Damage to any victim (before damage apply)
88 virtual void DamageDeal(Unit
* /*done_to*/, uint32
& /*damage*/) {}
90 // Called at any Damage from any attacker (before damage apply)
91 // Note: it for recalculation damage or special reaction at damage
92 // for attack reaction use AttackedBy called for not DOT damage in Unit::DealDamage also
93 virtual void DamageTaken(Unit
* /*done_by*/, uint32
& /*damage*/) {}
95 // Called when the creature is killed
96 virtual void JustDied(Unit
*) {}
98 // Called when the creature summon is killed
99 virtual void SummonedCreatureJustDied(Creature
* /*unit*/) {}
101 // Called when the creature kills a unit
102 virtual void KilledUnit(Unit
*) {}
104 // Called when the creature summon successfully other creature
105 virtual void JustSummoned(Creature
* ) {}
107 // Called when the creature summon despawn
108 virtual void SummonedCreatureDespawn(Creature
* /*unit*/) {}
110 // Called when hit by a spell
111 virtual void SpellHit(Unit
*, const SpellEntry
*) {}
113 // Called when spell hits creature's target
114 virtual void SpellHitTarget(Unit
*, const SpellEntry
*) {}
116 // Called when the creature is target of hostile action: swing, hostile spell landed, fear/etc)
117 virtual void AttackedBy(Unit
* attacker
);
119 // Called when creature is spawned or respawned (for reseting variables)
120 virtual void JustRespawned() {}
122 // Called at waypoint reached or point movement finished
123 virtual void MovementInform(uint32
/*MovementType*/, uint32
/*Data*/) {}
125 // Called if a temporary summoned of m_creature reach a move point
126 virtual void SummonedMovementInform(Creature
* /*summoned*/, uint32
/*motion_type*/, uint32
/*point_id*/) {}
128 // Called at text emote receive from player
129 virtual void ReceiveEmote(Player
* /*pPlayer*/, uint32
/*text_emote*/) {}
131 ///== Triggered Actions Requested ==================
133 // Called when creature attack expected (if creature can and no have current victim)
134 // Note: for reaction at hostile action must be called AttackedBy function.
135 virtual void AttackStart(Unit
*) {}
137 // Called at World update tick
138 virtual void UpdateAI(const uint32
/*diff*/) {}
140 ///== State checks =================================
142 // Is unit visible for MoveInLineOfSight
143 virtual bool IsVisible(Unit
*) const { return false; }
145 // called when the corpse of this creature gets removed
146 virtual void CorpseRemoved(uint32
& /*respawnDelay*/) {}
148 // Called when victim entered water and creature can not enter water
149 virtual bool canReachByRangeAttack(Unit
*) { return false; }
151 ///== Fields =======================================
153 // Pointer to controlled by AI creature
154 Creature
* const m_creature
;
157 struct SelectableAI
: public FactoryHolder
<CreatureAI
>, public Permissible
<Creature
>
160 SelectableAI(const char *id
) : FactoryHolder
<CreatureAI
>(id
) {}
163 template<class REAL_AI
>
164 struct CreatureAIFactory
: public SelectableAI
166 CreatureAIFactory(const char *name
) : SelectableAI(name
) {}
168 CreatureAI
* Create(void *) const;
170 int Permit(const Creature
*c
) const { return REAL_AI::Permissible(c
); }
176 PERMIT_BASE_IDLE
= 1,
177 PERMIT_BASE_REACTIVE
= 100,
178 PERMIT_BASE_PROACTIVE
= 200,
179 PERMIT_BASE_FACTION_SPECIFIC
= 400,
180 PERMIT_BASE_SPECIAL
= 800
183 typedef FactoryHolder
<CreatureAI
> CreatureAICreator
;
184 typedef FactoryHolder
<CreatureAI
>::FactoryHolderRegistry CreatureAIRegistry
;
185 typedef FactoryHolder
<CreatureAI
>::FactoryHolderRepository CreatureAIRepository
;