TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SmartAI.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 "Common.h"
22 #include "Creature.h"
23 #include "CreatureAI.h"
24 #include "Unit.h"
25 #include "Spell.h"
26 
27 #include "SmartScript.h"
28 #include "SmartScriptMgr.h"
29 #include "GameObjectAI.h"
30 
32 {
33  SMART_ESCORT_NONE = 0x000, //nothing in progress
34  SMART_ESCORT_ESCORTING = 0x001, //escort is in progress
35  SMART_ESCORT_RETURNING = 0x002, //escort is returning after being in combat
36  SMART_ESCORT_PAUSED = 0x004 //will not proceed with waypoints before state is removed
37 };
38 
40 {
43 };
44 
46 {
47  public:
48  ~SmartAI(){ }
49  explicit SmartAI(Creature* c);
50 
51  // Start moving to the desired MovePoint
52  void StartPath(bool run = false, uint32 path = 0, bool repeat = false, Unit* invoker = NULL);
53  bool LoadPath(uint32 entry);
54  void PausePath(uint32 delay, bool forced = false);
55  void StopPath(uint32 DespawnTime = 0, uint32 quest = 0, bool fail = false);
56  void EndPath(bool fail = false);
57  void ResumePath();
58  WayPoint* GetNextWayPoint();
59  bool HasEscortState(uint32 uiEscortState) const { return (mEscortState & uiEscortState) != 0; }
60  void AddEscortState(uint32 uiEscortState) { mEscortState |= uiEscortState; }
61  void RemoveEscortState(uint32 uiEscortState) { mEscortState &= ~uiEscortState; }
62  void SetAutoAttack(bool on) { mCanAutoAttack = on; }
63  void SetCombatMove(bool on);
64  bool CanCombatMove() { return mCanCombatMove; }
65  void SetFollow(Unit* target, float dist = 0.0f, float angle = 0.0f, uint32 credit = 0, uint32 end = 0, uint32 creditType = 0);
66  void StopFollow();
67 
68  void SetScript9(SmartScriptHolder& e, uint32 entry, Unit* invoker);
69  SmartScript* GetScript() { return &mScript; }
70  bool IsEscortInvokerInRange();
71 
72  // Called when creature is spawned or respawned
73  void JustRespawned() override;
74 
75  // Called at reaching home after evade, InitializeAI(), EnterEvadeMode() for resetting variables
76  void JustReachedHome() override;
77 
78  // Called for reaction at enter to combat if not in combat yet (enemy can be NULL)
79  void EnterCombat(Unit* enemy) override;
80 
81  // Called for reaction at stopping attack at no attackers or targets
82  void EnterEvadeMode(EvadeReason why = EVADE_REASON_OTHER) override;
83 
84  // Called when the creature is killed
85  void JustDied(Unit* killer) override;
86 
87  // Called when the creature kills a unit
88  void KilledUnit(Unit* victim) override;
89 
90  // Called when the creature summon successfully other creature
91  void JustSummoned(Creature* creature) override;
92 
93  // Tell creature to attack and follow the victim
94  void AttackStart(Unit* who) override;
95 
96  // Called if IsVisible(Unit* who) is true at each *who move, reaction at visibility zone enter
97  void MoveInLineOfSight(Unit* who) override;
98 
99  // Called when hit by a spell
100  void SpellHit(Unit* unit, const SpellInfo* spellInfo) override;
101 
102  // Called when spell hits a target
103  void SpellHitTarget(Unit* target, const SpellInfo* spellInfo) override;
104 
105  // Called at any Damage from any attacker (before damage apply)
106  void DamageTaken(Unit* doneBy, uint32& damage) override;
107 
108  // Called when the creature receives heal
109  void HealReceived(Unit* doneBy, uint32& addhealth) override;
110 
111  // Called at World update tick
112  void UpdateAI(uint32 diff) override;
113 
114  // Called at text emote receive from player
115  void ReceiveEmote(Player* player, uint32 textEmote) override;
116 
117  // Called at waypoint reached or point movement finished
118  void MovementInform(uint32 MovementType, uint32 Data) override;
119 
120  // Called when creature is summoned by another unit
121  void IsSummonedBy(Unit* summoner) override;
122 
123  // Called at any Damage to any victim (before damage apply)
124  void DamageDealt(Unit* doneTo, uint32& damage, DamageEffectType /*damagetype*/) override;
125 
126  // Called when a summoned creature dissapears (UnSommoned)
127  void SummonedCreatureDespawn(Creature* unit) override;
128 
129  // called when the corpse of this creature gets removed
130  void CorpseRemoved(uint32& respawnDelay) override;
131 
132  // Called at World update tick if creature is charmed
133  void UpdateAIWhileCharmed(const uint32 diff);
134 
135  // Called when a Player/Creature enters the creature (vehicle)
136  void PassengerBoarded(Unit* who, int8 seatId, bool apply) override;
137 
138  // Called when gets initialized, when creature is added to world
139  void InitializeAI() override;
140 
141  // Called when creature gets charmed by another unit
142  void OnCharmed(bool apply) override;
143 
144  // Called when victim is in line of sight
145  bool CanAIAttack(const Unit* who) const override;
146 
147  // Used in scripts to share variables
148  void DoAction(int32 param = 0) override;
149 
150  // Used in scripts to share variables
151  uint32 GetData(uint32 id = 0) const override;
152 
153  // Used in scripts to share variables
154  void SetData(uint32 id, uint32 value) override;
155 
156  // Used in scripts to share variables
157  void SetGUID(ObjectGuid guid, int32 id = 0) override;
158 
159  // Used in scripts to share variables
160  ObjectGuid GetGUID(int32 id = 0) const override;
161 
162  //core related
163  static int Permissible(const Creature*);
164 
165  // Called at movepoint reached
166  void MovepointReached(uint32 id);
167 
168  // Makes the creature run/walk
169  void SetRun(bool run = true);
170 
171  void SetFly(bool fly = true);
172 
173  void SetSwim(bool swim = true);
174 
175  void SetInvincibilityHpLevel(uint32 level) { mInvincibilityHpLevel = level; }
176 
177  void sGossipHello(Player* player) override;
178  void sGossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override;
179  void sGossipSelectCode(Player* player, uint32 menuId, uint32 gossipListId, const char* code) override;
180  void sQuestAccept(Player* player, Quest const* quest) override;
181  //void sQuestSelect(Player* player, Quest const* quest) override;
182  void sQuestReward(Player* player, Quest const* quest, uint32 opt) override;
183  bool sOnDummyEffect(Unit* caster, uint32 spellId, SpellEffIndex effIndex) override;
184  void sOnGameEvent(bool start, uint16 eventId) override;
185 
187 
189  {
190  mDespawnTime = t;
191  mDespawnState = t ? 1 : 0;
192  }
193  void StartDespawn() { mDespawnState = 2; }
194 
195  void OnSpellClick(Unit* clicker, bool& result) override;
196 
197  private:
203  float mFollowDist;
205 
206  void ReturnToLastOOCPos();
207  void UpdatePath(const uint32 diff);
216  Position mLastOOCPos;//set on enter combat
217  uint32 GetWPCount() const { return mWayPoints ? uint32(mWayPoints->size()) : 0; }
219  bool mRun;
224  bool AssistPlayerInCombat(Unit* who);
225 
228  void UpdateDespawn(const uint32 diff);
231 };
232 
234 {
235  public:
238 
239  void UpdateAI(uint32 diff) override;
240  void InitializeAI() override;
241  void Reset() override;
242  SmartScript* GetScript() { return &mScript; }
243  static int Permissible(const GameObject* g);
244 
245  bool GossipHello(Player* player) override;
246  bool GossipSelect(Player* player, uint32 sender, uint32 action) override;
247  bool GossipSelectCode(Player* /*player*/, uint32 /*sender*/, uint32 /*action*/, const char* /*code*/) override;
248  bool QuestAccept(Player* player, Quest const* quest) override;
249  bool QuestReward(Player* player, Quest const* quest, uint32 opt) override;
250  void Destroyed(Player* player, uint32 eventId) override;
251  void SetData(uint32 id, uint32 value) override;
252  void SetScript9(SmartScriptHolder& e, uint32 entry, Unit* invoker);
253  void OnGameEvent(bool start, uint16 eventId) override;
254  void OnStateChanged(uint32 state, Unit* unit) override;
255  void EventInform(uint32 eventId) override;
256 
257  private:
259 };
260 
262 void AddSC_SmartScripts();
263 
264 #endif
virtual void SetData(uint32, uint32)
Definition: UnitAI.h:140
virtual void PassengerBoarded(Unit *, int8, bool)
== Fields =======================================
Definition: CreatureAI.h:183
virtual bool QuestAccept(Player *, Quest const *)
Definition: GameObjectAI.h:53
Definition: SmartAI.h:233
virtual void sGossipSelect(Player *, uint32, uint32)
Definition: UnitAI.h:254
virtual ObjectGuid GetGUID(int32=0) const
Definition: UnitAI.h:142
virtual void sQuestReward(Player *, Quest const *, uint32)
Definition: UnitAI.h:258
int8_t int8
Definition: Define.h:148
virtual void OnStateChanged(uint32, Unit *)
Definition: GameObjectAI.h:62
void RemoveEscortState(uint32 uiEscortState)
Definition: SmartAI.h:61
uint32 mFollowCredit
Definition: SmartAI.h:200
virtual void OnGameEvent(bool, uint16)
Definition: GameObjectAI.h:61
virtual void sOnGameEvent(bool, uint16)
Definition: UnitAI.h:260
Definition: QuestDef.h:279
float mFollowAngle
Definition: SmartAI.h:204
virtual void SpellHitTarget(Unit *, SpellInfo const *)
Definition: CreatureAI.h:133
Definition: SpellInfo.h:326
Definition: SmartAI.h:35
uint32 mInvincibilityHpLevel
Definition: SmartAI.h:223
bool mForcedPaused
Definition: SmartAI.h:222
virtual bool GossipSelect(Player *, uint32, uint32)
Definition: GameObjectAI.h:51
virtual void ReceiveEmote(Player *, uint32)
Definition: CreatureAI.h:153
virtual void EnterEvadeMode(EvadeReason why=EVADE_REASON_OTHER)
Definition: CreatureAI.cpp:168
Definition: SmartAI.h:33
bool mCanRepeatPath
Definition: SmartAI.h:218
Definition: SmartAI.h:41
virtual void IsSummonedBy(Unit *)
Definition: CreatureAI.h:124
virtual bool GossipSelectCode(Player *, uint32, uint32, char const *)
Definition: GameObjectAI.h:52
void OnCharmed(bool apply) override
Definition: CreatureAI.cpp:32
SmartScript * GetScript()
Definition: SmartAI.h:242
uint32 mCurrentWPID
Definition: SmartAI.h:211
uint32 mEscortQuestID
Definition: SmartAI.h:186
virtual void JustRespawned()
Definition: CreatureAI.h:140
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 mDespawnTime
Definition: SmartAI.h:226
uint32 mFollowArrivedTimer
Definition: SmartAI.h:199
virtual void SetData(uint32, uint32)
Definition: GameObjectAI.h:60
~SmartAI()
Definition: SmartAI.h:48
Definition: SmartAI.h:45
virtual void UpdateAI(uint32 diff)=0
Definition: Creature.h:467
bool mCanAutoAttack
Definition: SmartAI.h:220
void AddSC_SmartScripts()
Registers scripts required by the SAI scripting system.
Definition: SmartAI.cpp:926
SmartScript mScript
Definition: SmartAI.h:208
void StartDespawn()
Definition: SmartAI.h:193
void apply(T *val)
Definition: ByteConverter.h:41
uint32 mWPPauseTimer
Definition: SmartAI.h:214
virtual void SetGUID(ObjectGuid, int32=0)
Definition: UnitAI.h:141
Definition: GameObjectAI.h:29
virtual void DamageTaken(Unit *, uint32 &)
Definition: UnitAI.h:228
SmartScript mScript
Definition: SmartAI.h:258
virtual bool CanAIAttack(Unit const *) const
Definition: UnitAI.h:126
Definition: SmartScriptMgr.h:31
virtual void sGossipSelectCode(Player *, uint32, uint32, char const *)
Definition: UnitAI.h:255
virtual void UpdateAI(uint32)
Definition: GameObjectAI.h:37
ObjectGuid mFollowGuid
Definition: SmartAI.h:202
SmartEscortState
Definition: SmartAI.h:31
virtual void InitializeAI()
Definition: UnitAI.h:130
virtual void EnterCombat(Unit *)
Definition: CreatureAI.h:114
uint32 mDespawnState
Definition: SmartAI.h:227
virtual bool sOnDummyEffect(Unit *, uint32, SpellEffIndex)
Definition: UnitAI.h:259
virtual bool GossipHello(Player *)
Definition: GameObjectAI.h:50
uint32 mFollowArrivedEntry
Definition: SmartAI.h:201
virtual bool QuestReward(Player *, Quest const *, uint32)
Definition: GameObjectAI.h:54
WayPoint * mLastWP
Definition: SmartAI.h:215
Definition: SmartAI.h:36
virtual void SpellHit(Unit *, SpellInfo const *)
Definition: CreatureAI.h:130
Position mLastOOCPos
Definition: SmartAI.h:216
bool CanCombatMove()
Definition: SmartAI.h:64
virtual void sQuestAccept(Player *, Quest const *)
Definition: UnitAI.h:256
DamageEffectType
Definition: Unit.h:660
virtual void KilledUnit(Unit *)
Definition: CreatureAI.h:120
bool HasEscortState(uint32 uiEscortState) const
Definition: SmartAI.h:59
virtual void Destroyed(Player *, uint32)
Definition: GameObjectAI.h:56
SpellEffIndex
Definition: SharedDefines.h:26
int32_t int32
Definition: Define.h:146
Definition: CreatureAI.h:68
void SetDespawnTime(uint32 t)
Definition: SmartAI.h:188
virtual void JustDied(Unit *)
Definition: CreatureAI.h:117
uint32 mEscortState
Definition: SmartAI.h:210
uint32_t uint32
Definition: Define.h:150
bool mJustReset
Definition: SmartAI.h:230
uint32 mFollowCreditType
Definition: SmartAI.h:198
std::unordered_map< uint32, WayPoint * > WPPath
Definition: SmartScriptMgr.h:1389
uint16_t uint16
Definition: Define.h:151
static int Permissible(GameObject const *go)
Definition: GameObjectAI.cpp:22
Definition: GameObject.h:880
virtual void DoAction(int32)
Definition: UnitAI.h:138
SmartEscortVars
Definition: SmartAI.h:39
float mFollowDist
Definition: SmartAI.h:203
Definition: SmartAI.h:34
Definition: inftrees.h:24
Definition: SmartAI.h:42
virtual void InitializeAI()
Definition: GameObjectAI.h:39
virtual void OnSpellClick(Unit *, bool &)
Definition: CreatureAI.h:185
#define TC_GAME_API
Definition: Define.h:134
virtual void DamageDealt(Unit *, uint32 &, DamageEffectType)
Definition: UnitAI.h:223
virtual void JustReachedHome()
Definition: CreatureAI.h:148
Definition: Position.h:27
virtual void MoveInLineOfSight(Unit *)
Definition: CreatureAI.cpp:128
uint32 GetWPCount() const
Definition: SmartAI.h:217
bool mCanCombatMove
Definition: SmartAI.h:221
SmartScript * GetScript()
Definition: SmartAI.h:69
const FieldDescriptor value
Definition: descriptor.h:1522
bool mWPReached
Definition: SmartAI.h:213
Definition: ObjectGuid.h:189
Definition: SmartScriptMgr.h:1361
uint32_t uint32
Definition: g3dmath.h:168
virtual void Reset()
Definition: GameObjectAI.h:41
virtual void HealReceived(Unit *, uint32 &)
Definition: UnitAI.h:231
virtual uint32 GetData(uint32) const
Definition: UnitAI.h:139
Definition: FactoryHolder.h:52
virtual void CorpseRemoved(uint32 &)
== Triggered Actions Requested ==================
Definition: CreatureAI.h:176
WPPath * mWayPoints
Definition: SmartAI.h:209
virtual void EventInform(uint32)
Definition: GameObjectAI.h:63
uint32 mEscortInvokerCheckTimer
Definition: SmartAI.h:229
void SetInvincibilityHpLevel(uint32 level)
Definition: SmartAI.h:175
virtual void SummonedCreatureDespawn(Creature *)
Definition: CreatureAI.h:126
Definition: Unit.h:1305
virtual void AttackStart(Unit *)
Definition: UnitAI.cpp:29
virtual void MovementInform(uint32, uint32)
Definition: CreatureAI.h:143
~SmartGameObjectAI()
Definition: SmartAI.h:237
Definition: SmartScript.h:31
void SetAutoAttack(bool on)
Definition: SmartAI.h:62
Data
Definition: molten_core.h:69
virtual void JustSummoned(Creature *)
Definition: CreatureAI.h:123
void AddEscortState(uint32 uiEscortState)
Definition: SmartAI.h:60
bool mRun
Definition: SmartAI.h:219
SmartGameObjectAI(GameObject *g)
Definition: SmartAI.h:236
uint32 mLastWPIDReached
Definition: SmartAI.h:212
virtual void sGossipHello(Player *)
Definition: UnitAI.h:253