TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ThreatManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _THREATMANAGER
20 #define _THREATMANAGER
21 
22 #include "Common.h"
23 #include "SharedDefines.h"
25 #include "UnitEvents.h"
26 #include "ObjectGuid.h"
27 
28 #include <list>
29 
30 //==============================================================
31 
32 class Unit;
33 class Creature;
34 class ThreatManager;
35 class SpellInfo;
36 
37 #define THREAT_UPDATE_INTERVAL 1 * IN_MILLISECONDS // Server should send threat update to client periodically each second
38 
39 //==============================================================
40 // Class to calculate the real threat based
41 
43 {
44  static float calcThreat(Unit* hatedUnit, Unit* hatingUnit, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = NULL);
45  static bool isValidProcess(Unit* hatedUnit, Unit* hatingUnit, SpellInfo const* threatSpell = NULL);
46 };
47 
48 //==============================================================
49 class TC_GAME_API HostileReference : public Reference<Unit, ThreatManager>
50 {
51  public:
52  HostileReference(Unit* refUnit, ThreatManager* threatManager, float threat);
53 
54  //=================================================
55  void addThreat(float modThreat);
56 
57  void setThreat(float threat) { addThreat(threat - getThreat()); }
58 
59  void addThreatPercent(int32 percent);
60 
61  float getThreat() const { return iThreat; }
62 
63  bool isOnline() const { return iOnline; }
64 
65  // The Unit might be in water and the creature can not enter the water, but has range attack
66  // in this case online = true, but accessible = false
67  bool isAccessible() const { return iAccessible; }
68 
69  // used for temporary setting a threat and reducting it later again.
70  // the threat modification is stored
71  void setTempThreat(float threat)
72  {
73  addTempThreat(threat - getThreat());
74  }
75 
76  void addTempThreat(float threat)
77  {
78  iTempThreatModifier = threat;
79  if (iTempThreatModifier != 0.0f)
80  addThreat(iTempThreatModifier);
81  }
82 
84  {
85  if (iTempThreatModifier != 0.0f)
86  {
87  addThreat(-iTempThreatModifier);
88  iTempThreatModifier = 0.0f;
89  }
90  }
91 
92  float getTempThreatModifier() { return iTempThreatModifier; }
93 
94  //=================================================
95  // check, if source can reach target and set the status
96  void updateOnlineStatus();
97 
98  void setOnlineOfflineState(bool isOnline);
99 
100  void setAccessibleState(bool isAccessible);
101  //=================================================
102 
103  bool operator == (const HostileReference& hostileRef) const { return hostileRef.getUnitGuid() == getUnitGuid(); }
104 
105  //=================================================
106 
107  ObjectGuid getUnitGuid() const { return iUnitGuid; }
108 
109  //=================================================
110  // reference is not needed anymore. realy delete it !
111 
112  void removeReference();
113 
114  //=================================================
115 
117 
118  //=================================================
119 
120  // Tell our refTo (target) object that we have a link
121  void targetObjectBuildLink() override;
122 
123  // Tell our refTo (taget) object, that the link is cut
124  void targetObjectDestroyLink() override;
125 
126  // Tell our refFrom (source) object, that the link is cut (Target destroyed)
127  void sourceObjectDestroyLink() override;
128  private:
129  // Inform the source, that the status of that reference was changed
130  void fireStatusChanged(ThreatRefStatusChangeEvent& threatRefStatusChangeEvent);
131 
132  Unit* GetSourceUnit();
133  private:
134  float iThreat;
135  float iTempThreatModifier; // used for taunt
137  bool iOnline;
139 };
140 
141 //==============================================================
142 class ThreatManager;
143 
145 {
146  friend class ThreatManager;
147 
148  public:
149  typedef std::list<HostileReference*> StorageType;
150 
151  ThreatContainer(): iDirty(false) { }
152 
154 
155  HostileReference* addThreat(Unit* victim, float threat);
156 
157  void modifyThreatPercent(Unit* victim, int32 percent);
158 
159  HostileReference* selectNextVictim(Creature* attacker, HostileReference* currentVictim) const;
160 
161  void setDirty(bool isDirty) { iDirty = isDirty; }
162 
163  bool isDirty() const { return iDirty; }
164 
165  bool empty() const
166  {
167  return iThreatList.empty();
168  }
169 
171  {
172  return iThreatList.empty() ? NULL : iThreatList.front();
173  }
174 
175  HostileReference* getReferenceByTarget(Unit* victim) const;
176 
177  StorageType const & getThreatList() const { return iThreatList; }
178 
179  private:
180  void remove(HostileReference* hostileRef)
181  {
182  iThreatList.remove(hostileRef);
183  }
184 
185  void addReference(HostileReference* hostileRef)
186  {
187  iThreatList.push_back(hostileRef);
188  }
189 
190  void clearReferences();
191 
192  // Sort the list if necessary
193  void update();
194 
195  StorageType iThreatList;
196  bool iDirty;
197 };
198 
199 //=================================================
200 
202 {
203  public:
204  friend class HostileReference;
205 
206  explicit ThreatManager(Unit* owner);
207 
208  ~ThreatManager() { clearReferences(); }
209 
210  void clearReferences();
211 
212  void addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = NULL);
213 
214  void doAddThreat(Unit* victim, float threat);
215 
216  void modifyThreatPercent(Unit* victim, int32 percent);
217 
218  float getThreat(Unit* victim, bool alsoSearchOfflineList = false);
219 
220  bool isThreatListEmpty() const { return iThreatContainer.empty(); }
221 
222  void processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent);
223 
224  bool isNeedUpdateToClient(uint32 time);
225 
226  HostileReference* getCurrentVictim() const { return iCurrentVictim; }
227 
228  Unit* GetOwner() const { return iOwner; }
229 
230  Unit* getHostilTarget();
231 
232  void tauntApply(Unit* taunter);
233  void tauntFadeOut(Unit* taunter);
234 
235  void setCurrentVictim(HostileReference* hostileRef);
236 
237  void setDirty(bool isDirty) { iThreatContainer.setDirty(isDirty); }
238 
239  // Reset all aggro without modifying the threadlist.
240  void resetAllAggro();
241 
242  // Reset all aggro of unit in threadlist satisfying the predicate.
243  template<class PREDICATE> void resetAggro(PREDICATE predicate)
244  {
245  ThreatContainer::StorageType &threatList = iThreatContainer.iThreatList;
246  if (threatList.empty())
247  return;
248 
249  for (ThreatContainer::StorageType::iterator itr = threatList.begin(); itr != threatList.end(); ++itr)
250  {
251  HostileReference* ref = (*itr);
252 
253  if (predicate(ref->getTarget()))
254  {
255  ref->setThreat(0);
256  setDirty(true);
257  }
258  }
259  }
260 
261  // methods to access the lists from the outside to do some dirty manipulation (scriping and such)
262  // I hope they are used as little as possible.
263  ThreatContainer::StorageType const & getThreatList() const { return iThreatContainer.getThreatList(); }
264  ThreatContainer::StorageType const & getOfflineThreatList() const { return iThreatOfflineContainer.getThreatList(); }
265  ThreatContainer& getOnlineContainer() { return iThreatContainer; }
266  ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
267  private:
268  void _addThreat(Unit* victim, float threat);
269 
275 };
276 
277 //=================================================
278 
279 namespace Trinity
280 {
281  // Binary predicate for sorting HostileReferences based on threat value
283  {
284  public:
285  ThreatOrderPred(bool ascending = false) : m_ascending(ascending) { }
286  bool operator() (HostileReference const* a, HostileReference const* b) const
287  {
288  return m_ascending ? a->getThreat() < b->getThreat() : a->getThreat() > b->getThreat();
289  }
290  private:
291  const bool m_ascending;
292  };
293 }
294 #endif
bool isOnline() const
Definition: ThreatManager.h:63
HostileReference * getCurrentVictim() const
Definition: ThreatManager.h:226
ThreatContainer & getOnlineContainer()
Definition: ThreatManager.h:265
void modifyThreatPercent(Unit *victim, int32 percent)
Definition: ThreatManager.cpp:452
StorageType iThreatList
Definition: ThreatManager.h:195
void addThreat(Unit *victim, float threat, SpellSchoolMask schoolMask=SPELL_SCHOOL_MASK_NORMAL, SpellInfo const *threatSpell=NULL)
Definition: ThreatManager.cpp:406
void addThreat(float modThreat)
Definition: ThreatManager.cpp:136
SpellSchoolMask
Definition: SharedDefines.h:285
Definition: SpellInfo.h:326
virtual void targetObjectDestroyLink()=0
float getThreat() const
Definition: ThreatManager.h:61
Definition: ThreatManager.h:282
bool empty() const
Definition: ThreatManager.h:165
arena_t NULL
Definition: jemalloc_internal.h:624
ThreatContainer::StorageType const & getThreatList() const
Definition: ThreatManager.h:263
#define false
Definition: CascPort.h:18
bool operator()(HostileReference const *a, HostileReference const *b) const
Definition: ThreatManager.h:286
virtual void sourceObjectDestroyLink()=0
void setDirty(bool isDirty)
Definition: ThreatManager.h:237
void setTempThreat(float threat)
Definition: ThreatManager.h:71
Definition: ThreatManager.h:201
Definition: Creature.h:467
void resetAggro(PREDICATE predicate)
Definition: ThreatManager.h:243
bool isThreatListEmpty() const
Definition: ThreatManager.h:220
void addReference(HostileReference *hostileRef)
Definition: ThreatManager.h:185
uint32 iUpdateTimer
Definition: ThreatManager.h:272
ObjectGuid getUnitGuid() const
Definition: ThreatManager.h:107
Definition: ThreatManager.h:42
float iThreat
Definition: ThreatManager.h:134
void setThreat(float threat)
Definition: ThreatManager.h:57
float iTempThreatModifier
Definition: ThreatManager.h:135
bool iAccessible
Definition: ThreatManager.h:138
float getTempThreatModifier()
Definition: ThreatManager.h:92
~ThreatContainer()
Definition: ThreatManager.h:153
Definition: SharedDefines.h:288
ThreatContainer::StorageType const & getOfflineThreatList() const
Definition: ThreatManager.h:264
ThreatContainer iThreatOfflineContainer
Definition: ThreatManager.h:274
ThreatContainer()
Definition: ThreatManager.h:151
HostileReference * next()
Definition: ThreatManager.h:116
ThreatContainer iThreatContainer
Definition: ThreatManager.h:273
Definition: ThreatManager.h:49
int32_t int32
Definition: Define.h:146
bool operator==(const CoordPair< LIMIT > &p1, const CoordPair< LIMIT > &p2)
Definition: GridDefines.h:160
Definition: Reference.h:27
ObjectGuid iUnitGuid
Definition: ThreatManager.h:136
uint32_t uint32
Definition: Define.h:150
ThreatOrderPred(bool ascending=false)
Definition: ThreatManager.h:285
void clearReferences()
Definition: ThreatManager.cpp:396
HostileReference * iCurrentVictim
Definition: ThreatManager.h:270
const bool m_ascending
Definition: ThreatManager.h:291
Definition: ThreatManager.h:144
std::list< HostileReference * > StorageType
Definition: ThreatManager.h:149
~ThreatManager()
Definition: ThreatManager.h:208
#define TC_GAME_API
Definition: Define.h:134
virtual void targetObjectBuildLink()=0
StorageType const & getThreatList() const
Definition: ThreatManager.h:177
bool iDirty
Definition: ThreatManager.h:196
Unit * GetOwner() const
Definition: ThreatManager.h:228
bool isAccessible() const
Definition: ThreatManager.h:67
HostileReference * getMostHated() const
Definition: ThreatManager.h:170
Definition: ObjectGuid.h:189
Definition: Common.h:172
void resetTempThreat()
Definition: ThreatManager.h:83
Unit * iOwner
Definition: ThreatManager.h:271
Definition: Unit.h:1305
void setDirty(bool isDirty)
Definition: ThreatManager.h:161
bool isDirty() const
Definition: ThreatManager.h:163
Definition: UnitEvents.h:84
ThreatContainer & getOfflineContainer()
Definition: ThreatManager.h:266
void addTempThreat(float threat)
Definition: ThreatManager.h:76
bool iOnline
Definition: ThreatManager.h:137
TO * getTarget() const
Definition: Reference.h:94