Files
TCPlayerbotCore/src/game/ThreatManager.cpp
T

554 lines
19 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
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 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
#include "ThreatManager.h"
#include "Unit.h"
#include "Creature.h"
#include "CreatureAI.h"
#include "Map.h"
#include "Player.h"
#include "ObjectAccessor.h"
#include "UnitEvents.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//==============================================================
//================= ThreatCalcHelper ===========================
//==============================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// The pHatingUnit is not used yet
2010-04-19 09:26:37 +02:00
float ThreatCalcHelper::calcThreat(Unit* pHatedUnit, Unit* /*pHatingUnit*/, float fThreat, SpellSchoolMask schoolMask, SpellEntry const *pThreatSpell)
2008-10-02 16:23:55 -05:00
{
if (pThreatSpell)
if (Player* modOwner = pHatedUnit->GetSpellModOwner())
2009-10-18 18:38:46 -07:00
modOwner->ApplySpellMod(pThreatSpell->Id, SPELLMOD_THREAT, fThreat);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
return pHatedUnit->ApplyTotalThreatModifier(fThreat, schoolMask);
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
//================= HostileReference ==========================
2008-10-02 16:23:55 -05:00
//============================================================
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference::HostileReference(Unit* pUnit, ThreatManager *pThreatManager, float fThreat)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
iThreat = fThreat;
iTempThreatModifier = 0.0f;
2008-10-02 16:23:55 -05:00
link(pUnit, pThreatManager);
iUnitGuid = pUnit->GetGUID();
iOnline = true;
iAccessible = true;
}
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
2010-03-09 11:11:10 +01:00
void HostileReference::targetObjectBuildLink()
2008-10-02 16:23:55 -05:00
{
getTarget()->addHatedBy(this);
}
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
2010-03-09 11:11:10 +01:00
void HostileReference::targetObjectDestroyLink()
2008-10-02 16:23:55 -05:00
{
getTarget()->removeHatedBy(this);
}
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)
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void HostileReference::sourceObjectDestroyLink()
2008-10-02 16:23:55 -05:00
{
setOnlineOfflineState(false);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// Inform the source, that the status of the reference changed
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void HostileReference::fireStatusChanged(ThreatRefStatusChangeEvent& pThreatRefStatusChangeEvent)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
if (getSource())
2008-10-02 16:23:55 -05:00
getSource()->processThreatEvent(&pThreatRefStatusChangeEvent);
}
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-03-09 11:11:10 +01:00
void HostileReference::addThreat(float fModThreat)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
iThreat += fModThreat;
// the threat is changed. Source and target unit have to be available
2008-10-02 16:23:55 -05:00
// if the link was cut before relink it again
2009-10-18 18:38:46 -07:00
if (!isOnline())
2008-10-02 16:23:55 -05:00
updateOnlineStatus();
2009-10-18 18:38:46 -07:00
if (fModThreat != 0.0f)
{
2009-10-18 18:38:46 -07:00
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_THREAT_CHANGE, this, fModThreat);
fireStatusChanged(event);
}
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
if (isValid() && fModThreat >= 0.0f)
2008-10-02 16:23:55 -05:00
{
Unit* victim_owner = getTarget()->GetCharmerOrOwner();
2009-10-18 18:38:46 -07:00
if (victim_owner && victim_owner->isAlive())
2008-10-02 16:23:55 -05:00
getSource()->addThreat(victim_owner, 0.0f); // create a threat to the owner of a pet, if the pet attacks
}
}
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
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void HostileReference::updateOnlineStatus()
2008-10-02 16:23:55 -05:00
{
bool online = false;
bool accessible = false;
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
if (!isValid())
if (Unit* target = ObjectAccessor::GetUnit(*getSourceUnit(), getUnitGuid()))
2008-10-02 16:23:55 -05:00
link(target, getSource());
2009-10-18 18:38:46 -07:00
2008-10-02 16:23:55 -05:00
// only check for online status if
// ref is valid
// target is no player or not gamemaster
// target is not in flight
2009-10-18 18:38:46 -07:00
if (isValid() &&
2008-10-02 16:23:55 -05:00
((getTarget()->GetTypeId() != TYPEID_PLAYER || !((Player*)getTarget())->isGameMaster()) ||
!getTarget()->hasUnitState(UNIT_STAT_IN_FLIGHT)))
{
Creature* creature = getSourceUnit()->ToCreature();
2008-11-14 16:28:45 -06:00
online = getTarget()->isInAccessiblePlaceFor(creature);
2009-10-18 18:38:46 -07:00
if (!online)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
if (creature->IsWithinCombatRange(getTarget(), creature->m_CombatDistance))
2008-11-14 16:28:45 -06:00
online = true; // not accessible but stays online
2008-10-02 16:23:55 -05:00
}
else
accessible = true;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
}
setAccessibleState(accessible);
setOnlineOfflineState(online);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// set the status and fire the event on status change
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void HostileReference::setOnlineOfflineState(bool pIsOnline)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
if (iOnline != pIsOnline)
2008-10-02 16:23:55 -05:00
{
iOnline = pIsOnline;
2009-10-18 18:38:46 -07:00
if (!iOnline)
setAccessibleState(false); // if not online that not accessable as well
2009-10-17 15:51:44 -07:00
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_ONLINE_STATUS, this);
fireStatusChanged(event);
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-03-09 11:11:10 +01:00
void HostileReference::setAccessibleState(bool pIsAccessible)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
if (iAccessible != pIsAccessible)
2008-10-02 16:23:55 -05:00
{
iAccessible = pIsAccessible;
2009-10-17 15:51:44 -07:00
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_ASSECCIBLE_STATUS, this);
fireStatusChanged(event);
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// prepare the reference for deleting
// this is called be the target
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
void HostileReference::removeReference()
2008-10-02 16:23:55 -05:00
{
invalidate();
2009-10-17 15:51:44 -07:00
ThreatRefStatusChangeEvent event(UEV_THREAT_REF_REMOVE_FROM_LIST, this);
fireStatusChanged(event);
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-03-09 11:11:10 +01:00
Unit* HostileReference::getSourceUnit()
2008-10-02 16:23:55 -05:00
{
return (getSource()->getOwner());
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
//================ ThreatContainer ===========================
//============================================================
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void ThreatContainer::clearReferences()
{
2010-03-09 11:11:10 +01:00
for (std::list<HostileReference*>::const_iterator i = iThreatList.begin(); i != iThreatList.end(); ++i)
2008-10-02 16:23:55 -05:00
{
(*i)->unlink();
delete (*i);
}
iThreatList.clear();
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
2010-03-09 11:11:10 +01:00
// Return the HostileReference of NULL, if not found
HostileReference* ThreatContainer::getReferenceByTarget(Unit* pVictim)
2008-10-02 16:23:55 -05:00
{
2010-03-09 11:11:10 +01:00
HostileReference* result = NULL;
2009-10-17 16:20:24 -07:00
2008-10-02 16:23:55 -05:00
uint64 guid = pVictim->GetGUID();
2010-03-09 11:11:10 +01:00
for (std::list<HostileReference*>::const_iterator i = iThreatList.begin(); i != iThreatList.end(); ++i)
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ((*i)->getUnitGuid() == guid)
2008-10-02 16:23:55 -05:00
{
result = (*i);
break;
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return result;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// Add the threat, if we find the reference
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* ThreatContainer::addThreat(Unit* pVictim, float fThreat)
2008-10-02 16:23:55 -05:00
{
2010-03-09 11:11:10 +01:00
HostileReference* ref = getReferenceByTarget(pVictim);
2009-10-18 18:38:46 -07:00
if (ref)
ref->addThreat(fThreat);
2008-10-02 16:23:55 -05:00
return ref;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void ThreatContainer::modifyThreatPercent(Unit *pVictim, int32 iPercent)
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if (HostileReference* ref = getReferenceByTarget(pVictim))
2009-10-18 18:38:46 -07:00
ref->addThreatPercent(iPercent);
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-03-09 11:11:10 +01:00
bool HostileReferenceSortPredicate(const HostileReference* lhs, const HostileReference* rhs)
2008-10-02 16:23:55 -05:00
{
2010-04-07 23:25:02 +02:00
// std::list::sort ordering predicate must be: (Pred(x,y)&&Pred(y,x)) == false
2008-10-02 16:23:55 -05:00
return lhs->getThreat() > rhs->getThreat(); // reverse sorting
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// Check if the list is dirty and sort if necessary
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void ThreatContainer::update()
{
2010-04-07 19:14:10 +02:00
if (iDirty && iThreatList.size() >1)
2008-10-02 16:23:55 -05:00
{
2010-03-09 11:11:10 +01:00
iThreatList.sort(HostileReferenceSortPredicate);
2008-10-02 16:23:55 -05:00
}
iDirty = false;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// return the next best victim
// could be the current victim
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* ThreatContainer::selectNextVictim(Creature* pAttacker, HostileReference* pCurrentVictim)
2008-10-02 16:23:55 -05:00
{
2010-03-09 11:11:10 +01:00
HostileReference* currentRef = NULL;
2008-10-02 16:23:55 -05:00
bool found = false;
2009-01-02 11:03:43 -06:00
bool noPriorityTargetFound = false;
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
std::list<HostileReference*>::const_iterator lastRef = iThreatList.end();
lastRef--;
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
for (std::list<HostileReference*>::const_iterator iter = iThreatList.begin(); iter != iThreatList.end() && !found;)
2008-10-02 16:23:55 -05:00
{
currentRef = (*iter);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Unit* target = currentRef->getTarget();
assert(target); // if the ref has status online the target must be there !
2009-10-17 15:51:44 -07:00
2009-01-02 11:03:43 -06:00
// some units are prefered in comparison to others
2010-04-07 22:59:46 +02:00
if (!noPriorityTargetFound && (target->IsImmunedToDamage(pAttacker->GetMeleeDamageSchoolMask()) || target->HasNegativeAuraWithInterruptFlag(AURA_INTERRUPT_FLAG_TAKE_DAMAGE)))
{
2010-04-07 19:14:10 +02:00
if (iter != lastRef)
2009-01-02 11:03:43 -06:00
{
// current victim is a second choice target, so don't compare threat with it below
2010-04-07 19:14:10 +02:00
if (currentRef == pCurrentVictim)
2009-01-02 11:03:43 -06:00
pCurrentVictim = NULL;
++iter;
continue;
}
else
{
// if we reached to this point, everyone in the threatlist is a second choice target. In such a situation the target with the highest threat should be attacked.
noPriorityTargetFound = true;
iter = iThreatList.begin();
continue;
}
}
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if (pAttacker->canCreatureAttack(target)) // skip non attackable currently targets
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if (pCurrentVictim) // select 1.3/1.1 better target in comparison current target
2008-10-02 16:23:55 -05:00
{
// list sorted and and we check current target, then this is best case
2010-04-07 22:59:46 +02:00
if (pCurrentVictim == currentRef || currentRef->getThreat() <= 1.1f * pCurrentVictim->getThreat())
2008-10-02 16:23:55 -05:00
{
currentRef = pCurrentVictim; // for second case
found = true;
break;
}
2009-10-17 15:51:44 -07:00
if (currentRef->getThreat() > 1.3f * pCurrentVictim->getThreat() ||
currentRef->getThreat() > 1.1f * pCurrentVictim->getThreat() &&
pAttacker->IsWithinMeleeRange(target))
2008-10-02 16:23:55 -05:00
{ //implement 110% threat rule for targets in melee range
found = true; //and 130% rule for targets in ranged distances
break; //for selecting alive targets
}
}
else // select any
{
found = true;
break;
}
}
2009-01-02 11:03:43 -06:00
++iter;
2008-10-02 16:23:55 -05:00
}
2010-04-07 19:14:10 +02:00
if (!found)
2008-10-02 16:23:55 -05:00
currentRef = NULL;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return currentRef;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
//=================== ThreatManager ==========================
//============================================================
2009-10-17 15:51:44 -07:00
ThreatManager::ThreatManager(Unit* owner) : iCurrentVictim(NULL), iOwner(owner), iUpdateTimer(THREAT_UPDATE_INTERVAL)
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
2008-10-02 16:23:55 -05:00
void ThreatManager::clearReferences()
{
iThreatContainer.clearReferences();
iThreatOfflineContainer.clearReferences();
iCurrentVictim = NULL;
iUpdateTimer = THREAT_UPDATE_INTERVAL;
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
2009-10-18 18:38:46 -07:00
void ThreatManager::addThreat(Unit* pVictim, float fThreat, SpellSchoolMask schoolMask, SpellEntry const *pThreatSpell)
2008-10-02 16:23:55 -05:00
{
//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.)
2009-10-17 15:51:44 -07:00
// not to self
if (pVictim == getOwner())
2008-10-02 16:23:55 -05:00
return;
2009-10-17 15:51:44 -07:00
// not to GM
2010-03-07 15:20:19 +01:00
if (!pVictim || (pVictim->GetTypeId() == TYPEID_PLAYER && pVictim->ToPlayer()->isGameMaster()))
2008-10-02 16:23:55 -05:00
return;
2009-10-17 15:51:44 -07:00
// not to dead and not for dead
2009-10-18 18:38:46 -07:00
if (!pVictim->isAlive() || !getOwner()->isAlive())
return;
2009-10-17 15:51:44 -07:00
2009-11-20 19:11:28 -08:00
assert(getOwner()->GetTypeId() == TYPEID_UNIT);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
float threat = ThreatCalcHelper::calcThreat(pVictim, iOwner, fThreat, schoolMask, pThreatSpell);
2009-10-17 15:51:44 -07:00
2009-02-18 11:01:21 -06:00
// must check > 0.0f, otherwise dead loop
2009-10-18 18:38:46 -07:00
if (threat > 0.0f && pVictim->GetReducedThreatPercent())
2008-12-17 20:07:25 -06:00
{
float reducedThreat = threat * pVictim->GetReducedThreatPercent() / 100;
threat -= reducedThreat;
2010-04-07 19:14:10 +02:00
if (Unit *unit = pVictim->GetMisdirectionTarget())
2009-01-16 18:49:41 -06:00
_addThreat(unit, reducedThreat);
2008-12-17 20:07:25 -06:00
}
2009-10-17 15:51:44 -07:00
2009-01-16 18:49:41 -06:00
_addThreat(pVictim, threat);
}
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void ThreatManager::_addThreat(Unit *pVictim, float fThreat)
2009-01-16 18:49:41 -06:00
{
2010-03-09 11:11:10 +01:00
HostileReference* ref = iThreatContainer.addThreat(pVictim, fThreat);
2008-10-02 16:23:55 -05:00
// Ref is not in the online refs, search the offline refs next
2009-10-18 18:38:46 -07:00
if (!ref)
ref = iThreatOfflineContainer.addThreat(pVictim, fThreat);
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
if (!ref) // there was no ref => create a new one
2008-10-02 16:23:55 -05:00
{
// threat has to be 0 here
2010-03-09 11:11:10 +01:00
HostileReference* hostilReference = new HostileReference(pVictim, this, 0);
2008-10-02 16:23:55 -05:00
iThreatContainer.addReference(hostilReference);
2009-10-18 18:38:46 -07:00
hostilReference->addThreat(fThreat); // now we add the real threat
2010-04-07 19:14:10 +02:00
if (pVictim->GetTypeId() == TYPEID_PLAYER && pVictim->ToPlayer()->isGameMaster())
2008-10-02 16:23:55 -05:00
hostilReference->setOnlineOfflineState(false); // GM is always offline
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
2009-10-17 15:51:44 -07:00
2009-10-18 18:38:46 -07:00
void ThreatManager::modifyThreatPercent(Unit *pVictim, int32 iPercent)
2008-10-02 16:23:55 -05:00
{
2009-10-18 18:38:46 -07:00
iThreatContainer.modifyThreatPercent(pVictim, iPercent);
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
2008-10-02 16:23:55 -05:00
Unit* ThreatManager::getHostilTarget()
{
iThreatContainer.update();
2010-04-07 22:59:46 +02:00
HostileReference* nextVictim = iThreatContainer.selectNextVictim(getOwner()->ToCreature(), getCurrentVictim());
2008-10-02 16:23:55 -05:00
setCurrentVictim(nextVictim);
return getCurrentVictim() != NULL ? getCurrentVictim()->getTarget() : NULL;
}
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
float ThreatManager::getThreat(Unit *pVictim, bool pAlsoSearchOfflineList)
{
float threat = 0.0f;
2010-03-09 11:11:10 +01:00
HostileReference* ref = iThreatContainer.getReferenceByTarget(pVictim);
2010-04-07 19:14:10 +02:00
if (!ref && pAlsoSearchOfflineList)
2008-10-02 16:23:55 -05:00
ref = iThreatOfflineContainer.getReferenceByTarget(pVictim);
2010-04-07 19:14:10 +02:00
if (ref)
2008-10-02 16:23:55 -05:00
threat = ref->getThreat();
return threat;
}
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
void ThreatManager::tauntApply(Unit* pTaunter)
{
2010-03-09 11:11:10 +01:00
HostileReference* ref = iThreatContainer.getReferenceByTarget(pTaunter);
2010-04-07 19:14:10 +02:00
if (getCurrentVictim() && ref && (ref->getThreat() < getCurrentVictim()->getThreat()))
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if (ref->getTempThreatModifier() == 0.0f) // Ok, temp threat is unused
2008-10-02 16:23:55 -05:00
ref->setTempThreat(getCurrentVictim()->getThreat());
}
}
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
void ThreatManager::tauntFadeOut(Unit *pTaunter)
{
2010-03-09 11:11:10 +01:00
HostileReference* ref = iThreatContainer.getReferenceByTarget(pTaunter);
2010-04-07 19:14:10 +02:00
if (ref)
2008-10-02 16:23:55 -05:00
ref->resetTempThreat();
}
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-03-09 11:11:10 +01:00
void ThreatManager::setCurrentVictim(HostileReference* pHostileReference)
2008-10-02 16:23:55 -05:00
{
2010-03-09 11:11:10 +01:00
if (pHostileReference && pHostileReference != iCurrentVictim)
{
2010-03-09 11:11:10 +01:00
iOwner->SendChangeCurrentVictimOpcode(pHostileReference);
}
2010-03-09 11:11:10 +01:00
iCurrentVictim = pHostileReference;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//============================================================
// The hated unit is gone, dead or deleted
// return true, if the event is consumed
2009-10-17 15:51:44 -07:00
void ThreatManager::processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusChangeEvent)
2008-10-02 16:23:55 -05:00
{
threatRefStatusChangeEvent->setThreatManager(this); // now we can set the threat manager
2009-10-17 15:51:44 -07:00
2010-03-09 11:11:10 +01:00
HostileReference* hostilReference = threatRefStatusChangeEvent->getReference();
2009-10-17 15:51:44 -07:00
switch(threatRefStatusChangeEvent->getType())
2008-10-02 16:23:55 -05:00
{
case UEV_THREAT_REF_THREAT_CHANGE:
2010-04-07 19:14:10 +02:00
if ((getCurrentVictim() == hostilReference && threatRefStatusChangeEvent->getFValue()<0.0f) ||
2008-10-02 16:23:55 -05:00
(getCurrentVictim() != hostilReference && threatRefStatusChangeEvent->getFValue()>0.0f))
setDirty(true); // the order in the threat list might have changed
break;
case UEV_THREAT_REF_ONLINE_STATUS:
2010-04-07 19:14:10 +02:00
if (!hostilReference->isOnline())
2008-10-02 16:23:55 -05:00
{
if (hostilReference == getCurrentVictim())
{
setCurrentVictim(NULL);
setDirty(true);
}
iThreatContainer.remove(hostilReference);
iThreatOfflineContainer.addReference(hostilReference);
}
else
{
2010-04-07 19:14:10 +02:00
if (getCurrentVictim() && hostilReference->getThreat() > (1.1f * getCurrentVictim()->getThreat()))
2008-10-02 16:23:55 -05:00
setDirty(true);
iThreatContainer.addReference(hostilReference);
iThreatOfflineContainer.remove(hostilReference);
}
break;
case UEV_THREAT_REF_REMOVE_FROM_LIST:
if (hostilReference == getCurrentVictim())
{
setCurrentVictim(NULL);
setDirty(true);
}
iOwner->SendRemoveFromThreatListOpcode(hostilReference);
2010-04-07 19:14:10 +02:00
if (hostilReference->isOnline())
2008-10-02 16:23:55 -05:00
iThreatContainer.remove(hostilReference);
else
iThreatOfflineContainer.remove(hostilReference);
break;
}
}
2009-10-17 15:51:44 -07:00
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 threadlist.
void ThreatManager::resetAllAggro()
{
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)
{
(*itr)->setThreat(0);
}
setDirty(true);
}