First Commit

For Azeroth!
This commit is contained in:
Yehonal
2016-06-26 10:39:44 +02:00
commit e8e94a0a66
3777 changed files with 1419268 additions and 0 deletions

View File

@@ -0,0 +1,219 @@
/*
* Copyright (C)
* Copyright (C)
*
* 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/>.
*/
#include "HostileRefManager.h"
#include "ThreatManager.h"
#include "Unit.h"
#include "DBCStructure.h"
#include "SpellMgr.h"
#include "SpellInfo.h"
HostileRefManager::~HostileRefManager()
{
deleteReferences();
}
//=================================================
// send threat to all my hateres for the victim
// The victim is hated than by them as well
// use for buffs and healing threat functionality
void HostileRefManager::threatAssist(Unit* victim, float baseThreat, SpellInfo const* threatSpell)
{
if (getSize() == 0)
return;
HostileReference* ref = getFirst();
float threat = ThreatCalcHelper::calcThreat(victim, iOwner, baseThreat, (threatSpell ? threatSpell->GetSchoolMask() : SPELL_SCHOOL_MASK_NORMAL), threatSpell);
threat /= getSize();
while (ref)
{
if (ThreatCalcHelper::isValidProcess(victim, ref->GetSource()->GetOwner(), threatSpell))
ref->GetSource()->doAddThreat(victim, threat);
ref = ref->next();
}
}
//=================================================
void HostileRefManager::addTempThreat(float threat, bool apply)
{
HostileReference* ref = getFirst();
while (ref)
{
if (apply)
{
if (ref->getTempThreatModifier() == 0.0f)
ref->addTempThreat(threat);
}
else
ref->resetTempThreat();
ref = ref->next();
}
}
//=================================================
void HostileRefManager::addThreatPercent(int32 percent)
{
HostileReference* ref = getFirst();
while (ref)
{
ref->addThreatPercent(percent);
ref = ref->next();
}
}
//=================================================
// The online / offline status is given to the method. The calculation has to be done before
void HostileRefManager::setOnlineOfflineState(bool isOnline)
{
HostileReference* ref = getFirst();
while (ref)
{
ref->setOnlineOfflineState(isOnline);
ref = ref->next();
}
}
//=================================================
// The online / offline status is calculated and set
void HostileRefManager::updateThreatTables()
{
HostileReference* ref = getFirst();
while (ref)
{
ref->updateOnlineStatus();
ref = ref->next();
}
}
//=================================================
// The references are not needed anymore
// tell the source to remove them from the list and free the mem
void HostileRefManager::deleteReferences()
{
HostileReference* ref = getFirst();
while (ref)
{
HostileReference* nextRef = ref->next();
ref->removeReference();
delete ref;
ref = nextRef;
}
}
//=================================================
// delete one reference, defined by faction
void HostileRefManager::deleteReferencesForFaction(uint32 faction)
{
HostileReference* ref = getFirst();
while (ref)
{
HostileReference* nextRef = ref->next();
if (ref->GetSource()->GetOwner()->GetFactionTemplateEntry()->faction == faction)
{
ref->removeReference();
delete ref;
}
ref = nextRef;
}
}
//=================================================
// delete one reference, defined by Unit
void HostileRefManager::deleteReference(Unit* creature)
{
HostileReference* ref = getFirst();
while (ref)
{
HostileReference* nextRef = ref->next();
if (ref->GetSource()->GetOwner() == creature)
{
ref->removeReference();
delete ref;
break;
}
ref = nextRef;
}
}
//=================================================
// delete all references out of specified range
void HostileRefManager::deleteReferencesOutOfRange(float range)
{
HostileReference* ref = getFirst();
range = range*range;
while (ref)
{
HostileReference* nextRef = ref->next();
Unit* owner = ref->GetSource()->GetOwner();
if (!owner->isActiveObject() && owner->GetExactDist2dSq(GetOwner()) > range)
{
ref->removeReference();
delete ref;
}
ref = nextRef;
}
}
//=================================================
// set state for one reference, defined by Unit
void HostileRefManager::setOnlineOfflineState(Unit* creature, bool isOnline)
{
HostileReference* ref = getFirst();
while (ref)
{
HostileReference* nextRef = ref->next();
if (ref->GetSource()->GetOwner() == creature)
{
ref->setOnlineOfflineState(isOnline);
break;
}
ref = nextRef;
}
}
//=================================================
void HostileRefManager::UpdateVisibility(bool checkThreat)
{
HostileReference* ref = getFirst();
while (ref)
{
HostileReference* nextRef = ref->next();
if ((!checkThreat || ref->GetSource()->getThreatList().size() <= 1) &&
!ref->GetSource()->GetOwner()->CanSeeOrDetect(GetOwner()))
{
nextRef = ref->next();
ref->removeReference();
delete ref;
}
ref = nextRef;
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C)
* Copyright (C)
*
* 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/>.
*/
#ifndef _HOSTILEREFMANAGER
#define _HOSTILEREFMANAGER
#include "Common.h"
#include "RefManager.h"
class Unit;
class ThreatManager;
class HostileReference;
class SpellInfo;
//=================================================
class HostileRefManager : public RefManager<Unit, ThreatManager>
{
private:
Unit* iOwner;
public:
explicit HostileRefManager(Unit* owner) { iOwner = owner; }
~HostileRefManager();
Unit* GetOwner() { return iOwner; }
// send threat to all my hateres for the victim
// The victim is hated than by them as well
// use for buffs and healing threat functionality
void threatAssist(Unit* victim, float baseThreat, SpellInfo const* threatSpell = NULL);
void addTempThreat(float threat, bool apply);
void addThreatPercent(int32 percent);
// The references are not needed anymore
// tell the source to remove them from the list and free the mem
void deleteReferences();
// Remove specific faction references
void deleteReferencesForFaction(uint32 faction);
// pussywizard: for combat bugs
void deleteReferencesOutOfRange(float range);
HostileReference* getFirst() { return ((HostileReference*) RefManager<Unit, ThreatManager>::getFirst()); }
void updateThreatTables();
void setOnlineOfflineState(bool isOnline);
// set state for one reference, defined by Unit
void setOnlineOfflineState(Unit* creature, bool isOnline);
// delete one reference, defined by Unit
void deleteReference(Unit* creature);
void UpdateVisibility(bool checkThreat);
};
//=================================================
#endif

View File

@@ -0,0 +1,616 @@
/*
* Copyright (C)
* Copyright (C)
*
* 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/>.
*/
#include "ThreatManager.h"
#include "Unit.h"
#include "Creature.h"
#include "CreatureAI.h"
#include "Map.h"
#include "Player.h"
#include "ObjectAccessor.h"
#include "UnitEvents.h"
#include "SpellAuras.h"
#include "SpellMgr.h"
//==============================================================
//================= ThreatCalcHelper ===========================
//==============================================================
// The hatingUnit is not used yet
float ThreatCalcHelper::calcThreat(Unit* hatedUnit, Unit* /*hatingUnit*/, float threat, SpellSchoolMask schoolMask, SpellInfo const* threatSpell)
{
if (threatSpell)
{
if (SpellThreatEntry const* threatEntry = sSpellMgr->GetSpellThreatEntry(threatSpell->Id))
if (threatEntry->pctMod != 1.0f)
threat *= threatEntry->pctMod;
// Energize is not affected by Mods
for (uint8 i = 0; i < MAX_SPELL_EFFECTS; i++)
if (threatSpell->Effects[i].Effect == SPELL_EFFECT_ENERGIZE || threatSpell->Effects[i].ApplyAuraName == SPELL_AURA_PERIODIC_ENERGIZE)
return threat;
if (Player* modOwner = hatedUnit->GetSpellModOwner())
modOwner->ApplySpellMod(threatSpell->Id, SPELLMOD_THREAT, threat);
}
return hatedUnit->ApplyTotalThreatModifier(threat, schoolMask);
}
bool ThreatCalcHelper::isValidProcess(Unit* hatedUnit, Unit* hatingUnit, SpellInfo const* threatSpell)
{
//function deals with adding threat and adding players and pets into ThreatList
//mobs, NPCs, guards have ThreatList and HateOfflineList
//players and pets have only InHateListOf
//HateOfflineList is used co contain unattackable victims (in-flight, in-water, GM etc.)
if (!hatedUnit || !hatingUnit)
return false;
// not to self
if (hatedUnit == hatingUnit)
return false;
// not to GM
if (hatedUnit->GetTypeId() == TYPEID_PLAYER && hatedUnit->ToPlayer()->IsGameMaster())
return false;
// not to dead and not for dead
if (!hatedUnit->IsAlive() || !hatingUnit->IsAlive())
return false;
// not in same map or phase
if (!hatedUnit->IsInMap(hatingUnit) || !hatedUnit->InSamePhase(hatingUnit))
return false;
// spell not causing threat
if (threatSpell && threatSpell->HasAttribute(SPELL_ATTR1_NO_THREAT))
return false;
ASSERT(hatingUnit->GetTypeId() == TYPEID_UNIT);
return true;
}
//============================================================
//================= HostileReference ==========================
//============================================================
HostileReference::HostileReference(Unit* refUnit, ThreatManager* threatManager, float threat)
{
iThreat = threat;
iTempThreatModifier = 0.0f;
link(refUnit, threatManager);
iUnitGuid = refUnit->GetGUID();
iOnline = true;
}
//============================================================
// Tell our refTo (target) object that we have a link
void HostileReference::targetObjectBuildLink()
{
getTarget()->addHatedBy(this);
}
//============================================================
// Tell our refTo (taget) object, that the link is cut
void HostileReference::targetObjectDestroyLink()
{
getTarget()->removeHatedBy(this);
}
//============================================================
// Tell our refFrom (source) object, that the link is cut (Target destroyed)
void HostileReference::sourceObjectDestroyLink()
{
setOnlineOfflineState(false);
}
//============================================================
// Inform the source, that the status of the reference changed
void HostileReference::fireStatusChanged(ThreatRefStatusChangeEvent& threatRefStatusChangeEvent)
{
if (GetSource())
GetSource()->processThreatEvent(&threatRefStatusChangeEvent);
}
//============================================================
void HostileReference::addThreat(float modThreat)
{
iThreat += modThreat;
// the threat is changed. Source and target unit have to be available
// if the link was cut before relink it again
if (!isOnline())
updateOnlineStatus();
if (modThreat != 0.0f)
{
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_THREAT_CHANGE, this, modThreat);
fireStatusChanged(event);
}
if (isValid() && modThreat >= 0.0f)
{
Unit* victimOwner = getTarget()->GetCharmerOrOwner();
if (victimOwner && victimOwner->IsAlive())
GetSource()->addThreat(victimOwner, 0.0f); // create a threat to the owner of a pet, if the pet attacks
}
}
void HostileReference::addThreatPercent(int32 percent)
{
// Xinef: Do not allow to modify threat by percent if threat is negative (forced to big value < 0 by spells adding temporary threat)
// Xinef: When the temporary effect ends, temporary threat is added back which results in huge additional amount of threat
if (iThreat <= 0)
return;
float tmpThreat = iThreat;
AddPct(tmpThreat, percent);
addThreat(tmpThreat - iThreat);
}
//============================================================
// check, if source can reach target and set the status
void HostileReference::updateOnlineStatus()
{
bool online = false;
if (!isValid())
if (Unit* target = ObjectAccessor::GetUnit(*GetSourceUnit(), getUnitGuid()))
link(target, GetSource());
// only check for online status if
// ref is valid
// target is no player or not gamemaster
// target is not in flight
if (isValid()
&& (getTarget()->GetTypeId() != TYPEID_PLAYER || !getTarget()->ToPlayer()->IsGameMaster())
&& !getTarget()->IsInFlight()
&& getTarget()->IsInMap(GetSourceUnit())
&& getTarget()->InSamePhase(GetSourceUnit())
)
{
Creature* creature = GetSourceUnit()->ToCreature();
online = getTarget()->isInAccessiblePlaceFor(creature);
if (!online)
{
if (creature->IsWithinCombatRange(getTarget(), creature->m_CombatDistance))
online = true; // not accessible but stays online
}
}
setOnlineOfflineState(online);
}
//============================================================
// set the status and fire the event on status change
void HostileReference::setOnlineOfflineState(bool isOnline)
{
if (iOnline != isOnline)
{
iOnline = isOnline;
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_ONLINE_STATUS, this);
fireStatusChanged(event);
}
}
//============================================================
// prepare the reference for deleting
// this is called be the target
void HostileReference::removeReference()
{
invalidate();
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_REMOVE_FROM_LIST, this);
fireStatusChanged(event);
}
//============================================================
Unit* HostileReference::GetSourceUnit()
{
return (GetSource()->GetOwner());
}
//============================================================
//================ ThreatContainer ===========================
//============================================================
void ThreatContainer::clearReferences()
{
for (ThreatContainer::StorageType::const_iterator i = iThreatList.begin(); i != iThreatList.end(); ++i)
{
(*i)->unlink();
delete (*i);
}
iThreatList.clear();
}
//============================================================
// Return the HostileReference of NULL, if not found
HostileReference* ThreatContainer::getReferenceByTarget(Unit* victim) const
{
if (!victim)
return NULL;
uint64 const guid = victim->GetGUID();
for (ThreatContainer::StorageType::const_iterator i = iThreatList.begin(); i != iThreatList.end(); ++i)
{
HostileReference *ref = (*i);
if (ref && ref->getUnitGuid() == guid)
return ref;
}
return NULL;
}
//============================================================
// Add the threat, if we find the reference
HostileReference* ThreatContainer::addThreat(Unit* victim, float threat)
{
HostileReference* ref = getReferenceByTarget(victim);
if (ref)
ref->addThreat(threat);
return ref;
}
//============================================================
void ThreatContainer::modifyThreatPercent(Unit* victim, int32 percent)
{
if (HostileReference* ref = getReferenceByTarget(victim))
ref->addThreatPercent(percent);
}
//============================================================
// Check if the list is dirty and sort if necessary
void ThreatContainer::update()
{
if (iDirty && iThreatList.size() > 1)
iThreatList.sort(Trinity::ThreatOrderPred());
iDirty = false;
}
//============================================================
// return the next best victim
// could be the current victim
HostileReference* ThreatContainer::selectNextVictim(Creature* attacker, HostileReference* currentVictim) const
{
// pussywizard: pretty much remade this whole function
HostileReference* currentRef = NULL;
bool found = false;
bool noPriorityTargetFound = false;
uint32 currTime = sWorld->GetGameTime();
// pussywizard: currentVictim is needed to compare if threat was exceeded by 10%/30% for melee/range targets (only then switching current target)
if (currentVictim)
{
Unit* cvUnit = currentVictim->getTarget();
if (!attacker->_CanDetectFeignDeathOf(cvUnit) || !attacker->CanCreatureAttack(cvUnit) || attacker->isTargetNotAcceptableByMMaps(cvUnit->GetGUID(), currTime, cvUnit)) // pussywizard: if currentVictim is not valid => don't compare the threat with it, just take the highest threat valid target
currentVictim = NULL;
else if (cvUnit->IsImmunedToDamageOrSchool(attacker->GetMeleeDamageSchoolMask()) || cvUnit->HasNegativeAuraWithInterruptFlag(AURA_INTERRUPT_FLAG_TAKE_DAMAGE)) // pussywizard: no 10%/30% if currentVictim is immune to damage or has auras breakable by damage
currentVictim = NULL;
}
ThreatContainer::StorageType::const_iterator lastRef = iThreatList.end();
--lastRef;
// pussywizard: iterate from highest to lowest threat
for (ThreatContainer::StorageType::const_iterator iter = iThreatList.begin(); iter != iThreatList.end() && !found;)
{
currentRef = (*iter);
Unit* target = currentRef->getTarget();
ASSERT(target); // if the ref has status online the target must be there !
// pussywizard: don't go to threat comparison if this ref is immune to damage or has aura breakable on damage (second choice target)
// pussywizard: if this is the last entry on the threat list, then all targets are second choice, set bool to true and loop threat list again, ignoring this section
if (!noPriorityTargetFound && (target->IsImmunedToDamageOrSchool(attacker->GetMeleeDamageSchoolMask()) || target->HasNegativeAuraWithInterruptFlag(AURA_INTERRUPT_FLAG_TAKE_DAMAGE) || target->HasAuraTypeWithCaster(SPELL_AURA_IGNORED, attacker->GetGUID())))
{
if (iter != lastRef)
{
++iter;
continue;
}
else
{
noPriorityTargetFound = true;
iter = iThreatList.begin();
continue;
}
}
// pussywizard: skip not valid targets
if (attacker->_CanDetectFeignDeathOf(target) && attacker->CanCreatureAttack(target) && !attacker->isTargetNotAcceptableByMMaps(target->GetGUID(), currTime, target))
{
if (currentVictim) // pussywizard: if not NULL then target must have 10%/30% more threat
{
if (currentVictim == currentRef) // pussywizard: nothing found previously was good and enough, currentRef passed all necessary tests, so end now
{
found = true;
break;
}
// pussywizard: implement 110% threat rule for targets in melee range and 130% rule for targets in ranged distances
if (currentRef->getThreat() > 1.3f * currentVictim->getThreat()) // pussywizard: enough in all cases, end
{
found = true;
break;
}
else if (currentRef->getThreat() > 1.1f * currentVictim->getThreat()) // pussywizard: enought only if target in melee range
{
if (attacker->IsWithinMeleeRange(target))
{
found = true;
break;
}
}
else // pussywizard: nothing found previously was good and enough, this and next entries on the list have less than 110% threat, and currentVictim is present and valid as checked before the loop (otherwise it's NULL), so end now
{
currentRef = currentVictim;
found = true;
break;
}
}
else // pussywizard: no currentVictim, first passing all checks is chosen (highest threat, list is sorted)
{
found = true;
break;
}
}
++iter;
}
if (!found)
currentRef = NULL;
return currentRef;
}
//============================================================
//=================== ThreatManager ==========================
//============================================================
ThreatManager::ThreatManager(Unit* owner) : iCurrentVictim(NULL), iOwner(owner), iUpdateTimer(THREAT_UPDATE_INTERVAL)
{
}
//============================================================
void ThreatManager::clearReferences()
{
iThreatContainer.clearReferences();
iThreatOfflineContainer.clearReferences();
iCurrentVictim = NULL;
iUpdateTimer = THREAT_UPDATE_INTERVAL;
}
//============================================================
void ThreatManager::addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask, SpellInfo const* threatSpell)
{
if (!ThreatCalcHelper::isValidProcess(victim, GetOwner(), threatSpell))
return;
doAddThreat(victim, ThreatCalcHelper::calcThreat(victim, iOwner, threat, schoolMask, threatSpell));
}
void ThreatManager::doAddThreat(Unit* victim, float threat)
{
uint32 redirectThreadPct = victim->GetRedirectThreatPercent();
// must check > 0.0f, otherwise dead loop
if (threat > 0.0f && redirectThreadPct)
{
if (Unit* redirectTarget = victim->GetRedirectThreatTarget())
{
float redirectThreat = CalculatePct(threat, redirectThreadPct);
threat -= redirectThreat;
if (ThreatCalcHelper::isValidProcess(redirectTarget, GetOwner()))
_addThreat(redirectTarget, redirectThreat);
}
}
_addThreat(victim, threat);
}
void ThreatManager::_addThreat(Unit* victim, float threat)
{
HostileReference* ref = iThreatContainer.addThreat(victim, threat);
// Ref is not in the online refs, search the offline refs next
if (!ref)
ref = iThreatOfflineContainer.addThreat(victim, threat);
if (!ref) // there was no ref => create a new one
{
// threat has to be 0 here
HostileReference* hostileRef = new HostileReference(victim, this, 0);
iThreatContainer.addReference(hostileRef);
hostileRef->addThreat(threat); // now we add the real threat
if (victim->GetTypeId() == TYPEID_PLAYER && victim->ToPlayer()->IsGameMaster())
hostileRef->setOnlineOfflineState(false); // GM is always offline
}
}
//============================================================
void ThreatManager::modifyThreatPercent(Unit* victim, int32 percent)
{
iThreatContainer.modifyThreatPercent(victim, percent);
}
//============================================================
Unit* ThreatManager::getHostilTarget()
{
iThreatContainer.update();
HostileReference* nextVictim = iThreatContainer.selectNextVictim(GetOwner()->ToCreature(), getCurrentVictim());
setCurrentVictim(nextVictim);
return getCurrentVictim() != NULL ? getCurrentVictim()->getTarget() : NULL;
}
//============================================================
float ThreatManager::getThreat(Unit* victim, bool alsoSearchOfflineList)
{
float threat = 0.0f;
HostileReference* ref = iThreatContainer.getReferenceByTarget(victim);
if (!ref && alsoSearchOfflineList)
ref = iThreatOfflineContainer.getReferenceByTarget(victim);
if (ref)
threat = ref->getThreat();
return threat;
}
//============================================================
float ThreatManager::getThreatWithoutTemp(Unit* victim, bool alsoSearchOfflineList)
{
float threat = 0.0f;
HostileReference* ref = iThreatContainer.getReferenceByTarget(victim);
if (!ref && alsoSearchOfflineList)
ref = iThreatOfflineContainer.getReferenceByTarget(victim);
if (ref)
threat = ref->getThreat() - ref->getTempThreatModifier();;
return threat;
}
//============================================================
void ThreatManager::tauntApply(Unit* taunter)
{
HostileReference* ref = iThreatContainer.getReferenceByTarget(taunter);
if (getCurrentVictim() && ref && (ref->getThreat() < getCurrentVictim()->getThreat()))
{
if (ref->getTempThreatModifier() == 0.0f) // Ok, temp threat is unused
ref->setTempThreat(getCurrentVictim()->getThreat());
}
}
//============================================================
void ThreatManager::tauntFadeOut(Unit* taunter)
{
HostileReference* ref = iThreatContainer.getReferenceByTarget(taunter);
if (ref)
ref->resetTempThreat();
}
//============================================================
void ThreatManager::setCurrentVictim(HostileReference* pHostileReference)
{
if (pHostileReference && pHostileReference != iCurrentVictim)
{
iOwner->SendChangeCurrentVictimOpcode(pHostileReference);
}
iCurrentVictim = pHostileReference;
}
//============================================================
// The hated unit is gone, dead or deleted
// return true, if the event is consumed
void ThreatManager::processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent)
{
threatRefStatusChangeEvent->setThreatManager(this); // now we can set the threat manager
HostileReference* hostilRef = threatRefStatusChangeEvent->getReference();
switch (threatRefStatusChangeEvent->getType())
{
case UEV_THREAT_REF_THREAT_CHANGE:
if ((getCurrentVictim() == hostilRef && threatRefStatusChangeEvent->getFValue()<0.0f) ||
(getCurrentVictim() != hostilRef && threatRefStatusChangeEvent->getFValue()>0.0f))
setDirty(true); // the order in the threat list might have changed
break;
case UEV_THREAT_REF_ONLINE_STATUS:
if (!hostilRef->isOnline())
{
if (hostilRef == getCurrentVictim())
{
setCurrentVictim(NULL);
setDirty(true);
}
if (GetOwner() && GetOwner()->IsInWorld())
if (Unit* target = ObjectAccessor::GetUnit(*GetOwner(), hostilRef->getUnitGuid()))
if (GetOwner()->IsInMap(target))
GetOwner()->SendRemoveFromThreatListOpcode(hostilRef);
iThreatContainer.remove(hostilRef);
iThreatOfflineContainer.addReference(hostilRef);
}
else
{
if (getCurrentVictim() && hostilRef->getThreat() > (1.1f * getCurrentVictim()->getThreat()))
setDirty(true);
iThreatContainer.addReference(hostilRef);
iThreatOfflineContainer.remove(hostilRef);
}
break;
case UEV_THREAT_REF_REMOVE_FROM_LIST:
if (hostilRef == getCurrentVictim())
{
setCurrentVictim(NULL);
setDirty(true);
}
iOwner->SendRemoveFromThreatListOpcode(hostilRef);
if (hostilRef->isOnline())
iThreatContainer.remove(hostilRef);
else
iThreatOfflineContainer.remove(hostilRef);
break;
}
}
bool ThreatManager::isNeedUpdateToClient(uint32 time)
{
if (isThreatListEmpty())
return false;
if (time >= iUpdateTimer)
{
iUpdateTimer = THREAT_UPDATE_INTERVAL;
return true;
}
iUpdateTimer -= time;
return false;
}
// Reset all aggro without modifying the threatlist.
void ThreatManager::resetAllAggro()
{
ThreatContainer::StorageType &threatList = iThreatContainer.iThreatList;
if (threatList.empty())
return;
for (ThreatContainer::StorageType::iterator itr = threatList.begin(); itr != threatList.end(); ++itr)
(*itr)->setThreat(0);
setDirty(true);
}

View File

@@ -0,0 +1,289 @@
/*
* Copyright (C)
* Copyright (C)
*
* 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/>.
*/
#ifndef _THREATMANAGER
#define _THREATMANAGER
#include "Common.h"
#include "SharedDefines.h"
#include "LinkedReference/Reference.h"
#include "UnitEvents.h"
#include <list>
//==============================================================
class Unit;
class Creature;
class ThreatManager;
class SpellInfo;
#define THREAT_UPDATE_INTERVAL 2 * IN_MILLISECONDS // Server should send threat update to client periodically each second
//==============================================================
// Class to calculate the real threat based
struct ThreatCalcHelper
{
static float calcThreat(Unit* hatedUnit, Unit* hatingUnit, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = NULL);
static bool isValidProcess(Unit* hatedUnit, Unit* hatingUnit, SpellInfo const* threatSpell = NULL);
};
//==============================================================
class HostileReference : public Reference<Unit, ThreatManager>
{
public:
HostileReference(Unit* refUnit, ThreatManager* threatManager, float threat);
//=================================================
void addThreat(float modThreat);
void setThreat(float threat) { addThreat(threat - getThreat()); }
void addThreatPercent(int32 percent);
float getThreat() const { return iThreat; }
bool isOnline() const { return iOnline; }
// used for temporary setting a threat and reducting it later again.
// the threat modification is stored
void setTempThreat(float threat)
{
addTempThreat(threat - getThreat());
}
void addTempThreat(float threat)
{
iTempThreatModifier = threat;
if (iTempThreatModifier != 0.0f)
addThreat(iTempThreatModifier);
}
void resetTempThreat()
{
if (iTempThreatModifier != 0.0f)
{
addThreat(-iTempThreatModifier);
iTempThreatModifier = 0.0f;
}
}
float getTempThreatModifier() { return iTempThreatModifier; }
//=================================================
// check, if source can reach target and set the status
void updateOnlineStatus();
void setOnlineOfflineState(bool isOnline);
//=================================================
bool operator == (const HostileReference& hostileRef) const { return hostileRef.getUnitGuid() == getUnitGuid(); }
//=================================================
uint64 getUnitGuid() const { return iUnitGuid; }
//=================================================
// reference is not needed anymore. realy delete it !
void removeReference();
//=================================================
HostileReference* next() { return ((HostileReference*) Reference<Unit, ThreatManager>::next()); }
//=================================================
// Tell our refTo (target) object that we have a link
void targetObjectBuildLink();
// Tell our refTo (taget) object, that the link is cut
void targetObjectDestroyLink();
// 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& threatRefStatusChangeEvent);
Unit* GetSourceUnit();
private:
float iThreat;
float iTempThreatModifier; // used for taunt
uint64 iUnitGuid;
bool iOnline;
};
//==============================================================
class ThreatManager;
class ThreatContainer
{
friend class ThreatManager;
public:
typedef std::list<HostileReference*> StorageType;
ThreatContainer(): iDirty(false) { }
~ThreatContainer() { clearReferences(); }
HostileReference* addThreat(Unit* victim, float threat);
void modifyThreatPercent(Unit* victim, int32 percent);
HostileReference* selectNextVictim(Creature* attacker, HostileReference* currentVictim) const;
void setDirty(bool isDirty) { iDirty = isDirty; }
bool isDirty() const { return iDirty; }
bool empty() const
{
return iThreatList.empty();
}
HostileReference* getMostHated() const
{
return iThreatList.empty() ? NULL : iThreatList.front();
}
HostileReference* getReferenceByTarget(Unit* victim) const;
StorageType const & getThreatList() const { return iThreatList; }
private:
void remove(HostileReference* hostileRef)
{
iThreatList.remove(hostileRef);
}
void addReference(HostileReference* hostileRef)
{
iThreatList.push_back(hostileRef);
}
void clearReferences();
// Sort the list if necessary
void update();
StorageType iThreatList;
bool iDirty;
};
//=================================================
class ThreatManager
{
public:
friend class HostileReference;
explicit ThreatManager(Unit* owner);
~ThreatManager() { clearReferences(); }
void clearReferences();
void addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = NULL);
void doAddThreat(Unit* victim, float threat);
void modifyThreatPercent(Unit* victim, int32 percent);
float getThreat(Unit* victim, bool alsoSearchOfflineList = false);
float getThreatWithoutTemp(Unit* victim, bool alsoSearchOfflineList = false);
bool isThreatListEmpty() const { return iThreatContainer.empty(); }
bool areThreatListsEmpty() const { return iThreatContainer.empty() && iThreatOfflineContainer.empty(); }
void processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent);
bool isNeedUpdateToClient(uint32 time);
HostileReference* getCurrentVictim() const { return iCurrentVictim; }
Unit* GetOwner() const { return iOwner; }
Unit* getHostilTarget();
void tauntApply(Unit* taunter);
void tauntFadeOut(Unit* taunter);
void setCurrentVictim(HostileReference* hostileRef);
void setDirty(bool isDirty) { iThreatContainer.setDirty(isDirty); }
// 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)
{
ThreatContainer::StorageType &threatList = iThreatContainer.iThreatList;
if (threatList.empty())
return;
for (ThreatContainer::StorageType::iterator itr = threatList.begin(); itr != threatList.end(); ++itr)
{
HostileReference* ref = (*itr);
if (predicate(ref->getTarget()))
{
ref->setThreat(0);
setDirty(true);
}
}
}
// methods to access the lists from the outside to do some dirty manipulation (scriping and such)
// I hope they are used as little as possible.
ThreatContainer::StorageType const & getThreatList() const { return iThreatContainer.getThreatList(); }
ThreatContainer::StorageType const & getOfflineThreatList() const { return iThreatOfflineContainer.getThreatList(); }
ThreatContainer& getOnlineContainer() { return iThreatContainer; }
ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
private:
void _addThreat(Unit* victim, float threat);
HostileReference* iCurrentVictim;
Unit* iOwner;
uint32 iUpdateTimer;
ThreatContainer iThreatContainer;
ThreatContainer iThreatOfflineContainer;
};
//=================================================
namespace Trinity
{
// Binary predicate for sorting HostileReferences based on threat value
class ThreatOrderPred
{
public:
ThreatOrderPred(bool ascending = false) : m_ascending(ascending) {}
bool operator() (HostileReference const* a, HostileReference const* b) const
{
return m_ascending ? a->getThreat() < b->getThreat() : a->getThreat() > b->getThreat();
}
private:
const bool m_ascending;
};
}
#endif

View File

@@ -0,0 +1,137 @@
/*
* Copyright (C)
* Copyright (C)
*
* 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/>.
*/
#ifndef _UNITEVENTS
#define _UNITEVENTS
#include "Common.h"
class ThreatContainer;
class ThreatManager;
class HostileReference;
//==============================================================
//==============================================================
enum UNIT_EVENT_TYPE
{
// Player/Pet changed on/offline status
UEV_THREAT_REF_ONLINE_STATUS = 1<<0,
// Threat for Player/Pet changed
UEV_THREAT_REF_THREAT_CHANGE = 1<<1,
// Player/Pet will be removed from list (dead) [for internal use]
UEV_THREAT_REF_REMOVE_FROM_LIST = 1<<2,
// Player/Pet entered/left water or some other place where it is/was not accessible for the creature
UEV_THREAT_REF_ASSECCIBLE_STATUS = 1<<3,
// Threat list is going to be sorted (if dirty flag is set)
UEV_THREAT_SORT_LIST = 1<<4,
// New target should be fetched, could tbe the current target as well
UEV_THREAT_SET_NEXT_TARGET = 1<<5,
// A new victim (target) was set. Could be NULL
UEV_THREAT_VICTIM_CHANGED = 1<<6,
// Future use
//UEV_UNIT_KILLED = 1<<7,
//Future use
//UEV_UNIT_HEALTH_CHANGE = 1<<8,
};
#define UEV_THREAT_REF_EVENT_MASK (UEV_THREAT_REF_ONLINE_STATUS | UEV_THREAT_REF_THREAT_CHANGE | UEV_THREAT_REF_REMOVE_FROM_LIST | UEV_THREAT_REF_ASSECCIBLE_STATUS)
#define UEV_THREAT_MANAGER_EVENT_MASK (UEV_THREAT_SORT_LIST | UEV_THREAT_SET_NEXT_TARGET | UEV_THREAT_VICTIM_CHANGED)
#define UEV_ALL_EVENT_MASK (0xffffffff)
// Future use
//#define UEV_UNIT_EVENT_MASK (UEV_UNIT_KILLED | UEV_UNIT_HEALTH_CHANGE)
//==============================================================
class UnitBaseEvent
{
private:
uint32 iType;
public:
UnitBaseEvent(uint32 pType) { iType = pType; }
uint32 getType() const { return iType; }
bool matchesTypeMask(uint32 pMask) const { return iType & pMask; }
void setType(uint32 pType) { iType = pType; }
};
//==============================================================
class ThreatRefStatusChangeEvent : public UnitBaseEvent
{
private:
HostileReference* iHostileReference;
union
{
float iFValue;
int32 iIValue;
bool iBValue;
};
ThreatManager* iThreatManager;
public:
ThreatRefStatusChangeEvent(uint32 pType) : UnitBaseEvent(pType), iThreatManager(NULL) { iHostileReference = NULL; }
ThreatRefStatusChangeEvent(uint32 pType, HostileReference* pHostileReference) : UnitBaseEvent(pType), iThreatManager(NULL) { iHostileReference = pHostileReference; }
ThreatRefStatusChangeEvent(uint32 pType, HostileReference* pHostileReference, float pValue) : UnitBaseEvent(pType), iThreatManager(NULL) { iHostileReference = pHostileReference; iFValue = pValue; }
ThreatRefStatusChangeEvent(uint32 pType, HostileReference* pHostileReference, bool pValue) : UnitBaseEvent(pType), iThreatManager(NULL) { iHostileReference = pHostileReference; iBValue = pValue; }
int32 getIValue() const { return iIValue; }
float getFValue() const { return iFValue; }
bool getBValue() const { return iBValue; }
void setBValue(bool pValue) { iBValue = pValue; }
HostileReference* getReference() const { return iHostileReference; }
void setThreatManager(ThreatManager* pThreatManager) { iThreatManager = pThreatManager; }
ThreatManager* getThreatManager() const { return iThreatManager; }
};
//==============================================================
class ThreatManagerEvent : public ThreatRefStatusChangeEvent
{
private:
ThreatContainer* iThreatContainer;
public:
ThreatManagerEvent(uint32 pType) : ThreatRefStatusChangeEvent(pType), iThreatContainer(NULL) {}
ThreatManagerEvent(uint32 pType, HostileReference* pHostileReference) : ThreatRefStatusChangeEvent(pType, pHostileReference), iThreatContainer(NULL) {}
void setThreatContainer(ThreatContainer* pThreatContainer) { iThreatContainer = pThreatContainer; }
ThreatContainer* getThreatContainer() const { return iThreatContainer; }
};
//==============================================================
#endif