Files
TCPlayerbotCore/src/game/ThreatManager.h
T

237 lines
8.0 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2008-10-14 11:57:03 -05:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2008-10-14 11:57:03 -05:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#ifndef _THREATMANAGER
#define _THREATMANAGER
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "Common.h"
#include "SharedDefines.h"
#include "Utilities/LinkedReference/Reference.h"
#include "UnitEvents.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include <list>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//==============================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
class Unit;
class Creature;
class ThreatManager;
struct SpellEntry;
2009-10-17 15:51:44 -07:00
#define THREAT_UPDATE_INTERVAL 1 * IN_MILISECONDS // Server should send threat update to client periodically each second
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//==============================================================
// Class to calculate the real threat based
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
class ThreatCalcHelper
{
public:
2009-10-18 18:38:46 -07:00
static float calcThreat(Unit* pHatedUnit, Unit* pHatingUnit, float fThreat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//==============================================================
2008-10-14 11:57:03 -05:00
class TRINITY_DLL_SPEC HostilReference : public Reference<Unit, ThreatManager>
2008-10-02 16:23:55 -05:00
{
public:
2009-10-18 18:38:46 -07:00
HostilReference(Unit* pUnit, ThreatManager *pThreatManager, float fThreat);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-18 18:38:46 -07:00
void addThreat(float fModThreat);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void setThreat(float fThreat) { addThreat(fThreat - getThreat()); }
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void addThreatPercent(int32 pPercent)
{
float tmpThreat = iThreat;
tmpThreat = tmpThreat * (pPercent+100.0f) / 100.0f;
addThreat(tmpThreat-iThreat);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
float getThreat() const { return iThreat; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool isOnline() const { return iOnline; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// The Unit might be in water and the creature can not enter the water, but has range attack
2008-11-14 16:28:45 -06:00
// in this case online = true, but accessible = false
bool isAccessible() const { return iAccessible; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// used for temporary setting a threat and reducting it later again.
// the threat modification is stored
2009-10-18 18:38:46 -07:00
void setTempThreat(float fThreat)
{
iTempThreatModifier = fThreat - getThreat();
if (iTempThreatModifier != 0.0f)
addThreat(iTempThreatModifier);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void resetTempThreat()
{
2009-10-18 18:38:46 -07:00
if (iTempThreatModifier != 0.0f)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
addThreat(-iTempThreatModifier);
iTempThreatModifier = 0.0f;
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
float getTempThreatModifier() { return iTempThreatModifier; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
// check, if source can reach target and set the status
void updateOnlineStatus();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void setOnlineOfflineState(bool pIsOnline);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void setAccessibleState(bool pIsAccessible);
//=================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool operator ==(const HostilReference& pHostilReference) const { return pHostilReference.getUnitGuid() == getUnitGuid(); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
uint64 getUnitGuid() const { return iUnitGuid; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
// reference is not needed anymore. realy delete it !
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void removeReference();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
HostilReference* next() { return ((HostilReference* ) Reference<Unit, ThreatManager>::next()); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Tell our refTo (target) object that we have a link
void targetObjectBuildLink();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Tell our refTo (taget) object, that the link is cut
void targetObjectDestroyLink();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Tell our refFrom (source) object, that the link is cut (Target destroyed)
void sourceObjectDestroyLink();
private:
// Inform the source, that the status of that reference was changed
void fireStatusChanged(ThreatRefStatusChangeEvent& pThreatRefStatusChangeEvent);
2009-10-17 15:51:44 -07:00
Unit* getSourceUnit();
private:
float iThreat;
2009-10-18 18:38:46 -07:00
float iTempThreatModifier; // used for taunt
uint64 iUnitGuid;
bool iOnline;
bool iAccessible;
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//==============================================================
class ThreatManager;
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
class TRINITY_DLL_SPEC ThreatContainer
2008-10-02 16:23:55 -05:00
{
private:
std::list<HostilReference*> iThreatList;
bool iDirty;
protected:
friend class ThreatManager;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void remove(HostilReference* pRef) { iThreatList.remove(pRef); }
void addReference(HostilReference* pHostilReference) { iThreatList.push_back(pHostilReference); }
void clearReferences();
// Sort the list if necessary
void update();
public:
ThreatContainer() { iDirty = false; }
~ThreatContainer() { clearReferences(); }
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
HostilReference* addThreat(Unit* pVictim, float fThreat);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void modifyThreatPercent(Unit *pVictim, int32 iPercent);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
HostilReference* selectNextVictim(Creature* pAttacker, HostilReference* pCurrentVictim);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void setDirty(bool pDirty) { iDirty = pDirty; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool isDirty() { return iDirty; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool empty() { return(iThreatList.empty()); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
HostilReference* getMostHated() { return iThreatList.empty() ? NULL : iThreatList.front(); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
HostilReference* getReferenceByTarget(Unit* pVictim);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
std::list<HostilReference*>& getThreatList() { return iThreatList; }
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
class TRINITY_DLL_SPEC ThreatManager
2008-10-02 16:23:55 -05:00
{
public:
friend class HostilReference;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
explicit ThreatManager(Unit *pOwner);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
~ThreatManager() { clearReferences(); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void clearReferences();
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void addThreat(Unit* pVictim, float fThreat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellEntry const *threatSpell = NULL);
void modifyThreatPercent(Unit *pVictim, int32 iPercent);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
float getThreat(Unit *pVictim, bool pAlsoSearchOfflineList = false);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
bool isThreatListEmpty() { return iThreatContainer.empty(); }
2009-10-17 15:51:44 -07:00
void processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent);
2009-10-17 15:51:44 -07:00
bool isNeedUpdateToClient(uint32 time);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
HostilReference* getCurrentVictim() { return iCurrentVictim; }
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
Unit* getOwner() { return iOwner; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Unit* getHostilTarget();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void tauntApply(Unit* pTaunter);
void tauntFadeOut(Unit *pTaunter);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void setCurrentVictim(HostilReference* pHostilReference);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void setDirty(bool bDirty) { iThreatContainer.setDirty(bDirty); }
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
// methods to access the lists from the outside to do some dirty manipulation (scriping and such)
2008-10-02 16:23:55 -05:00
// I hope they are used as little as possible.
2009-01-20 19:59:43 -06:00
std::list<HostilReference*>& getThreatList() { return iThreatContainer.getThreatList(); }
std::list<HostilReference*>& getOfflieThreatList() { return iThreatOfflineContainer.getThreatList(); }
ThreatContainer& getOnlineContainer() { return iThreatContainer; }
ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
private:
2009-10-18 18:38:46 -07:00
void _addThreat(Unit *pVictim, float fThreat);
2009-10-17 15:51:44 -07:00
HostilReference* iCurrentVictim;
Unit* iOwner;
uint32 iUpdateTimer;
ThreatContainer iThreatContainer;
ThreatContainer iThreatOfflineContainer;
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
#endif
2009-02-17 20:07:49 -06:00