TrinityCore
Loading...
Searching...
No Matches
UnitAI.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_UNITAI_H
19#define TRINITY_UNITAI_H
20
21#include "Errors.h"
22#include "Hash.h"
23#include "ObjectGuid.h"
24#include "SharedDefines.h"
25#include "SpellDefines.h"
26#include "UnitAICommon.h"
27#include <unordered_map>
28
29#define CAST_AI(a, b) (dynamic_cast<a*>(b))
30#define ENSURE_AI(a,b) (EnsureAI<a>(b))
31
32template<class T, class U>
33T* EnsureAI(U* ai)
34{
35 T* cast_ai = dynamic_cast<T*>(ai);
36 ASSERT(cast_ai);
37 return cast_ai;
38}
39
40class Player;
41class Quest;
42class SpellInfo;
43class Unit;
44struct AISpellInfoType;
46enum Difficulty : int16;
48enum SpellEffIndex : uint8;
49
51{
52 protected:
53 Unit* const me;
54 public:
55 explicit UnitAI(Unit* unit) : me(unit) { }
56 virtual ~UnitAI() { }
57
58 virtual bool CanAIAttack(Unit const* /*target*/) const { return true; }
59 virtual void AttackStart(Unit* victim);
60 virtual void UpdateAI(uint32 diff) = 0;
61
62 virtual void InitializeAI();
63
64 virtual void Reset() { }
65
66 // Called when unit's charm state changes with isNew = false
67 // Implementation should call me->ScheduleAIChange() if AI replacement is desired
68 // If this call is made, AI will be replaced on the next tick
69 // When replacement is made, OnCharmed is called with isNew = true
70 virtual void OnCharmed(bool isNew);
71
72 // Pass parameters between AI
73 virtual void DoAction([[maybe_unused]] int32 param) { }
74 virtual uint32 GetData([[maybe_unused]] uint32 id) const { return 0; }
75 virtual void SetData([[maybe_unused]] uint32 id, [[maybe_unused]] uint32 value) { }
76 virtual void SetGUID([[maybe_unused]] ObjectGuid const& guid, [[maybe_unused]] int32 id) { }
77 virtual ObjectGuid GetGUID([[maybe_unused]] int32 id) const { return ObjectGuid::Empty; }
78
79 // Select the best target (in <targetType> order) from the threat list that fulfill the following:
80 // - Not among the first <offset> entries in <targetType> order (or SelectTargetMethod::MaxThreat order,
81 // if <targetType> is SelectTargetMethod::Random).
82 // - Within at most <dist> yards (if dist > 0.0f)
83 // - At least -<dist> yards away (if dist < 0.0f)
84 // - Is a player (if playerOnly = true)
85 // - Not the current tank (if withTank = false)
86 // - Has aura with ID <aura> (if aura > 0)
87 // - Does not have aura with ID -<aura> (if aura < 0)
88 Unit* SelectTarget(SelectTargetMethod targetType, uint32 offset = 0, float dist = 0.0f, bool playerOnly = false, bool withTank = true, int32 aura = 0);
89
90 // Select the best target (in <targetType> order) satisfying <predicate> from the threat list.
91 // If <offset> is nonzero, the first <offset> entries in <targetType> order (or SelectTargetMethod::MaxThreat
92 // order, if <targetType> is SelectTargetMethod::Random) are skipped.
93 template<class PREDICATE>
94 Unit* SelectTarget(SelectTargetMethod targetType, uint32 offset, PREDICATE const& predicate)
95 {
96 std::list<Unit*> targetList;
97 SelectTargetList(targetList, std::numeric_limits<uint32>::max(), targetType, offset, predicate);
98
99 return FinalizeTargetSelection(targetList, targetType);
100 }
101
102 // Select the best (up to) <num> targets (in <targetType> order) from the threat list that fulfill the following:
103 // - Not among the first <offset> entries in <targetType> order (or SelectTargetMethod::MaxThreat order,
104 // if <targetType> is SelectTargetMethod::Random).
105 // - Within at most <dist> yards (if dist > 0.0f)
106 // - At least -<dist> yards away (if dist < 0.0f)
107 // - Is a player (if playerOnly = true)
108 // - Not the current tank (if withTank = false)
109 // - Has aura with ID <aura> (if aura > 0)
110 // - Does not have aura with ID -<aura> (if aura < 0)
111 // The resulting targets are stored in <targetList> (which is cleared first).
112 void SelectTargetList(std::list<Unit*>& targetList, uint32 num, SelectTargetMethod targetType, uint32 offset = 0, float dist = 0.0f, bool playerOnly = false, bool withTank = true, int32 aura = 0);
113
114 // Select the best (up to) <num> targets (in <targetType> order) satisfying <predicate> from the threat list and stores them in <targetList> (which is cleared first).
115 // If <offset> is nonzero, the first <offset> entries in <targetType> order (or SelectTargetMethod::MaxThreat
116 // order, if <targetType> is SelectTargetMethod::Random) are skipped.
117 template <class PREDICATE>
118 void SelectTargetList(std::list<Unit*>& targetList, uint32 num, SelectTargetMethod targetType, uint32 offset, PREDICATE const& predicate)
119 {
120 if (!PrepareTargetListSelection(targetList, targetType, offset))
121 return;
122
123 // then finally filter by predicate
124 targetList.remove_if([&predicate](Unit* target) { return !predicate(target); });
125
126 FinalizeTargetListSelection(targetList, num, targetType);
127 }
128
129 // Called when the unit enters combat
130 // (NOTE: Creature engage logic should NOT be here, but in JustEngagedWith, which happens once threat is established!)
131 virtual void JustEnteredCombat(Unit* /*who*/) { }
132
133 // Called when the unit leaves combat
134 virtual void JustExitedCombat() { }
135
136 // Called when the unit is about to be removed from the world (despawn, grid unload, corpse disappearing, player logging out etc.)
137 virtual void OnDespawn() { }
138
139 // Called at any Damage to any victim (before damage apply)
140 virtual void DamageDealt(Unit* /*victim*/, uint32& /*damage*/, DamageEffectType /*damageType*/) { }
141
142 // Called at any Damage from any attacker (before damage apply)
143 // Note: it for recalculation damage or special reaction at damage
144 virtual void DamageTaken(Unit* /*attacker*/, uint32& /*damage*/, DamageEffectType /*damageType*/, SpellInfo const* /*spellInfo = nullptr*/) { }
145
146 // Called when the creature receives heal
147 virtual void HealReceived(Unit* /*done_by*/, uint32& /*addhealth*/) { }
148
149 // Called when the unit heals
150 virtual void HealDone(Unit* /*done_to*/, uint32& /*addhealth*/) { }
151
154 virtual void SpellInterrupted(uint32 /*spellId*/, uint32 /*unTimeMs*/) { }
155
156 void AttackStartCaster(Unit* victim, float dist);
157
158 SpellCastResult DoCast(uint32 spellId);
159 SpellCastResult DoCast(Unit* victim, uint32 spellId, CastSpellExtraArgs const& args = {});
160 SpellCastResult DoCastSelf(uint32 spellId, CastSpellExtraArgs const& args = {}) { return DoCast(me, spellId, args); }
161 SpellCastResult DoCastVictim(uint32 spellId, CastSpellExtraArgs const& args = {});
162 SpellCastResult DoCastAOE(uint32 spellId, CastSpellExtraArgs const& args = {}) { return DoCast(nullptr, spellId, args); }
163
164 bool DoSpellAttackIfReady(uint32 spellId);
165
166 static std::unordered_map<std::pair<uint32, Difficulty>, AISpellInfoType> AISpellInfo;
167 static void FillAISpellInfo();
168
169 // Called when a game event starts or ends
170 virtual void OnGameEvent(bool /*start*/, uint16 /*eventId*/) { }
171
172 virtual std::string GetDebugInfo() const;
173
174 private:
175 UnitAI(UnitAI const& right) = delete;
176 UnitAI& operator=(UnitAI const& right) = delete;
177
178 Unit* FinalizeTargetSelection(std::list<Unit*>& targetList, SelectTargetMethod targetType);
179 bool PrepareTargetListSelection(std::list<Unit*>& targetList, SelectTargetMethod targetType, uint32 offset);
180 void FinalizeTargetListSelection(std::list<Unit*>& targetList, uint32 num, SelectTargetMethod targetType);
181};
182
183#endif
Difficulty
Definition DBCEnums.h:932
#define TC_GAME_API
Definition Define.h:129
uint8_t uint8
Definition Define.h:156
int16_t int16
Definition Define.h:151
int32_t int32
Definition Define.h:150
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
std::string GetDebugInfo()
Definition Errors.cpp:170
#define ASSERT
Definition Errors.h:80
MovementGeneratorType
SpellEffIndex
SpellCastResult
SelectTargetMethod
T * EnsureAI(U *ai)
Definition UnitAI.h:33
DamageEffectType
static ObjectGuid const Empty
Definition ObjectGuid.h:314
virtual void HealReceived(Unit *, uint32 &)
Definition UnitAI.h:147
virtual void SetData(uint32 id, uint32 value)
Definition UnitAI.h:75
virtual void JustEnteredCombat(Unit *)
Definition UnitAI.h:131
virtual void SpellInterrupted(uint32, uint32)
Definition UnitAI.h:154
virtual ~UnitAI()
Definition UnitAI.h:56
virtual void Reset()
Definition UnitAI.h:64
SpellCastResult DoCastSelf(uint32 spellId, CastSpellExtraArgs const &args={})
Definition UnitAI.h:160
UnitAI(Unit *unit)
Definition UnitAI.h:55
Unit * SelectTarget(SelectTargetMethod targetType, uint32 offset, PREDICATE const &predicate)
Definition UnitAI.h:94
virtual void SetGUID(ObjectGuid const &guid, int32 id)
Definition UnitAI.h:76
virtual bool CanAIAttack(Unit const *) const
Definition UnitAI.h:58
void SelectTargetList(std::list< Unit * > &targetList, uint32 num, SelectTargetMethod targetType, uint32 offset, PREDICATE const &predicate)
Definition UnitAI.h:118
virtual void DoAction(int32 param)
Definition UnitAI.h:73
static std::unordered_map< std::pair< uint32, Difficulty >, AISpellInfoType > AISpellInfo
Definition UnitAI.h:166
UnitAI & operator=(UnitAI const &right)=delete
virtual void DamageTaken(Unit *, uint32 &, DamageEffectType, SpellInfo const *)
Definition UnitAI.h:144
virtual void OnGameEvent(bool, uint16)
Definition UnitAI.h:170
virtual ObjectGuid GetGUID(int32 id) const
Definition UnitAI.h:77
virtual void OnDespawn()
Definition UnitAI.h:137
virtual void JustExitedCombat()
Definition UnitAI.h:134
Unit *const me
Definition UnitAI.h:53
virtual void HealDone(Unit *, uint32 &)
Definition UnitAI.h:150
virtual void DamageDealt(Unit *, uint32 &, DamageEffectType)
Definition UnitAI.h:140
virtual void UpdateAI(uint32 diff)=0
UnitAI(UnitAI const &right)=delete
SpellCastResult DoCastAOE(uint32 spellId, CastSpellExtraArgs const &args={})
Definition UnitAI.h:162
virtual uint32 GetData(uint32 id) const
Definition UnitAI.h:74
Definition Unit.h:635