CoreUpdate - ThreatMgr (#2228)

<!--
Thank you for contributing to mod-playerbots, please make sure that
you...
1. Submit your PR to the test-staging branch, not master.
2. Read the guidelines below before submitting.
3. Don't delete parts of this template.

DESIGN PHILOSOPHY: We prioritize STABILITY, PERFORMANCE, AND
PREDICTABILITY over behavioral realism.

Every action and decision executes PER BOT AND PER TRIGGER. Small
increases in logic complexity scale
poorly across thousands of bots and negatively affect all. We prioritize
a stable system over a smarter
one. Bots don't need to behave perfectly; believable behavior is the
goal, not human simulation.
Default behavior must be cheap in processing; expensive behavior must be
opt-in.

Before submitting, make sure your changes aligns with these principles.
-->

## Pull Request Description
<!-- Describe what this change does and why it is needed -->
Modification to threat system required for current core update PR. 



## Feature Evaluation
<!--
If your PR is very minimal (comment typo, wrong ID reference, etc), and
it is very obvious it will not have
any impact on performance, you may skip these question. If necessary, a
maintainer may ask you for them later.
-->

<!-- Please answer the following: -->
- Describe the **minimum logic** required to achieve the intended
behavior.
- Describe the **processing cost** when this logic executes across many
bots.



## How to Test the Changes
<!--
- Step-by-step instructions to test the change.
- Any required setup (e.g. multiple players, number of bots, specific
configuration).
- Expected behavior and how to verify it.
-->



## Impact Assessment
<!-- As a generic test, before and after measure of pmon (playerbot pmon
tick) can help you here. -->
- Does this change increase per-bot/per-tick processing or risk scaling
poorly with thousands of bots?
    - - [x] No, not at all
    - - [ ] Minimal impact (**explain below**)
    - - [ ] Moderate impact (**explain below**)



- Does this change modify default bot behavior?
    - - [x] No
    - - [ ] Yes (**explain why**)



- Does this change add new decision branches or increase maintenance
complexity?
    - - [x] No
    - - [ ] Yes (**explain below**)



## Messages to Translate
<!--
Bot messages have to be translatable, but you don't need to do the
translations here. You only need to make sure
the message is in a translatable format, and list in the table the
message_key and the default English message.
Search for GetBotTextOrDefault in the codebase for examples.
-->
Does this change add bot messages to translate?
    - - [x] No
    - - [ ] Yes (**list messages in the table**)

| Message key  | Default message |
| --------------- | ------------------ |
|			 |			      |
|			 |			      |

## AI Assistance
<!--
AI assistance is allowed, but all submitted code must be fully
understood, reviewed, and owned by the contributor.
We expect contributors to be honest about what they do and do not
understand.
-->
Was AI assistance used while working on this change?
    - - [ ] No
    - - [x] Yes (**explain below**)
<!--
If yes, please specify:
- Purpose of usage (e.g. brainstorming, refactoring, documentation, code
generation).
- Which parts of the change were influenced or generated, and whether it
was thoroughly reviewed.
-->
Claude. Module search for changes made. It also identified a section of
dead code in EnemyPlayerValue due to incorrect ref that was fixed.


## Final Checklist

- - [X] Stability is not compromised.
- - [X] Performance impact is understood, tested, and acceptable.
- - [X] Added logic complexity is justified and explained.
- - [X] Documentation updated if needed (Conf comments, WiKi commands).

## Notes for Reviewers
<!-- Anything else that's helpful to review or test your pull request.
-->
This commit is contained in:
Keleborn 2026-03-21 15:41:07 -07:00 committed by GitHub
parent 32af1b95de
commit 9f875a7c81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 92 additions and 145 deletions

View File

@ -1387,8 +1387,8 @@ bool MovementAction::Flee(Unit* target)
}
}
HostileReference* ref = target->GetThreatMgr().getCurrentVictim();
if (ref && ref->getTarget() == bot) // bot is target - try to flee to tank or master
Unit* currentVictim = target->GetThreatMgr().GetCurrentVictim();
if (currentVictim && currentVictim == bot) // bot is target - try to flee to tank or master
{
if (Group* group = bot->GetGroup())
{

View File

@ -6,7 +6,8 @@
#include "TellTargetAction.h"
#include "Event.h"
#include "ThreatMgr.h"
#include "CombatManager.h"
#include "ThreatManager.h"
#include "AiObjectContext.h"
#include "PlayerbotAI.h"
@ -42,21 +43,21 @@ bool TellAttackersAction::Execute(Event /*event*/)
botAI->TellMaster("--- Threat ---");
HostileReference* ref = bot->getHostileRefMgr().getFirst();
if (!ref)
auto const& threatenedByMe = bot->GetThreatMgr().GetThreatenedByMeList();
if (threatenedByMe.empty())
return true;
while (ref)
for (auto const& [guid, ref] : threatenedByMe)
{
ThreatMgr* threatMgr = ref->GetSource();
Unit* unit = threatMgr->GetOwner();
Unit* unit = ref->GetOwner();
if (!unit)
continue;
float threat = ref->GetThreat();
std::ostringstream out;
out << unit->GetName() << " (" << threat << ")";
botAI->TellMaster(out);
ref = ref->next();
}
return true;

View File

@ -16,7 +16,7 @@
#include "PositionValue.h"
#include "SharedDefines.h"
#include "TemporarySummon.h"
#include "ThreatMgr.h"
#include "ThreatManager.h"
#include "Timer.h"
#include "PlayerbotAI.h"
#include "Player.h"
@ -217,7 +217,7 @@ bool LowTankThreatTrigger::IsActive()
if (!current_target)
return false;
ThreatMgr& mgr = current_target->GetThreatMgr();
ThreatManager& mgr = current_target->GetThreatMgr();
float threat = mgr.GetThreat(bot);
float tankThreat = mgr.GetThreat(mt);
return tankThreat == 0.0f || threat > tankThreat * 0.5f;

View File

@ -92,21 +92,15 @@ void AttackersValue::AddAttackersOf(Player* player, std::unordered_set<Unit*>& t
if (!player || !player->IsInWorld() || player->IsBeingTeleported())
return;
HostileRefMgr& refManager = player->getHostileRefMgr();
HostileReference* ref = refManager.getFirst();
if (!ref)
return;
while (ref)
for (auto const& [guid, ref] : player->GetThreatMgr().GetThreatenedByMeList())
{
ThreatMgr* threatMgr = ref->GetSource();
Unit* attacker = threatMgr->GetOwner();
Unit* attacker = ref->GetOwner();
if (!attacker)
continue;
if (player->IsValidAttackTarget(attacker) &&
player->GetDistance2d(attacker) < sPlayerbotAIConfig.sightDistance)
targets.insert(attacker);
ref = ref->next();
}
}
@ -131,7 +125,6 @@ bool AttackersValue::hasRealThreat(Unit* attacker)
return attacker && attacker->IsInWorld() && attacker->IsAlive() && !attacker->IsPolymorphed() &&
// !attacker->isInRoots() &&
!attacker->IsFriendlyTo(bot);
(attacker->GetThreatMgr().getCurrentVictim() || dynamic_cast<Player*>(attacker));
}
bool AttackersValue::IsPossibleTarget(Unit* attacker, Player* bot, float /*range*/)
@ -241,9 +234,6 @@ bool AttackersValue::IsPossibleTarget(Unit* attacker, Player* bot, float /*range
bool AttackersValue::IsValidTarget(Unit* attacker, Player* bot)
{
return IsPossibleTarget(attacker, bot) && bot->IsWithinLOSInMap(attacker);
// (attacker->GetThreatMgr().getCurrentVictim() || attacker->GetGuidValue(UNIT_FIELD_TARGET) ||
// attacker->GetGUID().IsPlayer() || attacker->GetGUID() ==
// GET_PLAYERBOT_AI(bot)->GetAiObjectContext()->GetValue<ObjectGuid>("pull target")->Get());
}
bool PossibleAddsValue::Calculate()
@ -255,13 +245,11 @@ bool PossibleAddsValue::Calculate()
{
if (find(attackers.begin(), attackers.end(), guid) != attackers.end())
continue;
if (Unit* add = botAI->GetUnit(guid))
{
if (!add->IsInWorld() || add->IsDuringRemoveFromWorld())
Unit* add = botAI->GetUnit(guid);
if (!add || !add->IsInWorld() || add->IsDuringRemoveFromWorld())
continue;
if (!add->GetTarget() && !add->GetThreatMgr().getCurrentVictim() && add->IsHostileTo(bot))
if (!add->GetTarget() && !add->GetThreatMgr().GetLastVictim() && add->IsHostileTo(bot))
{
for (ObjectGuid const attackerGUID : attackers)
{
@ -278,7 +266,6 @@ bool PossibleAddsValue::Calculate()
}
}
}
}
return false;
}

View File

@ -20,7 +20,7 @@ public:
}
public:
void CheckAttacker(Unit* creature, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* creature, ThreatManager* threatMgr) override
{
Player* bot = botAI->GetBot();
if (!botAI->CanCastSpell(spell, creature))

View File

@ -13,7 +13,7 @@ public:
{
}
void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) override
{
if (botAI->HasAura(spell, attacker))
result = attacker;

View File

@ -13,16 +13,14 @@ class FindMaxThreatGapTargetStrategy : public FindTargetStrategy
public:
FindMaxThreatGapTargetStrategy(PlayerbotAI* botAI) : FindTargetStrategy(botAI), minThreat(0) {}
void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) override
{
if (!attacker->IsAlive())
{
return;
}
if (foundHighPriority)
{
return;
}
if (IsHighPriority(attacker))
{
result = attacker;
@ -32,7 +30,7 @@ public:
if (!result || CalcThreatGap(attacker, threatMgr) > CalcThreatGap(result, &result->GetThreatMgr()))
result = attacker;
}
float CalcThreatGap(Unit* attacker, ThreatMgr* threatMgr)
float CalcThreatGap(Unit* attacker, ThreatManager* threatMgr)
{
Unit* victim = attacker->GetVictim();
return threatMgr->GetThreat(victim) - threatMgr->GetThreat(attacker);
@ -52,7 +50,7 @@ public:
result = nullptr;
}
void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) override
{
if (Group* group = botAI->GetBot()->GetGroup())
{
@ -61,13 +59,11 @@ public:
return;
}
if (!attacker->IsAlive())
{
return;
}
if (foundHighPriority)
{
return;
}
if (IsHighPriority(attacker))
{
result = attacker;
@ -90,24 +86,19 @@ public:
int new_level = GetIntervalLevel(new_unit);
int old_level = GetIntervalLevel(old_unit);
if (new_level != old_level)
{
return new_level > old_level;
}
int32_t level = new_level;
if (level % 10 == 2 || level % 10 == 0)
{
return new_time < old_time;
}
// dont switch targets when all of them with low health
Unit* currentTarget = botAI->GetAiObjectContext()->GetValue<Unit*>("current target")->Get();
if (currentTarget == new_unit)
{
return true;
}
if (currentTarget == old_unit)
{
return false;
}
return new_time > old_time;
}
int32_t GetIntervalLevel(Unit* unit)
@ -119,13 +110,11 @@ public:
attackRange += 5.0f;
int level = dis < attackRange ? 10 : 0;
if (time >= 5 && time <= 30)
{
return level + 2;
}
if (time > 30)
{
return level;
}
return level + 1;
}
@ -143,7 +132,7 @@ public:
{
}
void CheckAttacker(Unit* attacker, ThreatMgr*) override
void CheckAttacker(Unit* attacker, ThreatManager*) override
{
if (Group* group = botAI->GetBot()->GetGroup())
{
@ -152,13 +141,11 @@ public:
return;
}
if (!attacker->IsAlive())
{
return;
}
if (foundHighPriority)
{
return;
}
if (IsHighPriority(attacker))
{
result = attacker;
@ -186,9 +173,8 @@ public:
// attack enemy in range and with lowest health
int level = new_level;
if (level == 10)
{
return new_time < old_time;
}
// all targets are far away, choose the closest one
return botAI->GetBot()->GetDistance(new_unit) < botAI->GetBot()->GetDistance(old_unit);
}
@ -216,7 +202,7 @@ public:
{
}
void CheckAttacker(Unit* attacker, ThreatMgr*) override
void CheckAttacker(Unit* attacker, ThreatManager*) override
{
if (Group* group = botAI->GetBot()->GetGroup())
{
@ -225,13 +211,11 @@ public:
return;
}
if (!attacker->IsAlive())
{
return;
}
if (foundHighPriority)
{
return;
}
if (IsHighPriority(attacker))
{
result = attacker;
@ -254,9 +238,8 @@ public:
int new_level = GetIntervalLevel(new_unit);
int old_level = GetIntervalLevel(old_unit);
if (new_level != old_level)
{
return new_level > old_level;
}
// attack enemy in range and with lowest health
int level = new_level;
Player* bot = botAI->GetBot();
@ -264,9 +247,8 @@ public:
{
Unit* combo_unit = bot->GetComboTarget();
if (new_unit == combo_unit)
{
return true;
}
return new_time < old_time;
}
// all targets are far away, choose the closest one
@ -319,7 +301,7 @@ class FindMaxHpTargetStrategy : public FindTargetStrategy
public:
FindMaxHpTargetStrategy(PlayerbotAI* botAI) : FindTargetStrategy(botAI), maxHealth(0) {}
void CheckAttacker(Unit* attacker, ThreatMgr*) override
void CheckAttacker(Unit* attacker, ThreatManager*) override
{
if (Group* group = botAI->GetBot()->GetGroup())
{

View File

@ -5,6 +5,7 @@
#include "EnemyPlayerValue.h"
#include "CombatManager.h"
#include "Playerbots.h"
#include "ServerFacade.h"
#include "Vehicle.h"
@ -51,35 +52,22 @@ Unit* EnemyPlayerValue::Calculate()
controllingVehicle = true;
}
// 1. Check units we are currently in combat with.
// 1. Check units we are currently in PvP combat with.
std::vector<Unit*> targets;
Unit* pVictim = bot->GetVictim();
HostileReference* pReference = bot->getHostileRefMgr().getFirst();
while (pReference)
for (auto const& [guid, combatRef] : bot->GetCombatManager().GetPvPCombatRefs())
{
ThreatMgr* threatMgr = pReference->GetSource();
if (Unit* pTarget = threatMgr->GetOwner())
{
if (pTarget != pVictim && pTarget->IsPlayer() && pTarget->CanSeeOrDetect(bot) &&
bot->IsWithinDist(pTarget, VISIBILITY_DISTANCE_NORMAL))
{
if (bot->GetTeamId() == TEAM_HORDE)
{
if (pTarget->HasAura(23333))
Unit* pTarget = combatRef->GetOther(bot);
if (!pTarget || pTarget == pVictim || !pTarget->IsPlayer() || !pTarget->CanSeeOrDetect(bot) ||
!bot->IsWithinDist(pTarget, VISIBILITY_DISTANCE_NORMAL))
continue;
if ((bot->GetTeamId() == TEAM_HORDE && Target->HasAura(23333)) ||
(bot->GetTeamId() == TEAM_ALLIANCE && pTarget->HasAura(23335)))
return pTarget;
}
else
{
if (pTarget->HasAura(23335))
return pTarget;
}
targets.push_back(pTarget);
}
}
pReference = pReference->next();
}
if (!targets.empty())
{

View File

@ -13,7 +13,7 @@ class FindLeastHpTargetStrategy : public FindNonCcTargetStrategy
public:
FindLeastHpTargetStrategy(PlayerbotAI* botAI) : FindNonCcTargetStrategy(botAI), minHealth(0) {}
void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) override
{
if (IsCcTarget(attacker))
return;

View File

@ -15,12 +15,11 @@ class FindTargetForTankStrategy : public FindNonCcTargetStrategy
public:
FindTargetForTankStrategy(PlayerbotAI* botAI) : FindNonCcTargetStrategy(botAI), minThreat(0) {}
void CheckAttacker(Unit* creature, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* creature, ThreatManager* threatMgr) override
{
if (!creature || !creature->IsAlive())
{
return;
}
Player* bot = botAI->GetBot();
float threat = threatMgr->GetThreat(bot);
if (!result)
@ -29,15 +28,11 @@ public:
result = creature;
}
// neglect if victim is main tank, or no victim (for untauntable target)
if (threatMgr->getCurrentVictim())
{
// float max_threat = threatMgr->GetThreat(threatMgr->getCurrentVictim()->getTarget());
Unit* victim = threatMgr->getCurrentVictim()->getTarget();
if (victim && victim->ToPlayer() && botAI->IsMainTank(victim->ToPlayer()))
if (Unit* victim = threatMgr->GetCurrentVictim())
{
if (victim->ToPlayer() && botAI->IsMainTank(victim->ToPlayer()))
return;
}
}
if (minThreat >= threat)
{
minThreat = threat;
@ -54,7 +49,7 @@ class FindTankTargetSmartStrategy : public FindTargetStrategy
public:
FindTankTargetSmartStrategy(PlayerbotAI* botAI) : FindTargetStrategy(botAI) {}
void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) override
void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) override
{
if (Group* group = botAI->GetBot()->GetGroup())
{
@ -63,14 +58,11 @@ public:
return;
}
if (!attacker->IsAlive())
{
return;
}
if (!result || IsBetter(attacker, result))
{
result = attacker;
}
}
bool IsBetter(Unit* new_unit, Unit* old_unit)
{
Player* bot = botAI->GetBot();
@ -80,6 +72,7 @@ public:
{
if (old_unit == currentTarget)
return false;
if (new_unit == currentTarget)
return true;
}
@ -89,26 +82,22 @@ public:
float old_dis = bot->GetDistance(old_unit);
// hasAggro? -> withinMelee? -> threat
if (GetIntervalLevel(new_unit) != GetIntervalLevel(old_unit))
{
return GetIntervalLevel(new_unit) > GetIntervalLevel(old_unit);
}
int32_t interval = GetIntervalLevel(new_unit);
if (interval == 2)
{
return new_dis < old_dis;
}
return new_threat < old_threat;
}
int32_t GetIntervalLevel(Unit* unit)
{
if (!botAI->HasAggro(unit))
{
return 2;
}
if (botAI->GetBot()->IsWithinMeleeRange(unit))
{
return 1;
}
return 0;
}
};

View File

@ -5,12 +5,13 @@
#include "TargetValue.h"
#include "CombatManager.h"
#include "LastMovementValue.h"
#include "ObjectGuid.h"
#include "Playerbots.h"
#include "RtiTargetValue.h"
#include "ScriptedCreature.h"
#include "ThreatMgr.h"
#include "ThreatManager.h"
Unit* FindTargetStrategy::GetResult() { return result; }
@ -23,8 +24,8 @@ Unit* TargetValue::FindTarget(FindTargetStrategy* strategy)
if (!unit)
continue;
ThreatMgr& ThreatMgr = unit->GetThreatMgr();
strategy->CheckAttacker(unit, &ThreatMgr);
ThreatManager& threatMgr = unit->GetThreatMgr();
strategy->CheckAttacker(unit, &threatMgr);
}
return strategy->GetResult();
@ -144,24 +145,23 @@ Unit* FindTargetValue::Calculate()
{
return nullptr;
}
HostileReference* ref = bot->getHostileRefMgr().getFirst();
while (ref)
for (auto const& [guid, ref] : bot->GetThreatMgr().GetThreatenedByMeList())
{
ThreatMgr* threatManager = ref->GetSource();
Unit* unit = threatManager->GetOwner();
Unit* unit = ref->GetOwner();
if (!unit)
continue;
std::wstring wnamepart;
Utf8toWStr(unit->GetName(), wnamepart);
wstrToLower(wnamepart);
if (!qualifier.empty() && qualifier.length() == wnamepart.length() && Utf8FitTo(qualifier, wnamepart))
{
return unit;
}
ref = ref->next();
}
return nullptr;
}
void FindBossTargetStrategy::CheckAttacker(Unit* attacker, ThreatMgr* threatManager)
void FindBossTargetStrategy::CheckAttacker(Unit* attacker, ThreatManager* threatManager)
{
UnitAI* unitAI = attacker->GetAI();
BossAI* bossAI = dynamic_cast<BossAI*>(unitAI);

View File

@ -11,7 +11,7 @@
#include "Value.h"
class PlayerbotAI;
class ThreatMgr;
class ThreatManager;
class Unit;
class FindTargetStrategy
@ -20,7 +20,7 @@ public:
FindTargetStrategy(PlayerbotAI* botAI) : result(nullptr), botAI(botAI) {}
Unit* GetResult();
virtual void CheckAttacker(Unit* attacker, ThreatMgr* threatMgr) = 0;
virtual void CheckAttacker(Unit* attacker, ThreatManager* threatMgr) = 0;
void GetPlayerCount(Unit* creature, uint32* tankCount, uint32* dpsCount);
bool IsHighPriority(Unit* attacker);
@ -129,7 +129,7 @@ class FindBossTargetStrategy : public FindTargetStrategy
{
public:
FindBossTargetStrategy(PlayerbotAI* ai) : FindTargetStrategy(ai) {}
virtual void CheckAttacker(Unit* attacker, ThreatMgr* threatManager);
virtual void CheckAttacker(Unit* attacker, ThreatManager* threatManager);
};
class BossTargetValue : public TargetValue, public Qualified

View File

@ -6,7 +6,7 @@
#include "ThreatValues.h"
#include "Playerbots.h"
#include "ThreatMgr.h"
#include "ThreatManager.h"
uint8 ThreatValue::Calculate()
{