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