root/trunk/src/game/ThreatManager.h @ 37

Revision 2, 7.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#ifndef _THREATMANAGER
20#define _THREATMANAGER
21
22#include "Common.h"
23#include "SharedDefines.h"
24#include "Utilities/LinkedReference/Reference.h"
25#include "UnitEvents.h"
26
27#include <list>
28
29//==============================================================
30
31class Unit;
32class Creature;
33class ThreatManager;
34struct SpellEntry;
35
36//==============================================================
37// Class to calculate the real threat based
38
39class ThreatCalcHelper
40{
41    public:
42        static float calcThreat(Unit* pHatedUnit, Unit* pHatingUnit, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
43};
44
45//==============================================================
46
47class MANGOS_DLL_SPEC HostilReference : public Reference<Unit, ThreatManager>
48{
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);
58
59        Unit* getSourceUnit();
60    public:
61        HostilReference(Unit* pUnit, ThreatManager *pThreatManager, float pThreat);
62
63        //=================================================
64        void addThreat(float pMod);
65
66        void setThreat(float pThreat) { addThreat(pThreat - getThreat()); }
67
68        void addThreatPercent(int32 pPercent) { float tmpThreat = iThreat; tmpThreat = tmpThreat * (pPercent+100) / 100; addThreat(tmpThreat-iThreat); }
69
70        float getThreat() const { return iThreat; }
71
72        bool isOnline() const { return iOnline; }
73
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; }
77
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);  }
81
82        void resetTempThreat()
83        {
84            if(iTempThreatModifyer != 0.0f)
85            {
86                addThreat(-iTempThreatModifyer);  iTempThreatModifyer = 0.0f;
87            }
88        }
89
90        float getTempThreatModifyer() { return iTempThreatModifyer; }
91
92        //=================================================
93        // check, if source can reach target and set the status
94        void updateOnlineStatus();
95
96        void setOnlineOfflineState(bool pIsOnline);
97
98        void setAccessibleState(bool pIsAccessible);
99        //=================================================
100
101        bool operator ==(const HostilReference& pHostilReference) const { return pHostilReference.getUnitGuid() == getUnitGuid(); }
102
103        //=================================================
104
105        uint64 getUnitGuid() const { return iUnitGuid; }
106
107        //=================================================
108        // reference is not needed anymore. realy delete it !
109
110        void removeReference();
111
112        //=================================================
113
114        HostilReference* next() { return ((HostilReference* ) Reference<Unit, ThreatManager>::next()); }
115
116        //=================================================
117
118        // Tell our refTo (target) object that we have a link
119        void targetObjectBuildLink();
120
121        // Tell our refTo (taget) object, that the link is cut
122        void targetObjectDestroyLink();
123
124        // Tell our refFrom (source) object, that the link is cut (Target destroyed)
125        void sourceObjectDestroyLink();
126};
127
128//==============================================================
129class ThreatManager;
130
131class MANGOS_DLL_SPEC ThreatContainer
132{
133    private:
134        std::list<HostilReference*> iThreatList;
135        bool iDirty;
136    protected:
137        friend class ThreatManager;
138
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(); }
147
148        HostilReference* addThreat(Unit* pVictim, float pThreat);
149
150        void modifyThreatPercent(Unit *pVictim, int32 percent);
151
152        HostilReference* selectNextVictim(Creature* pAttacker, HostilReference* pCurrentVictim);
153
154        void setDirty(bool pDirty) { iDirty = pDirty; }
155
156        bool isDirty() { return iDirty; }
157
158        bool empty() { return(iThreatList.empty()); }
159
160        HostilReference* getMostHated() { return iThreatList.empty() ? NULL : iThreatList.front(); }
161
162        HostilReference* getReferenceByTarget(Unit* pVictim);
163
164        std::list<HostilReference*>& getThreatList() { return iThreatList; }
165};
166
167//=================================================
168
169class MANGOS_DLL_SPEC ThreatManager
170{
171    private:
172        HostilReference* iCurrentVictim;
173        Unit* iOwner;
174        ThreatContainer iThreatContainer;
175        ThreatContainer iThreatOfflineContainer;
176    public:
177        explicit ThreatManager(Unit *pOwner);
178
179        ~ThreatManager() { clearReferences(); }
180
181        void clearReferences();
182
183        void addThreat(Unit* pVictim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
184        void modifyThreatPercent(Unit *pVictim, int32 pPercent);
185
186        float getThreat(Unit *pVictim, bool pAlsoSearchOfflineList = false);
187
188        bool isThreatListEmpty() { return iThreatContainer.empty();}
189
190        bool processThreatEvent(const UnitBaseEvent* pUnitBaseEvent);
191
192        HostilReference* getCurrentVictim() { return iCurrentVictim; }
193
194        Unit*  getOwner() { return iOwner; }
195
196        Unit* getHostilTarget();
197
198        void tauntApply(Unit* pTaunter);
199        void tauntFadeOut(Unit *pTaunter);
200
201        void setCurrentVictim(HostilReference* pHostilReference);
202
203        void setDirty(bool pDirty) { iThreatContainer.setDirty(pDirty); }
204
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; }
211};
212
213//=================================================
214#endif
Note: See TracBrowser for help on using the browser.