[7717] Use more safe code in EventAI.
[AHbot.git] / src / game / ThreatManager.h
blobd569480f935c9875ecf3fd4eb11e8d51cfae5278
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _THREATMANAGER
20 #define _THREATMANAGER
22 #include "Common.h"
23 #include "SharedDefines.h"
24 #include "Utilities/LinkedReference/Reference.h"
25 #include "UnitEvents.h"
27 #include <list>
29 //==============================================================
31 class Unit;
32 class Creature;
33 class ThreatManager;
34 struct SpellEntry;
36 //==============================================================
37 // Class to calculate the real threat based
39 class ThreatCalcHelper
41 public:
42 static float calcThreat(Unit* pHatedUnit, Unit* pHatingUnit, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
45 //==============================================================
46 class MANGOS_DLL_SPEC HostilReference : public Reference<Unit, ThreatManager>
48 public:
49 HostilReference(Unit* pUnit, ThreatManager *pThreatManager, float pThreat);
51 //=================================================
52 void addThreat(float pMod);
54 void setThreat(float pThreat) { addThreat(pThreat - getThreat()); }
56 void addThreatPercent(int32 pPercent) { float tmpThreat = iThreat; tmpThreat = tmpThreat * (pPercent+100) / 100; addThreat(tmpThreat-iThreat); }
58 float getThreat() const { return iThreat; }
60 bool isOnline() const { return iOnline; }
62 // The Unit might be in water and the creature can not enter the water, but has range attack
63 // in this case online = true, but accessable = false
64 bool isAccessable() const { return iAccessible; }
66 // used for temporary setting a threat and reducting it later again.
67 // the threat modification is stored
68 void setTempThreat(float pThreat) { iTempThreatModifyer = pThreat - getThreat(); if(iTempThreatModifyer != 0.0f) addThreat(iTempThreatModifyer); }
70 void resetTempThreat()
72 if(iTempThreatModifyer != 0.0f)
74 addThreat(-iTempThreatModifyer); iTempThreatModifyer = 0.0f;
78 float getTempThreatModifyer() { return iTempThreatModifyer; }
80 //=================================================
81 // check, if source can reach target and set the status
82 void updateOnlineStatus();
84 void setOnlineOfflineState(bool pIsOnline);
86 void setAccessibleState(bool pIsAccessible);
87 //=================================================
89 bool operator ==(const HostilReference& pHostilReference) const { return pHostilReference.getUnitGuid() == getUnitGuid(); }
91 //=================================================
93 uint64 getUnitGuid() const { return iUnitGuid; }
95 //=================================================
96 // reference is not needed anymore. realy delete it !
98 void removeReference();
100 //=================================================
102 HostilReference* next() { return ((HostilReference* ) Reference<Unit, ThreatManager>::next()); }
104 //=================================================
106 // Tell our refTo (target) object that we have a link
107 void targetObjectBuildLink();
109 // Tell our refTo (taget) object, that the link is cut
110 void targetObjectDestroyLink();
112 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
113 void sourceObjectDestroyLink();
114 private:
115 // Inform the source, that the status of that reference was changed
116 void fireStatusChanged(ThreatRefStatusChangeEvent& pThreatRefStatusChangeEvent);
118 Unit* getSourceUnit();
119 private:
120 float iThreat;
121 float iTempThreatModifyer; // used for taunt
122 uint64 iUnitGuid;
123 bool iOnline;
124 bool iAccessible;
127 //==============================================================
128 class ThreatManager;
130 class MANGOS_DLL_SPEC ThreatContainer
132 private:
133 std::list<HostilReference*> iThreatList;
134 bool iDirty;
135 protected:
136 friend class ThreatManager;
138 void remove(HostilReference* pRef) { iThreatList.remove(pRef); }
139 void addReference(HostilReference* pHostilReference) { iThreatList.push_back(pHostilReference); }
140 void clearReferences();
141 // Sort the list if necessary
142 void update();
143 public:
144 ThreatContainer() { iDirty = false; }
145 ~ThreatContainer() { clearReferences(); }
147 HostilReference* addThreat(Unit* pVictim, float pThreat);
149 void modifyThreatPercent(Unit *pVictim, int32 percent);
151 HostilReference* selectNextVictim(Creature* pAttacker, HostilReference* pCurrentVictim);
153 void setDirty(bool pDirty) { iDirty = pDirty; }
155 bool isDirty() { return iDirty; }
157 bool empty() { return(iThreatList.empty()); }
159 HostilReference* getMostHated() { return iThreatList.empty() ? NULL : iThreatList.front(); }
161 HostilReference* getReferenceByTarget(Unit* pVictim);
163 std::list<HostilReference*>& getThreatList() { return iThreatList; }
166 //=================================================
168 class MANGOS_DLL_SPEC ThreatManager
170 public:
171 friend class HostilReference;
173 explicit ThreatManager(Unit *pOwner);
175 ~ThreatManager() { clearReferences(); }
177 void clearReferences();
179 void addThreat(Unit* pVictim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
180 void modifyThreatPercent(Unit *pVictim, int32 pPercent);
182 float getThreat(Unit *pVictim, bool pAlsoSearchOfflineList = false);
184 bool isThreatListEmpty() { return iThreatContainer.empty();}
186 void processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent);
188 HostilReference* getCurrentVictim() { return iCurrentVictim; }
190 Unit* getOwner() { return iOwner; }
192 Unit* getHostilTarget();
194 void tauntApply(Unit* pTaunter);
195 void tauntFadeOut(Unit *pTaunter);
197 void setCurrentVictim(HostilReference* pHostilReference);
199 void setDirty(bool pDirty) { iThreatContainer.setDirty(pDirty); }
201 // methods to access the lists from the outside to do sume dirty manipulation (scriping and such)
202 // I hope they are used as little as possible.
203 std::list<HostilReference*>& getThreatList() { return iThreatContainer.getThreatList(); }
204 std::list<HostilReference*>& getOfflieThreatList() { return iThreatOfflineContainer.getThreatList(); }
205 ThreatContainer& getOnlineContainer() { return iThreatContainer; }
206 ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
207 private:
208 HostilReference* iCurrentVictim;
209 Unit* iOwner;
210 ThreatContainer iThreatContainer;
211 ThreatContainer iThreatOfflineContainer;
214 //=================================================
215 #endif