Files
TCPlayerbotCore/src/server/game/Combat/ThreatManager.h
T

278 lines
9.2 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* 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 MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the 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, see <http://www.gnu.org/licenses/>.
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"
2010-06-08 13:05:47 -06:00
#include "SharedDefines.h"
#include "LinkedReference/Reference.h"
2010-06-08 13:05:47 -06:00
#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_MILLISECONDS // 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
//==============================================================
2010-03-09 11:11:10 +01:00
class HostileReference : public Reference<Unit, ThreatManager>
2008-10-02 16:23:55 -05:00
{
public:
2010-03-09 11:11:10 +01:00
HostileReference(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)
{
addTempThreat(fThreat - getThreat());
}
void addTempThreat(float fThreat)
{
iTempThreatModifier = fThreat;
2009-10-18 18:38:46 -07:00
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
2010-04-07 23:56:35 +02:00
bool operator == (const HostileReference& pHostileReference) const { return pHostileReference.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
2010-04-07 22:59:46 +02:00
HostileReference* next() { return ((HostileReference*) 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
2010-02-05 00:31:28 +01:00
class ThreatContainer
2008-10-02 16:23:55 -05:00
{
private:
2010-03-09 11:11:10 +01:00
std::list<HostileReference*> iThreatList;
2008-10-02 16:23:55 -05:00
bool iDirty;
protected:
friend class ThreatManager;
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void remove(HostileReference* pRef) { iThreatList.remove(pRef); }
void addReference(HostileReference* pHostileReference) { iThreatList.push_back(pHostileReference); }
2008-10-02 16:23:55 -05:00
void clearReferences();
// Sort the list if necessary
void update();
public:
ThreatContainer() { iDirty = false; }
~ThreatContainer() { clearReferences(); }
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* 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
2010-03-09 11:11:10 +01:00
HostileReference* selectNextVictim(Creature* pAttacker, HostileReference* 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
2010-12-06 02:07:53 +01:00
bool isDirty() const { return iDirty; }
2009-10-17 15:51:44 -07:00
2010-12-06 02:07:53 +01:00
bool empty() const { return(iThreatList.empty()); }
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* getMostHated() { return iThreatList.empty() ? NULL : iThreatList.front(); }
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* getReferenceByTarget(Unit* pVictim);
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
std::list<HostileReference*>& getThreatList() { return iThreatList; }
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//=================================================
2009-10-17 15:51:44 -07:00
2010-02-05 00:31:28 +01:00
class ThreatManager
2008-10-02 16:23:55 -05:00
{
public:
2010-03-09 11:11:10 +01:00
friend class HostileReference;
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
2010-03-09 11:11:10 +01:00
HostileReference* 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
2010-03-09 11:11:10 +01:00
void setCurrentVictim(HostileReference* pHostileReference);
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
// Reset all aggro without modifying the threadlist.
void resetAllAggro();
// Reset all aggro of unit in threadlist satisfying the predicate.
template<class PREDICATE> void resetAggro(PREDICATE predicate)
{
2010-03-09 11:11:10 +01:00
std::list<HostileReference*> &threatlist = getThreatList();
if (threatlist.empty())
return;
2010-03-09 11:11:10 +01:00
for (std::list<HostileReference*>::iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
{
2010-03-09 11:11:10 +01:00
HostileReference* ref = (*itr);
if (predicate(ref->getTarget()))
{
ref->setThreat(0);
setDirty(true);
}
}
}
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.
2010-03-09 11:11:10 +01:00
std::list<HostileReference*>& getThreatList() { return iThreatContainer.getThreatList(); }
std::list<HostileReference*>& getOfflieThreatList() { return iThreatOfflineContainer.getThreatList(); }
2009-01-20 19:59:43 -06:00
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
2010-03-09 11:11:10 +01:00
HostileReference* 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
//=================================================
namespace Trinity
{
// Binary predicate for sorting HostileReferences based on threat value
class ThreatOrderPred
{
public:
ThreatOrderPred(bool ascending = false) : m_ascending(ascending) {}
bool operator() (const HostileReference *a, const HostileReference *b) const
{
return m_ascending ? a->getThreat() < b->getThreat() : a->getThreat() > b->getThreat();
}
private:
const bool m_ascending;
};
}
2008-10-02 16:23:55 -05:00
#endif
2009-02-17 20:07:49 -06:00