TrinityCore
Loading...
Searching...
No Matches
SmartAI.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_SMARTAI_H
19#define TRINITY_SMARTAI_H
20
21#include "Define.h"
22#include "AreaTriggerAI.h"
23#include "CreatureAI.h"
24#include "GameObjectAI.h"
25#include "SmartScript.h"
26#include "WaypointDefines.h"
27
28enum class AreaTriggerExitReason : uint8;
29enum class MovementStopReason : uint8;
30
32{
33 SMART_ESCORT_NONE = 0x00, // nothing in progress
34 SMART_ESCORT_ESCORTING = 0x01, // escort is in progress
35 SMART_ESCORT_RETURNING = 0x02, // escort is returning after being in combat
36 SMART_ESCORT_PAUSED = 0x04 // will not proceed with waypoints before state is removed
37};
38
39static float constexpr SMART_ESCORT_MAX_PLAYER_DIST = 60.f;
40static float constexpr SMART_MAX_AID_DIST = SMART_ESCORT_MAX_PLAYER_DIST / 2.f;
41
43{
44 public:
46 explicit SmartAI(Creature* creature, uint32 scriptId = {});
47
48 // core related
49 static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; }
50
51 // Check whether we are currently permitted to make the creature take action
52 bool IsAIControlled() const;
53
54 // Start moving to the desired MovePoint
55 void StartPath(uint32 pathId = 0, bool repeat = false, Unit* invoker = nullptr, uint32 nodeId = 0,
56 uint32 fadeObjectDuration = 0, Scripting::v2::ActionResultSetter<MovementStopReason>&& scriptResult = {});
57 WaypointPath const* LoadPath(uint32 entry);
58 void PausePath(uint32 delay, bool forced = false);
59 bool CanResumePath();
60 void StopPath(uint32 DespawnTime = 0, uint32 quest = 0, bool fail = false);
61 void EndPath(bool fail = false);
62 void ResumePath();
63 bool HasEscortState(uint32 escortState) const
64 {
65 return (_escortState & escortState) != 0;
66 }
67 void AddEscortState(uint32 escortState)
68 {
69 _escortState |= escortState;
70 }
71 void RemoveEscortState(uint32 escortState)
72 {
73 _escortState &= ~escortState;
74 }
75 void SetCombatMove(bool on, bool stopMoving = false);
76 bool CanCombatMove() const
77 {
78 return _canCombatMove;
79 }
80 void SetFollow(Unit* target, float dist = 0.0f, float angle = 0.0f, uint32 credit = 0, uint32 end = 0, uint32 creditType = 0);
81 void StopFollow(bool complete);
82 bool IsEscortInvokerInRange();
83
84 void WaypointReached(uint32 nodeId, uint32 pathId) override;
85 void WaypointPathEnded(uint32 nodeId, uint32 pathId) override;
86
87 void SetTimedActionList(SmartScriptHolder& e, uint32 entry, Unit* invoker, uint32 startFromEventId = 0);
89 {
90 return &_script;
91 }
92
93 // Called at reaching home after evade, InitializeAI(), EnterEvadeMode() for resetting variables
94 void JustReachedHome() override;
95
96 // Called for reaction at enter to combat if not in combat yet (enemy can be nullptr)
97 void JustEngagedWith(Unit* enemy) override;
98
99 // Called for reaction at stopping attack at no attackers or targets
100 void EnterEvadeMode(EvadeReason why) override;
101
102 // Called when the creature is killed
103 void JustDied(Unit* killer) override;
104
105 // Called when the creature kills a unit
106 void KilledUnit(Unit* victim) override;
107
108 // Called when the creature summon successfully other creature
109 void JustSummoned(Creature* creature) override;
110
111 // Called when a summoned unit dies
112 void SummonedCreatureDies(Creature* summon, Unit* killer) override;
113
114 // Tell creature to attack and follow the victim
115 void AttackStart(Unit* who) override;
116
117 // Called if IsVisible(Unit* who) is true at each *who move, reaction at visibility zone enter
118 void MoveInLineOfSight(Unit* who) override;
119
120 // Called when hit by a spell
121 void SpellHit(WorldObject* caster, SpellInfo const* spellInfo) override;
122
123 // Called when spell hits a target
124 void SpellHitTarget(WorldObject* target, SpellInfo const* spellInfo) override;
125
126 // Called when a spell finishes
127 void OnSpellCast(SpellInfo const* spellInfo) override;
128
129 // Called when a spell fails
130 void OnSpellFailed(SpellInfo const* spellInfo) override;
131
132 // Called when a spell starts
133 void OnSpellStart(SpellInfo const* spellInfo) override;
134
135 // Called when aura is applied
136 void OnAuraApplied(AuraApplication const* aurApp) override;
137
138 // Called when aura is removed
139 void OnAuraRemoved(AuraApplication const* aurApp) override;
140
141 // Called at any Damage from any attacker (before damage apply)
142 void DamageTaken(Unit* doneBy, uint32& damage, DamageEffectType /*damageType*/, SpellInfo const* /*spellInfo = nullptr*/) override;
143
144 // Called when the creature receives heal
145 void HealReceived(Unit* doneBy, uint32& addhealth) override;
146
147 // Called at World update tick
148 void UpdateAI(uint32 diff) override;
149
150 // Called at text emote receive from player
151 void ReceiveEmote(Player* player, uint32 textEmote) override;
152
153 // Called at waypoint reached or point movement finished
154 void MovementInform(uint32 MovementType, uint32 Data) override;
155
156 // Called when creature is summoned by another unit
157 void IsSummonedBy(WorldObject* summoner) override;
158
159 // Called at any Damage to any victim (before damage apply)
160 void DamageDealt(Unit* doneTo, uint32& damage, DamageEffectType /*damagetype*/) override;
161
162 // Called when a summoned creature dissapears (UnSommoned)
163 void SummonedCreatureDespawn(Creature* unit) override;
164
165 // called when the corpse of this creature gets removed
166 void CorpseRemoved(uint32& respawnDelay) override;
167
168 // Called when the unit is about to be removed from the world (despawn, grid unload, corpse disappearing)
169 void OnDespawn() override;
170
171 // Called when a Player/Creature enters the creature (vehicle)
172 void PassengerBoarded(Unit* who, int8 seatId, bool apply) override;
173
174 // Called when gets initialized
175 void InitializeAI() override;
176
177 // Called once creature is fully added to world
178 void JustAppeared() override;
179
180 // Called when creature gets charmed by another unit
181 void OnCharmed(bool isNew) override;
182
183 // Used in scripts to share variables
184 void DoAction(int32 param) override;
185
186 // Used in scripts to share variables
187 uint32 GetData(uint32 id) const override;
188
189 // Used in scripts to share variables
190 void SetData(uint32 id, uint32 value) override { SetData(id, value, nullptr); }
191 void SetData(uint32 id, uint32 value, Unit* invoker);
192
193 // Used in scripts to share variables
194 void SetGUID(ObjectGuid const& guid, int32 id) override;
195
196 // Used in scripts to share variables
197 ObjectGuid GetGUID(int32 id) const override;
198
199 // Makes the creature run/walk
200 void SetRun(bool run = true);
201
202 void SetEvadeDisabled(bool disable = true);
203
205 {
206 _invincibilityHPLevel = level;
207 }
208
209 bool OnGossipHello(Player* player) override;
210 bool OnGossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override;
211 bool OnGossipSelectCode(Player* player, uint32 menuId, uint32 gossipListId, char const* code) override;
212 void OnQuestAccept(Player* player, Quest const* quest) override;
213 void OnQuestReward(Player* player, Quest const* quest, LootItemType type, uint32 opt) override;
214 void OnGameEvent(bool start, uint16 eventId) override;
215
217 {
218 _despawnTime = t;
219 _despawnState = t ? 1 : 0;
220 }
222 {
223 _despawnState = 2;
224 }
225
226 void OnSpellClick(Unit* clicker, bool spellClickHandled) override;
227
229 {
230 _waypointPauseTimer = time;
231 }
232
233 void SetGossipReturn(bool val)
234 {
235 _gossipReturn = val;
236 }
237
238 void SetEscortQuest(uint32 questID)
239 {
240 _escortQuestId = questID;
241 }
242
243 private:
244 bool AssistPlayerInCombatAgainst(Unit* who);
245 void ReturnToLastOOCPos();
246 void CheckConditions(uint32 diff);
247 void UpdatePath(uint32 diff);
248 void UpdateFollow(uint32 diff);
249 void UpdateDespawn(uint32 diff);
250
260
271
272 bool _run;
276
279
280 // Vehicle conditions
283
284 // Gossip
286
288};
289
291{
292 public:
293 SmartGameObjectAI(GameObject* go, uint32 scriptId = {}) : GameObjectAI(go, scriptId), _gossipReturn(false) { }
295
296 void UpdateAI(uint32 diff) override;
297 void InitializeAI() override;
298 void Reset() override;
300 {
301 return &_script;
302 }
303 static int32 Permissible(GameObject const* /*go*/)
304 {
305 return PERMIT_BASE_NO;
306 }
307
308 bool OnGossipHello(Player* player) override;
309 bool OnGossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override;
310 bool OnGossipSelectCode(Player* player, uint32 menuId, uint32 gossipListId, char const* code) override;
311 void OnQuestAccept(Player* player, Quest const* quest) override;
312 void OnQuestReward(Player* player, Quest const* quest, LootItemType type, uint32 opt) override;
313 bool OnReportUse(Player* player) override;
314 void Destroyed(WorldObject* attacker, uint32 eventId) override;
315 void SetData(uint32 id, uint32 value, Unit* invoker);
316 void SetData(uint32 id, uint32 value) override { SetData(id, value, nullptr); }
317 void SetTimedActionList(SmartScriptHolder& e, uint32 entry, Unit* invoker);
318 void OnGameEvent(bool start, uint16 eventId) override;
319 void OnLootStateChanged(uint32 state, Unit* unit) override;
320 void EventInform(uint32 eventId) override;
321
322 // Called when hit by a spell
323 void SpellHit(WorldObject* caster, SpellInfo const* spellInfo) override;
324
325 // Called when the gameobject summon successfully other creature
326 void JustSummoned(Creature* creature) override;
327
328 // Called when a summoned unit dies
329 void SummonedCreatureDies(Creature* summon, Unit* killer) override;
330
331 // Called when a summoned creature dissapears (UnSommoned)
332 void SummonedCreatureDespawn(Creature* unit) override;
333
334 void SetGossipReturn(bool val) { _gossipReturn = val; }
335
336 private:
338
339 // Gossip
341};
342
344{
345public:
347
348 void OnInitialize() override;
349 void OnUpdate(uint32 diff) override;
350 void OnUnitEnter(Unit* unit) override;
351 void OnUnitExit(Unit* unit, AreaTriggerExitReason reason) override;
352
353 SmartScript* GetScript() { return &mScript; }
354 void SetTimedActionList(SmartScriptHolder& e, uint32 entry, Unit* invoker);
355
356private:
358};
359
361void AddSC_SmartScripts();
362
363#endif
AreaTriggerExitReason
Definition AreaTrigger.h:69
@ PERMIT_BASE_NO
Definition CreatureAI.h:46
#define TC_GAME_API
Definition Define.h:129
uint8_t uint8
Definition Define.h:156
int8_t int8
Definition Define.h:152
int32_t int32
Definition Define.h:150
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
LootItemType
MovementStopReason
static float constexpr SMART_MAX_AID_DIST
Definition SmartAI.h:40
SmartEscortState
Definition SmartAI.h:32
@ SMART_ESCORT_ESCORTING
Definition SmartAI.h:34
@ SMART_ESCORT_PAUSED
Definition SmartAI.h:36
@ SMART_ESCORT_RETURNING
Definition SmartAI.h:35
@ SMART_ESCORT_NONE
Definition SmartAI.h:33
void AddSC_SmartScripts()
Registers scripts required by the SAI scripting system.
Definition SmartAI.cpp:1269
static float constexpr SMART_ESCORT_MAX_PLAYER_DIST
Definition SmartAI.h:39
EvadeReason
DamageEffectType
AreaTriggerAI(AreaTrigger *a, uint32 scriptId={}) noexcept
virtual bool OnGossipHello(Player *)
Definition CreatureAI.h:206
virtual void WaypointPathEnded(uint32, uint32)
Definition CreatureAI.h:224
virtual void WaypointReached(uint32, uint32)
Definition CreatureAI.h:223
virtual void MoveInLineOfSight(Unit *)
virtual void JustEngagedWith(Unit *)
Definition CreatureAI.h:101
virtual void JustSummoned(Creature *)
Definition CreatureAI.h:113
virtual void OnAuraApplied(AuraApplication const *)
Definition CreatureAI.h:150
virtual void OnAuraRemoved(AuraApplication const *)
Definition CreatureAI.h:153
virtual void OnSpellClick(Unit *, bool)
Definition CreatureAI.h:230
virtual void OnSpellStart(SpellInfo const *)
Definition CreatureAI.h:144
virtual bool OnGossipSelectCode(Player *, uint32, uint32, char const *)
Definition CreatureAI.h:212
virtual void SpellHitTarget(WorldObject *, SpellInfo const *)
Definition CreatureAI.h:135
virtual void JustReachedHome()
Definition CreatureAI.h:167
virtual void JustDied(Unit *)
Definition CreatureAI.h:107
virtual void SpellHit(WorldObject *, SpellInfo const *)
Definition CreatureAI.h:132
virtual void OnQuestReward(Player *, Quest const *, LootItemType, uint32)
Definition CreatureAI.h:218
virtual void EnterEvadeMode(EvadeReason why=EvadeReason::Other)
virtual void OnSpellFailed(SpellInfo const *)
Definition CreatureAI.h:141
virtual void OnSpellCast(SpellInfo const *)
Definition CreatureAI.h:138
virtual void SummonedCreatureDies(Creature *, Unit *)
Definition CreatureAI.h:117
virtual void MovementInform(uint32, uint32)
Definition CreatureAI.h:162
virtual void KilledUnit(Unit *)
Definition CreatureAI.h:110
virtual bool OnGossipSelect(Player *, uint32, uint32)
Definition CreatureAI.h:209
virtual void ReceiveEmote(Player *, uint32)
Definition CreatureAI.h:173
void OnCharmed(bool isNew) override
virtual void JustAppeared()
virtual void PassengerBoarded(Unit *, int8, bool)
== Fields =======================================
Definition CreatureAI.h:228
virtual void SummonedCreatureDespawn(Creature *)
Definition CreatureAI.h:116
virtual void CorpseRemoved(uint32 &)
== State checks =================================
Definition CreatureAI.h:195
virtual void OnQuestAccept(Player *, Quest const *)
Definition CreatureAI.h:215
virtual void IsSummonedBy(WorldObject *)
Definition CreatureAI.h:114
void AttackStart(Unit *victim) override
== Triggered Actions Requested ==================
uint32 _escortQuestId
Definition SmartAI.h:287
void AddEscortState(uint32 escortState)
Definition SmartAI.h:67
void SetEscortQuest(uint32 questID)
Definition SmartAI.h:238
bool _charmed
Definition SmartAI.h:252
bool HasEscortState(uint32 escortState) const
Definition SmartAI.h:63
SmartScript _script
Definition SmartAI.h:251
uint32 _followArrivedEntry
Definition SmartAI.h:256
float _followDistance
Definition SmartAI.h:258
~SmartAI()
Definition SmartAI.h:45
bool _run
Definition SmartAI.h:272
uint32 _vehicleConditionsTimer
Definition SmartAI.h:282
uint32 _escortInvokerCheckTimer
Definition SmartAI.h:263
bool _waypointPathEnded
Definition SmartAI.h:270
uint32 _currentWaypointNodeId
Definition SmartAI.h:264
void SetDespawnTime(uint32 t)
Definition SmartAI.h:216
void StartDespawn()
Definition SmartAI.h:221
bool _canCombatMove
Definition SmartAI.h:274
bool _vehicleConditions
Definition SmartAI.h:281
bool _waypointReached
Definition SmartAI.h:265
uint32 _despawnTime
Definition SmartAI.h:277
uint32 _despawnState
Definition SmartAI.h:278
bool CanCombatMove() const
Definition SmartAI.h:76
bool _gossipReturn
Definition SmartAI.h:285
float _followAngle
Definition SmartAI.h:259
void SetWPPauseTimer(uint32 time)
Definition SmartAI.h:228
void RemoveEscortState(uint32 escortState)
Definition SmartAI.h:71
uint32 _escortNPCFlags
Definition SmartAI.h:262
bool _waypointPauseForced
Definition SmartAI.h:267
uint32 _followArrivedTimer
Definition SmartAI.h:254
ObjectGuid _followGUID
Definition SmartAI.h:257
void SetGossipReturn(bool val)
Definition SmartAI.h:233
bool _evadeDisabled
Definition SmartAI.h:273
uint32 _waypointPauseTimer
Definition SmartAI.h:266
bool _OOCReached
Definition SmartAI.h:269
uint32 _followCredit
Definition SmartAI.h:255
uint32 _followCreditType
Definition SmartAI.h:253
static int32 Permissible(Creature const *)
Definition SmartAI.h:49
SmartScript * GetScript()
Definition SmartAI.h:88
void SetInvincibilityHpLevel(uint32 level)
Definition SmartAI.h:204
void SetData(uint32 id, uint32 value) override
Definition SmartAI.h:190
uint32 _escortState
Definition SmartAI.h:261
uint32 _invincibilityHPLevel
Definition SmartAI.h:275
bool _repeatWaypointPath
Definition SmartAI.h:268
SmartScript mScript
Definition SmartAI.h:357
SmartScript * GetScript()
Definition SmartAI.h:353
SmartGameObjectAI(GameObject *go, uint32 scriptId={})
Definition SmartAI.h:293
static int32 Permissible(GameObject const *)
Definition SmartAI.h:303
SmartScript * GetScript()
Definition SmartAI.h:299
void SetData(uint32 id, uint32 value) override
Definition SmartAI.h:316
SmartScript _script
Definition SmartAI.h:337
void SetGossipReturn(bool val)
Definition SmartAI.h:334
virtual void HealReceived(Unit *, uint32 &)
Definition UnitAI.h:147
virtual void Reset()
Definition UnitAI.h:64
virtual void SetGUID(ObjectGuid const &guid, int32 id)
Definition UnitAI.h:76
virtual void InitializeAI()
Definition UnitAI.cpp:43
virtual void DoAction(int32 param)
Definition UnitAI.h:73
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 UpdateAI(uint32 diff)=0
virtual uint32 GetData(uint32 id) const
Definition UnitAI.h:74
Definition Unit.h:635