TrinityCore
Loading...
Searching...
No Matches
CreatureAI.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_CREATUREAI_H
19#define TRINITY_CREATUREAI_H
20
21#include "Duration.h"
22#include "LootItemType.h"
23#include "ObjectDefines.h"
24#include "Optional.h"
25#include "QuestDef.h"
26#include "UnitAI.h"
27
28class AreaBoundary;
29class AreaTrigger;
30class AuraApplication;
31class Creature;
32class DynamicObject;
33class GameObject;
34class PlayerAI;
35class WorldObject;
36struct Position;
37enum class QuestGiverStatus : uint64;
38
39typedef std::vector<AreaBoundary const*> CreatureBoundary;
40
41#define TIME_INTERVAL_LOOK 5000
42#define VISIBILITY_RANGE 10000
43
53
59
61{
62 protected:
63 Creature* const me;
64
65 bool UpdateVictim();
66
67 Creature* DoSummon(uint32 entry, Position const& pos, Milliseconds despawnTime = 30s, TempSummonType summonType = TEMPSUMMON_CORPSE_TIMED_DESPAWN);
68 Creature* DoSummon(uint32 entry, WorldObject* obj, float radius = 5.0f, Milliseconds despawnTime = 30s, TempSummonType summonType = TEMPSUMMON_CORPSE_TIMED_DESPAWN);
69 Creature* DoSummonFlyer(uint32 entry, WorldObject* obj, float flightZ, float radius = 5.0f, Milliseconds despawnTime = 30s, TempSummonType summonType = TEMPSUMMON_CORPSE_TIMED_DESPAWN);
70
71 public:
72 explicit CreatureAI(Creature* creature, uint32 scriptId = {}) noexcept;
73
74 virtual ~CreatureAI();
75
76 // Gets the id of the AI (script id)
77 uint32 GetId() const { return _scriptId; }
78
79 bool IsEngaged() const { return _isEngaged; }
80
81 void Talk(uint8 id, WorldObject const* whisperTarget = nullptr);
82
84
85 // Called if IsVisible(Unit* who) is true at each who move, reaction at visibility zone enter
86 void MoveInLineOfSight_Safe(Unit* who);
87
88 // Trigger Creature "Alert" state (creature can see stealthed unit)
89 void TriggerAlert(Unit const* who) const;
90
91 // Called for reaction at stopping attack at no attackers or targets
92 virtual void EnterEvadeMode(EvadeReason why = EvadeReason::Other);
93
94 // Called for reaction whenever we start being in combat (overridden from base UnitAI)
95 void JustEnteredCombat(Unit* /*who*/) override;
96
97 // Called for reaction whenever a new non-offline unit is added to the threat list
98 virtual void JustStartedThreateningMe(Unit* who) { if (!IsEngaged()) EngagementStart(who); }
99
100 // Called for reaction when initially engaged - this will always happen _after_ JustEnteredCombat
101 virtual void JustEngagedWith(Unit* /*who*/) { }
102
103 // Called when the creature reaches 0 health (or 1 if unkillable).
104 virtual void OnHealthDepleted(Unit* /*attacker*/, bool /*isKill*/) { }
105
106 // Called when the creature is killed
107 virtual void JustDied(Unit* /*killer*/) { }
108
109 // Called when the creature kills a unit
110 virtual void KilledUnit(Unit* /*victim*/) { }
111
112 // Called when the creature summon successfully other creature
113 virtual void JustSummoned(Creature* /*summon*/) { }
114 virtual void IsSummonedBy(WorldObject* /*summoner*/) { }
115
116 virtual void SummonedCreatureDespawn(Creature* /*summon*/) { }
117 virtual void SummonedCreatureDies(Creature* /*summon*/, Unit* /*killer*/) { }
118
119 // Called when the creature successfully summons a gameobject
120 virtual void JustSummonedGameobject(GameObject* /*gameobject*/) { }
121 virtual void SummonedGameobjectDespawn(GameObject* /*gameobject*/) { }
122
123 // Called when the creature successfully registers a dynamicobject
124 virtual void JustRegisteredDynObject(DynamicObject* /*dynObject*/) { }
125 virtual void JustUnregisteredDynObject(DynamicObject* /*dynObject*/) { }
126
127 // Called when the creature successfully registers an areatrigger
128 virtual void JustRegisteredAreaTrigger(AreaTrigger* /*areaTrigger*/) { }
129 virtual void JustUnregisteredAreaTrigger(AreaTrigger* /*areaTrigger*/) { }
130
131 // Called when hit by a spell
132 virtual void SpellHit(WorldObject* /*caster*/, SpellInfo const* /*spellInfo*/) { }
133
134 // Called when spell hits a target
135 virtual void SpellHitTarget(WorldObject* /*target*/, SpellInfo const* /*spellInfo*/) { }
136
137 // Called when a spell finishes
138 virtual void OnSpellCast(SpellInfo const* /*spell*/) { }
139
140 // Called when a spell fails
141 virtual void OnSpellFailed(SpellInfo const* /*spell*/) { }
142
143 // Called when a spell starts
144 virtual void OnSpellStart(SpellInfo const* /*spell*/) { }
145
146 // Called when a channeled spell finishes
147 virtual void OnChannelFinished(SpellInfo const* /*spell*/) { }
148
149 // Called when aura is applied
150 virtual void OnAuraApplied(AuraApplication const* /*aurApp*/) { }
151
152 // Called when aura is removed
153 virtual void OnAuraRemoved(AuraApplication const* /*aurApp*/) { }
154
155 // Should return true if the NPC is currently being escorted
156 virtual bool IsEscorted() const { return false; }
157
158 // Called when creature appears in the world (spawn, respawn, grid load etc...)
159 virtual void JustAppeared();
160
161 // Called at waypoint reached or point movement finished
162 virtual void MovementInform(uint32 /*type*/, uint32 /*id*/) { }
163
164 void OnCharmed(bool isNew) override;
165
166 // Called at reaching home after evade
167 virtual void JustReachedHome() { }
168
170 static void DoZoneInCombat(Creature* creature);
171
172 // Called at text emote receive from player
173 virtual void ReceiveEmote(Player* /*player*/, uint32 /*emoteId*/) { }
174
175 // Called when owner takes damage
176 virtual void OwnerAttackedBy(Unit* attacker) { OnOwnerCombatInteraction(attacker); }
177
178 // Called when owner attacks something
179 virtual void OwnerAttacked(Unit* target) { OnOwnerCombatInteraction(target); }
180
182
183 // Called when creature attack expected (if creature can and no have current victim)
184 void AttackStart(Unit* victim) override;
185
186 // Called at World update tick
187 //virtual void UpdateAI(const uint32 /*diff*/) { }
188
190
191 // Is unit visible for MoveInLineOfSight
192 //virtual bool IsVisible(Unit*) const { return false; }
193
194 // called when the corpse of this creature gets removed
195 virtual void CorpseRemoved(uint32& /*respawnDelay*/) { }
196
197 // Called when victim entered water and creature can not enter water
198 //virtual bool CanReachByRangeAttack(Unit*) { return false; }
199
201
202 // Called when the dialog status between a player and the creature is requested.
203 virtual Optional<QuestGiverStatus> GetDialogStatus(Player const* player);
204
205 // Called when a player opens a gossip dialog with the creature.
206 virtual bool OnGossipHello(Player* /*player*/) { return false; }
207
208 // Called when a player selects a gossip item in the creature's gossip menu.
209 virtual bool OnGossipSelect(Player* /*player*/, uint32 /*menuId*/, uint32 /*gossipListId*/) { return false; }
210
211 // Called when a player selects a gossip with a code in the creature's gossip menu.
212 virtual bool OnGossipSelectCode(Player* /*player*/, uint32 /*menuId*/, uint32 /*gossipListId*/, char const* /*code*/) { return false; }
213
214 // Called when a player accepts a quest from the creature.
215 virtual void OnQuestAccept(Player* /*player*/, Quest const* /*quest*/) { }
216
217 // Called when a player completes a quest and is rewarded, opt is the selected item's index or 0
218 virtual void OnQuestReward(Player* /*player*/, Quest const* /*quest*/, LootItemType /*type*/, uint32 /*opt*/) { }
219
221
222 virtual void WaypointStarted(uint32 /*nodeId*/, uint32 /*pathId*/) { }
223 virtual void WaypointReached(uint32 /*nodeId*/, uint32 /*pathId*/) { }
224 virtual void WaypointPathEnded(uint32 /*nodeId*/, uint32 /*pathId*/) { }
225
227
228 virtual void PassengerBoarded(Unit* /*passenger*/, int8 /*seatId*/, bool /*apply*/) { }
229
230 virtual void OnSpellClick(Unit* /*clicker*/, bool /*spellClickHandled*/) { }
231
232 virtual bool CanSeeAlways(WorldObject const* /*obj*/) { return false; }
233
234 // Called when a player is charmed by the creature
235 // If a PlayerAI* is returned, that AI is placed on the player instead of the default charm AI
236 // Object destruction is handled by Unit::RemoveCharmedBy
237 virtual PlayerAI* GetAIForCharmedPlayer(Player* /*who*/) { return nullptr; }
238
239 // intended for encounter design/debugging. do not use for other purposes. expensive.
240 int32 VisualizeBoundary(Seconds duration, Unit* owner = nullptr, bool fill = false) const;
241
242 // boundary system methods
243 virtual bool CheckInRoom();
244 CreatureBoundary const* GetBoundary() const { return _boundary; }
245 void SetBoundary(CreatureBoundary const* boundary, bool negativeBoundaries = false);
246
247 static bool IsInBounds(CreatureBoundary const& boundary, Position const* who);
248 bool IsInBoundary(Position const* who = nullptr) const;
249
250 protected:
251 void EngagementStart(Unit* who);
252 void EngagementOver();
253 virtual void MoveInLineOfSight(Unit* /*who*/);
254
255 bool _EnterEvadeMode(EvadeReason why = EvadeReason::Other);
256
259
260 private:
261 void OnOwnerCombatInteraction(Unit* target);
262
266};
267
268#endif
std::vector< AreaBoundary const * > CreatureBoundary
Definition CreatureAI.h:39
SCEquip
Definition CreatureAI.h:55
@ EQUIP_UNEQUIP
Definition CreatureAI.h:57
@ EQUIP_NO_CHANGE
Definition CreatureAI.h:56
Permitions
Definition CreatureAI.h:45
@ PERMIT_BASE_SPECIAL
Definition CreatureAI.h:51
@ PERMIT_BASE_PROACTIVE
Definition CreatureAI.h:49
@ PERMIT_BASE_NO
Definition CreatureAI.h:46
@ PERMIT_BASE_REACTIVE
Definition CreatureAI.h:48
@ PERMIT_BASE_FACTION_SPECIFIC
Definition CreatureAI.h:50
@ PERMIT_BASE_IDLE
Definition CreatureAI.h:47
#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
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition Duration.h:24
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition Duration.h:28
LootItemType
TempSummonType
@ TEMPSUMMON_CORPSE_TIMED_DESPAWN
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
QuestGiverStatus
Definition QuestDef.h:158
EvadeReason
virtual void JustUnregisteredDynObject(DynamicObject *)
Definition CreatureAI.h:125
virtual bool OnGossipHello(Player *)
Definition CreatureAI.h:206
virtual void OnHealthDepleted(Unit *, bool)
Definition CreatureAI.h:104
virtual bool CanSeeAlways(WorldObject const *)
Definition CreatureAI.h:232
virtual void WaypointPathEnded(uint32, uint32)
Definition CreatureAI.h:224
virtual void WaypointReached(uint32, uint32)
Definition CreatureAI.h:223
virtual void JustEngagedWith(Unit *)
Definition CreatureAI.h:101
virtual void JustSummonedGameobject(GameObject *)
Definition CreatureAI.h:120
void DoZoneInCombat()
Definition CreatureAI.h:169
virtual void OnChannelFinished(SpellInfo const *)
Definition CreatureAI.h:147
bool IsEngaged() const
Definition CreatureAI.h:79
virtual void JustSummoned(Creature *)
Definition CreatureAI.h:113
CreatureBoundary const * _boundary
Definition CreatureAI.h:257
virtual PlayerAI * GetAIForCharmedPlayer(Player *)
Definition CreatureAI.h:237
virtual void OnAuraApplied(AuraApplication const *)
Definition CreatureAI.h:150
virtual void OnAuraRemoved(AuraApplication const *)
Definition CreatureAI.h:153
virtual void OwnerAttacked(Unit *target)
Definition CreatureAI.h:179
CreatureBoundary const * GetBoundary() const
Definition CreatureAI.h:244
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 SummonedGameobjectDespawn(GameObject *)
Definition CreatureAI.h:121
virtual void JustStartedThreateningMe(Unit *who)
Definition CreatureAI.h:98
virtual void SpellHitTarget(WorldObject *, SpellInfo const *)
Definition CreatureAI.h:135
bool _isEngaged
Definition CreatureAI.h:264
bool _negateBoundary
Definition CreatureAI.h:258
virtual void JustReachedHome()
Definition CreatureAI.h:167
virtual bool IsEscorted() const
Definition CreatureAI.h:156
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 OnSpellFailed(SpellInfo const *)
Definition CreatureAI.h:141
uint32 GetId() const
Definition CreatureAI.h:77
virtual void OnSpellCast(SpellInfo const *)
Definition CreatureAI.h:138
uint32 const _scriptId
Definition CreatureAI.h:263
virtual void JustUnregisteredAreaTrigger(AreaTrigger *)
Definition CreatureAI.h:129
virtual void SummonedCreatureDies(Creature *, Unit *)
Definition CreatureAI.h:117
virtual void MovementInform(uint32, uint32)
Definition CreatureAI.h:162
virtual void OwnerAttackedBy(Unit *attacker)
Definition CreatureAI.h:176
virtual void KilledUnit(Unit *)
Definition CreatureAI.h:110
virtual bool OnGossipSelect(Player *, uint32, uint32)
Definition CreatureAI.h:209
virtual void WaypointStarted(uint32, uint32)
== Waypoints system =============================
Definition CreatureAI.h:222
virtual void ReceiveEmote(Player *, uint32)
Definition CreatureAI.h:173
virtual void PassengerBoarded(Unit *, int8, bool)
== Fields =======================================
Definition CreatureAI.h:228
virtual void JustRegisteredAreaTrigger(AreaTrigger *)
Definition CreatureAI.h:128
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
Creature *const me
Definition CreatureAI.h:63
virtual void JustRegisteredDynObject(DynamicObject *)
Definition CreatureAI.h:124
bool _moveInLOSLocked
Definition CreatureAI.h:265
virtual void JustEnteredCombat(Unit *)
Definition UnitAI.h:131
virtual void AttackStart(Unit *victim)
Definition UnitAI.cpp:29
virtual void OnCharmed(bool isNew)
Definition UnitAI.cpp:49
Definition Unit.h:635