OregonCore  revision fb2a440-git
Your Favourite TBC server
UnitAI.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore 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 OREGON_UNITAI_H
19 #define OREGON_UNITAI_H
20 
21 #include "Platform/Define.h"
22 #include <list>
23 #include "Unit.h"
24 #include "QuestDef.h"
25 #include "Utilities/EventMap.h"
26 
27 class Unit;
28 class Player;
29 struct AISpellInfoType;
30 
31 //Selection method used by SelectTarget
33 {
34  SELECT_TARGET_RANDOM = 0, //Just selects a random target
35  SELECT_TARGET_TOPAGGRO, //Selects targes from top aggro to bottom
36  SELECT_TARGET_BOTTOMAGGRO, //Selects targets from bottom aggro to top
39 };
40 
41 class UnitAI
42 {
43  protected:
44  Unit* const me;
45  public:
46  explicit UnitAI(Unit* u) : me(u) {}
47  virtual ~UnitAI() {}
48 
49  virtual bool CanAIAttack(const Unit* /*who*/) const
50  {
51  return true;
52  }
53  virtual void AttackStart(Unit*);
54  virtual void UpdateAI(const uint32 /*diff*/) = 0;
55 
56  virtual void InitializeAI()
57  {
58  if (!me->isDead()) Reset();
59  }
60 
61  virtual void Reset() {};
62 
63  virtual void EventHappens(uint32) {}
64  virtual void ScheduleEvent(uint32 data, uint32 delay);
65 
66  // Called when unit is charmed
67  virtual void OnCharmed(bool /*apply*/) = 0;
68 
69  // Pass parameters between AI
70  virtual void DoAction(const int32 param = 0)
71  {
72  UNUSED(param);
73  }
74  virtual uint32 GetData(uint32 id = 0)
75  {
76  UNUSED(id);
77  return 0;
78  }
79  virtual void SetData(uint32 /*id*/, uint32 /*value*/) {}
80  virtual void SetGUID(const uint64& /*guid*/, int32 id = 0)
81  {
82  UNUSED(id);
83  }
84  virtual uint64 GetGUID(int32 id = 0)
85  {
86  UNUSED(id);
87  return 0;
88  }
89 
90  // Called at any Damage to any victim (before damage apply)
91  virtual void DamageDealt(Unit* /*victim*/, uint32& /*damage*/, DamageEffectType /*damageType*/) { }
92 
93  Unit* SelectTarget(SelectAggroTarget target, uint32 position = 0, float dist = 0, bool playerOnly = false, int32 aura = 0);
94  void SelectTargetList(std::list<Unit*>& targetList, uint32 num, SelectAggroTarget target, float dist = 0, bool playerOnly = false, int32 aura = 0);
95 
96  void AttackStartCaster(Unit* victim, float dist);
97 
98  // Called when the creature receives heal
99  virtual void HealReceived(Unit* /*done_by*/, uint32& /*addhealth*/) { }
100 
101  void DoCast(uint32 spellId);
102  void DoCast(Unit* victim, uint32 spellId, bool triggered = false);
103  void DoCastSelf(uint32 spellId, bool triggered = false) { DoCast(me, spellId, triggered); }
104  void DoCastVictim(uint32 spellId, bool triggered = false);
105  void DoCastAOE(uint32 spellId, bool triggered = false);
106 
107  float DoGetSpellMaxRange(uint32 spellId, bool positive = false);
108 
109  void DoMeleeAttackIfReady();
110  bool DoSpellAttackIfReady(uint32 spell);
111 
112  virtual void sGossipHello(Player* /*player*/) { }
113  virtual void sGossipSelect(Player* /*player*/, uint32 /*sender*/, uint32 /*action*/) { }
114  virtual void sGossipSelectCode(Player* /*player*/, uint32 /*sender*/, uint32 /*action*/, char const* /*code*/) { }
115  virtual void QuestAccept(Player* /*player*/, Quest const* /*quest*/) { }
116  virtual void sQuestSelect(Player* /*player*/, Quest const* /*quest*/) { }
117  virtual void sQuestReward(Player* /*player*/, Quest const* /*quest*/, uint32 /*opt*/) { }
118  virtual bool sOnDummyEffect(Unit* /*caster*/, uint32 /*spellId*/, SpellEffIndex /*effIndex*/) { return false; }
119  virtual void sOnGameEvent(bool /*start*/, uint16 /*eventId*/) { }
120 
122  static void FillAISpellInfo();
123  static void ClearAISpellInfo();
124 };
125 
126 class PlayerAI : public UnitAI
127 {
128  protected:
129  Player* const me;
130  public:
131  explicit PlayerAI(Player* p) : UnitAI((Unit*)p), me(p) {}
132 
133  void OnCharmed(bool apply) override;
134 };
135 
136 class SimpleCharmedAI : public PlayerAI
137 {
138  public:
139  void UpdateAI(const uint32 diff) override;
140 };
141 
142 class ScriptEvent : public BasicEvent
143 {
144  public:
145  ScriptEvent(Unit* unit, uint32 data = 0) : BasicEvent(), m_unit(unit), m_data(data) {}
146  bool Execute(uint64, uint32) override
147  {
148  if (m_unit->IsAIEnabled)
149  m_unit->i_AI->EventHappens(m_data);
150  return true;
151  }
152 
153  private:
156 };
157 
158 inline void UnitAI::ScheduleEvent(uint32 data, uint32 delay)
159 {
160  me->m_Events.AddEvent(new ScriptEvent(me, data), me->m_Events.CalculateTime(delay), false);
161 }
162 
163 #endif
164 
virtual void SetData(uint32, uint32)
Definition: UnitAI.h:79
static AISpellInfoType * AISpellInfo
Definition: UnitAI.h:121
virtual void sGossipSelect(Player *, uint32, uint32)
Definition: UnitAI.h:113
virtual void sQuestReward(Player *, Quest const *, uint32)
Definition: UnitAI.h:117
EventProcessor m_Events
Definition: Unit.h:1636
UnitAI(Unit *u)
Definition: UnitAI.h:46
ScriptEvent(Unit *unit, uint32 data=0)
Definition: UnitAI.h:145
virtual ~UnitAI()
Definition: UnitAI.h:47
virtual void SetGUID(const uint64 &, int32 id=0)
Definition: UnitAI.h:80
void DoCastAOE(uint32 spellId, bool triggered=false)
Definition: UnitAI.cpp:262
Unit * m_unit
Definition: UnitAI.h:154
virtual void DoAction(const int32 param=0)
Definition: UnitAI.h:70
virtual void sOnGameEvent(bool, uint16)
Definition: UnitAI.h:119
Player *const me
Definition: UnitAI.h:129
static void ClearAISpellInfo()
Definition: UnitAI.cpp:325
PlayerAI(Player *p)
Definition: UnitAI.h:131
bool Execute(uint64, uint32) override
Definition: UnitAI.h:146
void AddEvent(BasicEvent *Event, uint64 e_time, bool set_addtime=true)
void SelectTargetList(std::list< Unit * > &targetList, uint32 num, SelectAggroTarget target, float dist=0, bool playerOnly=false, int32 aura=0)
Definition: UnitAI.cpp:169
virtual void EventHappens(uint32)
Definition: UnitAI.h:63
virtual void ScheduleEvent(uint32 data, uint32 delay)
Definition: UnitAI.h:158
ACE_INT32 int32
Definition: Define.h:67
virtual bool CanAIAttack(const Unit *) const
Definition: UnitAI.h:49
Unit *const me
Definition: UnitAI.h:44
uint64 CalculateTime(uint64 t_offset)
bool DoSpellAttackIfReady(uint32 spell)
Definition: UnitAI.cpp:71
void apply(T *val)
Definition: ByteConverter.h:41
void DoCastSelf(uint32 spellId, bool triggered=false)
Definition: UnitAI.h:103
virtual void OnCharmed(bool)=0
virtual void sGossipSelectCode(Player *, uint32, uint32, char const *)
Definition: UnitAI.h:114
virtual void InitializeAI()
Definition: UnitAI.h:56
virtual uint32 GetData(uint32 id=0)
Definition: UnitAI.h:74
virtual bool sOnDummyEffect(Unit *, uint32, SpellEffIndex)
Definition: UnitAI.h:118
virtual void UpdateAI(const uint32)=0
SelectAggroTarget
Definition: UnitAI.h:32
DamageEffectType
Definition: Unit.h:537
SpellEffIndex
Definition: SharedDefines.h:24
void DoCast(uint32 spellId)
Definition: UnitAI.cpp:203
static void FillAISpellInfo()
Definition: UnitAI.cpp:272
ACE_UINT64 uint64
Definition: Define.h:70
#define UNUSED(x)
Definition: Common.h:175
bool isDead() const
Definition: Unit.h:1338
virtual void DamageDealt(Unit *, uint32 &, DamageEffectType)
Definition: UnitAI.h:91
void DoMeleeAttackIfReady()
Definition: UnitAI.cpp:45
void DoCastVictim(uint32 spellId, bool triggered=false)
Definition: UnitAI.cpp:256
virtual void HealReceived(Unit *, uint32 &)
Definition: UnitAI.h:99
ACE_UINT16 uint16
Definition: Define.h:72
virtual void QuestAccept(Player *, Quest const *)
Definition: UnitAI.h:115
ACE_UINT32 uint32
Definition: Define.h:71
uint32 m_data
Definition: UnitAI.h:155
virtual void Reset()
Definition: UnitAI.h:61
Definition: Unit.h:908
virtual uint64 GetGUID(int32 id=0)
Definition: UnitAI.h:84
Unit * SelectTarget(SelectAggroTarget target, uint32 position=0, float dist=0, bool playerOnly=false, int32 aura=0)
Definition: UnitAI.cpp:126
Definition: Player.h:922
virtual void AttackStart(Unit *)
Definition: UnitAI.cpp:25
float DoGetSpellMaxRange(uint32 spellId, bool positive=false)
Definition: UnitAI.cpp:198
virtual void sQuestSelect(Player *, Quest const *)
Definition: UnitAI.h:116
Definition: UnitAI.h:41
void AttackStartCaster(Unit *victim, float dist)
Definition: UnitAI.cpp:39
virtual void sGossipHello(Player *)
Definition: UnitAI.h:112