Character re-customization fix
[getmangos.git] / src / game / ThreatManager.h
blobe5e3fdcfa04bf0fb38bc09fc51a187b5a595a463
1 /*
2 * Copyright (C) 2005-2008 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 //==============================================================
47 class MANGOS_DLL_SPEC HostilReference : public Reference<Unit, ThreatManager>
49 private:
50 float iThreat;
51 float iTempThreatModifyer; // used for taunt
52 uint64 iUnitGuid;
53 bool iOnline;
54 bool iAccessible;
55 private:
56 // Inform the source, that the status of that reference was changed
57 void fireStatusChanged(const ThreatRefStatusChangeEvent& pThreatRefStatusChangeEvent);
59 Unit* getSourceUnit();
60 public:
61 HostilReference(Unit* pUnit, ThreatManager *pThreatManager, float pThreat);
63 //=================================================
64 void addThreat(float pMod);
66 void setThreat(float pThreat) { addThreat(pThreat - getThreat()); }
68 void addThreatPercent(int32 pPercent) { float tmpThreat = iThreat; tmpThreat = tmpThreat * (pPercent+100) / 100; addThreat(tmpThreat-iThreat); }
70 float getThreat() const { return iThreat; }
72 bool isOnline() const { return iOnline; }
74 // The Unit might be in water and the creature can not enter the water, but has range attack
75 // in this case online = true, but accessable = false
76 bool isAccessable() const { return iAccessible; }
78 // used for temporary setting a threat and reducting it later again.
79 // the threat modification is stored
80 void setTempThreat(float pThreat) { iTempThreatModifyer = pThreat - getThreat(); if(iTempThreatModifyer != 0.0f) addThreat(iTempThreatModifyer); }
82 void resetTempThreat()
84 if(iTempThreatModifyer != 0.0f)
86 addThreat(-iTempThreatModifyer); iTempThreatModifyer = 0.0f;
90 float getTempThreatModifyer() { return iTempThreatModifyer; }
92 //=================================================
93 // check, if source can reach target and set the status
94 void updateOnlineStatus();
96 void setOnlineOfflineState(bool pIsOnline);
98 void setAccessibleState(bool pIsAccessible);
99 //=================================================
101 bool operator ==(const HostilReference& pHostilReference) const { return pHostilReference.getUnitGuid() == getUnitGuid(); }
103 //=================================================
105 uint64 getUnitGuid() const { return iUnitGuid; }
107 //=================================================
108 // reference is not needed anymore. realy delete it !
110 void removeReference();
112 //=================================================
114 HostilReference* next() { return ((HostilReference* ) Reference<Unit, ThreatManager>::next()); }
116 //=================================================
118 // Tell our refTo (target) object that we have a link
119 void targetObjectBuildLink();
121 // Tell our refTo (taget) object, that the link is cut
122 void targetObjectDestroyLink();
124 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
125 void sourceObjectDestroyLink();
128 //==============================================================
129 class ThreatManager;
131 class MANGOS_DLL_SPEC ThreatContainer
133 private:
134 std::list<HostilReference*> iThreatList;
135 bool iDirty;
136 protected:
137 friend class ThreatManager;
139 void remove(HostilReference* pRef) { iThreatList.remove(pRef); }
140 void addReference(HostilReference* pHostilReference) { iThreatList.push_back(pHostilReference); }
141 void clearReferences();
142 // Sort the list if necessary
143 void update();
144 public:
145 ThreatContainer() { iDirty = false; }
146 ~ThreatContainer() { clearReferences(); }
148 HostilReference* addThreat(Unit* pVictim, float pThreat);
150 void modifyThreatPercent(Unit *pVictim, int32 percent);
152 HostilReference* selectNextVictim(Creature* pAttacker, HostilReference* pCurrentVictim);
154 void setDirty(bool pDirty) { iDirty = pDirty; }
156 bool isDirty() { return iDirty; }
158 bool empty() { return(iThreatList.empty()); }
160 HostilReference* getMostHated() { return iThreatList.empty() ? NULL : iThreatList.front(); }
162 HostilReference* getReferenceByTarget(Unit* pVictim);
164 std::list<HostilReference*>& getThreatList() { return iThreatList; }
167 //=================================================
169 class MANGOS_DLL_SPEC ThreatManager
171 private:
172 HostilReference* iCurrentVictim;
173 Unit* iOwner;
174 ThreatContainer iThreatContainer;
175 ThreatContainer iThreatOfflineContainer;
176 public:
177 explicit ThreatManager(Unit *pOwner);
179 ~ThreatManager() { clearReferences(); }
181 void clearReferences();
183 void addThreat(Unit* pVictim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
184 void modifyThreatPercent(Unit *pVictim, int32 pPercent);
186 float getThreat(Unit *pVictim, bool pAlsoSearchOfflineList = false);
188 bool isThreatListEmpty() { return iThreatContainer.empty();}
190 bool processThreatEvent(const UnitBaseEvent* pUnitBaseEvent);
192 HostilReference* getCurrentVictim() { return iCurrentVictim; }
194 Unit* getOwner() { return iOwner; }
196 Unit* getHostilTarget();
198 void tauntApply(Unit* pTaunter);
199 void tauntFadeOut(Unit *pTaunter);
201 void setCurrentVictim(HostilReference* pHostilReference);
203 void setDirty(bool pDirty) { iThreatContainer.setDirty(pDirty); }
205 // methods to access the lists from the outside to do sume dirty manipulation (scriping and such)
206 // I hope they are used as little as possible.
207 inline std::list<HostilReference*>& getThreatList() { return iThreatContainer.getThreatList(); }
208 inline std::list<HostilReference*>& getOfflieThreatList() { return iThreatOfflineContainer.getThreatList(); }
209 inline ThreatContainer& getOnlineContainer() { return iThreatContainer; }
210 inline ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
213 //=================================================
214 #endif