refactor(Core/Combat): Code style and improvement of ThreatMgr (#12992)

This commit is contained in:
Maelthyr
2022-09-25 20:20:34 +02:00
committed by GitHub
parent ddcf0e79b6
commit 1f472bddd3
61 changed files with 212 additions and 196 deletions

View File

@@ -53,7 +53,7 @@ void HostileRefMgr::threatAssist(Unit* victim, float baseThreat, SpellInfo const
}
}
ref->GetSource()->doAddThreat(victim, threat);
ref->GetSource()->DoAddThreat(victim, threat);
}
ref = ref->next();
@@ -239,7 +239,7 @@ void HostileRefMgr::UpdateVisibility(bool checkThreat)
while (ref)
{
HostileReference* nextRef = ref->next();
if ((!checkThreat || ref->GetSource()->getThreatList().size() <= 1))
if ((!checkThreat || ref->GetSource()->GetThreatListSize() <= 1))
{
nextRef = ref->next();
ref->removeReference();

View File

@@ -131,14 +131,17 @@ void HostileReference::fireStatusChanged(ThreatRefStatusChangeEvent& threatRefSt
GetSource()->processThreatEvent(&threatRefStatusChangeEvent);
}
// -- compatibility layer for combat rewrite
Unit* HostileReference::GetOwner() const { return GetSource()->GetOwner(); }
//============================================================
void HostileReference::addThreat(float modThreat)
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())
if (!IsOnline())
updateOnlineStatus();
if (modThreat != 0.0f)
{
@@ -154,7 +157,7 @@ void HostileReference::addThreat(float modThreat)
Unit* victimOwner = target->GetCharmerOrOwner();
if (victimOwner && victimOwner->IsAlive())
{
GetSource()->addThreat(victimOwner, 0.0f); // create a threat to the owner of a pet, if the pet attacks
GetSource()->AddThreat(victimOwner, 0.0f); // create a threat to the owner of a pet, if the pet attacks
}
}
}
@@ -169,7 +172,7 @@ void HostileReference::addThreatPercent(int32 percent)
float tmpThreat = iThreat;
AddPct(tmpThreat, percent);
addThreat(tmpThreat - iThreat);
AddThreat(tmpThreat - iThreat);
}
//============================================================
@@ -256,7 +259,7 @@ void ThreatContainer::clearReferences()
//============================================================
// Return the HostileReference of nullptr, if not found
HostileReference* ThreatContainer::getReferenceByTarget(Unit* victim) const
HostileReference* ThreatContainer::getReferenceByTarget(Unit const* victim) const
{
if (!victim)
return nullptr;
@@ -281,17 +284,17 @@ HostileReference* ThreatContainer::getReferenceByTarget(ObjectGuid const& guid)
//============================================================
// Add the threat, if we find the reference
HostileReference* ThreatContainer::addThreat(Unit* victim, float threat)
HostileReference* ThreatContainer::AddThreat(Unit* victim, float threat)
{
HostileReference* ref = getReferenceByTarget(victim);
if (ref)
ref->addThreat(threat);
ref->AddThreat(threat);
return ref;
}
//============================================================
void ThreatContainer::modifyThreatPercent(Unit* victim, int32 percent)
void ThreatContainer::ModifyThreatByPercent(Unit* victim, int32 percent)
{
if (HostileReference* ref = getReferenceByTarget(victim))
ref->addThreatPercent(percent);
@@ -312,7 +315,7 @@ void ThreatContainer::update()
// return the next best victim
// could be the current victim
HostileReference* ThreatContainer::selectNextVictim(Creature* attacker, HostileReference* currentVictim) const
HostileReference* ThreatContainer::SelectNextVictim(Creature* attacker, HostileReference* currentVictim) const
{
// pussywizard: pretty much remade this whole function
@@ -370,12 +373,12 @@ HostileReference* ThreatContainer::selectNextVictim(Creature* attacker, HostileR
}
// 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
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
else if (currentRef->GetThreat() > 1.1f * currentVictim->GetThreat()) // pussywizard: enought only if target in melee range
{
if (attacker->IsWithinMeleeRange(target))
{
@@ -431,7 +434,7 @@ void ThreatMgr::clearReferences()
//============================================================
void ThreatMgr::addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask, SpellInfo const* threatSpell)
void ThreatMgr::AddThreat(Unit* victim, float threat, SpellSchoolMask schoolMask, SpellInfo const* threatSpell)
{
if (!ThreatCalcHelper::isValidProcess(victim, iOwner, threatSpell))
return;
@@ -445,10 +448,10 @@ void ThreatMgr::addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask
}
}
doAddThreat(victim, threat);
DoAddThreat(victim, threat);
}
void ThreatMgr::doAddThreat(Unit* victim, float threat)
void ThreatMgr::DoAddThreat(Unit* victim, float threat)
{
uint32 redirectThreadPct = victim->GetRedirectThreatPercent();
@@ -469,17 +472,17 @@ void ThreatMgr::doAddThreat(Unit* victim, float threat)
void ThreatMgr::_addThreat(Unit* victim, float threat)
{
HostileReference* ref = iThreatContainer.addThreat(victim, 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);
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
hostileRef->AddThreat(threat); // now we add the real threat
if (victim->GetTypeId() == TYPEID_PLAYER && victim->ToPlayer()->IsGameMaster())
hostileRef->setOnlineOfflineState(false); // GM is always offline
}
@@ -487,9 +490,9 @@ void ThreatMgr::_addThreat(Unit* victim, float threat)
//============================================================
void ThreatMgr::modifyThreatPercent(Unit* victim, int32 percent)
void ThreatMgr::ModifyThreatByPercent(Unit* victim, int32 percent)
{
iThreatContainer.modifyThreatPercent(victim, percent);
iThreatContainer.ModifyThreatByPercent(victim, percent);
}
//============================================================
@@ -497,21 +500,21 @@ void ThreatMgr::modifyThreatPercent(Unit* victim, int32 percent)
Unit* ThreatMgr::getHostileTarget()
{
iThreatContainer.update();
HostileReference* nextVictim = iThreatContainer.selectNextVictim(GetOwner()->ToCreature(), getCurrentVictim());
HostileReference* nextVictim = iThreatContainer.SelectNextVictim(GetOwner()->ToCreature(), getCurrentVictim());
setCurrentVictim(nextVictim);
return getCurrentVictim() != nullptr ? getCurrentVictim()->getTarget() : nullptr;
}
//============================================================
float ThreatMgr::getThreat(Unit* victim, bool alsoSearchOfflineList)
float ThreatMgr::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();
threat = ref->GetThreat();
return threat;
}
@@ -524,7 +527,7 @@ float ThreatMgr::getThreatWithoutTemp(Unit* victim, bool alsoSearchOfflineList)
if (!ref && alsoSearchOfflineList)
ref = iThreatOfflineContainer.getReferenceByTarget(victim);
if (ref)
threat = ref->getThreat() - ref->getTempThreatModifier();
threat = ref->GetThreat() - ref->getTempThreatModifier();
return threat;
}
@@ -533,10 +536,10 @@ float ThreatMgr::getThreatWithoutTemp(Unit* victim, bool alsoSearchOfflineList)
void ThreatMgr::tauntApply(Unit* taunter)
{
HostileReference* ref = iThreatContainer.getReferenceByTarget(taunter);
if (getCurrentVictim() && ref && (ref->getThreat() < getCurrentVictim()->getThreat()))
if (getCurrentVictim() && ref && (ref->GetThreat() < getCurrentVictim()->GetThreat()))
{
if (ref->getTempThreatModifier() == 0.0f) // Ok, temp threat is unused
ref->setTempThreat(getCurrentVictim()->getThreat());
ref->setTempThreat(getCurrentVictim()->GetThreat());
}
}
@@ -578,7 +581,7 @@ void ThreatMgr::processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusCh
setDirty(true); // the order in the threat list might have changed
break;
case UEV_THREAT_REF_ONLINE_STATUS:
if (!hostileRef->isOnline())
if (!hostileRef->IsOnline())
{
if (hostileRef == getCurrentVictim())
{
@@ -594,7 +597,7 @@ void ThreatMgr::processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusCh
}
else
{
if (getCurrentVictim() && hostileRef->getThreat() > (1.1f * getCurrentVictim()->getThreat()))
if (getCurrentVictim() && hostileRef->GetThreat() > (1.1f * getCurrentVictim()->GetThreat()))
setDirty(true);
iThreatContainer.addReference(hostileRef);
iThreatOfflineContainer.remove(hostileRef);
@@ -607,7 +610,7 @@ void ThreatMgr::processThreatEvent(ThreatRefStatusChangeEvent* threatRefStatusCh
setDirty(true);
}
iOwner->SendRemoveFromThreatListOpcode(hostileRef);
if (hostileRef->isOnline())
if (hostileRef->IsOnline())
iThreatContainer.remove(hostileRef);
else
iThreatOfflineContainer.remove(hostileRef);
@@ -630,14 +633,14 @@ bool ThreatMgr::isNeedUpdateToClient(uint32 time)
}
// Reset all aggro without modifying the threatlist.
void ThreatMgr::resetAllAggro()
void ThreatMgr::ResetAllThreat()
{
ThreatContainer::StorageType& threatList = iThreatContainer.iThreatList;
if (threatList.empty())
return;
for (ThreatContainer::StorageType::iterator itr = threatList.begin(); itr != threatList.end(); ++itr)
(*itr)->setThreat(0);
(*itr)->SetThreat(0);
setDirty(true);
}

View File

@@ -19,6 +19,7 @@
#define _THREATMANAGER
#include "Common.h"
#include "IteratorPair.h"
#include "ObjectGuid.h"
#include "Reference.h"
#include "SharedDefines.h"
@@ -49,36 +50,43 @@ class HostileReference : public Reference<Unit, ThreatMgr>
public:
HostileReference(Unit* refUnit, ThreatMgr* threatMgr, float threat);
//=================================================
void addThreat(float modThreat);
Unit* GetOwner() const;
Unit* GetVictim() const { return getTarget(); }
void setThreat(float threat) { addThreat(threat - getThreat()); }
//=================================================
void AddThreat(float modThreat);
void SetThreat(float threat) { AddThreat(threat - GetThreat()); }
void addThreatPercent(int32 percent);
[[nodiscard]] float getThreat() const { return iThreat; }
[[nodiscard]] float GetThreat() const { return iThreat; }
[[nodiscard]] bool isOnline() const { return iOnline; }
void ClearThreat() { removeReference(); }
[[nodiscard]] bool IsOnline() const { return iOnline; }
[[nodiscard]] bool IsAvailable() const { return iOnline; } // unused for now
[[nodiscard]] bool IsOffline() const { return !iOnline; } // unused for now
// used for temporary setting a threat and reducting it later again.
// the threat modification is stored
void setTempThreat(float threat)
{
addTempThreat(threat - getThreat());
addTempThreat(threat - GetThreat());
}
void addTempThreat(float threat)
{
iTempThreatModifier = threat;
if (iTempThreatModifier != 0.0f)
addThreat(iTempThreatModifier);
AddThreat(iTempThreatModifier);
}
void resetTempThreat()
{
if (iTempThreatModifier != 0.0f)
{
addThreat(-iTempThreatModifier);
AddThreat(-iTempThreatModifier);
iTempThreatModifier = 0.0f;
}
}
@@ -143,11 +151,11 @@ public:
~ThreatContainer() { clearReferences(); }
HostileReference* addThreat(Unit* victim, float threat);
HostileReference* AddThreat(Unit* victim, float threat);
void modifyThreatPercent(Unit* victim, int32 percent);
void ModifyThreatByPercent(Unit* victim, int32 percent);
HostileReference* selectNextVictim(Creature* attacker, HostileReference* currentVictim) const;
HostileReference* SelectNextVictim(Creature* attacker, HostileReference* currentVictim) const;
void setDirty(bool isDirty) { iDirty = isDirty; }
@@ -163,10 +171,10 @@ public:
return iThreatList.empty() ? nullptr : iThreatList.front();
}
HostileReference* getReferenceByTarget(Unit* victim) const;
HostileReference* getReferenceByTarget(Unit const* victim) const;
HostileReference* getReferenceByTarget(ObjectGuid const& guid) const;
[[nodiscard]] StorageType const& getThreatList() const { return iThreatList; }
[[nodiscard]] StorageType const& GetThreatList() const { return iThreatList; }
private:
void remove(HostileReference* hostileRef)
@@ -190,6 +198,8 @@ private:
//=================================================
typedef HostileReference ThreatReference;
class ThreatMgr
{
public:
@@ -199,16 +209,17 @@ public:
~ThreatMgr() { clearReferences(); }
Unit* SelectVictim() { return getHostileTarget(); }
Unit* GetCurrentVictim() const { if (ThreatReference* ref = getCurrentVictim()) return ref->GetVictim(); else return nullptr; }
Unit* GetAnyTarget() const { auto const& list = GetThreatList(); if (!list.empty()) return list.front()->getTarget(); return nullptr; }
void clearReferences();
void addThreat(Unit* victim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = nullptr);
void doAddThreat(Unit* victim, float threat);
void modifyThreatPercent(Unit* victim, int32 percent);
float getThreat(Unit* victim, bool alsoSearchOfflineList = false);
void AddThreat(Unit* victim, float threat, SpellSchoolMask schoolMask = SPELL_SCHOOL_MASK_NORMAL, SpellInfo const* threatSpell = nullptr);
void DoAddThreat(Unit* victim, float threat);
void ModifyThreatByPercent(Unit* victim, int32 percent);
float GetThreat(Unit* victim, bool alsoSearchOfflineList = false);
float GetThreatListSize() const { return GetThreatList().size(); }
float getThreatWithoutTemp(Unit* victim, bool alsoSearchOfflineList = false);
[[nodiscard]] bool isThreatListEmpty() const { return iThreatContainer.empty(); }
@@ -232,11 +243,10 @@ public:
void setDirty(bool isDirty) { iThreatContainer.setDirty(isDirty); }
// Reset all aggro without modifying the threadlist.
void resetAllAggro();
// -- compatibility layer for combat rewrite
void ResetAllThreat() { resetAllAggro(); }
void ResetThreat(Unit const* who) { if (auto* ref = FindReference(who, true)) ref->SetThreat(0.0f); }
void ResetAllThreat();
void ClearThreat(Unit const* who) { if (auto* ref = FindReference(who, true)) ref->removeReference(); }
void ClearAllThreat();
// Reset all aggro of unit in threadlist satisfying the predicate.
@@ -250,7 +260,7 @@ public:
{
if (predicate(ref->getTarget()))
{
ref->setThreat(0);
ref->SetThreat(0);
setDirty(true);
}
}
@@ -258,11 +268,14 @@ public:
// 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.
[[nodiscard]] ThreatContainer::StorageType const& getThreatList() const { return iThreatContainer.getThreatList(); }
[[nodiscard]] ThreatContainer::StorageType const& getOfflineThreatList() const { return iThreatOfflineContainer.getThreatList(); }
ThreatContainer& getOnlineContainer() { return iThreatContainer; }
ThreatContainer& getOfflineContainer() { return iThreatOfflineContainer; }
[[nodiscard]] ThreatContainer::StorageType const& GetThreatList() const { return iThreatContainer.GetThreatList(); }
[[nodiscard]] ThreatContainer::StorageType const& GetOfflineThreatList() const { return iThreatOfflineContainer.GetThreatList(); }
ThreatContainer& GetOnlineContainer() { return iThreatContainer; }
ThreatContainer& GetOfflineContainer() { return iThreatOfflineContainer; }
private:
HostileReference* FindReference(Unit const* who, bool includeOffline) const { if (auto* ref = iThreatContainer.getReferenceByTarget(who)) return ref; if (includeOffline) if (auto* ref = iThreatOfflineContainer.getReferenceByTarget(who)) return ref; return nullptr; }
void _addThreat(Unit* victim, float threat);
HostileReference* iCurrentVictim;
@@ -283,7 +296,7 @@ namespace Acore
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();
return m_ascending ? a->GetThreat() < b->GetThreat() : a->GetThreat() > b->GetThreat();
}
private:
const bool m_ascending;