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
23 #include "SharedDefines.h"
24 #include "Utilities/LinkedReference/Reference.h"
25 #include "UnitEvents.h"
29 //==============================================================
36 #define THREAT_UPDATE_INTERVAL 1 * IN_MILISECONDS // Server should send threat update to client periodically each second
38 //==============================================================
39 // Class to calculate the real threat based
41 class ThreatCalcHelper
44 static float calcThreat(Unit
* pHatedUnit
, Unit
* pHatingUnit
, float threat
, bool crit
, SpellSchoolMask schoolMask
, SpellEntry
const *threatSpell
);
47 //==============================================================
48 class MANGOS_DLL_SPEC HostileReference
: public Reference
<Unit
, ThreatManager
>
51 HostileReference(Unit
* pUnit
, ThreatManager
*pThreatManager
, float pThreat
);
53 //=================================================
54 void addThreat(float pMod
);
56 void setThreat(float pThreat
) { addThreat(pThreat
- getThreat()); }
58 void addThreatPercent(int32 pPercent
) { float tmpThreat
= iThreat
; tmpThreat
= tmpThreat
* (pPercent
+100) / 100; addThreat(tmpThreat
-iThreat
); }
60 float getThreat() const { return iThreat
; }
62 bool isOnline() const { return iOnline
; }
64 // The Unit might be in water and the creature can not enter the water, but has range attack
65 // in this case online = true, but accessable = false
66 bool isAccessable() const { return iAccessible
; }
68 // used for temporary setting a threat and reducting it later again.
69 // the threat modification is stored
70 void setTempThreat(float pThreat
) { iTempThreatModifyer
= pThreat
- getThreat(); if(iTempThreatModifyer
!= 0.0f
) addThreat(iTempThreatModifyer
); }
72 void resetTempThreat()
74 if(iTempThreatModifyer
!= 0.0f
)
76 addThreat(-iTempThreatModifyer
); iTempThreatModifyer
= 0.0f
;
80 float getTempThreatModifyer() { return iTempThreatModifyer
; }
82 //=================================================
83 // check, if source can reach target and set the status
84 void updateOnlineStatus();
86 void setOnlineOfflineState(bool pIsOnline
);
88 void setAccessibleState(bool pIsAccessible
);
89 //=================================================
91 bool operator ==(const HostileReference
& pHostileReference
) const { return pHostileReference
.getUnitGuid() == getUnitGuid(); }
93 //=================================================
95 uint64
getUnitGuid() const { return iUnitGuid
; }
97 //=================================================
98 // reference is not needed anymore. realy delete it !
100 void removeReference();
102 //=================================================
104 HostileReference
* next() { return ((HostileReference
* ) Reference
<Unit
, ThreatManager
>::next()); }
106 //=================================================
108 // Tell our refTo (target) object that we have a link
109 void targetObjectBuildLink();
111 // Tell our refTo (taget) object, that the link is cut
112 void targetObjectDestroyLink();
114 // Tell our refFrom (source) object, that the link is cut (Target destroyed)
115 void sourceObjectDestroyLink();
117 // Inform the source, that the status of that reference was changed
118 void fireStatusChanged(ThreatRefStatusChangeEvent
& pThreatRefStatusChangeEvent
);
120 Unit
* getSourceUnit();
123 float iTempThreatModifyer
; // used for taunt
129 //==============================================================
132 typedef std::list
<HostileReference
*> ThreatList
;
135 class MANGOS_DLL_SPEC ThreatContainer
138 ThreatList iThreatList
;
141 friend class ThreatManager
;
143 void remove(HostileReference
* pRef
) { iThreatList
.remove(pRef
); }
144 void addReference(HostileReference
* pHostileReference
) { iThreatList
.push_back(pHostileReference
); }
145 void clearReferences();
146 // Sort the list if necessary
149 ThreatContainer() { iDirty
= false; }
150 ~ThreatContainer() { clearReferences(); }
152 HostileReference
* addThreat(Unit
* pVictim
, float pThreat
);
154 void modifyThreatPercent(Unit
*pVictim
, int32 percent
);
156 HostileReference
* selectNextVictim(Creature
* pAttacker
, HostileReference
* pCurrentVictim
);
158 void setDirty(bool pDirty
) { iDirty
= pDirty
; }
160 bool isDirty() const { return iDirty
; }
162 bool empty() const { return(iThreatList
.empty()); }
164 HostileReference
* getMostHated() { return iThreatList
.empty() ? NULL
: iThreatList
.front(); }
166 HostileReference
* getReferenceByTarget(Unit
* pVictim
);
168 ThreatList
const& getThreatList() const { return iThreatList
; }
171 //=================================================
173 class MANGOS_DLL_SPEC ThreatManager
176 friend class HostileReference
;
178 explicit ThreatManager(Unit
*pOwner
);
180 ~ThreatManager() { clearReferences(); }
182 void clearReferences();
184 void addThreat(Unit
* pVictim
, float threat
, bool crit
, SpellSchoolMask schoolMask
, SpellEntry
const *threatSpell
);
185 void addThreat(Unit
* pVictim
, float threat
) { addThreat(pVictim
,threat
,false,SPELL_SCHOOL_MASK_NONE
,NULL
); }
186 void modifyThreatPercent(Unit
*pVictim
, int32 pPercent
);
188 float getThreat(Unit
*pVictim
, bool pAlsoSearchOfflineList
= false);
190 bool isThreatListEmpty() const { return iThreatContainer
.empty(); }
192 void processThreatEvent(ThreatRefStatusChangeEvent
* threatRefStatusChangeEvent
);
194 void UpdateForClient(uint32 time
);
196 HostileReference
* getCurrentVictim() { return iCurrentVictim
; }
198 Unit
* getOwner() const { return iOwner
; }
200 Unit
* getHostileTarget();
202 void tauntApply(Unit
* pTaunter
);
203 void tauntFadeOut(Unit
*pTaunter
);
205 void setCurrentVictim(HostileReference
* pHostileReference
);
207 void setDirty(bool pDirty
) { iThreatContainer
.setDirty(pDirty
); }
209 // Don't must be used for explicit modify threat values in iterator return pointers
210 ThreatList
const& getThreatList() const { return iThreatContainer
.getThreatList(); }
212 HostileReference
* iCurrentVictim
;
214 TimeTrackerSmall iUpdateTimer
;
216 ThreatContainer iThreatContainer
;
217 ThreatContainer iThreatOfflineContainer
;
220 //=================================================