mod-playerbots/src/Ai/Base/Value/PossibleRpgTargetsValue.cpp
Boidl ae9b76aaa5
Fix Undead/Draenei bots stuck in starting zones (#2298)
## Pull Request Description

Undead and Draenei bots get stuck in an idle/rest loop in their starting
zones because the default NPC scan range (150f) and quest giver filter
(80f) are too small and not enough. NPCs fall within range for
`WANDER_NPC` to activate (requires >= 3).

This adds a configurable area-based override that increases both ranges
to 200f only in affected areas. All other zones remain at default
values.

  ## Feature Evaluation

- **Minimum logic**: One `std::set::count()` lookup per `Calculate()`
call to check if the bot's current area is in the override list. If yes,
scan range is 200f instead of 150f.
- **Processing cost**: `GetAreaId()` is a cached uint32 read (~1ns).
`std::set::count()` on a 2-element set is O(log n) ≈ 1 comparison.
Negligible compared to the grid scan itself (~33,000-53,000ns).

  ## How to Test the Changes

  1. Create Undead or Draenei bots (level 1)
2. Observe that they pick up quests and start moving in Deathknell/Ammen
Vale
  3. Without this fix, they sit in REST status indefinitely
4. Optionally add/remove area IDs via
`AiPlayerbot.RpgScanRangeOverrideAreaIds` in playerbots.conf

  ## Impact Assessment

- Does this change increase per-bot/per-tick processing or risk scaling
poorly with thousands of bots?
      - [x] Minimal impact
      - pmon data (500 bots, extended run):
        - Default 150f: 0.033ms avg
        - Global 200f: 0.053ms avg
        - Area check (this PR): 0.042ms avg
- The 0.009ms increase over default is caused by bots currently in
starting zones scanning at 200f. Bots outside override areas are
unaffected.

  - Does this change modify default bot behavior?
      - [x] No

- Does this change add new decision branches or increase maintenance
complexity?
      - [x] No
- Uses the existing `LoadSet`/`std::set` config pattern already used
throughout the codebase.

  ## AI Assistance

  - [x] Yes
- Used AI to speed up understanding the codebase, locate relevant
functions, and compare with the cmangos playerbots implementation. All
code was reviewed and tested manually.

  ## Final Checklist

  - [x] Stability is not compromised.
  - [x] Performance impact is understood, tested, and acceptable.
  - [x] Added logic complexity is justified and explained.
  - [x] Any new bot dialogue lines are translated.
  - [x] Documentation updated if needed (Conf comments, WiKi commands).

  ## Notes for Reviewers

The root cause is that `WANDER_NPC` requires `possibleTargets.size() >=
3`, but sparse starting zones have fewer than 3 NPC-flagged units within
150f. Increasing the scan range to 200f brings enough NPCs into range
for the status check to pass. The override is configurable via
`AiPlayerbot.RpgScanRangeOverrideAreaIds` so server admins can add more
areas without code changes.
2026-04-10 22:16:15 -07:00

220 lines
7.6 KiB
C++

/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license, you may redistribute it
* and/or modify it under version 3 of the License, or (at your option), any later version.
*/
#include "PossibleRpgTargetsValue.h"
#include "CellImpl.h"
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
#include "ObjectGuid.h"
#include "Playerbots.h"
#include "ServerFacade.h"
#include "SharedDefines.h"
#include "NearestGameObjects.h"
#include <unordered_set>
std::vector<uint32> PossibleRpgTargetsValue::allowedNpcFlags;
PossibleRpgTargetsValue::PossibleRpgTargetsValue(PlayerbotAI* botAI, float range)
: NearestUnitsValue(botAI, "possible rpg targets", range, true)
{
if (allowedNpcFlags.empty())
{
allowedNpcFlags.push_back(UNIT_NPC_FLAG_INNKEEPER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_GOSSIP);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_QUESTGIVER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_FLIGHTMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_BANKER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_GUILD_BANKER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER_CLASS);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER_PROFESSION);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_AMMO);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_FOOD);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_POISON);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_REAGENT);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_AUCTIONEER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_STABLEMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_PETITIONER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TABARDDESIGNER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_BATTLEMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_REPAIR);
}
}
void PossibleRpgTargetsValue::FindUnits(std::list<Unit*>& targets)
{
Acore::AnyUnitInObjectRangeCheck u_check(bot, range);
Acore::UnitListSearcher<Acore::AnyUnitInObjectRangeCheck> searcher(bot, targets, u_check);
Cell::VisitObjects(bot, searcher, range);
}
bool PossibleRpgTargetsValue::AcceptUnit(Unit* unit)
{
if (!unit || !unit->IsInWorld() || unit->IsDuringRemoveFromWorld())
return false;
if (unit->IsHostileTo(bot) || unit->IsPlayer())
return false;
if (ServerFacade::instance().GetDistance2d(bot, unit) <= sPlayerbotAIConfig.tooCloseDistance)
return false;
if (unit->HasNpcFlag(UNIT_NPC_FLAG_SPIRITHEALER))
return false;
for (uint32 npcFlag : allowedNpcFlags)
{
if (unit->HasNpcFlag(static_cast<NPCFlags>(npcFlag)))
return true;
}
TravelTarget* travelTarget = context->GetValue<TravelTarget*>("travel target")->Get();
if (travelTarget && travelTarget->getDestination() &&
travelTarget->getDestination()->getEntry() == unit->GetEntry())
return true;
if (urand(1, 100) < 25 && unit->IsFriendlyTo(bot))
return true;
if (urand(1, 100) < 5)
return true;
return false;
}
std::vector<uint32> PossibleNewRpgTargetsValue::allowedNpcFlags;
// Sparse starting zones where the default scan range is insufficient for WANDER_NPC (requires >= 3 NPCs)
static const std::unordered_set<uint32> rpgRangeOverrideAreaIds = { 3526 /* Ammen Vale */, 2117 /* Deathknell */ };
PossibleNewRpgTargetsValue::PossibleNewRpgTargetsValue(PlayerbotAI* botAI, float range)
: NearestUnitsValue(botAI, "possible new rpg targets", range, true), defaultRange(range)
{
if (allowedNpcFlags.empty())
{
allowedNpcFlags.push_back(UNIT_NPC_FLAG_INNKEEPER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_GOSSIP);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_QUESTGIVER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_FLIGHTMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_BANKER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_GUILD_BANKER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER_CLASS);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER_PROFESSION);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_AMMO);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_FOOD);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_POISON);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR_REAGENT);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_AUCTIONEER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_STABLEMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_PETITIONER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TABARDDESIGNER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_BATTLEMASTER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_TRAINER);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_VENDOR);
allowedNpcFlags.push_back(UNIT_NPC_FLAG_REPAIR);
}
}
GuidVector PossibleNewRpgTargetsValue::Calculate()
{
if (rpgRangeOverrideAreaIds.count(bot->GetAreaId()) && defaultRange < 200.0f)
range = 200.0f;
else
range = defaultRange;
std::list<Unit*> targets;
FindUnits(targets);
GuidVector results;
std::vector<std::pair<ObjectGuid, float>> guidDistancePairs;
for (Unit* unit : targets)
{
if (AcceptUnit(unit) && (ignoreLos || bot->IsWithinLOSInMap(unit)))
guidDistancePairs.push_back({unit->GetGUID(), bot->GetExactDist(unit)});
}
// Override to sort by distance
std::sort(guidDistancePairs.begin(), guidDistancePairs.end(), [](auto const& a, auto const& b) {
return a.second < b.second;
});
for (auto const& pair : guidDistancePairs) {
results.push_back(pair.first);
}
return results;
}
void PossibleNewRpgTargetsValue::FindUnits(std::list<Unit*>& targets)
{
Acore::AnyUnitInObjectRangeCheck u_check(bot, range);
Acore::UnitListSearcher<Acore::AnyUnitInObjectRangeCheck> searcher(bot, targets, u_check);
Cell::VisitObjects(bot, searcher, range);
}
bool PossibleNewRpgTargetsValue::AcceptUnit(Unit* unit)
{
if (!unit || !unit->IsInWorld() || unit->IsDuringRemoveFromWorld())
return false;
if (unit->IsHostileTo(bot) || unit->IsPlayer())
return false;
if (unit->HasNpcFlag(UNIT_NPC_FLAG_SPIRITHEALER))
return false;
for (uint32 npcFlag : allowedNpcFlags)
{
if (unit->HasNpcFlag(static_cast<NPCFlags>(npcFlag)))
return true;
}
return false;
}
std::vector<GameobjectTypes> PossibleNewRpgGameObjectsValue::allowedGOFlags;
GuidVector PossibleNewRpgGameObjectsValue::Calculate()
{
std::list<GameObject*> targets;
AnyGameObjectInObjectRangeCheck u_check(bot, range);
Acore::GameObjectListSearcher<AnyGameObjectInObjectRangeCheck> searcher(bot, targets, u_check);
Cell::VisitObjects(bot, searcher, range);
std::vector<std::pair<ObjectGuid, float>> guidDistancePairs;
for (GameObject* go : targets)
{
bool flagCheck = false;
for (uint32 goFlag : allowedGOFlags)
{
if (go->GetGoType() == goFlag)
{
flagCheck = true;
break;
}
}
if (!flagCheck)
continue;
if (!ignoreLos && !bot->IsWithinLOSInMap(go))
continue;
guidDistancePairs.push_back({go->GetGUID(), bot->GetExactDist(go)});
}
GuidVector results;
// Sort by distance
std::sort(guidDistancePairs.begin(), guidDistancePairs.end(), [](auto const& a, auto const& b) {
return a.second < b.second;
});
for (auto const& pair : guidDistancePairs) {
results.push_back(pair.first);
}
return results;
}