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