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

@@ -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);
}