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