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