Files
ForeverCore/src/modules/PlayerbotV2/PlayerbotV2.cpp
T
Zapgun 1f86c6ef66 PlayerbotV2: race/sex RPG first names + reusable surnames for bot chars
BotNamePool gains three first-name tiers: curated per-race/sex names from
the new playerbots_race_names table (consumed on spawn, released on bot
delete, orphans reclaimed by ReconcileOnBoot), then the generic
playerbots_names pool, then the existing syllable fallback. CharacterCache
collision checks self-heal taken rows.

BotComposition Pick carries a surname; name hints with a space split into
first name + surname (a hinted surname is never re-rolled).
BotCharacterFactory writes characters.surname in the same transaction as
the INSERT and mirrors it into the character cache, capped at 48 UTF-8
chars like the player path.

Surnames come from playerbots_surnames (whole names) or
playerbots_surname_parts (prefix/suffix compounds that reject degenerate
pairs), chosen by Playerbot.Names.SurnameCompoundChance; whole vs compound
is gated by Playerbot.Names.Surnames.

SQL: 9001 creates the three tables on the shared db (plain `race` column
so Skyborne ids 95/96 fit); 9002 seeds 995 first names, 404 surnames and
551 parts for all races incl. Skyborne. The 9000-series keeps the
community migrations clear of upstream numbering.

Also adds ".playerbot selftest botnames <race> <sex> [n]" to sample the
pool without spawning, and guards "selftest namegen" with IsValidRace /
IsValidGender before calling GetNameGenEntry (sex 3 crashed the client).
2026-11-23 22:13:07 +00:00

5458 lines
282 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Playerbot V2 - Module entry implementation
// All TrinityCore hook surfaces from PlayerbotV2.h are wired here. Some carry
// dispatch (login/logout register the bot; OnDeath taps loot; DamageTaken /
// HealReceived push event-bus entries; Whisper routes through the command
// parser). Hooks marked "Intentionally a no-op" rely on snapshot deltas
// instead of event pushes — see per-handler comments for the rationale.
#include "Util/SlowCall.h"
#include "PlayerbotV2.h"
#include "LfgBotFill.h"
#include "Services.h"
#include "Fleet/RaidSubgroupPlan.h"
#include "Fleet/AutoRaid.h"
#include "Bot/BossCheck.h"
#include "Bot/BotBridge.h"
#include "Bot/BotRegistry.h"
#include "Bot/BotSnapshot.h"
#include "Bot/World/LearnFlightpaths.h"
#include "Bot/BotSnapshotBuilder.h"
#include "Bot/QuestReverseIndex.h"
#include "Bot/QuestObjectiveLocations.h"
#include "Bot/World/NavmeshPrewarm.h"
#include "Bot/World/ZonePicker.h"
#include "PlayerbotMovement.h"
#include "Session/AddonControl.h"
#include "Session/BotSessionMgr.h"
#include "Session/HumanLogin.h"
#include "Fleet/BotAccountMgr.h"
#include "Fleet/BotCharacterFactory.h"
#include "Fleet/BotNamePool.h"
#include "Fleet/BotPopulationManager.h"
#include "Fleet/BotGuildMgr.h"
#include "Fleet/BotCoordinationBus.h"
#include "Fleet/CraftOrderBoard.h"
#include "Fleet/PlayerCraftOrders.h"
#include "Fleet/BotQueueFiller.h"
#include "Fleet/PickupGroups.h" // the bots' own pickup groups break up at the end of their life
#include "Fleet/JunkQuestResolver.h"
#include "Bot/ClassTables.h" // MaxPlayerLevel()
#include "Bot/Battleground/BgTeamCoordinator.h"
#include "Bot/Dungeon/PveGroupCoordinator.h"
#include "Bot/Raid/BossWarnings.h"
#include "Bot/Raid/RaidBrain.h"
#include "DB2Stores.h"
#include "World.h"
#include "WorldSession.h"
#include "DungeonFinding/LFGMgr.h"
#include "Battleground.h"
#include "BattlegroundMgr.h"
#include "PhasingHandler.h"
#include "SpellAuraDefines.h"
#include "Unit.h"
#include "Bot/BotPersonality.h"
#include "Bot/BotArchetype.h"
#include "Bot/BotRng.h"
#include "Bot/BotCommandParser.h"
#include "Bot/BotChatModel.h"
#include "Bot/BotChatReactor.h"
#include "Bot/GuildWhisperInvite.h"
#include "Bot/ChannelChat.h"
#include "Bot/GroupOrderText.h"
#include "Bot/CcDeny.h"
#include "Bot/Raid/BossTactics.h"
#include "Util.h"
#include "Bot/AddonHint.h"
#include "Bot/BotTextMgr.h"
#include "Api/BotLanguage.h"
#include "Chat.h"
#include "Bot/BotIntent.h"
#include "Threading/IntentQueue.h"
#include "Creature.h"
#include "GameTime.h"
#include "SpellInfo.h"
#include "SpellMgr.h"
#include "Group/GroupSnapshotBuilder.h"
#include "Group.h"
#include "Transport.h" // DriveRideWithOwner: the ship or zeppelin a player rides
#include "BotGroupRules.h"
#include "GitRevision.h"
#include <filesystem>
#include <fstream>
#include "GroupReference.h"
#include "GroupMgr.h"
#include "ObjectAccessor.h"
#include "PathGenerator.h"
#include "Pet.h"
#include "Fleet/PetSnap.h"
#include "Fleet/OwnerCatchUp.h"
#include "Fleet/OwnerTrail.h"
#include "Diagnostics/ScenarioHuman.h"
#include "Random.h"
#include "MMapManager.h" // MMAP::ScopedThreadNavMeshQuery (same-map split build)
#include <functional>
#include <optional>
#include <unordered_set>
#include "WorldSession.h"
#include "Bot/BotCommandParser.h"
#include "Threading/SnapshotPublisher.h"
#include "Threading/TickScheduler.h"
#include "Threading/ReactionWake.h"
#include "Bot/ReactionTiming.h"
#include "Combat/Apl/ApInterrupt.h"
#include "Threading/SnapshotBuildPool.h"
#include "Persistence/PlayerbotMigrationMgr.h"
#include "Diagnostics/PerfCounters.h"
#include "Diagnostics/DamageParity.h"
#include "Diagnostics/ScenarioRunner.h"
#include "Util/ConfigReader.h"
#include "World/WorldMetadata.h"
#include "Fleet/BotIdentityRegistry.h"
#include "Fleet/OwnerRegistry.h"
#include "Fleet/BotPool.h"
#include "Bot/GroupControl.h"
#include "CharacterCache.h"
#include "Bot/BotAI.h"
#include "Bot/DeathHotspots.h"
#include "Bot/BotSnapshotView.h"
#include "Bot/CastPacing.h"
#include "PlayerbotAPI.h"
#include "Fleet/BotCharacterFactory.h"
#include "Fleet/BotComposition.h"
#include "Combat/ApRegistry.h"
#include "Player.h"
#include "Spell.h"
#include "Map.h"
#include "ObjectAccessor.h"
#include "Log.h"
#include "Timer.h"
#include "GameTime.h" // GameTime::GetGameTimeMS for FleetStatus stuck-bot check
#include "DatabaseEnv.h" // CharacterDatabase — R7 leveling-target hydration + #1C vitals row
#include "fmt/format.h" // #1C fleet-vitals async row INSERT formatting
#include "Config.h" // sConfigMgr — V1-active coexistence warning at boot
#include "WorldSession.h" // HandleMoveTeleportAck / HandleMoveWorldportAck for headless bots
#include "WorldPacket.h"
#include "MovementPackets.h"
#include "Opcodes.h"
#include "MotionMaster.h"
#include "PlayerbotMovement.h" // BotMovement::SafeTeleport for rescue paths
#include "ObjectMgr.h"
#include <shared_mutex>
#include <unordered_map>
#include <unordered_set>
#include <mutex>
#include <atomic>
#include <memory>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <array>
#include <cctype>
#include <string_view>
#include "Bot/DeathCause.h"
#if !TRINITY_PLAYERBOT_V2
#error "PlayerbotV2 module compiled without TRINITY_PLAYERBOT_V2 defined; CMake misconfiguration"
#endif
namespace Playerbot {
// Defined in Bot/BotSnapshotResetCheck.cpp. Aborts at boot if
// BotSnapshot::reset_for_reuse() leaves any container/field non-default —
// guards the recycle pool (SNAPSHOT_PERF_BACKLOG Tier 3.1) against drift.
bool VerifyResetClearsAll();
}
namespace Playerbot::V2 {
// Players still to be told about the add-on (world thread only: the login hook, the add-on's hello and the world update all run there).
static AddonHintTracker g_addon_hints;
// PlayerbotV2.SnapshotBuildSplitMap counters for the TickPerf window (world thread only).
struct SameMapSplitStats
{
uint64 ticks = 0; // ticks that split at least one map
uint64 parts = 0; // split parts built over those ticks
uint64 bots = 0; // bots built in split parts
uint64 deferred_grids = 0; // grid loads the split builds handed to the world thread
};
static SameMapSplitStats g_split_stats;
// Grid loads the bot logic threads handed to the world thread, for the TickPerf window (world thread only).
static uint64 g_bot_logic_grid_loads = 0;
namespace {
// Coarse spatial bucket key for the per-tick real-player proximity set.
// Quantises a world position to a ~106 yd grid cell (well above the
// classifier's "near a real player" intent of ≤~100 yd) and packs it with
// map_id into a single uint64. Building one such set per tick (over the
// handful of connected real players) lets the tier classifier answer
// "is this bot near a real player?" with an O(1) set lookup instead of a
// per-bot grid search. To keep the boundary honest (two entities ≤100 yd
// apart can straddle a cell edge), each real player stamps its own cell
// AND the 8 neighbouring cells; a bot is then "near" when its single cell
// is occupied. That guarantees any bot within one cell (~106 yd) of a real
// player matches, with no per-bot radius test — the cost (9 inserts per
// real player) is trivial since real players are few.
constexpr float kRealPlayerCellSize = 106.0f; // ~1.5× SIZE_OF_GRID-cell granularity, > 100y intent
inline uint64 PackPlayerCellKey(uint32 mapId, int32 cx, int32 cy)
{
// 16 bits map | 24 bits cx | 24 bits cy (cells, biased to unsigned).
// World coords span ±17066 → /106 ≈ ±161 cells, far inside 24 bits.
uint64 const ux = uint64(uint32(cx + (1 << 23))) & 0xFFFFFF;
uint64 const uy = uint64(uint32(cy + (1 << 23))) & 0xFFFFFF;
return (uint64(mapId) << 48) | (ux << 24) | uy;
}
inline int32 PlayerCellCoord(float v) { return int32(std::floor(v / kRealPlayerCellSize)); }
// Drives any pending teleport ack on a headless bot. Real clients send
// CMSG_MOVE_TELEPORT_ACK / CMSG_MOVE_WORLDPORT_ACK to finalize teleports;
// without those acks the player sits in WaitingForTeleportAck forever and
// Player::TeleportTo's effect is silently dropped (the m_position never
// advances to m_teleport_dest). The most visible symptom is the corpse-run
// flow: Player::RepopAtGraveyard issues TeleportTo, the bot stays at the
// death location instead of the graveyard, then ResurrectPlayer revives in
// place. We close the loop by manufacturing the ack on the world thread
// — same call the network handler would make on a real packet.
void DriveTeleportAck(Player* p)
{
if (!p) return;
WorldSession* sess = p->GetSession();
if (!sess) return;
// Selfbot guard: only manufacture acks for HEADLESS sessions. A bot
// driven through a REAL client (`.playerbot self`) performs the genuine
// teleport handshake — forging the worldport ack advances the server's
// packet sequence while the client is still loading, desyncing it
// permanently (live 2026-06-11: user's selfbot LFG-ported into Ragefire
// Chasm, server completed the transfer, client stuck on the loading
// screen forever).
if (!Services::SessionMgr().IsHeadless(p->GetGUID()))
return;
// Far teleport: fully synchronous server-side. The intermediate
// SuspendTokenResponse step is a network-only round-trip; we can
// jump straight to the worldport ack which performs the map-change
// and packet sequence.
if (p->GetTeleportState() == TeleportState::WaitingForWorldPortAck ||
p->GetTeleportState() == TeleportState::WaitingForSuspendTokenResponse)
{
// TeleportTo only deactivates the top movement generator (MotionMaster::InterruptOnTeleport); on the new map
// MotionMaster::Update resets it and a POINT/FOLLOW/CHASE generator re-splines toward its target on the OLD map. A bot
// set down in its levelling town walked toward it inside the Hall of Thanes, straight into the exit portal 13 y behind
// the entrance, before its throttled snapshot saw the map change and BotAI dropped the move (hot_faldrim, 2026-10-28:
// 3 of 5 back in Old Ironforge in the first second). Real clients drive their own movement, so this is bots only.
// A taxi flight keeps its generator: the worldport ack resumes it on the new map.
if (!p->IsInFlight())
{
p->StopMoving();
p->GetMotionMaster()->Clear();
p->GetMotionMaster()->MoveIdle();
}
sess->HandleMoveWorldportAck();
return;
}
// Near teleport (same map). HandleMoveTeleportAck only validates
// MoverGUID + the WaitingForTeleportAck state; AckIndex / MoveTime
// are unused beyond the debug log. Forge the minimum payload.
if (p->GetTeleportState() == TeleportState::WaitingForTeleportAck)
{
// Same as the far teleport above: the walk the bot was on keeps its spline, and the spline moved the bot straight back onto
// its old way after the ack set it down at the new spot. A catch-up in the Stormwind canals showed it (Discord 2026-11-09): two
// bots "brought" to their player 186 y away were 133 y from the spot a moment later, still walking the old way, and to the
// player the group warped about. The move ends first; the bot's rules give it the next one from where it now stands.
if (!p->IsInFlight() && !p->GetTransport())
{
p->StopMoving();
p->GetMotionMaster()->Clear();
p->GetMotionMaster()->MoveIdle();
}
WorldPacket data(CMSG_MOVE_TELEPORT_ACK, 8 + 4 + 4);
data << p->GetGUID();
data << int32(0);
data << int32(GameTime::GetGameTimeMS());
WorldPackets::Movement::MoveTeleportAck ackPacket(std::move(data));
ackPacket.Read();
sess->HandleMoveTeleportAck(ackPacket);
}
}
// Recover Z when the bot has fallen below the world. Conservative on
// purpose: we only act when the bot is more than kBelowGroundCutoff
// yards under the navmesh ground level, which catches "popped below
// map" and severe underground-feet glitches but ignores cosmetic
// 1-2y drift. Snapping aggressively (small threshold, both
// directions) caused visible twitching: a moving bot finishes its
// spline → Idle generator → we pull Z down a fraction → next tick
// the bot logic emits move_to and the spline starts again, repeat. Real
// players get small Z corrections via client-side ground snap and
// MSG_MOVE_HEARTBEAT updates; we approximate the catastrophic case
// only.
constexpr float kBelowGroundCutoff = 5.0f;
// High-Z stuck rescue threshold. Bots whose stored position landed them
// far ABOVE the actual ground (typical case: pre-fix ZonePicker data
// row with a wrong Z value baked into characters.position_z) sit forever
// at e.g. (1129, -4828, 205) on Northrend while real ground is Z~39.
// Path queries fail (no nearby poly) and the bot ends up an idle
// scarecrow. We only act when the delta exceeds kHighAboveCutoff so
// legitimate above-floor positions (rooftops, mountain ledges, multi-
// story buildings — typically ≤30y above local ground) aren't yanked
// down. Idle/not-flying/not-falling/not-transport gates above still
// apply, so legitimate flight is never disturbed.
constexpr float kHighAboveCutoff = 100.0f;
// Rescue bots whose saved position landed them in an orphaned BG / arena
// instance (the prior server crashed mid-BG before SaveToDB returned them
// to their entry point). Symptoms: bot is in a BG map but has no queue
// invite, no team set, no encounter — purely a ghost spawn. The BG init
// script still spawns flags/doors → BIH churn → SnapToGroundIfDrifted
// can crash (see project_v2_bih_crash.md). Teleport to entry point if
// known, else to Stormwind/Orgrimmar by faction.
//
// Fires at most once per bot per server uptime — once they're out of the
// BG map they stay out. Edge-triggered via Player m_bgData state: if the
// bot is in a BG map but Player::InBattleground() is false (no proper
// queue/instance association), they're orphaned.
void RescueOrphanedBgBot(Player* p)
{
if (!p) return;
Map const* m = p->FindMap();
if (!m) return;
if (!m->IsBattlegroundOrArena()) return;
if (p->InBattleground()) return; // legitimately in BG
if (p->IsBeingTeleported()) return; // mid-teleport, leave alone
// Try entry point first — the pre-BG location saved into m_bgData.joinPos.
WorldLocation entry = p->GetBattlegroundEntryPoint();
if (entry.GetMapId() != 0 && entry.GetMapId() != MAPID_INVALID
&& entry.GetMapId() != m->GetId())
{
TC_LOG_WARN("playerbot.v2",
"[RescueOrphanedBgBot] {} stuck in BG map {}; teleporting to entry point",
p->GetName(), m->GetId());
Playerbot::BotMovement::SafeTeleport(p, entry, /*options*/ 0);
return;
}
// No valid entry point — fall back to faction capital.
const bool alliance = (p->GetTeam() == ALLIANCE);
WorldLocation safe = alliance
? WorldLocation(/*EasternKingdoms*/ 0, Position(-8443.0f, 335.0f, 121.0f, 0.f)) // Stormwind Keep front
: WorldLocation(/*Kalimdor*/ 1, Position( 1924.0f, -4147.0f, 40.0f, 0.f)); // Orgrimmar Grommash Hold
TC_LOG_WARN("playerbot.v2",
"[RescueOrphanedBgBot] {} stuck in BG map {} with no entry point; teleporting to capital",
p->GetName(), m->GetId());
Playerbot::BotMovement::SafeTeleport(p, safe, /*options*/ 0);
}
// Per-bot last-attempted-snap timestamp. Throttles the per-tick BIH
// height query that happens for every idle bot every world tick — at
// 2000 bots × 50Hz = 100K UpdateAllowedPositionZ calls/sec which fan
// out into Map::GetHeight + DynamicMapTree probes. Parked idle bots
// don't move, so once-every-2-seconds Z-snap is more than enough; an
// actively-moving bot is filtered out earlier by isMoving() so the
// throttle never trips for them. Keyed by Player GUID counter.
constexpr uint32 kSnapZIntervalMs = 2000;
// Per-bot 2s throttle for SnapToGroundIfDrifted. Single-threaded
// access — called only from Module::OnWorldUpdate's reg.for_each loop
// on the world thread, so no synchronization is needed. (Pre-2026-05-21
// this was guarded by std::shared_mutex; the mutex was unused because
// the only caller is single-threaded, costing ~2000 lock/unlock per
// tick at fleet scale for nothing.)
std::unordered_map<uint64, uint32> g_last_snap_z_ms;
void SnapToGroundIfDrifted(Player* p)
{
if (!p || !p->IsAlive()) return;
// Selfbot guard: a real client's Z is authoritative from its own
// movement packets — server-side relocation rubber-bands the player.
// Only headless sessions need the drift rescue.
if (!Services::SessionMgr().IsHeadless(p->GetGUID())) return;
if (p->IsBeingTeleported()) return;
if (p->isMoving()) return;
if (p->GetMotionMaster()->GetCurrentMovementGeneratorType() != IDLE_MOTION_TYPE)
return;
if (p->GetTransport() || p->GetVehicle()) return;
if (p->IsFlying() || p->IsFalling()) return; // legitimate Z != ground
// Per-bot 2s throttle. Single-threaded (world-tick ForEachBot
// caller), no mutex required.
const uint64 key = p->GetGUID().GetCounter();
const uint32 now = getMSTime();
{
auto it = g_last_snap_z_ms.find(key);
if (it != g_last_snap_z_ms.end() && getMSTimeDiff(it->second, now) < kSnapZIntervalMs)
return;
}
g_last_snap_z_ms[key] = now;
// Skip ground-snap on BG maps. BG instances cycle dynamic GameObjects
// (flags, doors, capture-point gobs) rapidly during prep/start/end,
// and DynamicMapTree::getHeight occasionally hits a "invalid node
// overlap" exception when the BIH is mid-rebuild from concurrent
// GO add/remove. A crashing 1052-bot fleet doesn't deserve a Z snap;
// BG terrain is well-curated so drift is rare anyway.
if (p->InBattleground()) return;
const float curZ = p->GetPositionZ();
float groundZ = curZ;
// Defensive: DynamicMapTree::getHeight can throw std::logic_error
// ("invalid node overlap") from the BIH balance path when collision
// data is mid-rebuild. Catching here keeps a single bot's Z-snap
// from taking down the world thread. Crash 2026-05-13 05:59 had
// this exception escape into World::Update.
try
{
p->UpdateAllowedPositionZ(p->GetPositionX(), p->GetPositionY(), groundZ);
}
catch (std::exception const& e)
{
TC_LOG_WARN("playerbot.v2",
"[SnapToGroundIfDrifted] {} caught {} during UpdateAllowedPositionZ; skipping",
p->GetName(), e.what());
return;
}
catch (...)
{
TC_LOG_WARN("playerbot.v2",
"[SnapToGroundIfDrifted] {} unknown exception in UpdateAllowedPositionZ; skipping",
p->GetName());
return;
}
// Rescue from below-ground glitches.
if (curZ < groundZ - kBelowGroundCutoff)
{
p->NearTeleportTo(p->GetPositionX(), p->GetPositionY(), groundZ, p->GetOrientation());
return;
}
// Rescue from far-above-ground stuck positions (stale stored Z from
// pre-fix ZonePicker data, off-mesh spawns on top of a sealed building,
// etc.). All the idle/not-flying/not-falling/not-transport gates above
// ensure we never disturb a legitimately-airborne bot. The 100y delta
// avoids snapping bots off rooftops, towers, or mountain platforms.
if (curZ > groundZ + kHighAboveCutoff)
{
// The case above is a bot off the navmesh (no nearby poly, every path fails). A bot whose own position is on a walkable
// poly stands on a floor the height probe did not see (a hole in the collision model, a floor that is a game object):
// the probe answered with the terrain or water far below. b3ca0b4b67 hot_faldrim: the mage stood among the group on the
// floor of Faldrim's hall at z 500.8, the probe gave 0.0, and it was set down 500 y under the instance.
Position on_mesh;
if (BotMovement::NearestNavPoint(p, p->GetPositionX(), p->GetPositionY(), curZ, 1.5f, 3.0f, on_mesh))
return;
TC_LOG_WARN("playerbot.v2",
"[SnapToGroundIfDrifted] {} stuck high (Z={:.1f} vs ground={:.1f}); teleporting down",
p->GetName(), curZ, groundZ);
p->NearTeleportTo(p->GetPositionX(), p->GetPositionY(), groundZ, p->GetOrientation());
}
}
// Global off-mesh stuck rescue. Per-rule wedge guards (Pass 14 on
// idle:wander, plus existing ones on travel/hub/portal/dock rules) only
// fire when the rule itself sees the failures — but higher-priority
// emergency rules (idle:watchdog_escape, idle:flee_hazard,
// dead:walking_to_corpse) grab the tick first and never escalate.
// Bot 88584 on 2026-05-18 logged idle:watchdog_escape at blocks=8,10,12
// with no rescue. Result: 1500+ wasted move_to/sec from a handful of
// off-mesh bots.
//
// This catches the symptom at the bot level regardless of which rule
// emitted: count path failures in a 30s window, and after a threshold,
// teleport to homebind. Homebind innkeepers always sit on navmesh-valid
// polys, so the bot lands somewhere it can pathfind from. Cooldown
// between rescues prevents teleport-spam if homebind itself is somehow
// bad (rare but theoretically possible if owner moved homebind to a
// broken spawn point).
struct GlobalStuckState
{
uint32 baseline_blocks = 0;
uint32 baseline_ms = 0;
uint32 last_rescue_ms = 0;
};
// When the same bot needs rescue again within this window, its homebind
// is probably itself unreachable (observed 2026-05-18: bot Marinon
// rescued twice to map=2081 1621,536,Z=201.5 — Forbidden Reach Dracthyr
// starter, a flying-only zone with sparse navmesh). Escalate the second
// rescue to faction capital, which sits on dense navmesh-valid plaza.
constexpr uint32 kRescueRepeatWindowMs = 10u * 60u * 1000u; // 10 min
// Single-threaded access — GlobalStuckRescue is only called from
// Module::OnWorldUpdate's reg.for_each loop on the world thread.
// No synchronization needed. (Pre-2026-05-21 was guarded by
// std::shared_mutex; the mutex was unused because the only caller
// path is single-threaded.)
std::unordered_map<uint64, GlobalStuckState> g_global_stuck;
constexpr uint32 kStuckThreshold = 40; // blocks in window (raised from 15 — favour path retries over rescue)
constexpr uint32 kStuckWindowMs = 60u * 1000u; // 60 s (raised from 30 s)
constexpr uint32 kStuckRescueCooldown = 300u * 1000u; // 5 min (raised from 120 s)
struct CapitalInnkeeperCache
{
bool loaded = false;
bool alliance_valid = false;
float alliance_x = 0.f, alliance_y = 0.f, alliance_z = 0.f;
bool horde_valid = false;
float horde_x = 0.f, horde_y = 0.f, horde_z = 0.f;
};
static CapitalInnkeeperCache s_inn_cache;
static std::once_flag s_inn_cache_once;
WorldLocation FindCapitalRescuePos(bool alliance)
{
std::call_once(s_inn_cache_once, []()
{
constexpr float kSW_cx = -8850.f, kSW_cy = 660.f;
constexpr float kOR_cx = 1900.f, kOR_cy = -4300.f;
float best_sw_dsq = 600.f * 600.f;
float best_or_dsq = 600.f * 600.f;
auto const& all = sObjectMgr->GetAllCreatureData();
for (auto const& [spawn_id, cd] : all)
{
if (cd.mapId != 0 && cd.mapId != 1) continue;
auto const* ct = sObjectMgr->GetCreatureTemplate(cd.id);
if (!ct) continue;
if (!(ct->npcflag & UNIT_NPC_FLAG_INNKEEPER)) continue;
if (cd.mapId == 0)
{
const float dx = cd.spawnPoint.GetPositionX() - kSW_cx;
const float dy = cd.spawnPoint.GetPositionY() - kSW_cy;
const float dsq = dx*dx + dy*dy;
if (dsq < best_sw_dsq)
{
best_sw_dsq = dsq;
s_inn_cache.alliance_x = cd.spawnPoint.GetPositionX();
s_inn_cache.alliance_y = cd.spawnPoint.GetPositionY();
s_inn_cache.alliance_z = cd.spawnPoint.GetPositionZ();
s_inn_cache.alliance_valid = true;
}
}
else if (cd.mapId == 1)
{
const float dx = cd.spawnPoint.GetPositionX() - kOR_cx;
const float dy = cd.spawnPoint.GetPositionY() - kOR_cy;
const float dsq = dx*dx + dy*dy;
if (dsq < best_or_dsq)
{
best_or_dsq = dsq;
s_inn_cache.horde_x = cd.spawnPoint.GetPositionX();
s_inn_cache.horde_y = cd.spawnPoint.GetPositionY();
s_inn_cache.horde_z = cd.spawnPoint.GetPositionZ();
s_inn_cache.horde_valid = true;
}
}
}
s_inn_cache.loaded = true;
TC_LOG_INFO("playerbot.v2",
"[GlobalStuckRescue] Capital innkeeper cache: alliance={} ({:.1f},{:.1f},{:.1f}), horde={} ({:.1f},{:.1f},{:.1f})",
s_inn_cache.alliance_valid, s_inn_cache.alliance_x, s_inn_cache.alliance_y, s_inn_cache.alliance_z,
s_inn_cache.horde_valid, s_inn_cache.horde_x, s_inn_cache.horde_y, s_inn_cache.horde_z);
});
if (alliance && s_inn_cache.alliance_valid)
return WorldLocation(0, Position(s_inn_cache.alliance_x, s_inn_cache.alliance_y, s_inn_cache.alliance_z, 0.f));
if (!alliance && s_inn_cache.horde_valid)
return WorldLocation(1, Position(s_inn_cache.horde_x, s_inn_cache.horde_y, s_inn_cache.horde_z, 0.f));
return alliance
? WorldLocation(0, Position(-8868.f, 671.f, 98.f, 0.f))
: WorldLocation(1, Position(1633.f, -4439.f, 17.f, 0.f));
}
void GlobalStuckRescue(Player* p, BotAI* ai)
{
if (!p || !ai) return;
if (!p->IsInWorld()) return;
if (p->IsBeingTeleported()) return;
if (p->IsFlying() || p->IsFalling()) return;
// Taxi flight (UNIT_STATE_IN_FLIGHT) — the core owns movement; a homebind
// teleport here would abort the flight and strand the bot. IsFlying() above
// is a movement FLAG (free-flight mounts) and is NOT set during a taxi
// spline, so this needs its own guard.
if (p->IsInFlight()) return;
if (p->InBattleground()) return; // BG handles its own respawn loop
if (p->GetMap() && p->GetMap()->IsDungeon()) return; // dungeon party in progress
if (p->GetMap() && p->GetMap()->IsRaid()) return; // raid encounter — never yank
if (p->GetTransport() || p->GetVehicle()) return;
if (p->GetGroup() != nullptr) return; // grouped bot — leader-follow handles travel
// LFG-pipeline guard. The IsDungeon()/IsRaid()/group guards above only cover a
// bot that is CURRENTLY on the instance map AND still grouped. A dungeon-finder
// member that is path-blocked at the instance ENTRANCE (the portal sits on a
// world map — e.g. Ragefire's is inside Orgrimmar), mid-teleport between world
// and instance, or transiently solo for a tick while the LFG group settles is
// NONE of those — so a homebind teleport here yanks it out of the run and
// collapses the whole group (observed live: a path-blocked squad member was
// teleported to Stormwind and the Deadmines run dissolved). A teleport-rescue
// is the wrong tool for a dungeon-runner anyway ([[feedback_no_teleport_rescue]]).
// Skip the rescue for any bot in the LFG pipeline (queued → proposal → role
// check → in-dungeon → finished) or with dungeon-run mode active; the LFG /
// dungeon system owns its location and a stuck member re-paths toward the group.
if (ai->dungeon_run_engaged()) return;
if (sLFGMgr->GetState(p->GetGUID()) != lfg::LFG_STATE_NONE) return;
// Combat: flee_hazard legitimately fires mid-encounter for world-boss
// ground AoEs, elite quest mob cleave puddles, etc. Teleporting out
// would ruin the fight (the bot abandons combat AND the player's
// expectation that the bot is helping). Block while in combat; the
// path failures will continue but the per-rule wedges still apply,
// and once combat ends the threshold can trip on the next window.
if (p->IsInCombat()) return;
// Corpse run / ghost: dead:walking_to_corpse fires for a legitimate
// post-wipe recovery. The State_Dead 5-min timeout already escalates
// to SpiritResurrect when the corpse path is truly unreachable; we
// mustn't pre-empt that path with a homebind teleport (which would
// strand the corpse and not actually resurrect the bot).
if (!p->IsAlive()) return;
if (p->HasPlayerFlag(PLAYER_FLAGS_GHOST)) return;
// Charter founders + signers were just deliberately teleported onto
// the petitioner plaza by BotGuildMgr. Their path_blocks counter
// grows from in-plaza walks (5y movement to petitioner / signer ring
// jitter); homebind-rescuing them sends the founder cross-continent
// and the FSM aborts. The 20-min charter-grace fully covers the
// FSM budget — skip the global rescue while it's armed.
if (ai->in_charter_grace(getMSTime())) return;
// No-teleport-rescue for objective-unreachable bots. When the bot is wedged
// because its CURRENT quest objective is genuinely unpathable, the fast
// NoPath blacklist (note_obj_observed → ~6s) has already / is about to flag
// the objective so the snapshot Builder's picker moves to a reachable quest.
// That reroute is the correct fix (the bot walks somewhere it CAN path and
// path_blocked_count resets) — yanking it to homebind/capital instead would
// violate the no-teleport-rescue principle and strand it mid-zone. Defer the
// rescue while the wedge is attributable to the objective walk AND that
// objective is currently blacklisted; genuine off-mesh / fall-through cases
// (last rule was wander / watchdog_escape / hub travel, no objective
// blacklist) still escalate to the teleport below as a last resort.
{
char const* lr = ai->last_rule_fired();
const bool on_objective_walk =
lr != nullptr && std::string_view(lr) == "idle:quest_path";
if (on_objective_walk && ai->current_objective_blacklisted(getMSTime()))
return;
}
const uint32 cur = ai->path_blocked_count();
const uint32 now = getMSTime();
const uint64 key = p->GetGUID().GetCounter();
GlobalStuckState st{};
{
auto it = g_global_stuck.find(key);
if (it != g_global_stuck.end()) st = it->second;
}
// First observation OR baseline aged out → reset baseline.
if (st.baseline_ms == 0 || getMSTimeDiff(st.baseline_ms, now) > kStuckWindowMs)
{
st.baseline_blocks = cur;
st.baseline_ms = now;
g_global_stuck[key] = st;
return;
}
// Counter regressed below the baseline. path_blocked_count_ now resets to
// 0 on every successful move (note_move_succeeded), so `cur < baseline`
// means the bot made real progress since the window opened — it is NOT
// wedged. Re-baseline and bail; without this guard the uint32 subtraction
// below underflows to ~4e9 and trips an immediate false rescue.
if (cur < st.baseline_blocks)
{
st.baseline_blocks = cur;
st.baseline_ms = now;
g_global_stuck[key] = st;
return;
}
// Within the cooldown window after a recent rescue → don't re-rescue.
if (st.last_rescue_ms != 0 && getMSTimeDiff(st.last_rescue_ms, now) < kStuckRescueCooldown)
return;
const uint32 grew = cur - st.baseline_blocks;
if (grew < kStuckThreshold) return;
// If we already rescued this bot recently (within 10 min), its
// homebind is itself unreachable — observed for Dracthyr bots whose
// homebind landed on map 2081 (Forbidden Reach), a flying-only zone
// with sparse navmesh. Escalate to faction capital, which always sits
// on dense navmesh-valid terrain (Stormwind / Orgrimmar plaza).
const bool homebind_proven_bad =
st.last_rescue_ms != 0 &&
getMSTimeDiff(st.last_rescue_ms, now) < kRescueRepeatWindowMs;
WorldLocation tgt;
auto snap = Services::Snapshots().latest(key);
if (!homebind_proven_bad &&
snap && snap->travel.homebind_map_id != 0)
{
tgt = WorldLocation(snap->travel.homebind_map_id,
snap->travel.homebind_x,
snap->travel.homebind_y,
snap->travel.homebind_z, 0.f);
}
else
{
const bool alliance = (p->GetTeam() == ALLIANCE);
tgt = FindCapitalRescuePos(alliance);
if (homebind_proven_bad)
{
TC_LOG_WARN("playerbot.v2",
"[GlobalStuckRescue] {} previous homebind rescue didn't stick — "
"escalating to faction capital instead", p->GetName());
}
}
TC_LOG_WARN("playerbot.v2",
"[GlobalStuckRescue] {} path_blocks grew {}→{} in {}ms; teleporting to homebind (map={} {:.1f},{:.1f},{:.1f})",
p->GetName(), st.baseline_blocks, cur,
getMSTimeDiff(st.baseline_ms, now),
tgt.GetMapId(), tgt.GetPositionX(), tgt.GetPositionY(), tgt.GetPositionZ());
Playerbot::BotMovement::SafeTeleport(p, tgt, /*options*/ 0);
// Post-teleport state reset. Without this, the rules' next tick
// would carry forward:
// - the IntentQueue's pending move_to entries from the prior
// stuck position (now firing against fresh coords, which fails
// just as fast),
// - the rule-wedge slots still marking "idle:wander is wedged",
// - the monotonic path_blocked_count primed near the rescue
// threshold, so the very next failure would re-rescue.
// Drain the intent queue inline (single consumer = world thread =
// here), then ai.reset_after_rescue clears the wedge/counter state
// and arms the grace timer that travel rules consult.
if (auto* q = Services::Registry().intents(key))
{
Intent discarded;
while (q->pop(discarded)) { /* drop */ }
}
ai->reset_after_rescue(now);
st.last_rescue_ms = now;
st.baseline_blocks = cur;
st.baseline_ms = now;
g_global_stuck[key] = st;
}
// Companion tank threat (the owner's AiPlayerbot.TankThreatMultiplier on his
// VMaNGOS bots): a grouped bot owned by a human whose current duty is tank
// generates Playerbot.TankThreatMultiplier x threat, so the owner and the DPS
// bots rarely pull aggro off it. Duty = the PvE coordinator's order when one
// is active (tank_duty != 0; a tank the raid roster turned into DPS has
// tank_duty 0), else the bot's effective role (owner /setrole, else spec).
// Every other bot is held at 1.0, so leaving the group, dismissal (ownership
// cleared) or losing the duty resets it with the bot's next published
// snapshot. Applied core-side in ThreatManager::AddThreat to the bot's own
// generated threat only. World thread (post-barrier publish pass).
// Non-tank bots of such a group (not in a battleground, inside a dungeon or raid map)
// get Playerbot.DpsThreatMultiplier instead (Karrz's VMaNGOS 0.8: DPS pull less); 1.0 = off.
// On a raid map a tank gets Playerbot.Raid.TankThreatMultiplier instead, and with Playerbot.Raid.SureTaunt its taunts never miss:
// a raid boss's threat drops (Onyxia's and Broodlord's Knock Away, the BWL drakes' Wing Buffet) halve the tank, and x3 left a halved
// tank below the DPS (bwl_broodlord 256: the boss on DPS and healers from 25 s on, 40 deaths). The scenario squads have no owner;
// Playerbot.Scenario.OwnerThreat gives them the same multipliers, so a scenario tests what a player's raid gets.
struct TankThreatSettings
{
float tank = 3.0f, raid_tank = 5.0f, dps = 1.0f;
bool sure_taunt = true, scenario_as_owned = true;
};
void UpdateTankThreatMultiplier(BotId id, Player* p, BotAI const* ai,
BotSnapshot const& snap, TankThreatSettings const& cfg)
{
if (!p)
return;
float want = 1.0f;
bool sure_taunt = false;
std::string duty = "none";
bool const owned = Services::Owners().GetOwner(id).account_id != 0 || (cfg.scenario_as_owned && Pool::IsScenarioBot(id));
// Not in a battleground: no tank duty there, and the threat it multiplied would only be an NPC's (AV).
if ((cfg.tank != 1.0f || cfg.raid_tank != 1.0f || cfg.dps != 1.0f || cfg.sure_taunt) && p->GetGroup() &&
!snap.bg.in_battleground && owned)
{
PveOrder const& po = snap.pve_order;
bool tank = false;
if (po.active)
{
tank = po.tank_duty != 0;
duty = std::to_string(uint32(po.tank_duty));
}
else
{
BotSnapshotView const view(snap);
tank = (ai ? ai->effective_role(view) : view.my_role()) == Role::Tank;
duty = tank ? "role-tank" : "none";
}
if (tank)
{
want = snap.instance_ctx.is_in_raid ? cfg.raid_tank : cfg.tank;
sure_taunt = cfg.sure_taunt && snap.instance_ctx.is_in_raid;
}
else if (cfg.dps != 1.0f && (snap.instance_ctx.is_in_dungeon || snap.instance_ctx.is_in_raid))
want = cfg.dps;
}
if (p->GetThreatManager().GetTauntNeverMisses() != sure_taunt)
p->GetThreatManager().SetTauntNeverMisses(sure_taunt);
API api(p);
if (api.threat_multiplier() == want)
return;
api.set_threat_multiplier(want);
TC_LOG_DEBUG("playerbot.v2", "[tank_threat] {} threat x{:.2f} (duty {}){}",
p->GetName(), want, duty, sure_taunt ? ", taunts never miss" : "");
}
// Bot cast pacing (Playerbot.Cast.*, OFF by default): when enabled, raid bots cast faster
// than the shipped rates, BG and world-PvP bots slower, with a constant per-bot skill
// offset. Context comes from the published snapshot (CastPacing.h); the multiplier is
// applied core-side to this bot's cast time and GCD (API::set_cast_speed_multiplier).
// Rewritten only when the value changes, and logged once per change. World thread
// (post-barrier publish pass).
void UpdateCastSpeed(Player* p, BotSnapshot const& snap, ConfigReader::CastPacing const& cfg)
{
if (!p)
return;
BotSnapshotView const view(snap);
CastContext const ctx = ClassifyCastContext(view);
float const want = CastSpeedMultiplier(cfg, ctx, snap.guid);
API api(p);
if (api.cast_speed_multiplier() == want)
return;
api.set_cast_speed_multiplier(want);
TC_LOG_DEBUG("playerbot.v2", "[cast_speed] {} context={} cast/gcd x{:.2f} (skill {:+d}%)",
p->GetName(), CastContextName(ctx), want,
cfg.enabled ? CastSkillOffsetPct(snap.guid, cfg.variance_pct) : 0);
}
// Reaction speed (Playerbot.Reaction.WakeOnGcd) and its measurement. A bot in combat whose global cooldown or cast ends between two
// regular tick slots is woken on the world tick it ends in; the timing record notes, per bot, when each of the three reactions
// became possible (GCD end, a kickable cast with the interrupt ready, a leave-hazard order) so the executor can log how long the
// bot took. World thread (publish pass).
void NoteReaction(BotId id, BotSnapshot const& snap, Ms now, bool wake_on_gcd)
{
ReactionTiming& rt = ReactionTiming::Instance();
// The timing record covers bots grouped with a player or other bots (a dungeon / raid / companion group); the solo world bots
// fighting in the open world are most of the combat bots at any time and would drown the group's numbers. The wake is for both.
bool const grouped = !snap.group.group_guid.IsEmpty();
if (!snap.vitals.is_alive || !snap.vitals.in_combat)
{
rt.NoteKickPossible(id, false, snap.published_at_ms);
rt.NoteHazardOrder(id, false, snap.published_at_ms);
return;
}
int64 busy = snap.cooldowns.gcd_remaining.count();
if (snap.cast.is_casting)
busy = std::max<int64>(busy, snap.cast.current_cast_remaining.count());
if (busy > 0)
{
if (grouped)
rt.NoteGcdReady(id, snap.published_at_ms + uint32(busy), uint8(snap.group.my_role));
if (wake_on_gcd)
Services::Scheduler().wake_after_busy(id, busy, now);
}
if (!grouped)
return;
BotSnapshotView const view(snap);
Combat::Interrupt::Ability const ab = Combat::Interrupt::ClassicAbility(view);
bool kick = false;
if (ab.spell && Combat::Interrupt::Ready(view, ab))
{
for (NearbyUnit const& u : view.attackers())
if (Combat::Interrupt::Casting(u) && Combat::Interrupt::InRange(view, ab, u)) { kick = true; break; }
}
rt.NoteKickPossible(id, kick, snap.published_at_ms);
RaidOrder const& ro = snap.raid_order;
rt.NoteHazardOrder(id, ro.active && ro.move && ro.move_reason &&
(std::strcmp(ro.move_reason, "leave hazard") == 0 || std::strcmp(ro.move_reason, "leave cone") == 0), snap.published_at_ms);
}
void FlushReactionTiming()
{
ReactionTiming& rt = ReactionTiming::Instance();
if (!rt.FlushDue(GameTime::GetGameTimeMS()))
return;
TC_LOG_INFO("playerbot.v2.react", "[react] gcd, grouped bots only (GCD end -> next cast sent) {} idle_gaps={} wakes={}", rt.gcd().line(), rt.gcd_idle(),
Services::Scheduler().wakes_total());
TC_LOG_INFO("playerbot.v2.react", "[react] gcd by role: tank {} | healer {} | dps {} (idle gaps {}/{}/{})", rt.gcd_role(0).line(), rt.gcd_role(1).line(),
rt.gcd_role(2).line(), rt.gcd_idle_role(0), rt.gcd_idle_role(1), rt.gcd_idle_role(2));
TC_LOG_INFO("playerbot.v2.react", "[react] kick (kickable cast + ready kick -> kick sent) {}", rt.kick().line());
TC_LOG_INFO("playerbot.v2.react", "[react] hazard (leave-hazard order published -> first step sent) {}", rt.hazard().line());
rt.ResetWindow();
}
// PvP reaction floor (Playerbot.Pacing.PvpReactionDelayMs, the owner's AiPlayerbot.PvpReactionDelay):
// while the bot is in PvP - a battleground/arena, an active duel, or in combat with players
// (ClassifyCastContext BG/WorldPvp) - its bot tick and snapshot period is at least the floor,
// so a fleet does not out-react real players. PvE (raid/dungeon/world) never gets it. Survival
// bypass: a dead/ghost bot (release, resurrection) or one below 35% HP is ticked at its normal
// tier period, so healing, defensives and ress are not starved. World thread (publish pass).
void UpdatePvpFloor(BotId id, Player* p, BotSnapshot const& snap, uint32 floor_ms, Ms now)
{
uint32 ms = 0;
if (floor_ms && p)
{
BotSnapshotView const view(snap);
CastContext const ctx = ClassifyCastContext(view);
bool const duel = p->duel && p->duel->State == DUEL_STATE_IN_PROGRESS;
bool const pvp = ctx == CastContext::Battleground || ctx == CastContext::WorldPvp || duel;
bool const survival = !view.is_alive() || view.is_ghost() || view.hp_pct() < 35;
if (pvp && !survival)
ms = floor_ms;
}
Services::Scheduler().set_pvp_floor(id, Ms{ms}, now);
}
} // namespace
Module& Module::instance()
{
static Module m;
return m;
}
void Module::Init()
{
if (initialized_)
return;
TC_LOG_INFO("server.loading", "WoW Forever Bots {} by Karrz, based on agatho's PlayerbotV2 (built {}).",
"0.1", __DATE__);
TC_LOG_INFO("server.loading", "[PlayerbotV2] Initializing module.");
// Apply schema migrations before bringing up Services. If this fails,
// refuse to initialize so the server doesn't run with a corrupt schema.
PlayerbotMigrationMgr migrations;
if (!migrations.run_all())
{
TC_LOG_ERROR("server.loading", "[PlayerbotV2] Schema migration failed; module disabled.");
return;
}
// The bots' speech lines (shared DB, migration 0019). Loaded before any bot can tick; read-only
// afterwards, so bot workers and the world thread read it without a lock.
BotTextMgr::instance().Load();
// Which language each player reads the bots' replies in (Api/BotLanguage.h).
BotLanguage::Load();
// Bring up Services (SnapshotPublisher, IntentQueue, AiWorkerPool,
// FleetThread, TickScheduler, BotRegistry). Adds threads.
Services::Init();
// A capital's navmesh wades the canals. In a zone with no leveling range an ungrouped bot needs a dry route or does not
// move (RequireDry); a follower (group, companion) takes a dry route when one exists and otherwise follows through the
// water (PreferDry). A bot already in the water is exempt (its way out ends on a wet shore). BG, raid and instance
// movement are untouched.
::Playerbot::BotMovement::g_LiquidPathPolicy = [](Player* p) -> ::Playerbot::BotMovement::LiquidPolicy
{
if (!p || !p->GetMap())
return ::Playerbot::BotMovement::LiquidPolicy::None;
uint8 lo = 0, hi = 0;
return ::Playerbot::BotMovement::PickLiquidPolicy(
p->IsInWater(),
p->InBattleground() || p->GetMap()->IsDungeon() || p->GetMap()->IsBattlegroundOrArena(),
::Playerbot::V2::World::VanillaZoneLevelRange(p->GetZoneId(), lo, hi),
// A follower; the leader of a bot party picks the party's route and keeps out of the canals like a solo bot (live 244: a
// bot leader walked its party into the Stormwind canal on a quest route).
p->GetGroup() != nullptr && p->GetGroup()->GetLeaderGUID() != p->GetGUID(),
Services::Owners().GetOwner(p->GetGUID().GetCounter()).account_id != 0);
};
// Register class/spec rotations (single-threaded; runs before bot workers
// dispatch ticks). Read-only afterwards; safe for concurrent lookup.
Combat::RegisterAllRotations();
// Tier 3.1 guard: prove BotSnapshot::reset_for_reuse() clears every
// member before we start recycling snapshots in Build(). A leak here is a
// silent stale-data correctness bug; abort at boot instead.
Playerbot::VerifyResetClearsAll();
// Pre-warm grids + nav tiles for high-traffic bot zones (8 capitals + 3
// class-starter maps). Sync; ~1-3s of disk I/O on cold cache, paid once
// at startup so the population manager's batched logins don't cascade
// into per-tick navmesh loads on the map worker thread.
World::PrewarmCommonZones();
// Pin all battleground terrain (grid maps + vmap tiles + mmap tiles)
// resident for the server's lifetime. BG maps are instanced; without
// the pin their tiles unload when the last match ends and the next
// match re-reads them from disk synchronously on map-update threads,
// which showed up as 4-10s world-tick spikes around match boundaries
// once the 2026-06-12 nav regen gave BG maps real vmap/mmap data.
World::PinBattlegroundTerrain();
// Reconcile BotNamePool against the characters table — any name with
// is_used=1 whose used_by_guid no longer exists in characters gets
// released. Catches SQL-wipe / mid-create-crash orphans so a fresh
// boot doesn't see a phantom-claimed pool.
Fleet::BotNamePool::ReconcileOnBoot();
// Re-roll bot appearances the client would reject (retail-only
// customization options in the Classic 1.60 data crashed the owner's
// client when such a bot came into view). Before any bot logs in.
BotCharacterFactory::RepairAppearances();
// Load operator-curated world knowledge (roads, cities, danger zones,
// hubs, vendors, mailboxes, innkeepers, crossroads). Populated via
// `.playerbot meta add` GM commands during play; consumed by snapshot
// builder + mmaps_generator. Synchronous one-shot load — table is
// bounded at hundreds-to-thousands of rows, takes <100ms. Future
// mutations through HandleMeta update both DB and in-memory cache
// directly so the cache doesn't drift from the source of truth.
Playerbot::V2::World::WorldMetadataStore::Instance().ReloadFromDb();
// Future inits (as subsystems land):
// - ConfigReader::load(...) (Util/)
// - PopulationManager / LfgMediator / BgFiller (Fleet/)
// - EncounterRegistry::RegisterAll() (Combat/Encounters/)
// A player's Add Friend box sends a bot's name as a Battle.net invitation, which the bot's pool account never answers: the core
// makes a bot on a pool account a character friend instead.
WorldSession::SetAutomatedAccountCheck([](uint32 gameAccountId)
{
return Services::Initialized() && Services::Accounts().is_pool_account(gameAccountId);
});
initialized_ = true;
if (Services::Config().auto_resume_on_boot())
{
// B-2: clamp the boot fill to Population.TotalTarget. AutoResumeCap
// is an independent knob (default 100), so a 10-bot target still
// boot-logged 100+ sessions which then sat overshot until the
// population manager trimmed them — boot now respects the target
// immediately and Reconcile only ever tops UP from here.
auto_resume_pending_ = Services::Config().auto_resume_cap();
if (uint32 target = Services::Config().population_total_target())
auto_resume_pending_ = std::min(auto_resume_pending_, target);
TC_LOG_INFO("server.loading",
"[PlayerbotV2] AutoResumeOnBoot=true; will batch-login marked bots over multiple ticks (cap {}).",
auto_resume_pending_);
}
auto_spawn_pending_ = Services::Config().auto_spawn_on_boot();
if (auto_spawn_pending_)
{
TC_LOG_INFO("server.loading",
"[PlayerbotV2] AutoSpawnOnBoot={}; will create new bots on first world tick to reach that target (hard cap 200).",
auto_spawn_pending_);
}
// #1B: apply the configured wedge-confirm thresholds + displacement gate.
wedge_watchdog_.set_threshold_ms(Services::Config().wedge_watchdog_threshold_ms());
wedge_watchdog_.set_combat_threshold_ms(Services::Config().wedge_watchdog_combat_threshold_ms());
wedge_watchdog_.set_min_displacement(Services::Config().wedge_watchdog_min_displacement());
wedge_watchdog_.set_noprogress_enabled(Services::Config().wedge_noprogress_enabled());
wedge_watchdog_.set_noprogress_ms(Services::Config().wedge_noprogress_ms());
wedge_watchdog_.set_noprogress_radius(Services::Config().wedge_noprogress_radius());
TC_LOG_INFO("server.loading", "[PlayerbotV2] Initialization complete.");
}
// ---- Bot-group hygiene -----------------------------------------------------
//
// A group of pure bots must NOT persist across a server restart: it reloads
// with stale leader/position state and the followers wedge chasing a leader
// that's nowhere near where it was (the follow_recall spam in the logs). Only
// a group that contains a human is allowed to persist. And once that human has
// been logged out for more than 30 minutes, the group is disbanded too — the
// bots would otherwise follow an absent leader forever.
//
// Two entry points:
// * on_shutdown=true (Module::Shutdown): disband every pure-bot group so
// none survive to the next boot. Human groups persist.
// * on_shutdown=false (periodic OnWorldUpdate): disband human groups whose
// human member(s) have ALL been offline ≥30 min. Pure-bot groups are left
// alone at runtime — they're legitimate (dungeon / BG / quest) and the
// walk-first follow recovery handles any transient wedging.
namespace
{
constexpr uint32 kHumanOfflineDisbandMs = 30u * 60u * 1000u;
// group-id → first tick we observed ALL its humans offline. Cleared when a
// human comes back online or the group is disbanded. World-thread only.
std::unordered_map<ObjectGuid::LowType, uint32> g_group_humans_offline_since;
// purge_pure_bot=true → disband every pure-bot group (used at shutdown and
// once at boot, so none persist across a restart /
// survive a crash). Human groups are left intact.
// purge_pure_bot=false → runtime pass: leave pure-bot groups (legit dungeon
// / BG / quest groups) and only disband human groups
// whose human member(s) have ALL been offline ≥30min,
// and the bots' own pickup groups whose life is over.
//
// only_these (boot purge): the group ids that existed when the module's first tick ran. The purge runs when the boot gate opens,
// which with no human logged in is 60 s later, and a group formed in that minute is live, not persisted. 2026-10-09: the
// scenario runner's squad was disbanded mid-Deadmines by this purge ("sees a group of 0" on all five at once).
std::unordered_set<ObjectGuid::LowType> g_boot_group_ids;
void DisbandStaleBotGroups(bool purge_pure_bot, uint32 now_ms, std::unordered_set<ObjectGuid::LowType> const* only_these = nullptr)
{
if (!Services::Initialized())
return;
std::vector<std::pair<Group*, char const*>> to_disband;
for (auto const& [gid, group] : sGroupMgr->GetGroupStore())
{
if (!group)
continue;
if (only_these && !only_these->count(gid))
continue;
bool has_bot = false, has_human = false, any_human_online = false;
for (auto const& slot : group->GetMemberSlots())
{
Player const* member = ObjectAccessor::FindConnectedPlayer(slot.guid);
// a self-bot (`.playerbot self on`) is marked as a bot for the session but is a person on a real client
bool const realClient = member && member->GetSession() && !member->GetSession()->IsBot();
if (Services::Lifecycle().is_bot(slot.guid.GetCounter()) && !realClient)
{
has_bot = true;
}
else
{
has_human = true;
if (member)
any_human_online = true;
}
}
// Groups with no bot member are not our concern.
if (!has_bot)
continue;
// Two world bots' own pickup group (Fleet/PickupGroups.h): once a player is in it, it is his group; otherwise it breaks up at
// the end of its life, when no member is fighting, being teleported or in an instance (the spawner breaks it up sooner when
// no player is near).
if (PickupGroups::Has(gid))
{
if (has_human)
PickupGroups::Forget(gid);
else if (!purge_pure_bot && PickupGroups::Expired(PickupGroups::Table().at(gid), now_ms))
{
bool busy = false;
for (auto const& slot : group->GetMemberSlots())
if (Player* m = ObjectAccessor::FindConnectedPlayer(slot.guid))
if (m->IsInCombat() || m->IsBeingTeleported() || (m->GetMap() && m->GetMap()->Instanceable()))
{ busy = true; break; }
if (!busy)
{
to_disband.emplace_back(group, "pickup-life-over");
continue;
}
}
}
if (!has_human)
{
g_group_humans_offline_since.erase(gid);
if (purge_pure_bot)
{
// LFG dungeon groups that are actively inside an instance
// are NOT stale persisted leftovers — they're live runs
// (e.g. a pure-bot dungeon test that queued via LFG). Skip
// them at boot so the boot purge doesn't eject bots from a
// dungeon they just entered. The purge targets groups that
// survived a hard-kill/crash without running Shutdown, which
// means they exist in the DB but have no live members — those
// groups will have no connected players in their slots.
if (group->isLFGGroup())
{
bool any_connected = false;
for (auto const& slot : group->GetMemberSlots())
if (ObjectAccessor::FindConnectedPlayer(slot.guid))
{ any_connected = true; break; }
if (any_connected)
continue; // live LFG run — leave it alone
}
to_disband.emplace_back(group, "pure-bot");
}
continue;
}
// Human-containing group: persists across shutdown/boot by design.
if (purge_pure_bot)
continue;
if (any_human_online)
{
g_group_humans_offline_since.erase(gid); // reset the AFK timer
continue;
}
// All humans offline — start / check the 30-min disband timer.
auto it = g_group_humans_offline_since.find(gid);
if (it == g_group_humans_offline_since.end())
g_group_humans_offline_since[gid] = now_ms;
else if (now_ms - it->second >= kHumanOfflineDisbandMs)
to_disband.emplace_back(group, "human-afk-30min");
}
// Disband AFTER iterating — Group::Disband() mutates the GroupStore.
// A pickup group that is gone (it broke up, or its last member left it) is forgotten.
std::erase_if(PickupGroups::Table(),
[](auto const& kv) { return !sGroupMgr->GetGroupByGUID(ObjectGuid::Create<HighGuid::Party>(kv.first)); });
for (auto const& [g, reason] : to_disband)
{
g_group_humans_offline_since.erase(g->GetGUID().GetCounter());
PickupGroups::Forget(g->GetGUID().GetCounter());
TC_LOG_INFO("playerbot.v2",
"[group_hygiene] disbanding bot group {} reason={}",
g->GetGUID().GetCounter(), reason);
g->Disband();
}
}
}
void Module::Shutdown()
{
if (!initialized_)
return;
TC_LOG_INFO("server.loading", "[PlayerbotV2] Shutting down module.");
// Disband pure-bot groups BEFORE Services::Shutdown() (which tears down the
// bot-identity registry is_bot() relies on) so they don't persist to the
// next boot and reload into wedged follow loops.
DisbandStaleBotGroups(/*purge_pure_bot=*/true, getMSTime());
// #5 Phase 4: join the parallel snapshot-build workers before tearing down
// Services — the build tasks call into Services (Perf / Registry) and must
// be quiesced first. stop() joins all worker threads; destruction would
// also join, but doing it here makes the ordering explicit and ensures no
// worker is mid-Build when Services::Shutdown() runs.
if (snapshot_build_pool_)
{
snapshot_build_pool_->stop();
snapshot_build_pool_.reset();
}
// Log every headless bot out while ALL services are still alive: ~WorldSession runs LogoutPlayer, whose
// hooks (OnPlayerLogout, group/guild) reach Population/Guilds/Perf, which Services::Shutdown destroys
// before the session manager. Update() performs the logout for each kicked session and reaps it.
Services::SessionMgr().LogoutAll();
for (int pass = 0; pass < 10 && Services::SessionMgr().active_count() > 0; ++pass)
Services::SessionMgr().Update(0);
// Stop the chat model's worker thread (cancels a request in flight), and the bot bridge's writer (marks the stop as clean).
BotChatModel::Shutdown();
BotBridge::Shutdown();
WorldSession::SetAutomatedAccountCheck(nullptr);
// Stop threads first; then tear down state.
Services::Shutdown();
// Release the battleground terrain pins before static destruction, which
// would otherwise unload their navmeshes from an already-destroyed
// MMapManager.
World::UnpinBattlegroundTerrain();
initialized_ = false;
}
// Pets of playerbots that cannot walk to their owner are set down next to them (Fleet/PetSnap.h), the way a player's pet ends up beside
// its owner when it falls behind. Each pet is looked at every kPetSnapCheckMs; runs on the world thread, before the maps update.
static void DrivePetSnap(uint32 now_ms)
{
static std::unordered_map<uint64, Fleet::PetSnapState> state;
Services::Registry().for_each([&](BotId id, BotRegistryEntry const&)
{
Player* owner = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(id));
if (!owner || !owner->IsInWorld() || !owner->IsAlive() || !owner->GetMap() || owner->IsBeingTeleported() || owner->IsInFlight())
return;
Pet* pet = owner->GetPet();
if (!pet || !pet->IsInWorld() || !pet->IsAlive() || pet->GetMap() != owner->GetMap() || pet->IsInCombat())
return;
Fleet::PetSnapState& st = state[uint64(id)];
if (!Fleet::PetSnapDue(st, now_ms))
return;
if (pet->GetExactDist(owner) <= Fleet::kPetSnapMinGap)
{
Fleet::PetSnapUpdate(st, false, now_ms);
return;
}
PathGenerator pg(pet);
::Playerbot::BotMovement::SehSafeCalculatePath(pg, owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ());
PathType const type = pg.GetPathType();
G3D::Vector3 const& end = pg.GetActualEndPosition();
float const dx = end.x - owner->GetPositionX(), dy = end.y - owner->GetPositionY(), dz = end.z - owner->GetPositionZ();
float const end_gap = std::sqrt(dx * dx + dy * dy + dz * dz);
bool const unreachable = (type & (PATHFIND_NOPATH | PATHFIND_SHORTCUT)) || ((type & PATHFIND_INCOMPLETE) && end_gap > 4.0f);
if (!Fleet::PetSnapUpdate(st, unreachable, now_ms))
return;
pet->StopMoving();
pet->NearTeleportTo(Position(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation()));
TC_LOG_INFO("playerbot.v2", "[pet_snap] {}'s pet {} had no way to its owner at ({:.0f},{:.0f},{:.0f}), set down next to {}",
owner->GetName(), pet->GetName(), owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetName());
});
if (state.size() > 2048u)
for (auto it = state.begin(); it != state.end();)
it = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(it->first)) ? std::next(it) : state.erase(it);
}
// The bots that go where a player goes (Fleet/OwnerCatchUp.h): his own companions, and every bot of a group he leads. Alive or not,
// in the world, on any map.
static void ForEachBotWithOwner(Player* owner, std::function<void(Player*)> const& fn)
{
Group* group = owner->GetGroup();
if (!group)
return;
bool const leads = group->GetLeaderGUID() == owner->GetGUID();
uint64 const owner_low = owner->GetGUID().GetCounter();
for (GroupReference const& ref : group->GetMembers())
{
Player* m = ref.GetSource();
if (!m || m == owner || !m->IsInWorld() || !m->GetSession() || !m->GetSession()->IsBot() || ::Playerbot::Scenario::PlaysAsHuman(m))
continue;
if (!leads && Services::Owners().OwnerCharacter(m->GetGUID().GetCounter()) != owner_low)
continue;
fn(m);
}
}
// A player who takes a flight takes the bots by him along (Fleet/OwnerCatchUp.h, Playerbot.Behavior.FlyWithOwner): each boards the same
// route a moment after him, and the ones that could not are brought to him when he lands. Runs on the world thread, before the maps update.
static void DriveFlyWithOwner(uint32 now_ms)
{
struct Boarding { uint64 bot_low; uint32 due_ms; };
struct Watch
{
bool flying = false;
std::vector<uint32> nodes; // his route as he took off
std::vector<Boarding> boarding; // bots about to board
std::vector<uint64> left_behind; // bots brought to him when he lands
};
static std::unordered_map<uint64, Watch> watches;
static uint32 next_ms = 0;
if (int32(now_ms - next_ms) < 0)
return;
next_ms = now_ms + 100u;
if (!Services::Config().behavior_fly_with_owner())
{
watches.clear();
return;
}
auto bot_by_low = [](uint64 low) { return ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(low)); };
std::unordered_set<uint64> seen;
for (auto const& [guid, owner] : ObjectAccessor::GetPlayers())
{
if (!owner || !owner->IsInWorld() || !::Playerbot::Scenario::PlaysAsHuman(owner))
continue;
uint64 const owner_low = guid.GetCounter();
bool const flying = owner->IsInFlight() && owner->m_taxi.GetPath().size() >= 2;
auto it = watches.find(owner_low);
if (it == watches.end())
{
if (!flying)
continue;
it = watches.emplace(owner_low, Watch{}).first;
}
seen.insert(owner_low);
Watch& w = it->second;
if (flying && !w.flying)
{
// He took off: the bots by him board in a moment, the rest wait for him to land.
w.flying = true;
w.nodes.assign(owner->m_taxi.GetPath().begin(), owner->m_taxi.GetPath().end());
w.boarding.clear();
w.left_behind.clear();
uint32 boarders = 0;
ForEachBotWithOwner(owner, [&](Player* bot)
{
bool const same = bot->GetMap() == owner->GetMap();
float const dist = same ? bot->GetExactDist(owner) : -1.0f;
if (Fleet::FlyBoardEligible(bot->IsAlive(), bot->IsInCombat(), bot->IsInFlight(), bot->GetTransport() || bot->GetVehicle(),
same, dist))
{
w.boarding.push_back({ bot->GetGUID().GetCounter(), now_ms + urand(Fleet::kFlyBoardDelayMinMs, Fleet::kFlyBoardDelayMaxMs) });
++boarders;
}
else
w.left_behind.push_back(bot->GetGUID().GetCounter());
});
TC_LOG_INFO("playerbot.v2", "[fly_with_owner] {} takes off from taxi node {} to node {} ({} hops): {} bot(s) board, {} wait for the landing",
owner->GetName(), w.nodes.front(), w.nodes.back(), uint32(w.nodes.size() - 1), boarders, uint32(w.left_behind.size()));
}
// Board the ones whose moment has come, from where they stand (a bot that started a fight or died meanwhile waits for the landing).
for (auto b = w.boarding.begin(); b != w.boarding.end();)
{
if (int32(now_ms - b->due_ms) < 0) { ++b; continue; }
Player* bot = bot_by_low(b->bot_low);
if (bot && bot->IsInWorld() && !bot->IsBeingTeleported() && bot->GetMap() == owner->GetMap() && bot->IsAlive() && !bot->IsInCombat() &&
!bot->IsInFlight() && !bot->GetTransport() && !bot->GetVehicle())
{
// Bots pay the fare like players; the flight reserve of API::fly_to_node keeps one from staying behind for want of it.
constexpr uint64 kFlightReserve = 50000; // 5 gold in copper
if (bot->GetMoney() < kFlightReserve)
bot->ModifyMoney(int64(kFlightReserve - bot->GetMoney()));
bot->StopMoving();
bot->GetMotionMaster()->Clear();
if (bot->ActivateTaxiPathTo(w.nodes, nullptr, 0, 0))
TC_LOG_INFO("playerbot.v2", "[fly_with_owner] {} boards {}'s flight to node {}", bot->GetName(), owner->GetName(), w.nodes.back());
else
{
TC_LOG_INFO("playerbot.v2", "[fly_with_owner] {} could not board {}'s flight (route {} -> {}): waits for the landing",
bot->GetName(), owner->GetName(), w.nodes.front(), w.nodes.back());
w.left_behind.push_back(b->bot_low);
}
}
else
{
if (bot)
TC_LOG_INFO("playerbot.v2", "[fly_with_owner] {} did not board {}'s flight ({}): waits for the landing", bot->GetName(),
owner->GetName(), !bot->IsAlive() ? "dead" : bot->IsInCombat() ? "in combat" : "moved off");
w.left_behind.push_back(b->bot_low);
}
b = w.boarding.erase(b);
}
if (!flying && w.flying)
{
// He landed (or the flight ended some other way): the ones left behind come to him, as his "summon" brings them.
for (Boarding const& b : w.boarding)
w.left_behind.push_back(b.bot_low);
for (uint64 low : w.left_behind)
{
Player* bot = bot_by_low(low);
if (!bot || !bot->IsInWorld() || bot->IsInFlight() || bot->GetGroup() != owner->GetGroup())
continue;
if (bot->GetMap() == owner->GetMap() && bot->GetExactDist(owner) <= Fleet::kFlyBoardYards)
continue;
if (std::string const why = Services::Population().BringToOwner(owner, bot, "flight landing"); why.empty())
TC_LOG_INFO("playerbot.v2", "[fly_with_owner] {} brought to {} at the landing", bot->GetName(), owner->GetName());
}
watches.erase(it);
continue;
}
}
// Owners who logged out or left the world mid-flight.
for (auto it = watches.begin(); it != watches.end();)
it = seen.count(it->first) ? std::next(it) : watches.erase(it);
}
// A player who gets on a ship or zeppelin takes the bots by him along (Playerbot.Behavior.RideWithOwner): each gets on the deck beside him
// a moment after him (BotPopulationManager::BoardOwnerShip) and rides as a passenger, across the map change too. When he steps off, the
// ones aboard step off with him and the ones that never got on are brought to him, as a flight landing does. Before this a bot had no
// way onto his ship: it chased his x,y over the sea and "summon" over land set it down under the zeppelin (11-08, Orgrimmar ->
// Undercity). Runs on the world thread, before the maps update.
static void DriveRideWithOwner(uint32 now_ms)
{
struct Boarding { uint64 bot_low; uint32 due_ms; };
struct Watch
{
ObjectGuid ship; // the ship he rides
std::vector<Boarding> boarding; // bots about to get on
};
static std::unordered_map<uint64, Watch> watches;
static uint32 next_ms = 0;
if (int32(now_ms - next_ms) < 0)
return;
next_ms = now_ms + 100u;
if (!Services::Config().behavior_ride_with_owner())
{
watches.clear();
return;
}
auto bot_by_low = [](uint64 low) { return ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(low)); };
std::unordered_set<uint64> seen;
for (auto const& [guid, owner] : ObjectAccessor::GetPlayers())
{
if (!owner || !::Playerbot::Scenario::PlaysAsHuman(owner))
continue;
uint64 const owner_low = guid.GetCounter();
auto it = watches.find(owner_low);
// The ship carries him to its other map: he is out of the world for the load. The ride goes on.
if (!owner->IsInWorld() || owner->IsBeingTeleported())
{
if (it != watches.end())
seen.insert(owner_low);
continue;
}
Transport* ship = dynamic_cast<Transport*>(owner->GetTransport());
if (it == watches.end())
{
if (!ship)
continue;
it = watches.emplace(owner_low, Watch{}).first;
}
seen.insert(owner_low);
Watch& w = it->second;
if (ship && w.ship != ship->GetGUID())
{
// He got on: the bots by him follow him on in a moment, the rest wait for him to step off.
w.ship = ship->GetGUID();
w.boarding.clear();
uint32 boarders = 0, waiting = 0;
ForEachBotWithOwner(owner, [&](Player* bot)
{
if (bot->GetTransport() == ship)
return;
bool const same = bot->GetMap() == owner->GetMap();
float const dist = same ? bot->GetExactDist(owner) : -1.0f;
if (Fleet::FlyBoardEligible(bot->IsAlive(), bot->IsInCombat(), bot->IsInFlight(), bot->GetVehicle() != nullptr, same, dist))
{
w.boarding.push_back({ bot->GetGUID().GetCounter(), now_ms + urand(Fleet::kFlyBoardDelayMinMs, Fleet::kFlyBoardDelayMaxMs) });
++boarders;
}
else
++waiting;
});
TC_LOG_INFO("playerbot.v2", "[ride_with_owner] {} gets on ship {} at ({:.0f},{:.0f},{:.0f}) map {}: {} bot(s) get on with him, {} are brought when he steps off",
owner->GetName(), ship->GetEntry(), owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetMapId(),
boarders, waiting);
}
// Put on board the ones whose moment has come (a bot that started a fight, died or wandered off meanwhile waits for the step-off).
if (ship)
for (auto b = w.boarding.begin(); b != w.boarding.end();)
{
if (int32(now_ms - b->due_ms) < 0) { ++b; continue; }
Player* bot = bot_by_low(b->bot_low);
if (bot && bot->IsInWorld() && !bot->IsBeingTeleported() && bot->GetMap() == owner->GetMap() && bot->IsAlive() &&
!bot->IsInCombat() && !bot->IsInFlight() && !bot->GetVehicle() && bot->GetExactDist(owner) <= Fleet::kFlyBoardYards * 1.5f)
Services::Population().BoardOwnerShip(owner, bot, "ride with owner");
else if (bot)
TC_LOG_INFO("playerbot.v2", "[ride_with_owner] {} did not get on {}'s ship ({}): brought when he steps off", bot->GetName(),
owner->GetName(), !bot->IsAlive() ? "dead" : bot->IsInCombat() ? "in combat" : "moved off");
b = w.boarding.erase(b);
}
if (!ship)
{
// He stepped off (at a dock, or over the side): the ones aboard step off with him and every bot not by him comes to him.
ObjectGuid const left = w.ship;
ForEachBotWithOwner(owner, [&](Player* bot)
{
if (!bot->IsInWorld() || bot->IsBeingTeleported() || bot->IsInFlight() || bot->GetGroup() != owner->GetGroup())
return;
bool aboard = false;
if (Transport* t = dynamic_cast<Transport*>(bot->GetTransport()); t && t->GetGUID() == left)
{
// Off the deck: a bot has no client to walk it off, and the deck hangs beside the tower or the pier.
t->RemovePassenger(bot);
bot->SetFallInformation(0, bot->GetPositionZ());
aboard = true;
}
if (!aboard && bot->GetMap() == owner->GetMap() && bot->GetExactDist(owner) <= 15.0f)
return;
if (std::string const why = Services::Population().BringToOwner(owner, bot, aboard ? "ship step-off" : "ship landing"); why.empty())
TC_LOG_INFO("playerbot.v2", "[ride_with_owner] {} {} {} at ({:.0f},{:.0f},{:.0f}) map {}", bot->GetName(),
aboard ? "steps off the ship with" : "missed the ship, brought to", owner->GetName(), owner->GetPositionX(),
owner->GetPositionY(), owner->GetPositionZ(), owner->GetMapId());
});
watches.erase(it);
continue;
}
}
// Owners who logged out mid-ride.
for (auto it = watches.begin(); it != watches.end();)
it = seen.count(it->first) ? std::next(it) : watches.erase(it);
}
// A bot that cannot get any closer to the player it goes with while he is well away is set down beside him (Fleet/OwnerCatchUp.h,
// Playerbot.Behavior.CatchUpToOwner), on his map or from any other, into a dungeon and out of one. Not in battlegrounds, not while a
// dungeon run's autopilot moves it beside him, not on a stay / hold order, never in or out of a fight. Runs on the world thread.
static std::unordered_map<uint64, uint32> g_catch_up_count;
uint32_t Fleet::CatchUpCount(uint64_t bot_low)
{
auto it = g_catch_up_count.find(bot_low);
return it == g_catch_up_count.end() ? 0u : it->second;
}
static void DriveOwnerCatchUp(uint32 now_ms)
{
static std::unordered_map<uint64, Fleet::CatchUpState> state;
static uint32 next_ms = 0;
if (int32(now_ms - next_ms) < 0)
return;
next_ms = now_ms + 250u;
if (!Services::Config().behavior_catch_up_to_owner())
{
state.clear();
return;
}
uint32 const game_ms = GameTime::GetGameTimeMS();
for (auto const& [guid, owner] : ObjectAccessor::GetPlayers())
{
if (!owner || !owner->IsInWorld() || !::Playerbot::Scenario::PlaysAsHuman(owner) || !owner->GetGroup())
continue;
Map const* omap = owner->GetMap();
if (!omap || omap->IsBattlegroundOrArena())
continue;
// While he is in the air, on a ship, falling or being ported the place he will be is not known yet; a fight is his to call.
bool const owner_settled = owner->IsAlive() && !owner->HasPlayerFlag(PLAYER_FLAGS_GHOST) && !owner->IsInCombat() && !owner->IsInFlight() &&
!owner->GetTransport() && !owner->GetVehicle() && !owner->IsFalling() && !owner->IsBeingTeleported();
ForEachBotWithOwner(owner, [&](Player* bot)
{
uint64 const low = bot->GetGUID().GetCounter();
Fleet::CatchUpState& st = state[low];
if (!Fleet::CatchUpDue(st, now_ms))
return;
Map const* bmap = bot->GetMap();
bool const same = bmap == omap;
// Another map or another copy of his instance (a ship or zeppelin he took and the bot did not, a dungeon door it did not get
// to, a dungeon he left): no walk gets it there in time, so after the same 15 s it is set down by him like his summon would
// (BringToOwner takes the instance door checks). Never a battleground on its side.
bool const other_map = !same && bmap && !bmap->IsBattlegroundOrArena();
BotAI* ai = Services::Registry().ai(low);
bool const own_movement = ai && (ai->holding_position(game_ms) || ai->follow_drop_committed(game_ms) ||
(ai->is_manual_mode(game_ms) && (ai->owner_command() == BotAI::OwnerCommand::Engage || ai->owner_command() == BotAI::OwnerCommand::Action)) ||
(same && bmap->IsDungeon() && ai->dungeon_active()));
bool const eligible = owner_settled && (same || other_map) && !own_movement && bot->IsAlive() && !bot->IsInCombat() &&
!bot->IsInFlight() && !bot->GetTransport() && !bot->GetVehicle() && !bot->IsFalling() && !bot->IsBeingTeleported();
float const gap = same ? bot->GetExactDist(owner) : 0.0f;
float const dz = same ? bot->GetPositionZ() - owner->GetPositionZ() : 0.0f;
if (!Fleet::CatchUpUpdate(st, eligible, other_map, gap, dz, now_ms))
return;
TC_LOG_INFO("playerbot.v2", "[catch_up] {} has had no way closer to {} for {} s ({}): brought to him at ({:.0f},{:.0f},{:.0f}) map {}",
bot->GetName(), owner->GetName(), Fleet::kCatchUpStrikes * Fleet::kCatchUpCheckMs / 1000u,
other_map ? fmt::format("on map {}", bmap->GetId()) :
fmt::format("{:.0f} y away, {:.0f} y {} at ({:.0f},{:.0f},{:.0f})", gap, std::fabs(dz), dz < 0.f ? "below" : "above",
bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ()),
owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), omap->GetId());
if (Services::Population().BringToOwner(owner, bot, "catch up").empty())
++g_catch_up_count[low];
});
}
if (state.size() > 2048u)
for (auto it = state.begin(); it != state.end();)
it = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(it->first)) ? std::next(it) : state.erase(it);
}
// The bots go the way their player went where the navmesh has none (Fleet/OwnerTrail.h, Playerbot.Behavior.FollowOwnerSteps): his last
// steps and jumps are kept, and a bot of his group that has had no walking route to him for two looks while it stands on that way runs
// it after him and makes his jumps (the chains up to Upper Blackrock Spire). One that falls off or cannot is the catch-up's after its
// 15 s. Same gates as the catch-up. Runs on the world thread, before the maps update.
static void DriveOwnerTrail(uint32 now_ms)
{
struct Way { Fleet::OwnerTrail trail; uint32 map_id = 0; uint32 instance_id = 0; };
struct Walker
{
Fleet::TrailWalk walk;
uint32 next_step_ms = 0; // the step it heads for, by its time stamp (the owner's deque drops steps at the front)
uint32 issued_step_ms = 0; // the step the last walk order went to
uint32 probe_ms = 0;
uint8 no_route = 0; // looks running that found no walking route to him
};
static std::unordered_map<uint64, Way> ways;
static std::unordered_map<uint64, Walker> walkers;
static uint32 next_ms = 0;
if (int32(now_ms - next_ms) < 0)
return;
next_ms = now_ms + 100u;
uint32 const game_ms = GameTime::GetGameTimeMS();
auto bot_by_low = [](uint64 low) { return ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(low)); };
auto stop = [&](uint64 low, Walker& w)
{
w.walk.active = false;
w.no_route = 0;
if (BotAI* ai = Services::Registry().ai(low))
ai->clear_owner_steps();
};
if (!Services::Config().behavior_follow_owner_steps())
{
for (auto& [low, w] : walkers)
if (w.walk.active)
stop(low, w);
ways.clear();
walkers.clear();
return;
}
std::unordered_set<uint64> seen_owners, seen_bots;
for (auto const& [guid, owner] : ObjectAccessor::GetPlayers())
{
if (!owner || !owner->IsInWorld() || !::Playerbot::Scenario::PlaysAsHuman(owner) || !owner->GetGroup())
continue;
Map const* omap = owner->GetMap();
if (!omap || omap->IsBattlegroundOrArena())
continue;
uint64 const owner_low = guid.GetCounter();
seen_owners.insert(owner_low);
Way& way = ways[owner_low];
if (way.map_id != omap->GetId() || way.instance_id != omap->GetInstanceId())
{
way.trail.Clear();
way.map_id = omap->GetId();
way.instance_id = omap->GetInstanceId();
}
// His own feet only: a taxi, a ship, a vehicle, a swim or a flying mount is no way a bot can copy step by step.
bool const usable = owner->IsAlive() && !owner->HasPlayerFlag(PLAYER_FLAGS_GHOST) && !owner->IsInFlight() && !owner->GetTransport() &&
!owner->GetVehicle() && !owner->IsInWater() && !owner->IsFlying() && !owner->IsBeingTeleported();
Fleet::TrailRecord(way.trail, owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->IsFalling(), usable, now_ms);
Fleet::OwnerTrail const& trail = way.trail;
bool const owner_ok = usable && !owner->IsInCombat();
ForEachBotWithOwner(owner, [&](Player* bot)
{
uint64 const low = bot->GetGUID().GetCounter();
seen_bots.insert(low);
Walker& w = walkers[low];
BotAI* ai = Services::Registry().ai(low);
bool const same = bot->GetMap() == omap;
bool const own_movement = !ai || ai->holding_position(game_ms) || ai->follow_drop_committed(game_ms) ||
(ai->is_manual_mode(game_ms) && (ai->owner_command() == BotAI::OwnerCommand::Engage || ai->owner_command() == BotAI::OwnerCommand::Action)) ||
(same && omap->IsDungeon() && ai->dungeon_active());
bool const eligible = owner_ok && same && !own_movement && bot->IsAlive() && !bot->IsInCombat() && !bot->IsInFlight() &&
!bot->GetTransport() && !bot->GetVehicle() && !bot->IsBeingTeleported() && !bot->IsInWater();
if (!eligible)
{
if (w.walk.active)
{
TC_LOG_INFO("playerbot.v2", "[owner_steps] {} stops walking {}'s way ({})", bot->GetName(), owner->GetName(),
!owner_ok ? "he is in a fight, in the air or off his feet" : !bot->IsAlive() ? "dead" : bot->IsInCombat() ? "in combat" : "something else moves it");
stop(low, w);
}
w.no_route = 0;
return;
}
if (w.walk.active)
{
Fleet::TrailReindex(trail, w.walk, w.next_step_ms);
Fleet::TrailStep target;
Fleet::TrailAction const action = w.walk.active
? Fleet::TrailStepFor(trail, w.walk, bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ(), now_ms, target)
: Fleet::TrailAction::Lost;
switch (action)
{
case Fleet::TrailAction::Walk:
if (w.issued_step_ms != target.ms || bot->GetMotionMaster()->GetCurrentMovementGeneratorType() != POINT_MOTION_TYPE)
{
// His steps are a few yards apart and are where he ran: a straight move, as the navmesh has no link here.
bot->GetMotionMaster()->MovePoint(0, target.x, target.y, target.z, /*generatePath*/ false);
w.issued_step_ms = target.ms;
}
ai->hold_for_owner_steps(game_ms);
break;
case Fleet::TrailAction::Jump:
{
// His jump, from where he took off to where he came down, as long as he was in the air. Fall damage as the drop
// takes it (Player::HandleFall over the height) when it lands.
uint32 const air_ms = target.air_ms ? target.air_ms : 600u;
float const sz = bot->GetPositionZ();
Position dest = bot->GetPosition();
dest.Relocate(target.x, target.y, target.z);
bot->StopMoving();
bot->SetFacingTo(std::atan2(target.y - bot->GetPositionY(), target.x - bot->GetPositionX()));
bot->SetFallInformation(0, sz);
bot->GetMotionMaster()->MoveJump(0, dest, Milliseconds(air_ms), /*minHeight*/ 0.5f, /*maxHeight*/ 4.0f);
Player* const jp = bot;
float const lx = target.x, ly = target.y, lz = target.z;
jp->m_Events.AddEventAtOffset([jp, lx, ly, lz]()
{
if (!jp->IsInWorld() || !jp->IsAlive())
return;
jp->m_movementInfo.pos.Relocate(lx, ly, lz);
jp->HandleFall();
}, Milliseconds(air_ms + 50u));
w.issued_step_ms = target.ms;
ai->hold_for_owner_steps(game_ms);
TC_LOG_INFO("playerbot.v2", "[owner_steps] {} jumps after {}: ({:.1f},{:.1f},{:.1f}) -> ({:.1f},{:.1f},{:.1f}) in {} ms",
bot->GetName(), owner->GetName(), bot->GetPositionX(), bot->GetPositionY(), sz, target.x, target.y, target.z, air_ms);
break;
}
case Fleet::TrailAction::Wait:
ai->hold_for_owner_steps(game_ms);
break;
case Fleet::TrailAction::Done:
TC_LOG_INFO("playerbot.v2", "[owner_steps] {} came up {}'s way to ({:.0f},{:.0f},{:.0f}): the follow takes it from here",
bot->GetName(), owner->GetName(), bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ());
stop(low, w);
break;
default:
TC_LOG_INFO("playerbot.v2", "[owner_steps] {} lost {}'s way at ({:.0f},{:.0f},{:.0f}): the catch-up brings it if it stays stuck",
bot->GetName(), owner->GetName(), bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ());
bot->StopMoving();
stop(low, w);
w.probe_ms = now_ms + 3000u; // not straight back onto the way it just fell off
break;
}
w.next_step_ms = w.walk.active && w.walk.next < trail.steps.size() ? trail.steps[w.walk.next].ms : 0u;
return;
}
// Not walking: is there a walking route to him? Looked at once a second, and only when he is a way off or on another level.
if (int32(now_ms - w.probe_ms) < 0)
return;
w.probe_ms = now_ms + 1000u;
float const gap = bot->GetExactDist(owner);
if (trail.steps.size() < 2 || gap < 4.0f)
{
w.no_route = 0;
return;
}
PathGenerator pg(bot);
::Playerbot::BotMovement::SehSafeCalculatePath(pg, owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ());
PathType const type = pg.GetPathType();
G3D::Vector3 const& end = pg.GetActualEndPosition();
float const ex = end.x - owner->GetPositionX(), ey = end.y - owner->GetPositionY(), ez = end.z - owner->GetPositionZ();
bool const unreachable = (type & (PATHFIND_NOPATH | PATHFIND_SHORTCUT)) ||
((type & PATHFIND_INCOMPLETE) && std::sqrt(ex * ex + ey * ey + ez * ez) > 4.0f);
if (!unreachable)
{
w.no_route = 0;
return;
}
if (++w.no_route < 2)
return;
// Where it stands on his way, by a step it can walk to straight (not the steps on top of the ledge above it).
int const join = Fleet::TrailJoin(trail, bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ(), [&](Fleet::TrailStep const& s)
{
PathGenerator sp(bot);
::Playerbot::BotMovement::SehSafeCalculatePath(sp, s.x, s.y, s.z);
if (sp.GetPathType() != PATHFIND_NORMAL)
return false;
float const straight = bot->GetExactDist(Position(s.x, s.y, s.z));
return sp.GetPathLength() <= straight * 1.5f + 3.0f;
});
if (join < 0)
return;
Fleet::TrailBegin(trail, w.walk, join, now_ms);
if (!w.walk.active)
return;
w.next_step_ms = trail.steps[w.walk.next].ms;
w.issued_step_ms = 0;
bot->StopMoving();
bot->GetMotionMaster()->Clear();
ai->hold_for_owner_steps(game_ms);
uint32 jumps = 0;
for (size_t i = size_t(join) + 1; i < trail.steps.size(); ++i)
jumps += trail.steps[i].jump ? 1u : 0u;
TC_LOG_INFO("playerbot.v2", "[owner_steps] {} has no walking route to {} ({:.0f} y off): takes his way from ({:.0f},{:.0f},{:.0f}), {} step(s), {} jump(s)",
bot->GetName(), owner->GetName(), gap, trail.steps[join].x, trail.steps[join].y, trail.steps[join].z,
uint32(trail.steps.size() - size_t(join)), jumps);
});
}
for (auto it = ways.begin(); it != ways.end();)
it = seen_owners.count(it->first) ? std::next(it) : ways.erase(it);
for (auto it = walkers.begin(); it != walkers.end();)
{
if (seen_bots.count(it->first)) { ++it; continue; }
if (it->second.walk.active)
{
if (Player* b = bot_by_low(it->first); b && b->IsInWorld())
b->StopMoving();
stop(it->first, it->second);
}
it = walkers.erase(it);
}
}
void Module::OnWorldUpdate(std::chrono::milliseconds diff)
{
if (!initialized_) return;
static std::chrono::milliseconds total{0};
// #5 Phase 4: next_version is read-modify-write per built snapshot. Under
// parallel-by-Map* Build a plain ++ is a torn counter (duplicate/garbage
// versions break the bot worker's staleness/version checks). Versions need
// only be UNIQUE + monotonic, not contiguous, so an atomic fetch_add is
// the race-free fix. Pre-assigned per bot before the parallel region, so
// the parallel tasks never touch this counter themselves.
static std::atomic<SnapshotVer> next_version{1};
static TickId next_tick = 1;
total += diff;
BotLanguage::Update(); // the login queries for the players' client language
BotCommandParser::TickRaidResets(); // "raid reset": close the emptied instances, then clear the locks
{
static std::chrono::milliseconds last_hint_check{0};
if (total - last_hint_check >= std::chrono::milliseconds(2000))
{
last_hint_check = total;
SendDueAddonHints();
}
}
{
// A raid of a player and companions that fits a party again goes back to one (BotGroupRules::RevertBotRaidToParty), when the module
// made it a raid for one more companion (Fleet/AutoRaid.h). A raid its leader made stays one (ZMNC, Discord 2026-11-18).
static std::chrono::milliseconds last_raid_check{0};
if (total - last_raid_check >= std::chrono::milliseconds(5000))
{
last_raid_check = total;
std::vector<Group*> to_party;
for (auto const& [guid, p] : ObjectAccessor::GetPlayers())
{
Group* g = p ? p->GetGroup() : nullptr;
if (g && !g->isRaidGroup())
::Playerbot::V2::Fleet::AutoRaid::Forget(g->GetGUID()); // a party again (its leader turned it back): a later raid is theirs
if (!g || !g->isRaidGroup() || p->GetSession()->IsBot() || std::ranges::find(to_party, g) != to_party.end() ||
!::Playerbot::V2::Fleet::AutoRaid::Made(g->GetGUID()))
continue;
bool any_bot = false, any_on_raid_map = false;
for (GroupReference const& ref : g->GetMembers())
{
Player const* m = ref.GetSource();
any_bot = any_bot || m->GetSession()->IsBot();
any_on_raid_map = any_on_raid_map || (m->GetMap() && m->GetMap()->IsRaid());
}
if (BotGroupRules::RevertBotRaidToParty(true, g->isBGGroup() || g->isBFGroup() || g->isLFGGroup(), g->GetMembersCount(),
MAX_GROUP_SIZE, any_bot, true, any_on_raid_map, g->IsRaidChosenByLeader()))
to_party.push_back(g);
}
for (Group* g : to_party)
{
TC_LOG_INFO("playerbot.v2", "[raid_to_party] leader={} members={}", g->GetLeaderName(), g->GetMembersCount());
g->ConvertToGroup();
::Playerbot::V2::Fleet::AutoRaid::Forget(g->GetGUID());
}
}
}
::Playerbot::Diagnostics::Scenario::Tick(std::uint32_t(diff.count())); // bot scenario tests (.playerbot scenario)
BotChatModel::DeliverReplies(GameTime::GetGameTimeMS()); // Playerbot.ChatModel replies that came back
BotBridge::Update(GameTime::GetGameTimeMS()); // Playerbot.Bridge: events out, replies in (off: returns at once)
// [cast_state] (playtest, WSG start room 2026-10-11: a healer stood with the casting animation on while casting nothing). Twice a
// second, every bot in a battleground: a line when the spell it is casting changes (spell id, generic or channel, 0 = none), and a
// [cast_stale] line when the server holds no cast at all but the unit data still names a channel spell: what a client draws as a
// cast that never ends. The log answers which of the two it was; nothing is cleared here.
{
static std::chrono::milliseconds last_cast_check{0};
static std::unordered_map<BotId, uint32> cast_seen;
static std::unordered_map<BotId, uint32> cast_since;
static std::unordered_map<BotId, uint32> stale_logged_ms;
static uint32 cast_lines = 0;
static uint32 cast_window_ms = 0;
constexpr uint32 kCastLinesPerWindow = 6000;
if (total - last_cast_check >= std::chrono::milliseconds(500))
{
last_cast_check = total;
uint32 const now_ms = GameTime::GetGameTimeMS();
// The line budget is per 5 minutes, not per server run: 1,500 for the whole run were spent 145 s into a raid sweep's first
// fight (bwl_nefarian 0136-r9: all 1,499 lines in run 1, none in runs 2 and 3), so the end of a fight never showed what the casters cast (Nefarian's phase 3). A 40-man raid in a fight logs about 600 lines a minute.
if (now_ms - cast_window_ms >= 300000u)
{
cast_window_ms = now_ms;
cast_lines = 0;
}
Services::Registry().for_each([&](BotId id, BotRegistryEntry const& e)
{
if (!e.ai) return;
Player* p = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(id));
if (!p || !p->IsInWorld() || !p->GetMap() || !(p->GetMap()->IsBattleground() || p->GetMap()->IsRaid())) { cast_seen.erase(id); cast_since.erase(id); return; }
Spell const* gen = p->GetCurrentSpell(CURRENT_GENERIC_SPELL);
Spell const* chan = p->GetCurrentSpell(CURRENT_CHANNELED_SPELL);
uint32 const now_spell = chan ? chan->GetSpellInfo()->Id : gen ? gen->GetSpellInfo()->Id : 0u;
uint32& seen = cast_seen[id];
if (seen != now_spell)
{
cast_since[id] = now_ms;
if (cast_lines < kCastLinesPerWindow)
{
++cast_lines;
TC_LOG_INFO("playerbot.v2", "[cast_state] {} spell {} -> {} ({}) casting_state={} mounted={} combat={}", p->GetName(), seen, now_spell,
chan ? "channel" : gen ? "generic" : "none", p->HasUnitState(UNIT_STATE_CASTING), p->IsMounted(), p->IsInCombat());
}
seen = now_spell;
}
else if (now_spell && cast_since[id] && now_ms - cast_since[id] >= 8000u && cast_lines < kCastLinesPerWindow)
{
// One cast that has been the current spell for 8 s or more: a cast bar that is not filling.
++cast_lines;
cast_since[id] = now_ms;
Spell const* cur = chan ? chan : gen;
TC_LOG_INFO("playerbot.v2", "[cast_long] {} still on spell {} ({}) state={} timer={} ms target={} casting_state={} combat={}", p->GetName(), now_spell,
chan ? "channel" : "generic", uint32(cur->getState()), cur->GetTimer(), cur->m_targets.GetUnitTargetGUID().GetCounter(),
p->HasUnitState(UNIT_STATE_CASTING), p->IsInCombat());
}
if (!gen && !chan && !p->HasUnitState(UNIT_STATE_CASTING) && p->GetChannelSpellId() != 0)
{
uint32& last = stale_logged_ms[id];
if (!last || now_ms - last >= 5000u)
{
last = now_ms ? now_ms : 1u;
TC_LOG_INFO("playerbot.v2", "[cast_stale] {} no server cast but channel spell {} still in unit data (visual {})", p->GetName(),
p->GetChannelSpellId(), p->GetChannelSpellXSpellVisualId());
}
}
});
}
}
// Bots hovering over the floor in a battleground: the start points (AB Trollbane Hall z=-9.0 over a courtyard at -15) sit a few
// yards above the ground. A player falls client-side; a bot never does, so it stood in the air until it walked. Once a second,
// a quarter of the bots (by id) that stand still in a battleground and are 2..30 y above the floor drop to it with a falling
// spline (playtest, AB test 2026-10-08, "bots flying in the air").
{
static std::chrono::milliseconds last_settle{0};
static uint32 settle_phase = 0;
if (total - last_settle >= std::chrono::milliseconds(1000))
{
last_settle = total;
uint32 const phase = settle_phase++ & 3u;
Services::Registry().for_each([&](BotId id, BotRegistryEntry const& e)
{
if (!e.ai || (id & 3u) != phase) return;
Player* p = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(id));
if (!p || !p->IsInWorld() || !p->GetMap()) return;
if (!p->GetMap()->IsBattleground())
{
// Outside battlegrounds nothing dropped a bot left in the air either, and players saw bots hang there in fights
// (Discord #общий, doctorsun 2026-11-17: "bots sometimes hang in the air in combat", no place given). A bot seen
// standing still 2..30 y over the ground on two scans in a row (4 s apart; a knockback or a jump in flight is moving)
// falls to it, as the client makes a player fall. Each one is logged with its fight and its last rule, at most once
// a minute per bot, to find what put it there.
static std::unordered_map<BotId, uint32> hover_first_ms, hover_logged_ms;
uint32 const now_ms = GameTime::GetGameTimeMS();
float ground = INVALID_HEIGHT;
if (!::Playerbot::BotMovement::HoverGap(p, 2.0f, 30.0f, ground))
{
hover_first_ms.erase(id);
return;
}
auto const [first, fresh] = hover_first_ms.try_emplace(id, now_ms);
if (fresh)
return;
uint32 const hung_ms = now_ms - first->second;
hover_first_ms.erase(first);
// read before the fall: the spline makes the bot move, and the fall's own position is the ground
float const z0 = p->GetPositionZ();
Unit const* victim = p->GetVictim();
Spell const* cast = p->GetCurrentSpell(CURRENT_GENERIC_SPELL);
uint32 const castId = cast ? cast->GetSpellInfo()->Id : 0u;
bool const dropped = ::Playerbot::BotMovement::SettleToGround(p, 2.0f, 30.0f);
uint32& logged = hover_logged_ms[id];
if (logged && now_ms - logged < 60000u)
return;
logged = now_ms ? now_ms : 1u;
static uint32 total_logged = 0;
if (total_logged++ >= 2000)
return;
TC_LOG_INFO("playerbot.v2", "[hover_seen] {} map={} zone={} ({:.1f},{:.1f},{:.1f}) {:.1f} y over the ground for {} ms, combat {} "
"victim {} cast {} state {} last rule {} -> {}", p->GetName(), p->GetMapId(), p->GetZoneId(), p->GetPositionX(),
p->GetPositionY(), z0, z0 - ground, hung_ms, int(p->IsInCombat()),
victim ? victim->GetName() : std::string("-"), castId, uint32(e.ai->state()),
e.ai->last_rule_fired() ? e.ai->last_rule_fired() : "-", dropped ? "dropped" : "not dropped");
return;
}
// A bot already mounted indoors in a battleground (start tunnel / hall) gets off: nobody rides in there.
if (p->IsMounted() && !p->IsOutdoors())
{
p->RemoveAurasByType(SPELL_AURA_MOUNTED);
static uint32 dm_logged = 0;
if (dm_logged++ < 100)
TC_LOG_INFO("playerbot.v2", "[bg_indoor_dismount] {} map={} ({:.1f},{:.1f})", p->GetName(), p->GetMapId(),
p->GetPositionX(), p->GetPositionY());
return;
}
float const z0 = p->GetPositionZ();
if (::Playerbot::BotMovement::SettleToGround(p, 2.0f, 30.0f))
{
static uint32 logged = 0;
if (logged++ < 200)
TC_LOG_INFO("playerbot.v2", "[hover_settle] {} map={} ({:.1f},{:.1f}) z {:.1f} -> ground",
p->GetName(), p->GetMapId(), p->GetPositionX(), p->GetPositionY(), z0);
}
});
}
}
// First-tick: build quest reverse-indices (KillCredit aliases + creature
// labels). Templates are guaranteed loaded by world-update time; doing
// this once on first tick avoids a per-snapshot lazy build under contention.
{
static bool reverse_indices_built = false;
if (!reverse_indices_built)
{
EnsureQuestReverseIndicesBuilt();
// Quest objective locations for quests without QuestPOI (vanilla):
// spawn / loot-source / areatrigger clusters. One-time DB + store
// walk on the world thread; read-only afterwards.
EnsureQuestObjectiveLocationsBuilt();
reverse_indices_built = true;
}
}
// Boot-time human-login priority gate. AutoResume / AutoSpawn are
// deferred until either (a) a non-bot WorldSession is in-world, or
// (b) kBootGraceMs has elapsed since the first OnWorldUpdate.
//
// Without this gate, AutoSpawn fired synchronously on tick #1, doing
// up to 200 × BotCharacterFactory::Create (each SaveToDB ~30ms) on
// the world thread — ~6s of blocking during which inbound human
// login packets sat in the session queue and the player's client
// timed out / appeared frozen. Observed 2026-05-15 right after the
// V2 wipe: server came up, AutoSpawn ran, owner couldn't log in.
//
// Sticky once opened — first human triggers it for the rest of boot.
constexpr uint32 kBootGraceMs = 60'000; // 60s lights-out fallback
if (!boot_gate_open_)
{
if (boot_first_tick_at_.count() == 0)
{
boot_first_tick_at_ = total;
for (auto const& kv : sGroupMgr->GetGroupStore())
g_boot_group_ids.insert(kv.first); // what is persisted from the last run; anything formed later is live
}
const uint32 since_first_tick_ms =
uint32((total - boot_first_tick_at_).count());
// Scan WorldSessionMgr for any non-bot session that has loaded
// a Player (i.e. real human is in-world). Bot sessions live in
// BotSessionMgr's own map; sWorld->m_sessions covers humans.
bool human_present = false;
// Fully qualify ::World — Playerbot::V2::World is a sibling
// namespace (CapitalsTable etc.) and shadows the global class.
for (auto const& kv : ::World::instance()->GetAllSessions())
{
WorldSession* session = kv.second;
if (!session) continue;
Player* p = session->GetPlayer();
if (!p) continue;
// BotSession reuses real account ids, so account-id alone can't
// distinguish a bot from a human; check the V2 registry instead.
if (Services::Lifecycle().is_bot(p->GetGUID().GetCounter()))
continue;
human_present = true;
break;
}
if (human_present || since_first_tick_ms >= kBootGraceMs)
{
boot_gate_open_ = true;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] boot gate OPEN ({}); bot spawn/login passes resume.",
human_present ? "human in-world" : "grace expired");
}
}
// One-time boot purge: a hard crash (Shutdown never ran) can leave
// persisted pure-bot groups in the DB; they reload with stale state and
// wedge. Once the registry is ready (boot gate open), disband any that
// survived. Bots haven't formed new groups yet at this point, so every
// pure-bot group present is a stale persisted one.
{
static bool boot_group_purge_done = false;
if (!boot_group_purge_done && boot_gate_open_)
{
boot_group_purge_done = true;
DisbandStaleBotGroups(/*purge_pure_bot=*/true, getMSTime(), &g_boot_group_ids);
}
}
// Periodic bot-group hygiene (every 60s): disband human groups whose
// human member(s) have all been offline ≥30 min so the bots stop
// following an absent leader. Pure-bot runtime groups are left alone
// (handled at shutdown / boot). Cheap — a few dozen groups, once a minute.
{
static std::chrono::milliseconds last_group_hygiene{0};
constexpr std::chrono::milliseconds kGroupHygieneInterval{60'000};
if (total - last_group_hygiene >= kGroupHygieneInterval)
{
last_group_hygiene = total;
DisbandStaleBotGroups(/*purge_pure_bot=*/false, getMSTime());
}
}
// Auto-resume: log in marked bots up to AutoResumeCap, spread over
// many ticks. Previously this fired LoginAll(cap) on a single tick,
// submitting 100+ Player::SaveToDB calls simultaneously. Each save
// ends with REPLACE INTO battlenet_account_mounts keyed by the bot's
// shared bnet account row — multiple bots per pool account race on
// the same row lock. Innodb_lock_wait_timeout=50s, FreezeDetector
// fires at 60s. Observed 2026-05-16 crash: "MySQL errno 1205 Lock
// wait timeout exceeded" → world thread hung 60s.
//
// Spreading the resume kBootSpawnPerTick at a time keeps the bnet
// write rate well below the row-lock contention threshold.
constexpr uint32 kBootResumePerTick = 5;
if (auto_resume_pending_ > 0 && boot_gate_open_)
{
const uint32 batch = std::min(auto_resume_pending_, kBootResumePerTick);
// LoginAll's cap is "stop when active_count >= cap". Active count
// grows as we resume; passing (current + batch) acts as a per-call
// batch limit. Re-evaluates each tick.
const uint32 active_now = Services::SessionMgr().active_count();
auto r = Services::SessionMgr().LoginAll(active_now + batch);
if (r.attempted > 0)
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] AutoResume batch: attempted={} submitted={} already_in_world={} remaining={}.",
r.attempted, r.succeeded, r.skipped_already_in_world,
auto_resume_pending_ - r.attempted);
// Decrement by attempts (not just successes) so a steady stream
// of "already in world" entries doesn't starve us into an infinite
// loop. After cap ticks, pending hits 0 either way.
if (r.attempted >= auto_resume_pending_)
auto_resume_pending_ = 0;
else
auto_resume_pending_ -= r.attempted;
// If no attempts AND no progress, the registry is exhausted —
// drain so we don't spin forever.
if (r.attempted == 0)
auto_resume_pending_ = 0;
}
// Auto-spawn: top up the marked-bot count to AutoSpawnOnBoot. Spread
// over multiple ticks (kBootSpawnPerTick per tick) so each tick's
// synchronous SaveToDB cost (~30ms × per-tick limit) leaves headroom
// for human-login packet processing on the world thread. At 5/tick
// and 1Hz module tick that's ~150ms/sec of DB work — invisible to
// humans. A 200-bot boot completes in ~40s instead of a 6s freeze.
// Hard-capped at AUTO_SPAWN_HARD_CAP overall.
constexpr uint32 AUTO_SPAWN_HARD_CAP = 200;
constexpr uint32 kBootSpawnPerTick = 5;
if (auto_spawn_pending_ && boot_gate_open_)
{
const uint32 currentMarked = uint32(Services::Lifecycle().size());
const uint32 target_total = std::min(
auto_spawn_pending_ + currentMarked, currentMarked + AUTO_SPAWN_HARD_CAP);
if (currentMarked >= target_total)
{
auto_spawn_pending_ = 0;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] AutoSpawn: target reached (marked={}).", currentMarked);
}
else
{
const uint32 remaining = target_total - currentMarked;
const uint32 batch = std::min(remaining, kBootSpawnPerTick);
uint32 created = 0, login_ok = 0, login_fail = 0;
for (uint32 i = 0; i < batch; ++i)
{
auto picked = BotComposition::Roll();
if (picked.race == 0) break;
auto r = BotCharacterFactory::Create(
/*ownerSession*/ nullptr, picked.name, picked.race, picked.cls, picked.gender, picked.surname);
if (!r.ok) continue;
++created;
auto login = Services::SessionMgr().LoginBot(r.guid);
if (login.ok) ++login_ok; else ++login_fail;
if (auto_spawn_pending_ > 0) --auto_spawn_pending_;
}
if (created > 0)
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] AutoSpawn batch: created={} login_ok={} login_fail={} "
"remaining_target={} marked_now={}.",
created, login_ok, login_fail, auto_spawn_pending_,
uint32(Services::Lifecycle().size()));
}
}
const uint32 tick_start_ms = getMSTime();
// Per-phase latency capture so the FleetStatus log line surfaces which
// subsystem owns the world-thread cost. Without this we triage perf at
// scale by guesswork. Phases sum to ~tick latency; gap is unaccounted-for
// (TC core, scripts, etc).
uint32 t_snap_start_ms = 0, t_snap_ms = 0;
uint32 t_snap_setup_ms = 0, t_snap_build_ms = 0, t_group_build_ms = 0;
uint32 t_bgport_ms = 0, t_drain_ms = 0, t_session_ms = 0;
uint32 t_population_ms = 0, t_scheduler_ms = 0;
uint32 snaps_built_this_tick = 0;
uint32 snaps_skipped_this_tick = 0;
size_t intents_drained_this_tick = 0;
// Per ARCHITECTURE.md §1.1:
// 1. publish per-bot snapshots
// 2. (intent draining via PlayerbotAPI lands when API surface widens)
// 3. let TickScheduler decide which bots run this tick
// 1. Publish snapshots for every registered bot whose Player* is in-world.
t_snap_start_ms = getMSTime();
auto& reg = Services::Registry();
auto& pub = Services::Snapshots();
auto& sch = Services::Scheduler();
const TickId tick = next_tick++;
// Group snapshot dedup cache (group_snap_cache_, world-thread only).
// GroupSnapshotBuilder::Build walks every group member (HP/mana/casting/
// auras) so for a 5-bot party each bot was paying 5× the work (and a
// 40-bot raid 40×). The first build per group-guid is shared by every
// member that rebuilds its own snapshot within kGroupSnapMaxAgeMs.
// Pre-dedup cost: N_groups × N_members² member-walks per tick.
// Post-dedup: N_groups × N_members member-walks per window.
// The window used to be ONE tick. That was enough while a group's bots
// rebuilt in lock-step (all in the same tick), but the scheduler now
// spreads every bot over its tier period (TickScheduler NextPhaseDeadline),
// so a 40-bot raid has a few members rebuilding in EVERY tick — a per-tick
// cache would re-walk all 40 members every tick (~1 ms each in a raid).
// Half the Combat period bounds the staleness of the shared group view
// below the bots' own snapshot cadence while keeping raid group builds at
// ~20 Hz. Entries are also rebuilt when the group's member count or
// leader changed (a join/leave/promote is never served from a stale view).
constexpr uint32 kGroupSnapMaxAgeMs = 50;
const uint32 group_cache_now_ms = getMSTime();
for (auto it = group_snap_cache_.begin(); it != group_snap_cache_.end(); )
{
if (!it->second.snap ||
getMSTimeDiff(it->second.built_ms, group_cache_now_ms) >= kGroupSnapMaxAgeMs)
it = group_snap_cache_.erase(it);
else
++it;
}
// --- Per-tick real-player proximity/group precompute (Tier 2.1) ------
// The tier classifier below must answer "is this bot near, or grouped
// with, a REAL (non-bot) player?" without doing a per-bot grid search
// (that would re-introduce the build cost we're trying to shed). So we
// enumerate the connected real players ONCE here — there are only a
// handful even on a busy server — and stash:
// * real_player_cells : the coarse grid cell of every real player
// (see PackPlayerCellKey). A bot is "near" a
// real player when its own cell is occupied.
// * real_player_groups : the GUID of every group that contains at
// least one real player. A bot is "grouped
// with a real player" when its group_guid is
// in this set.
// Real players are enumerated from World::GetAllSessions() (the global
// human session map — bot sessions are flagged via the V2 lifecycle
// registry's is_bot()), same pattern as the human-present boot gate
// above. Runs on the world thread before the bot loop; both containers
// are read-only when consumed by the tier classifier.
//
// #5 Phase 4: these are plain function-local (NOT thread_local). The tier
// classifier that reads them runs in the POST-BARRIER world-thread pass
// (set_tier is world-thread-only anyway), so a single world-thread-visible
// copy is exactly what's needed. They were thread_local before parallel
// Build, which would have read EMPTY on worker threads — the audit's
// thread_local trap. Keeping them local also means zero cross-thread
// sharing during the parallel region.
std::unordered_set<uint64> real_player_cells;
std::unordered_set<ObjectGuid> real_player_groups;
{
const uint32 rp_t0 = getMSTime();
auto& lifecycle = Services::Lifecycle();
for (auto const& kv : ::World::instance()->GetAllSessions())
{
WorldSession* session = kv.second;
if (!session) continue;
Player* rp = session->GetPlayer();
if (!rp) continue;
if (lifecycle.is_bot(rp->GetGUID().GetCounter()))
continue; // a V2 bot, not a real human
const uint32 rp_map = rp->GetMapId();
const int32 rp_cx = PlayerCellCoord(rp->GetPositionX());
const int32 rp_cy = PlayerCellCoord(rp->GetPositionY());
for (int32 dx = -1; dx <= 1; ++dx)
for (int32 dy = -1; dy <= 1; ++dy)
real_player_cells.insert(
PackPlayerCellKey(rp_map, rp_cx + dx, rp_cy + dy));
if (Group const* rg = rp->GetGroup())
real_player_groups.insert(rg->GetGUID());
}
t_snap_setup_ms += getMSTimeDiff(rp_t0, getMSTime());
}
// ---------------------------------------------------------------------
// Team-level BG coordinator (BG audit N60) + group-level dungeon/raid
// coordinator. MUST run before the snapshot pass below so the orders
// they compute are copied into THIS tick's snapshots by the builder.
// Both are internally throttled (750ms / 500ms plan cadence); the
// off-cadence cost is one timestamp compare each.
{
const uint32 coord_t0 = getMSTime();
Services::BgCoordinator().Update(coord_t0);
Services::PveCoordinator().Update(coord_t0);
Raid::GetBrain().Update(coord_t0); // after the PvE plan: reads its main tank
Raid::UpdateBossWarnings(coord_t0);
t_snap_setup_ms += getMSTimeDiff(coord_t0, getMSTime());
}
// ============================ #5 Phase 4 =============================
// PARALLEL-BY-Map* SNAPSHOT BUILD.
//
// Pipeline (see the audit for the per-surface rationale):
// (A) WORLD THREAD, per bot, in registry order:
// DriveTeleportAck (every tick), should_build_snapshot tier gate
// (mutates per-bot deadline — TickScheduler is world-thread-only),
// then the latency-tolerant rescue trio (RescueOrphanedBgBot /
// SnapToGroundIfDrifted / GlobalStuckRescue — all touch live core
// mutating state). Bots that pass the gate are collected, grouped
// by Map* POINTER, each pre-assigned a UNIQUE snapshot version.
// (B) PARALLEL: one task per Map* partition builds ONLY the per-bot
// BotSnapshots (pure per-bot CPU + read-only core reads). Each
// Map's bots build sequentially on a single worker, so a given bot
// / BotAI is touched by exactly one thread. Results are staged.
// (C) BARRIER join.
// (D) WORLD THREAD, in the SAME registry order as (A): tier classify +
// set_tier, group-snapshot dedup + GroupSnapshotBuilder::Build +
// publish_group, per-bot publish, perf. Everything the audit marks
// must-stay-world-thread lives here, after the barrier.
//
// Order preservation: (A) appends to build_order in registry order; (D)
// iterates build_order in that same order, so set_tier / publish happen in
// the identical sequence as the old serial loop → byte-identical behavior.
// The only data that crosses the barrier is each bot's own freshly-built
// BotSnapshot (single-owner) plus scalar timing.
struct BuiltSlot
{
BotId bot_id = 0;
Player* player = nullptr; // valid for this tick (world quiescent)
BotAI* ai = nullptr; // pre-resolved on world thread (#5 Phase 4)
std::shared_ptr<BotSnapshot const> snap; // null = Build returned {}
uint32 build_us = 0; // this bot's Build wall time (TickPerf slowest-build)
};
// Grids the rules' nav-point probes asked for on the bot logic threads
// (BotMovement::ScopedBotLogicThread). The maps are still here, so this is
// where they load; the builds below already see them.
g_bot_logic_grid_loads += ::Playerbot::BotMovement::LoadDeferredGrids();
// Partition: Map* -> the slots whose bots live on that map. A vector of
// (Map*, slots) preserves a stable iteration; build_order records the
// global registry order for the post-barrier serial pass.
std::vector<BuiltSlot> build_order;
std::unordered_map<Map const*, std::vector<size_t>> by_map; // Map* -> indices into build_order
std::vector<uint64> split_key; // per build_order slot: group or bot, for PlayerbotV2.SnapshotBuildSplitMap
build_order.reserve(256);
split_key.reserve(256);
{
const uint32 selectA_t0 = getMSTime();
reg.for_each([&](BotId id, BotRegistryEntry const& e)
{
Player* p = ObjectAccessor::FindConnectedPlayer(
ObjectGuid::Create<HighGuid::Player>(id));
// Drive any outstanding teleport ack BEFORE building the snapshot
// so position-based fields reflect the post-teleport location.
// This MUST run every tick: it advances ack/packet state for the
// teleport handshake and is latency-sensitive (a dropped/late ack
// wedges the bot in WORLD_STATE_LOGGEDIN), so it stays outside the
// tier-throttle gate below. World-thread (mutates packet state).
if (p)
DriveTeleportAck(p);
// Per-bot tier-driven snapshot cadence gate. Mutates the per-bot
// deadline inside TickScheduler (world-thread-only), so it runs
// here in the serial selection pass, never on a worker.
if (!sch.should_build_snapshot(id, total)) { ++snaps_skipped_this_tick; return; }
// Latency-tolerant per-bot setup. All three touch live core
// mutating state (teleport, ground snap, motion master) and stay
// on the world thread. Gated behind should_build_snapshot so they
// run at the bot's tier rate. (Subsumes PERF-P1b + PERF-P3b.)
if (p)
{
// Rescue ghost-BG bots BEFORE SnapToGroundIfDrifted — the
// BG map's BIH can crash that call. RescueOrphanedBgBot is
// a no-op for bots not in an orphaned BG map.
RescueOrphanedBgBot(p);
SnapToGroundIfDrifted(p);
// Deep water: the swimming flag and the swim depth a player's client would give it (Api/BotSwim.h). Headless bots
// only: a client moves its own player.
if (Services::SessionMgr().IsHeadless(p->GetGUID()))
::Playerbot::BotMovement::UpdateSwimState(p);
// Resolve the bot's bot logic ONCE here on the world thread (registry
// quiescent under for_each's shared_lock) and thread it through
// the BuiltSlot. Build workers MUST NOT call Registry().ai()
// (unlocked map lookup races a concurrent rehash → garbage
// pointer → AV). Same pointer GlobalStuckRescue used before.
if (BotAI* ai = e.ai.get())
GlobalStuckRescue(p, ai);
}
// Bot is due to build this tick. Record it in registry order and
// bucket it by its Map* pointer for the parallel partition. A bot
// with no in-world Player still gets a slot so Build (which returns
// {} for null/!IsInWorld) is invoked uniformly and the post-barrier
// pass sees the same null result it would have serially.
const size_t slot = build_order.size();
build_order.push_back(BuiltSlot{ id, p, e.ai.get(), {} });
Map const* m = p ? p->GetMap() : nullptr;
by_map[m].push_back(slot);
// Same-map split key: a group's bots share one key, so they always
// build on one thread (the builder reads group-wide coordinator
// state); an ungrouped bot is its own key.
Group const* g = p ? p->GetGroup() : nullptr;
split_key.push_back(g ? (uint64(1) << 63) | g->GetGUID().GetCounter() : uint64(id));
});
t_snap_setup_ms += getMSTimeDiff(selectA_t0, getMSTime());
}
// Pre-assign a unique, monotonic version to each due bot BEFORE the
// parallel region so the build tasks never touch the shared counter.
// Contiguity is not required — only uniqueness + monotonicity — so a
// single fetch_add reserving the whole block is sufficient and race-free.
const SnapshotVer version_base =
next_version.fetch_add(build_order.size(), std::memory_order_relaxed);
// Decide serial vs. parallel for THIS tick. Read the kill-switch every
// tick so the operator can flip it live. The pool is created + started
// lazily on first parallel tick (a box that never enables pays nothing).
const bool parallel_on = Services::Config().parallel_snapshot_build() && build_order.size() > 1;
// PlayerbotV2.SnapshotBuildSplitMap: one busy open-world map may also be
// split over the pool (see the fan-out below). Off = one thread per map.
const bool split_on = parallel_on && Services::Config().snapshot_build_split_map();
const bool want_pool = parallel_on && (by_map.size() > 1 || split_on);
if (want_pool && !snapshot_build_pool_)
{
snapshot_build_pool_ = std::make_unique<Playerbot::SnapshotBuildPool>(
Services::Config().snapshot_build_threads());
snapshot_build_pool_->start();
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] #5 Phase 4 parallel snapshot Build ENABLED "
"(build workers={}, world thread participates).",
snapshot_build_pool_->worker_count());
}
// Per-task accumulated build time, summed into t_snap_build_ms after the
// barrier (the world-thread accumulators are NOT touched from workers).
std::atomic<uint32> par_build_ms_acc{0};
{
const uint32 build_phase_t0 = getMSTime();
// Build closure for one Map*'s slots [start,end). ONE task == one Map*
// == one worker thread for the whole batch. This partition-by-map rule is
// LOAD-BEARING for thread-safety, not just cache warmth:
// BotSnapshotBuilder::Build runs Detour pathfinding (route-feasibility
// probes → PathGenerator::CalculatePath → dtNavMeshQuery::findPath), and
// TrinityCore keeps a SINGLE dtNavMeshQuery per (mapId, instanceId) that
// is explicitly NOT thread-safe (MMapManager.h: "the returned
// dtNavMeshQuery const* is NOT threadsafe"; MMapManager.cpp: "single
// dtNavMeshQuery for every instance, since those are not thread safe").
// findPath mutates that query's node pool / open list, so two workers
// pathing for bots on the SAME map instance concurrently corrupts it and
// BuildPolyPath spins forever → world thread hangs in run_and_wait → 60s
// FreezeDetector abort (observed 2026-06-16 after a chunking experiment
// split one map across workers; reverted). Keeping all of a map's bots on
// one worker serializes that map's pathfinding, which is the invariant the
// shared query requires. (start/end kept as params for the serial path;
// the parallel path always passes the full range.)
//
// same_map_split (PlayerbotV2.SnapshotBuildSplitMap): this chunk is one
// part of a map whose other parts build on other threads at the same
// time. Two things then change for the length of the chunk:
// - every navmesh query on this thread uses the thread's OWN
// dtNavMeshQuery (MMAP::ScopedThreadNavMeshQuery), so the shared
// per-instance query above is never touched by two threads. The
// dtNavMesh itself is only read, and no tile loads or unloads while
// the build runs (tiles change in sMapMgr->Update, which is done).
// - a build never loads a grid itself (BotMovement::ScopedDeferGridLoads):
// a grid load adds creatures and navmesh tiles under the other parts'
// feet. The world thread loads those grids right after the barrier.
auto build_chunk =
[&](std::vector<size_t> const& slots, size_t start, size_t end, bool same_map_split)
{
std::optional<MMAP::ScopedThreadNavMeshQuery> own_query;
std::optional<BotMovement::ScopedDeferGridLoads> no_grid_loads;
if (same_map_split)
{
own_query.emplace();
no_grid_loads.emplace();
}
const uint32 part_t0 = getMSTime();
for (size_t i = start; i < end; ++i)
{
BuiltSlot& bs = build_order[slots[i]];
// Version is pre-assigned by slot index (unique + monotonic).
const SnapshotVer ver = version_base + static_cast<SnapshotVer>(slots[i]);
// Per-bot microsecond timing (steady_clock: getMSTime's 1 ms
// grain reads 0 for most builds) for the TickPerf slowest-build
// attribution after the barrier. Slot-local, so race-free.
const auto bot_t0 = std::chrono::steady_clock::now();
bs.snap = BotSnapshotBuilder::Build(bs.player, bs.ai, ver, tick);
bs.build_us = static_cast<uint32>(std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - bot_t0).count());
}
par_build_ms_acc.fetch_add(
getMSTimeDiff(part_t0, getMSTime()), std::memory_order_relaxed);
};
// Build parts: one per Map*, and with PlayerbotV2.SnapshotBuildSplitMap
// an open-world map with enough due bots becomes several parts. Only
// open-world maps (MAP_COMMON) are split: an instance's bots share
// instance-wide script / coordinator state, and an instance rarely holds
// more than one group anyway. Inside a split map the part is chosen by
// the bot's split_key (its group, or the bot alone), so a group never
// builds on two threads and a bot keeps landing in the same part while
// the part count stays the same (warm thread_local LoS / recycle pools).
struct BuildPart
{
void const* map; // sticky lane key
uint32 sub; // part index inside a split map
std::vector<size_t> const* slots;
bool split;
};
std::vector<BuildPart> parts;
std::vector<std::vector<size_t>> split_slots; // slot lists of the split maps' parts
if (want_pool && snapshot_build_pool_)
{
constexpr size_t kMinBotsPerSplitPart = 8; // below this the hand-off costs more than it saves
const size_t threads = snapshot_build_pool_->worker_count() + 1; // the world thread builds too
auto part_count = [&](Map const* m, size_t due) -> size_t
{
if (!split_on || !m || !m->IsWorldMap())
return 1;
return std::clamp<size_t>(due / kMinBotsPerSplitPart, 1, threads);
};
// split_slots must not reallocate once parts point into it.
size_t split_total = 0;
for (auto const& [m, slots] : by_map)
if (size_t const n = part_count(m, slots.size()); n > 1)
split_total += n;
split_slots.reserve(split_total);
for (auto const& [m, slots] : by_map)
{
if (slots.empty())
continue;
size_t const n = part_count(m, slots.size());
if (n <= 1)
{
parts.push_back({ m, 0, &slots, false });
continue;
}
size_t const first = split_slots.size();
split_slots.resize(first + n);
for (size_t slot : slots)
{
// splitmix64 finaliser: bot ids and group counters are
// sequential, a plain modulo would stripe them.
uint64 h = split_key[slot] + 0x9E3779B97F4A7C15ull;
h = (h ^ (h >> 30)) * 0xBF58476D1CE4E5B9ull;
h = (h ^ (h >> 27)) * 0x94D049BB133111EBull;
h ^= h >> 31;
split_slots[first + size_t(h % n)].push_back(slot);
}
for (size_t k = 0; k < n; ++k)
if (!split_slots[first + k].empty())
parts.push_back({ m, uint32(k), &split_slots[first + k], true });
}
}
const bool want_parallel = parts.size() > 1;
if (want_parallel)
{
// Fan out ONE task per part on that map's STABLE sticky lane
// (map_worker_slot_, plus the part index for a split map) so the
// builder's thread_local recycle/LoS pools stay warm via ping-pong
// reuse. An unsplit map's shared dtNavMeshQuery is only ever touched
// by one thread per batch; a split map's parts each use their own
// thread's query (see the build_chunk note above). Never split a map
// without same_map_split = true (concurrent findPath on the shared
// per-instance query → freeze).
std::vector<Playerbot::SnapshotBuildPool::LaneTask> tasks;
tasks.reserve(parts.size());
size_t split_parts = 0;
size_t split_bots = 0;
for (BuildPart const& part : parts)
{
// Sticky lane for this Map*. New maps get the next round-robin
// slot; despawned maps leave a harmless stale key.
uint32 lane;
auto it = map_worker_slot_.find(part.map);
if (it != map_worker_slot_.end())
lane = it->second;
else
{
lane = next_worker_slot_++;
map_worker_slot_.emplace(part.map, lane);
}
if (part.split)
{
++split_parts;
split_bots += part.slots->size();
}
// Capture a POINTER to the slot list (by_map element or
// split_slots entry, both outlive run_and_wait and are not
// mutated during the batch), never a reference to a loop binding.
tasks.push_back({ lane + part.sub,
[&build_chunk, sp = part.slots, split = part.split]() {
build_chunk(*sp, 0, sp->size(), split);
} });
}
snapshot_build_pool_->run_and_wait(tasks);
if (split_parts > 0)
{
// Grids the split builds wanted but could not load themselves.
// World thread, every part done: nothing else reads the maps now.
g_split_stats.ticks += 1;
g_split_stats.parts += split_parts;
g_split_stats.bots += split_bots;
g_split_stats.deferred_grids += ::Playerbot::BotMovement::LoadDeferredGrids();
}
}
else
{
// Serial fallback (kill-switch off, or <=1 partition): build every
// partition inline on the world thread. Identical behavior + same
// thread_local pools (the world thread's) as the pre-Phase-4 path.
for (auto& [m, slots] : by_map)
build_chunk(slots, 0, slots.size(), false);
}
t_snap_build_ms += par_build_ms_acc.load(std::memory_order_relaxed);
// The build-phase wall time minus accounted per-partition build time
// is fan-out/barrier overhead; fold it into setup so the phase sums
// still reconcile against total. (Cheap; diagnostics only.)
const uint32 build_phase_ms = getMSTimeDiff(build_phase_t0, getMSTime());
const uint32 acc = par_build_ms_acc.load(std::memory_order_relaxed);
if (build_phase_ms > acc)
t_snap_setup_ms += (build_phase_ms - acc);
}
// ---- (D) POST-BARRIER WORLD-THREAD PASS (registry order) ------------
// Tier classify + set_tier, group dedup/build + publish_group, per-bot
// publish, perf. Every item here is must-stay-world-thread per the audit.
{
const uint32 publish_t0 = getMSTime();
// Group-build sub-span is accumulated separately into t_group_build_ms
// below; track it here too so it can be subtracted out of the publish
// span when folding into t_snap_build_ms (keeps the TickPerf
// build/group sub-buckets non-overlapping, as in the serial path).
const uint32 group_ms_before = t_group_build_ms;
// Companion tank threat multiplier (UpdateTankThreatMultiplier); 1.0 disables.
TankThreatSettings threat_cfg;
threat_cfg.tank = std::clamp(float(sConfigMgr->GetFloatDefault("Playerbot.TankThreatMultiplier", 3.0f, true)), 1.0f, 10.0f);
threat_cfg.raid_tank = std::clamp(float(sConfigMgr->GetFloatDefault("Playerbot.Raid.TankThreatMultiplier", 5.0f, true)), 1.0f, 10.0f);
threat_cfg.dps = std::clamp(float(sConfigMgr->GetFloatDefault("Playerbot.DpsThreatMultiplier", 1.0f, true)), 0.3f, 1.5f);
threat_cfg.sure_taunt = sConfigMgr->GetBoolDefault("Playerbot.Raid.SureTaunt", true, true);
threat_cfg.scenario_as_owned = sConfigMgr->GetBoolDefault("Playerbot.Scenario.OwnerThreat", true, true);
ConfigReader::CastPacing const cast_pacing_cfg = Services::Config().cast_pacing();
uint32 const pvp_floor_ms = Services::Config().pacing().pvp_reaction_delay_ms;
bool const wake_on_gcd = sConfigMgr->GetBoolDefault("Playerbot.Reaction.WakeOnGcd", true, true);
for (BuiltSlot& bs : build_order)
{
BotId id = bs.bot_id;
Player* p = bs.player;
auto& snap = bs.snap;
if (!snap)
continue; // Build returned {} (null/!IsInWorld) — nothing to publish.
++snaps_built_this_tick;
if (p && bs.build_us > perf_slowest_build_us_)
{
perf_slowest_build_us_ = bs.build_us;
perf_slowest_build_bot_ = p->GetName();
}
// Promote/demote the scheduler tier from the freshly built
// snapshot. The world thread is the single writer to the
// scheduler, so this is safe without synchronization.
//
// Tier 2.1 classifier. The OLD code pinned EVERY alive OOC bot to
// Active (150 ms), so the snapshot-build throttle was effectively
// dead (build_rate ~100%, snaps_skipped=0). The classifier below
// keeps every bot that needs prompt reactivity on a fast cadence,
// and lets the genuinely-AFK long tail fall to Idle → Parked:
//
// dead -> Idle (500 ms)
// in combat -> Combat (~100 ms)
// owner-controlled / grouped-with-real- -> Active (150 ms)
// player / near-real-player / (responsiveness-
// path-blocked / casting critical set)
// else, solo open-world, moving OR -> Cruise (300 ms)
// has-objective (dominant long-haul
// travel/quest pop;
// never ramps to Parked)
// alive, OOC, stationary, solo, no real -> Idle (500 ms)
// player nearby, no objective → ramps to Parked
// (Hibernate, 2 s)
// after N idle frames
// (handled in set_tier)
//
// Cruise (#5 b1) splits the old all-or-nothing Active gate: the
// OLD code pinned every (moving OR has_objective) bot to Active
// (150 ms), so a 230-bot questing fleet ran build_rate ~93%. Solo
// travellers now build at half rate (300 ms) while the latency-
// sensitive set above is untouched.
//
// Every input is an ALREADY-POPULATED snapshot field plus the
// O(1) per-tick real-player set lookups built above — no new
// per-bot grid search or builder cost. set_tier resets
// next_snapshot=now on any tier change, so a bot that re-enters
// combat / gets controlled / starts moving / has a real player
// walk up rebuilds on the very next frame (no reactivity loss).
ActivityTier tier;
if (!snap->vitals.is_alive)
{
tier = ActivityTier::Idle;
}
else if (snap->vitals.in_combat)
{
tier = ActivityTier::Combat;
}
else
{
// "Owner-controlled" proxy: the bot is bound to an owner
// (owner_name populated by the builder). Owned bots take
// squad/whisper commands and follow the owner, so they must
// stay responsive even while standing still.
const bool owner_controlled = !snap->owner_name.empty();
const bool grouped_with_real =
!snap->group.group_guid.IsEmpty() &&
real_player_groups.find(snap->group.group_guid) != real_player_groups.end();
const bool near_real_player =
real_player_cells.find(PackPlayerCellKey(
snap->position.map_id,
PlayerCellCoord(snap->position.x),
PlayerCellCoord(snap->position.y))) != real_player_cells.end();
const bool moving =
snap->movement.is_moving || snap->movement.is_swimming;
// ACTIVE INTENT — a bot that wants to act but is momentarily
// stationary must NOT be parked. is_moving alone mis-classified
// these as idle (2026-06-01 regression): a path-BLOCKED bot
// (stuck — its unstick recovery ladder must run at full cadence),
// a bot chasing an unreached quest OBJECTIVE, and a bot mid-CAST
// all have is_moving=false. Parking them starved the unstick
// counters (they accrue inside the tier-gated snapshot build) and
// froze them — the worker skips stale parked snapshots, so a
// half-finished move stalled and a cast never completed (the
// "kneel when idle" + "stuck casting" + worsened back-and-forth
// the owner observed). Only a truly objective-less, unblocked,
// non-casting idle AFK bot (the L80 city-filler tail) still falls
// through to Idle->Parked, preserving the build-throttle win.
const bool path_blocked = snap->path_telemetry.count > 0;
const bool has_objective = snap->quest_log.current_quest_id != 0;
const bool casting = snap->cast.is_casting;
// INSTANCE GROUP RUN — a bot doing coordinated 5-man/raid content
// must react at full cadence even while momentarily stationary
// (a tank holding between pulls, a DPS waiting on a cohesion gate).
// The tank-advance / cohesion rules read the SHARED GroupSnapshot,
// which is only re-published to THIS bot on the ticks it is
// processed. A parked dungeon tank demoted to Idle/Parked sees a
// FROZEN group view — a healer that already ran up still reads
// "healer_far 52y", a member whose combat tag already dropped still
// reads "in combat" — so the advance gate blocks, the tank stays
// parked, and the stale view never refreshes: a self-reinforcing
// wedge (live 2026-06-28: pure-bot Deadmines squad stuck at the
// entrance with all 5 stacked + out of combat while the gate read a
// phantom far-healer / in-combat). Mirrors grouped_with_real, for
// bot-only instance groups. Instances are bounded so the cost is
// negligible. Open-world groups are NOT included (is_in_instance).
const bool instance_group_run =
!snap->group.group_guid.IsEmpty() &&
snap->instance_ctx.is_in_instance;
// RESPONSIVENESS-CRITICAL set keeps the full 150 ms Active
// cadence:
// owner_controlled / grouped_with_real / near_real_player —
// human-facing: a real player is watching or depending on
// this bot, so it must react at full speed.
// path_blocked — the unstick recovery ladder accrues its
// counters inside the tier-gated snapshot build and must
// run at full cadence or the bot wedges (see the 2026-06-01
// regression note above).
// casting — a slower cadence can let a cast stall / never
// complete ("stuck casting" regression); keep it Active so
// casts finish.
// (in_combat already routed to Combat above; dead to Idle.)
const bool responsiveness_critical =
owner_controlled || grouped_with_real || near_real_player ||
path_blocked || casting || instance_group_run;
if (responsiveness_critical)
{
tier = ActivityTier::Active;
}
else if (moving || has_objective)
{
// CRUISE (300 ms) — the dominant long-haul population: a
// solo, non-human-facing bot that is merely travelling or
// questing in the open world. Its movement spline runs
// server-side; the bot logic only needs to issue the next waypoint
// a touch less often, so half-rate snapshot builds are
// tolerable. NOT path_blocked and NOT casting (those are
// Active above), so no unstick/cast starvation. Cruise never
// ramps to Parked (set_tier resets the idle streak), so the
// bot can't freeze mid-travel; a block / combat / nearby
// real player promotes it to Active/Combat next frame.
tier = ActivityTier::Cruise;
}
else
{
tier = ActivityTier::Idle; // set_tier ramps to Parked after N
}
}
sch.set_tier(id, tier, total);
NoteReaction(id, *snap, total, wake_on_gcd);
// Build a fresh group snapshot when the bot is in a group; clear
// the slot otherwise so a former groupmate doesn't see stale data.
// Dedup: reuse a cached snapshot keyed by the group's GUID (see
// group_snap_cache_ above — at most kGroupSnapMaxAgeMs old, same
// member count + leader). Multi-bot groups (parties, raids, full
// BG raids) thereby pay the heavy GroupSnapshotBuilder::Build once
// per window instead of once per bot. Solo / no-group bots get a
// null publish. World-thread-only (RULE 5): the cache is a shared
// cross-bot view and GroupSnapshotBuilder walks live group members.
const uint32 group_t0 = getMSTime();
if (Group const* g = p ? p->GetGroup() : nullptr)
{
ObjectGuid const ggid = g->GetGUID();
auto cache_it = group_snap_cache_.find(ggid);
if (cache_it != group_snap_cache_.end() &&
cache_it->second.snap->members.size() == g->GetMembersCount() &&
cache_it->second.snap->leader == g->GetLeaderGUID())
{
// Cache hit — reuse the shared_ptr. publish_group takes
// a const shared_ptr by value so the refcount bumps and
// both bots end up pointing at the same snapshot.
pub.publish_group(id, cache_it->second.snap);
}
else
{
auto gsnap = GroupSnapshotBuilder::Build(p, snap->version);
if (gsnap)
{
group_snap_cache_[ggid] = GroupSnapCacheEntry{ gsnap, getMSTime() };
pub.publish_group(id, gsnap);
}
else
{
pub.publish_group(id, {});
}
}
}
else
{
pub.publish_group(id, {});
}
t_group_build_ms += getMSTimeDiff(group_t0, getMSTime());
UpdateTankThreatMultiplier(id, p, bs.ai, *snap, threat_cfg);
UpdateCastSpeed(p, *snap, cast_pacing_cfg);
UpdatePvpFloor(id, p, *snap, pvp_floor_ms, total);
pub.publish(id, std::move(snap));
Services::Perf().record_snapshot_publish();
}
// Publish/classify time is world-thread post-barrier cost; fold into
// the build sub-bucket so the existing TickPerf line still attributes
// it to the snapshot phase. Subtract the group sub-span (already in
// t_group_build_ms) so build/group stay non-overlapping.
FlushReactionTiming();
const uint32 publish_span = getMSTimeDiff(publish_t0, getMSTime());
const uint32 group_span = t_group_build_ms - group_ms_before;
t_snap_build_ms += (publish_span > group_span) ? (publish_span - group_span) : 0u;
}
t_snap_ms = getMSTimeDiff(t_snap_start_ms, getMSTime());
// 1.5. Stagger-fire any deferred BG ports + LFG proposal accepts whose
// delay has elapsed (see OnBGInvitationReceived /
// OnLfgProposalReceived). MUST run before DrainIntents so the
// freshly-pushed intents fire this same tick.
{
const uint32 t0 = getMSTime();
FireDueBgPorts();
FireDueLfgAccepts();
TopUpPendingLfg(getMSTime());
Playerbot::V2::LfgTick(getMSTime());
Playerbot::V2::LfgBotGroupsTick(getMSTime());
Playerbot::V2::DungeonFinderTick(getMSTime());
t_bgport_ms = getMSTimeDiff(t0, getMSTime());
}
// 2. Drain pending intents and execute them via PlayerbotAPI on this
// (world) thread. The BotIntentExecutor file owns the variant visitor.
{
const uint32 t0 = getMSTime();
intents_drained_this_tick = DrainIntents();
t_drain_ms = getMSTimeDiff(t0, getMSTime());
}
// 2.5. Tick headless bot sessions so async login callbacks land and
// WorldSession query holders progress. Sessions are NOT in
// sWorld->m_sessions (would kick the GM's session sharing the
// account), so we drive their per-tick Update ourselves.
{
const uint32 t0 = getMSTime();
Services::SessionMgr().Update(static_cast<uint32>(diff.count()));
t_session_ms = getMSTimeDiff(t0, getMSTime());
}
// 2.6. Population shaper tick (Phase A of WORLD_POPULATION_PLAN).
// Internally rate-limits to its tick interval (default 60s) so
// this is cheap to call every world frame.
{
const uint32 t0 = getMSTime();
Services::Population().OnWorldTick(static_cast<uint32>(total.count()));
t_population_ms = getMSTimeDiff(t0, getMSTime());
}
// 2.7. Pets that cannot walk to their bot owner are set down next to it.
DrivePetSnap(getMSTime());
// 2.8. Bots board their player's flight, ship or zeppelin, and a bot with no way to him is set down beside him (Fleet/OwnerCatchUp.h).
DriveFlyWithOwner(getMSTime());
DriveRideWithOwner(getMSTime());
DriveOwnerCatchUp(getMSTime());
DriveOwnerTrail(getMSTime());
// 2b. Bot guild manager tick (GUILD_PLAN.md Phase A.2). Internally
// 60s rate-limited; cheap to call every frame. Elects founders
// when guild slots are open + sweeps stale name reservations
// + aborts founders that exceeded the 30-min FSM budget.
Services::Guilds().Tick(getMSTime());
// 2c. Craft-order board tick (#4B-2). Ages out stale Claimed orders
// (timeout -> Fail + refund so escrow can't be trapped behind a stuck
// crafter) and prunes finished rows. Self-rate-limited cheaply via a
// local accumulator — the board only needs minute-granularity since
// the claim timeout is 30 min.
craft_board_total_ += diff;
if (craft_board_total_ - last_craft_board_at_ >= kCraftBoardIntervalMs)
{
last_craft_board_at_ = craft_board_total_;
if (Services::Initialized())
Services::CraftOrders().Tick(GameTime::GetGameTimeMS());
}
// Owner craft orders ("craft <item> x5" whispered to a bot): one pass a second.
player_craft_total_ += diff;
if (player_craft_total_ - last_player_craft_at_ >= std::chrono::milliseconds(1000))
{
last_player_craft_at_ = player_craft_total_;
if (Services::Initialized())
{
::Playerbot::V2::PlayerCraftOrders::Tick(GameTime::GetGameTimeMS());
BotCommandParser::TickVendorRuns(GameTime::GetGameTimeMS()); // "sell junk": sells on arrival
}
}
// 3. Schedule tick for due bots.
{
const uint32 t0 = getMSTime();
Services::Scheduler().on_world_tick(total);
t_scheduler_ms = getMSTimeDiff(t0, getMSTime());
}
// 3b. Runtime wedge watchdog (#1B). Slow cadence (kWedgeWatchdogIntervalMs)
// because it only reads cheap per-bot fields + the eventually-consistent
// snapshot. Walks the registry, classifies each in-world bot's wedge
// state, emits ONE structured [wedge] line per stuck episode, records
// the category in PerfCounters, and rebuilds the active-wedge list the
// `.playerbot wedges` digest reads. World-thread only — same lifecycle
// slot as GlobalStuckRescue + the fleet log.
wedge_wd_total_ += diff;
if (wedge_wd_total_ - last_wedge_wd_at_ >= kWedgeWatchdogIntervalMs)
{
last_wedge_wd_at_ = wedge_wd_total_;
wedge_watchdog_.Tick(GameTime::GetGameTimeMS());
}
// 3c. Group-level no-progress DIAGNOSE watchdog (nav-robustness program).
// READ-ONLY: classifies a wedged dungeon group (stranded / split /
// cohered-idle / false-combat) and emits one [group_wedge] line — the
// "diagnose" stage the remediation pass is built on. Self-throttled, so
// it's safe to call every world tick.
group_wedge_watchdog_.Update(GameTime::GetGameTimeMS());
// 4. Periodic fleet-status log (overnight friendly). Walks the registry +
// snapshots once per kFleetLogIntervalMs and emits one INFO line so the
// operator can see fleet health by tailing worldserver.log without
// needing a GM session. Cheap aggregation; capped to a single log
// line so it doesn't spam.
constexpr std::chrono::milliseconds kFleetLogIntervalMs{60 * 1000}; // 60s — gives readable TickPerf data without log spam
fleet_log_total_ += diff;
if (fleet_log_total_ - last_fleet_log_at_ >= kFleetLogIntervalMs)
{
last_fleet_log_at_ = fleet_log_total_;
uint32 in_world = 0, alive = 0, in_combat = 0, with_quest = 0, stuck = 0;
uint32 levels_total = 0, level_max = 0;
uint32 quests_total = 0;
const uint32 now_ms = GameTime::GetGameTimeMS();
// Last rule each bot fired, counted per rule (raid rules excluded): shows which of AH, mail, hub, travel and quest rules
// the world population is actually running. last_rule_fired() points at a string literal, so the pointer is the key.
std::unordered_map<std::string_view, uint32> rule_census;
Services::Registry().for_each([&](BotId id, BotRegistryEntry const& e)
{
if (!e.ai) return;
++in_world;
if (char const* rule = e.ai->last_rule_fired(); rule && std::string_view(rule).rfind("raid", 0) != 0)
++rule_census[rule];
if (auto snap = Services::Snapshots().latest(id))
{
if (snap->vitals.is_alive) ++alive;
if (snap->vitals.in_combat) ++in_combat;
if (snap->identity.level > level_max) level_max = snap->identity.level;
levels_total += snap->identity.level;
if (snap->quest_log.current_quest_id != 0) ++with_quest;
quests_total += static_cast<uint32>(snap->quest_log.quests.size());
}
// Currently blacklisted = deadline in future. Bots whose timer
// has expired aren't actively stuck (they're allowed to retry).
const uint32 dead = e.ai->objective_track().blacklisted_until_ms;
if (dead != 0 && dead > now_ms) ++stuck;
});
{
std::vector<std::pair<std::string_view, uint32>> ranked(rule_census.begin(), rule_census.end());
std::sort(ranked.begin(), ranked.end(), [](auto const& a, auto const& b) { return a.second != b.second ? a.second > b.second : a.first < b.first; });
std::string line;
for (size_t i = 0; i < ranked.size() && i < 40; ++i)
line += fmt::format("{}{}={}", i ? ", " : "", ranked[i].first, ranked[i].second);
TC_LOG_INFO("playerbot.v2", "[rule_census] bots={} rules={} | {}", in_world, ranked.size(), line);
}
const uint32 avg_level = in_world > 0 ? levels_total / in_world : 0;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] FleetStatus: in_world={} alive={} in_combat={} avg_lvl={} max_lvl={} "
"with_current_quest={} quests_in_log={} stuck_bots={} marked={}",
in_world, alive, in_combat, avg_level, level_max,
with_quest, quests_total, stuck, Services::Lifecycle().size());
// #1C fleet-vitals sample (rolling window + persistence + alerting).
// Runs on the SAME 60s cadence; reuses the census just aggregated so
// the registry isn't walked twice. avg_level passed x100 fixed-point.
const uint32 avg_level_x100 = in_world > 0
? static_cast<uint32>((uint64(levels_total) * 100ull) / in_world)
: 0u;
SampleFleetVitals(total, now_ms, in_world, alive, in_combat, avg_level_x100);
if (sConfigMgr->GetBoolDefault("Playerbot.LoadTest.Enable", false, true))
{
uint32 raid = 0, bg = 0;
Services::Registry().for_each([&](BotId id, BotRegistryEntry const& e)
{
if (!e.ai) return;
if (Player const* bp = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(id)))
{
if (bp->InBattleground()) ++bg;
else if (Group const* g = bp->GetGroup(); g && g->isRaidGroup()) ++raid;
}
});
EmitLoadTestRow(in_world, raid, bg);
}
loadtest_hist_.Reset();
// Per-phase tick-cost averages + worst-case latency for the window.
// Sum-of-phases vs total reveals unaccounted-for cost (TC core,
// script callbacks, other modules). Aim: total avg < 100ms, max
// < 200ms at 2000-bot scale. Reset window after emit.
if (perf_ticks_in_window_ > 0)
{
const uint32 n = perf_ticks_in_window_;
const uint32 sum_phases =
perf_snap_ms_total_ + perf_bgport_ms_total_ +
perf_drain_ms_total_ + perf_session_ms_total_ +
perf_pop_ms_total_ + perf_sched_ms_total_;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] TickPerf (avg ms over {} ticks): "
"snap={:.2f} (setup={:.2f} build={:.2f} group={:.2f}) "
"bgport={:.2f} drain={:.2f} session={:.2f} "
"pop={:.2f} sched={:.2f} | phases_sum={:.2f} total={:.2f} "
"max={}ms",
n,
double(perf_snap_ms_total_) / n,
double(perf_snap_setup_ms_total_) / n,
double(perf_snap_build_ms_total_) / n,
double(perf_group_build_ms_total_) / n,
double(perf_bgport_ms_total_) / n,
double(perf_drain_ms_total_) / n,
double(perf_session_ms_total_) / n,
double(perf_pop_ms_total_) / n,
double(perf_sched_ms_total_) / n,
double(sum_phases) / n,
double(perf_total_ms_total_) / n,
perf_total_ms_max_);
// Throughput counters — exposes "how much work happened" vs the
// ms timings above which show "how long it took". A high
// snap_built rate at a low snap_build_ms means Builder is fast;
// a low snap_built rate at a high snap_build_ms means a few
// bots are dominating cost. Same for intents.
const uint32 snap_total = perf_snap_built_count_ + perf_snap_skipped_count_;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] TickPerf throughput: snaps_built={} snaps_skipped={} "
"(build_rate={:.1f}%) intents_drained={} ({:.1f}/tick)",
perf_snap_built_count_, perf_snap_skipped_count_,
snap_total > 0 ? 100.0 * double(perf_snap_built_count_) / double(snap_total) : 0.0,
perf_intents_drained_,
double(perf_intents_drained_) / double(n));
// Same-map split (PlayerbotV2.SnapshotBuildSplitMap). With it on,
// "build" above is the time summed over all build threads, so it can
// exceed "snap", which stays the world thread's wall time.
if (g_split_stats.ticks > 0)
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] TickPerf same_map_split: ticks={} parts_per_tick={:.1f} bots_per_part={:.1f} deferred_grids={}",
g_split_stats.ticks,
double(g_split_stats.parts) / double(g_split_stats.ticks),
g_split_stats.parts > 0 ? double(g_split_stats.bots) / double(g_split_stats.parts) : 0.0,
g_split_stats.deferred_grids);
g_split_stats = {};
if (g_bot_logic_grid_loads > 0)
TC_LOG_INFO("playerbot.v2", "[PlayerbotV2] TickPerf bot_logic_grid_loads={} (asked by rule probes, loaded on the world thread)",
g_bot_logic_grid_loads);
g_bot_logic_grid_loads = 0;
// Spike attribution. The averages above hide a once-per-period
// spike, so name it: the phase breakdown of the single worst tick,
// each phase's own worst case, and the slowest single bot build /
// drained intent batch of the window.
{
WorstTickBreakdown const& w = perf_worst_tick_;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] TickPerf max (ms): total={} worst_tick[snap={} (setup={} build={} group={}) "
"bgport={} drain={} session={} pop={} sched={} other={} built={} drained={}] "
"phase_max[snap={} (setup={} build={} group={}) bgport={} drain={} session={} "
"pop={} sched={} other={}]",
perf_total_ms_max_,
w.snap, w.setup, w.build, w.group, w.bgport, w.drain, w.session, w.pop, w.sched,
w.other, w.snaps_built, w.intents_drained,
perf_snap_ms_max_, perf_snap_setup_ms_max_, perf_snap_build_ms_max_,
perf_group_build_ms_max_, perf_bgport_ms_max_, perf_drain_ms_max_,
perf_session_ms_max_, perf_pop_ms_max_, perf_sched_ms_max_, perf_other_ms_max_);
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] TickPerf slowest: build={} {:.2f}ms | drain_batch={} {:.2f}ms "
"intents={} | intent={} {:.2f}ms",
perf_slowest_build_bot_.empty() ? "-" : perf_slowest_build_bot_.c_str(),
double(perf_slowest_build_us_) / 1000.0,
perf_slowest_drain_bot_.empty() ? "-" : perf_slowest_drain_bot_.c_str(),
double(perf_slowest_drain_us_) / 1000.0,
perf_slowest_drain_count_,
(perf_slowest_intent_kind_ && *perf_slowest_intent_kind_) ? perf_slowest_intent_kind_ : "-",
double(perf_slowest_intent_us_) / 1000.0);
}
// BG advice cache hit rate. Sustained hit-rate < 90% means a
// script's snapshot consumption isn't in the cache key and
// advice is rebuilding too often. Total grows with BG-bot
// count × ticks, so high totals are expected at scale; the
// RATE is the signal.
auto perf_snap_diag = Playerbot::Services::Initialized()
? Playerbot::Services::Perf().snapshot()
: Playerbot::PerfCounters::Snapshot{};
uint64_t adv_hits = perf_snap_diag.bg_advice_cache_hits_total;
uint64_t adv_misses = perf_snap_diag.bg_advice_cache_misses_total;
uint64_t adv_total = adv_hits + adv_misses;
TC_LOG_INFO("playerbot.v2",
"[PlayerbotV2] BgAdviceCache: hits={} misses={} (hit_rate={:.1f}%)",
adv_hits, adv_misses,
adv_total > 0 ? 100.0 * double(adv_hits) / double(adv_total) : 0.0);
perf_ticks_in_window_ = 0;
perf_snap_ms_total_ = 0;
perf_snap_setup_ms_total_ = 0;
perf_snap_build_ms_total_ = 0;
perf_group_build_ms_total_ = 0;
perf_snap_built_count_ = 0;
perf_snap_skipped_count_ = 0;
perf_intents_drained_ = 0;
perf_bgport_ms_total_ = 0;
perf_drain_ms_total_ = 0;
perf_session_ms_total_ = 0;
perf_pop_ms_total_ = 0;
perf_sched_ms_total_ = 0;
perf_total_ms_total_ = 0;
perf_total_ms_max_ = 0;
perf_snap_ms_max_ = 0;
perf_snap_setup_ms_max_ = 0;
perf_snap_build_ms_max_ = 0;
perf_group_build_ms_max_ = 0;
perf_bgport_ms_max_ = 0;
perf_drain_ms_max_ = 0;
perf_session_ms_max_ = 0;
perf_pop_ms_max_ = 0;
perf_sched_ms_max_ = 0;
perf_other_ms_max_ = 0;
perf_worst_tick_ = WorstTickBreakdown{};
perf_slowest_build_us_ = 0;
perf_slowest_build_bot_.clear();
perf_slowest_drain_us_ = 0;
perf_slowest_drain_count_ = 0;
perf_slowest_drain_bot_.clear();
perf_slowest_intent_us_ = 0;
perf_slowest_intent_kind_ = "";
}
}
// Record world-update wall-clock latency for diagnostics.
const uint32 elapsed = getMSTimeDiff(tick_start_ms, getMSTime());
Services::Perf().record_world_update_latency(std::chrono::milliseconds{elapsed});
// Accumulate per-phase costs for the next FleetStatus emit window.
++perf_ticks_in_window_;
perf_snap_ms_total_ += t_snap_ms;
perf_snap_setup_ms_total_ += t_snap_setup_ms;
perf_snap_build_ms_total_ += t_snap_build_ms;
perf_group_build_ms_total_ += t_group_build_ms;
perf_snap_built_count_ += snaps_built_this_tick;
perf_snap_skipped_count_ += snaps_skipped_this_tick;
perf_intents_drained_ += static_cast<uint32>(intents_drained_this_tick);
perf_bgport_ms_total_ += t_bgport_ms;
perf_drain_ms_total_ += t_drain_ms;
perf_session_ms_total_ += t_session_ms;
perf_pop_ms_total_ += t_population_ms;
perf_sched_ms_total_ += t_scheduler_ms;
perf_total_ms_total_ += elapsed;
loadtest_hist_.Add(elapsed);
// Spike attribution: per-phase worst case + the worst tick's breakdown.
const uint32 phases_ms = t_snap_ms + t_bgport_ms + t_drain_ms + t_session_ms +
t_population_ms + t_scheduler_ms;
const uint32 other_ms = elapsed > phases_ms ? elapsed - phases_ms : 0u;
perf_snap_ms_max_ = std::max(perf_snap_ms_max_, t_snap_ms);
perf_snap_setup_ms_max_ = std::max(perf_snap_setup_ms_max_, t_snap_setup_ms);
perf_snap_build_ms_max_ = std::max(perf_snap_build_ms_max_, t_snap_build_ms);
perf_group_build_ms_max_ = std::max(perf_group_build_ms_max_, t_group_build_ms);
perf_bgport_ms_max_ = std::max(perf_bgport_ms_max_, t_bgport_ms);
perf_drain_ms_max_ = std::max(perf_drain_ms_max_, t_drain_ms);
perf_session_ms_max_ = std::max(perf_session_ms_max_, t_session_ms);
perf_pop_ms_max_ = std::max(perf_pop_ms_max_, t_population_ms);
perf_sched_ms_max_ = std::max(perf_sched_ms_max_, t_scheduler_ms);
perf_other_ms_max_ = std::max(perf_other_ms_max_, other_ms);
if (elapsed > perf_total_ms_max_)
{
perf_total_ms_max_ = elapsed;
WorstTickBreakdown& w = perf_worst_tick_;
w.snap = t_snap_ms; w.setup = t_snap_setup_ms;
w.build = t_snap_build_ms; w.group = t_group_build_ms;
w.bgport = t_bgport_ms; w.drain = t_drain_ms;
w.session = t_session_ms; w.pop = t_population_ms;
w.sched = t_scheduler_ms; w.other = other_ms;
w.snaps_built = snaps_built_this_tick;
w.intents_drained = static_cast<uint32>(intents_drained_this_tick);
}
}
// One load-test CSV row for the minute just ended (Playerbot.LoadTest.Enable). Appends to Playerbot.LoadTest.CsvPath
// (default playerbot_loadtest.csv beside the server), writing the header first when the file is new. The row carries its own
// PASS/FAIL against Playerbot.LoadTest.Max* so one glance at the last column says whether the build holds the scenario.
void V2::Module::EmitLoadTestRow(uint32 in_world, uint32 raid, uint32 bg)
{
if (!loadtest_hist_.Count())
return;
Diagnostics::LoadRow row;
row.unix_time = uint64(GameTime::GetGameTime());
row.build = GitRevision::GetHash();
row.label = sConfigMgr->GetStringDefault("Playerbot.LoadTest.Label", "", true);
row.bots_awake = in_world;
row.bots_raid = raid;
row.bots_bg = bg;
Fleet::WorldLoadFacts const wf = Services::Population().WorldLoad();
row.bots_world = wf.awake;
row.bots_sleeping = wf.sleeping;
row.ticks = loadtest_hist_.Count();
row.tick_avg_ms = loadtest_hist_.Avg();
row.tick_p50_ms = loadtest_hist_.Percentile(50);
row.tick_p99_ms = loadtest_hist_.Percentile(99);
row.tick_max_ms = loadtest_hist_.Max();
SlowCallWorst const worst = SlowCallWorst::Instance().Take();
row.slowest_call = worst.name ? worst.name : "";
row.slowest_call_ms = worst.ms;
row.rss_mb = wf.rss_mb;
row.cpu_pct = wf.cpu_pct;
Diagnostics::LoadLimits limits;
limits.max_p99_ms = uint32(sConfigMgr->GetIntDefault("Playerbot.LoadTest.MaxP99Ms", int(limits.max_p99_ms), true));
limits.max_avg_ms = double(sConfigMgr->GetIntDefault("Playerbot.LoadTest.MaxAvgMs", int(limits.max_avg_ms), true));
limits.max_rss_mb = uint32(sConfigMgr->GetIntDefault("Playerbot.LoadTest.MaxRssMb", int(limits.max_rss_mb), true));
limits.max_slow_call_ms = uint32(sConfigMgr->GetIntDefault("Playerbot.LoadTest.MaxSlowCallMs", int(limits.max_slow_call_ms), true));
std::string const path = sConfigMgr->GetStringDefault("Playerbot.LoadTest.CsvPath", "playerbot_loadtest.csv", true);
bool const fresh = !std::filesystem::exists(path);
std::ofstream out(path, std::ios::app);
if (!out)
{
TC_LOG_ERROR("playerbot.v2", "[loadtest] cannot append to '{}'", path);
return;
}
if (fresh)
out << Diagnostics::CsvHeader() << '\n';
out << Diagnostics::CsvRow(row, limits) << '\n';
TC_LOG_INFO("playerbot.v2", "[loadtest] {}", Diagnostics::CsvRow(row, limits));
}
V2::Module::TickPerfSnapshot V2::Module::tickperf_snapshot() const
{
TickPerfSnapshot s{};
s.ticks_in_window = perf_ticks_in_window_;
s.snap_ms_total = perf_snap_ms_total_;
s.snap_setup_ms_total = perf_snap_setup_ms_total_;
s.snap_build_ms_total = perf_snap_build_ms_total_;
s.group_build_ms_total = perf_group_build_ms_total_;
s.snap_built_count = perf_snap_built_count_;
s.snap_skipped_count = perf_snap_skipped_count_;
s.intents_drained = perf_intents_drained_;
s.bgport_ms_total = perf_bgport_ms_total_;
s.drain_ms_total = perf_drain_ms_total_;
s.session_ms_total = perf_session_ms_total_;
s.pop_ms_total = perf_pop_ms_total_;
s.sched_ms_total = perf_sched_ms_total_;
s.total_ms_total = perf_total_ms_total_;
s.total_ms_max = perf_total_ms_max_;
return s;
}
// #1C fleet-vitals sampler. World-thread, 60s cadence (driven from the
// FleetStatus block). Builds one VitalsBucket from the supplied census + the
// live PerfCounters / wedge-watchdog state, pushes it into the in-memory
// rolling window, async-persists one row (survives restart -> real trend), and
// evaluates the config-driven alert thresholds with per-metric throttling.
void Module::SampleFleetVitals(std::chrono::milliseconds total, uint32 now_ms,
uint32 in_world, uint32 alive, uint32 in_combat,
uint32 avg_level_x100)
{
if (!Services::Initialized())
return;
PerfCounters& perf = Services::Perf();
PerfCounters::Snapshot const ps = perf.snapshot();
// ---- Derive per-window RATES from cumulative-counter deltas. ----
// Path-fail = NoPath + FarFromPolyStart + FarFromPolyEnd (PathOutcome
// indices 1/2/3; the array is fixed at 6 — guarded below).
uint64 path_fail_total = 0;
if (ps.path_outcomes.size() >= 4)
path_fail_total = ps.path_outcomes[1] + ps.path_outcomes[2] + ps.path_outcomes[3];
const uint64 intents_exec_total = ps.intents_executed_total;
const uint64 intents_drop_total = ps.intents_dropped_total;
// Elapsed wall-clock since the previous sample (ms). First sample has no
// baseline -> rates emit 0 and we just seed the cumulative anchors.
const uint64 elapsed_ms = vitals_sample_ready_
? static_cast<uint64>((total - last_vitals_sample_at_).count())
: 0ull;
uint32 path_fail_per_min = 0;
uint32 intents_per_sec = 0;
uint32 intents_dropped = 0; // delta over the window
uint32 intents_executed = 0; // delta over the window
if (vitals_sample_ready_ && elapsed_ms > 0)
{
const uint64 d_pathfail = path_fail_total >= last_path_fail_total_
? path_fail_total - last_path_fail_total_ : 0;
const uint64 d_exec = intents_exec_total >= last_intents_exec_total_
? intents_exec_total - last_intents_exec_total_ : 0;
const uint64 d_drop = intents_drop_total >= last_intents_drop_total_
? intents_drop_total - last_intents_drop_total_ : 0;
path_fail_per_min = static_cast<uint32>((d_pathfail * 60000ull) / elapsed_ms);
intents_per_sec = static_cast<uint32>((d_exec * 1000ull) / elapsed_ms);
intents_dropped = static_cast<uint32>(d_drop);
intents_executed = static_cast<uint32>(d_exec);
}
// Roll the cumulative anchors forward for the next sample.
last_path_fail_total_ = path_fail_total;
last_intents_exec_total_ = intents_exec_total;
last_intents_drop_total_ = intents_drop_total;
last_vitals_sample_at_ = total;
vitals_sample_ready_ = true;
// ---- Wedge census from the watchdog's most-recent active list. ----
PerfCounters::VitalsBucket b;
b.sample_at_ms = now_ms;
b.in_world = in_world;
b.alive = alive;
b.in_combat = in_combat;
{
auto const& active = wedge_watchdog_.active();
b.wedged = static_cast<uint32>(active.size());
for (auto const& wi : active)
{
const size_t ci = static_cast<size_t>(wi.cat);
if (ci < b.wedged_by_category.size())
++b.wedged_by_category[ci];
}
}
b.path_fail_per_min = path_fail_per_min;
b.intents_per_sec = intents_per_sec;
b.intents_dropped = intents_dropped;
b.intents_executed = intents_executed;
b.tick_p50_us = perf.tick_latency_percentile_us(0.50);
b.tick_p99_us = perf.tick_latency_percentile_us(0.99);
b.avg_level_x100 = avg_level_x100;
// ---- Push into the in-memory rolling window. ----
perf.push_vitals_bucket(b);
// ---- Persist one row (async; survives restart -> real trend). ----
// CharacterDatabase hosts the playerbot_v2_* schema (see
// PlayerbotMigrationMgr::Db()), so the table is referenced unqualified.
// Execute() is non-blocking; ordering doesn't matter for an append-only
// sample table. avg_level written as a float (x100 -> /100.0).
CharacterDatabase.Execute(fmt::format(
"INSERT INTO playerbot_v2_fleet_vitals_sample "
"(in_world, alive, in_combat, wedged, "
" wedged_navmesh, wedged_offmesh, wedged_travel, "
" wedged_combatloop, wedged_pickernone, wedged_goalunreach, "
" tick_p50_us, tick_p99_us, intents_per_sec, intents_dropped, "
" path_fail_per_min, avg_level) "
"VALUES ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {:.2f})",
b.in_world, b.alive, b.in_combat, b.wedged,
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::Navmesh)],
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::OffMesh)],
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::Travel)],
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::CombatLoop)],
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::PickerNone)],
b.wedged_by_category[static_cast<size_t>(Diagnostics::WedgeCategory::GoalUnreachable)],
b.tick_p50_us, b.tick_p99_us, b.intents_per_sec, b.intents_dropped,
b.path_fail_per_min, double(b.avg_level_x100) / 100.0).c_str());
// ---- Alerting: throttled [fleet_alert] on threshold breach. ----
// A threshold of 0 disables that metric. The first sample (no rate
// baseline) still alerts on the gauge metrics (wedged / tick p99) but the
// rate metrics are 0 so they can't false-fire.
ConfigReader const& cfg = Services::Config();
const uint32 throttle_ms = cfg.alert_throttle_ms();
auto fire = [&](uint32& last_ms, uint32 thresh, uint32 value,
char const* metric, char const* unit)
{
if (thresh == 0 || value <= thresh)
return;
// Per-metric throttle: at most one line per throttle window.
if (last_ms != 0 && throttle_ms != 0 && (now_ms - last_ms) < throttle_ms)
return;
last_ms = now_ms;
TC_LOG_ERROR("playerbot.v2",
"[fleet_alert] {} breached: value={}{} threshold={}{} "
"(in_world={} wedged={} tick_p99={}us intents_dropped/win={} path_fail/min={})",
metric, value, unit, thresh, unit,
b.in_world, b.wedged, b.tick_p99_us, b.intents_dropped, b.path_fail_per_min);
};
fire(last_alert_wedged_ms_, cfg.alert_wedged_bots(), b.wedged, "wedged_bots", "");
fire(last_alert_p99_ms_, cfg.alert_tick_p99_us(), b.tick_p99_us, "tick_p99_us", "us");
fire(last_alert_drop_ms_, cfg.alert_intent_drop_per_min(),b.intents_dropped, "intent_drop/win", "");
fire(last_alert_pathfail_ms_, cfg.alert_path_fail_per_min(), b.path_fail_per_min, "path_fail/min", "");
}
// Hook handler bodies — concrete for login/logout so a connected real player
// (or eventually a bot character) registers with the bot registry.
//
// Note: these handlers fire for ALL Player logins, not just bots. For now,
// every connected player gets a registry entry — once Fleet::BotLifecycleManager
// lands, registration is gated to characters flagged as bots.
// ---- Hook handler stubs ---------------------------------------------------
// Every hook is wired but inert until the corresponding subsystem lands.
// Each one will dispatch to a Fleet or Bot-layer component per FEATURE_MATRIX.md.
void Module::OnPlayerLogin(Player* p)
{
if (!initialized_ || !p) return;
const BotId id = p->GetGUID().GetCounter();
// A person logging into a marked character with the game client is never driven by the bot logic (Session/HumanLogin.h):
// an alt once summoned into the party stayed marked and the bot logic fought its player for control (Discord report, 2026-10-27).
{
WorldSession const* sess = p->GetSession();
bool const headless = !sess || sess->IsBot();
bool const marked = Services::Lifecycle().is_bot(id);
bool const on_pool = marked && !headless && Services::Accounts().is_pool_account(sess->GetAccountId());
HumanLoginAction const action = HumanClientAction(marked, headless, on_pool);
if (action != HumanLoginAction::None)
{
if (Services::Registry().has(id))
{
Services::Scheduler().unregister_bot(id);
Services::Snapshots().remove(id);
Services::Registry().unregister_bot(id);
}
if (action == HumanLoginAction::Release)
{
Services::Lifecycle().unmark_as_bot(id);
Services::Owners().ClearOwner(id);
}
else
Services::Lifecycle().unmark_as_bot_transient(id);
TC_LOG_INFO("playerbot.v2", "[human_login] {} logged in with the game client while marked as a bot: {}",
p->GetName(), action == HumanLoginAction::Release ? "unmarked and unbound" : "no bot logic this login (pool character)");
}
}
// Real players are not driven by V2 but DO get owner-login greetings
// from any of their currently-online bots. Bots queue a one-shot
// WhisperIntent (executed on the next world-thread drain) so real
// players see "Welcome back, <player>!" from their squad on relog —
// small touch that makes the owner→bot relationship feel alive.
if (!Services::Lifecycle().is_bot(id))
{
if (WorldSession const* sess = p->GetSession();
sess && !sess->IsBot())
{
const uint32 owner_account = sess->GetAccountId();
BotLanguage::OnLogin(p); // the language this client reported, for the bots' replies
// Bots bound to the whole account treat this character as their owner while it is online.
Services::Owners().NoteOwnerCharacterOnline(owner_account, id);
if (Services::Config().addon_hint_enabled())
g_addon_hints.Login(id, GameTime::GetGameTimeMS(), Services::Config().addon_hint_delay_sec() * 1000);
auto owned = Services::Owners().BotsOwnedBy(owner_account);
std::string const& owner_name = p->GetName();
for (BotId bot_id : owned)
{
if (!Services::Registry().has(bot_id)) continue; // not online
Player* obot = ObjectAccessor::FindConnectedPlayer(
ObjectGuid::Create<HighGuid::Player>(bot_id));
if (!obot) continue;
// Queue via the bot's intent queue — runs on the next
// world-tick drain (so we don't whisper inside the login
// path while the new player's session is still mid-init).
if (Services::HasIntents(bot_id))
{
Intent it{};
it.bot_id = bot_id;
it.body = ChatIntent{WhisperIntent{
owner_name,
std::string("Welcome back!")}};
Services::Intents(bot_id).push(std::move(it));
}
}
}
// Restart resilience: a server restart logs every bot out, and the
// world-population pipeline respawns the fleet in arbitrary order —
// a human's dungeon group could sit half-empty for many minutes
// (2026-06-11: user back in Deadmines, tank + leader offline, run
// dead). Group membership persists in group_member, so when the
// human logs back in, spawn-login every offline bot member of their
// group immediately. LoginBot is idempotent (refuses while a login
// is in flight), so racing the population pipeline is harmless.
if (Group const* g = p->GetGroup())
{
for (Group::MemberSlot const& slot : g->GetMemberSlots())
{
const BotId member_id = slot.guid.GetCounter();
if (member_id == id) continue;
if (!Services::Lifecycle().is_bot(member_id)) continue;
if (ObjectAccessor::FindConnectedPlayer(slot.guid)) continue;
auto res = Services::SessionMgr().LoginBot(slot.guid);
TC_LOG_INFO("playerbot.v2",
"[GroupRelogin] {} logged in with offline bot groupmate {} — spawn-login {}",
p->GetName(), slot.name, res.ok ? "submitted" : res.reason);
}
}
return;
}
if (Services::Registry().has(id)) return;
// Raid relog placement (2026-10-07, MC wipe 17:16): a restart logs the whole group out wherever its last fight ended, so the bots
// log back in on the spot they were standing at, next to a pack that has since reset to its spawn, while the leader logs in at
// the entrance. The 40 bots pulled three Firelords, an Annihilator and a hound onto themselves with no tank in reach. A bot that
// logs into a raid map joins its human group leader there, like a raid zoning in together; once, at login, never afterwards.
if (Map* map = p->GetMap(); map && map->IsRaid() && p->IsInWorld())
{
if (Group const* g = p->GetGroup())
{
Player* human = nullptr;
for (GroupReference const& ref : g->GetMembers())
{
Player* m = ref.GetSource();
if (m && m != p && m->IsInWorld() && m->GetMap() == map && m->GetSession() && !m->GetSession()->IsBot()
&& (!human || m->GetGUID() == g->GetLeaderGUID()))
human = m;
}
if (human && p->GetExactDist(human) > 30.0f)
{
TC_LOG_INFO("playerbot.v2", "[raid_login] {} logged into map {} {:.0f} y from {} ({:.0f},{:.0f},{:.0f}): placed beside them",
p->GetName(), map->GetId(), p->GetExactDist(human), human->GetName(), p->GetPositionX(), p->GetPositionY(), p->GetPositionZ());
p->NearTeleportTo(human->GetPositionX(), human->GetPositionY(), human->GetPositionZ(), human->GetOrientation());
}
}
}
// Real clients drive Player::CanNeverSee past its first guard by sending
// CMSG_MOVE_INIT_ACTIVE_MOVER_COMPLETE, which sets PLAYER_LOCAL_FLAG_OVERRIDE_TRANSPORT_SERVER_TIME.
// Without it, Player::CanNeverSee always returns true, CanSeeOrDetect
// always returns false, and every offensive spell on a creature fails
// SPELL_FAILED_BAD_TARGETS — IsValidAttackTarget can't see the target.
// Bots have no client to send that opcode, so set the flag here once the
// bot is fully in-world. (CharacterHandler.cpp:1176 is the equivalent set
// for real players, gated on the time-sync queue which never populates
// for sessionless bots.)
p->SetPlayerLocalFlag(PLAYER_LOCAL_FLAG_OVERRIDE_TRANSPORT_SERVER_TIME);
// Defense and the weapon skills at the level's maximum on every login: a bot created or levelled before the skills were
// raised with the level (companions of an older build, world bots) otherwise keeps the skill-ups it happened to earn (a
// warrior at Defense 181, a paladin at 52) and takes crushing blows on most swings from a boss.
::Playerbot::API(p).learn_weapon_skills_free();
// Take off anything the client cannot draw BEFORE other players get a
// chance to inspect this bot. Without it a bot that logs in wearing such an
// item stays a client-crash hazard until the next 5-min gear-backfill pass.
Services::Population().HealUnrenderableGear(p);
// Cross-map travel on-ramp (2026-06-16): ensure the bot KNOWS its faction's
// flight paths. The travel graph prunes every taxi edge to a flight master
// NOT in the bot's taximask (UnifiedTravelGraph EdgeUsable → IsTaximaskNodeKnown),
// so a bot with an empty mask sees an EDGELESS flight-master subgraph and
// FindRoute returns 0 legs — the ~87% "both ends attached, legs=0" route
// failures that blocked all cross-continent travel/relocation. The setup
// pipeline only learned at SETUP and skipped sub-10 bots, so every
// already-distributed bot had an empty mask. LearnAllFactionFlightpaths is
// idempotent (SetTaximaskNode only flips unset bits) + cheap (~600 nodes), and
// the mask persists in the character taximask column, so a one-time learn on
// login repairs the whole fleet. (Sub-10 bots stay same-map via the relocation
// band gate regardless, so a full mask doesn't send them cross-continent.)
if (uint32 fp = ::Playerbot::V2::World::LearnAllFactionFlightpaths(p))
TC_LOG_INFO("playerbot.v2",
"[FlightLearn] {} learned {} flight paths on login (taxi network now routable)",
p->GetName(), fp);
// CRITICAL: Initialize phasing data for the bot.
// 2026-05-20 — confirmed via [neutral_scan_miss] phases=0 diagnostic:
// 77% of stuck quest-kill bots had EMPTY PhaseShift containers.
// SendInitialPacketsAfterAddToMap (Player.cpp:25224) is supposed to
// run PhasingHandler::OnMapChange during HandlePlayerLogin, but for
// headless bot sessions some pre-condition (m_seer? grid registration?
// session client-time-sync?) doesn't satisfy and the PhaseShift never
// populates. Result: bot is in NO phase → quest creatures in
// conditional phases (post-Shattering Cataclysm+, MoP+, BfA daily
// hubs, etc.) are invisible → 80y scan finds 0 matching entries →
// bot wanders forever. V1 had the same issue (BotWorldEntry.cpp:881);
// explicit re-init here matches that fix. Both calls are idempotent
// — if HandlePlayerLogin DID populate phases, this is a no-op.
p->UpdateZone(p->GetZoneId(), p->GetAreaId());
PhasingHandler::OnMapChange(p);
// Quest-log hygiene at login. TC core Player::PushQuests() re-pushes
// auto-granted feature quests (55660 "Time Trials", 84224 "To Delves!",
// …) at EVERY login — a bot can never turn them in, and abandoning them
// doesn't stick (CanTakeQuest passes for a non-rewarded quest, so the next
// login re-pushes). Force-complete them to REWARDED now (SatisfyQuestStatus
// then rejects them, ending the re-push), and resolve profession-spec
// choice quests. Doing it at login — the exact moment the junk is
// (re-)pushed — paces 1:1 with the re-push, which the idle-rule path alone
// could not keep up with (2026-06-15: online holders grew faster than the
// throttled rule cleared them). Bounded drain; one resolvable quest/call.
for (int guard = 0; guard < 40; ++guard)
if (V2::Fleet::JunkQuestResolver::RunFor(p).done)
break;
BotPersonality personality = Services::Config().random_personality()
? RandomPersonality(SeedForBot(id))
: DefaultPersonality();
Services::Registry().register_bot(id, personality, BotRng{SeedForBot(id)});
Services::Scheduler().register_bot(id, ActivityTier::Idle);
// Re-apply persisted squad state (formation type/slot, follow
// distance, verbose flag) so a relog comes back with the same
// owner-tunable preferences that were active before logout.
if (BotAI* ai = Services::Registry().ai(id))
{
// ONE blocking read of this bot's playerbot_v2_character row feeds all
// three login consumers below (squad state, R7 leveling target, #4A
// archetype). Previously each ran its own SELECT of the same row -
// three sync round trips per bot, ~120 when 40 companions log in
// together. Stays synchronous: formation state has no not-loaded gate.
// Columns: [0..3] squad state (OwnerRegistry::kSquadStateColumns order),
// [4..8] leveling target, [9] archetype_id. No row -> the same defaults
// each consumer used when its own read came back empty.
SLOW_CALL("bot login row read (database)");
QueryResult const row = CharacterDatabase.PQuery(
"SELECT {}, "
"leveling_target_hub, leveling_target_map, leveling_bracket_lo, "
"leveling_bracket_hi, UNIX_TIMESTAMP(leveling_chosen_at), "
"archetype_id "
"FROM playerbot_v2_character WHERE character_guid_low={}",
OwnerRegistry::kSquadStateColumns, id);
Field const* const rf = row ? row->Fetch() : nullptr;
OwnerRegistry::SquadState const s = rf ? OwnerRegistry::SquadStateFromFields(rf) : OwnerRegistry::SquadState{};
ai->set_formation_type(static_cast<FormationType>(s.formation_type));
ai->set_formation_slot(s.formation_slot);
ai->set_follow_distance(s.follow_distance);
ai->set_verbose_logging(s.owner_verbose);
// The role the owner gave a companion lives in the bot logic and the Classic virtual spec, both memory only: pin it
// again so a relog (or a restart) does not hand the bot's role back to a talent inference.
// A companion with no stored role (added before roles were stored, or never asked) is pinned to the role it
// plays now and stored: left unset, the raid quota fill treated it as free and moved paladins and druids from
// heal to tank after a restart.
if (Services::Owners().GetOwner(id).account_id != 0)
V2::Fleet::BotPopulationManager::ApplyCompanionRole(p, Role::Unknown, "login");
// R7: hydrate the sticky leveling-zone relocation target (0010 columns)
// so a bot resumes traveling toward the SAME hub it picked before the
// restart instead of re-picking (distance-ranked → would flip-flop). A
// one-shot read; set_leveling_target marks the target loaded so the
// builder won't clobber a persisted choice. hub_id 0 = never picked.
if (rf)
{
Field const* const f = rf + 4;
BotAI::LevelingZoneTarget t;
t.hub_id = f[0].GetUInt32();
t.map_id = f[1].GetUInt32();
t.bracket_lo = f[2].GetUInt8();
t.bracket_hi = f[3].GetUInt8();
t.chosen_at = f[4].IsNull() ? 0 : f[4].GetUInt64();
ai->set_leveling_target(t);
}
else
ai->mark_leveling_target_loaded();
// ---- #4A: per-bot ARCHETYPE (WHAT/WHEN the bot plays) ----
// Gate behind PlayerbotV2.Archetype.Enabled. When disabled every bot
// reads as the default CasualSolo (id 0) so behavior is uniform for
// controlled testing.
if (Services::Config().archetype_enabled())
{
// Deterministic roll from the per-bot seed (same seed used for
// personality + rng) so a bot's archetype is stable across
// restarts. We read the persisted archetype_id to decide whether
// a write-back is needed: on first spawn (or after a DB wipe) the
// column is still the schema default and differs from the roll, so
// we persist it once; thereafter the read matches and we skip the
// UPDATE. RollArchetype is idempotent, so even a stale persisted
// value converges to the deterministic roll without churn.
const BotArchetype rolled = RollArchetype(SeedForBot(id));
ai->set_archetype(rolled);
bool need_write = true;
if (rf && rf[9].GetUInt8() == rolled.archetype_id)
need_write = false;
if (need_write)
CharacterDatabase.PExecute(
"UPDATE playerbot_v2_character SET archetype_id={} "
"WHERE character_guid_low={}",
uint32(rolled.archetype_id), id);
}
}
// Bots taught by an earlier build can carry other classes' trainer spells (a rogue with Judgement): drop them at login.
if (uint32 const removed = ::Playerbot::API(p).purge_foreign_class_spells())
TC_LOG_INFO("playerbot.v2", "[levelup] {} lost {} spells of other classes at login", p->GetName(), removed);
// Vanilla riding (Bot/World/RidingTier.h): none below 40, 75 and a normal mount from 40, 150 and the swift mount from 60. A bot made by an earlier build can hold
// more (150 at 40-59, riding and mounts under 40): one correction at login. The call changes nothing for a bot that is already right, so it is one-shot by itself.
if (::Playerbot::IsVanillaRealm())
{
uint32 const riding_before = p->GetSkillValue(SKILL_RIDING);
if (uint32 const changed = ::Playerbot::API(p).learn_riding_free())
TC_LOG_INFO("playerbot.v2", "[riding] {} L{} corrected at login: skill {} -> {}, {} change(s)", p->GetName(), uint32(p->GetLevel()), riding_before,
uint32(p->GetSkillValue(SKILL_RIDING)), changed);
}
// INFO-level so the operator can see which bots actually attached bot logic
// when bringing up a fresh worldserver / spawning via .playerbot login.
// The DEBUG history can be inspected via /history or /inspect later.
TC_LOG_INFO("playerbot.v2", "[PlayerbotV2] Bot logic attached: {} (id {}, class {} race {} level {})",
p->GetName(), id, uint32(p->GetClass()), uint32(p->GetRace()), uint32(p->GetLevel()));
}
// A deleted character keeps no bot mark or owner: the mark would ask for a login of a character that is gone.
void Module::OnPlayerDeleted(uint64 guid_low)
{
if (!initialized_) return;
BotId const id = guid_low;
if (!Services::Lifecycle().is_bot(id)) return;
Services::Lifecycle().unmark_as_bot(id);
Services::Owners().ClearOwner(id);
}
void Module::OnPlayerLogout(Player* p)
{
if (!initialized_ || !p) return;
const BotId id = p->GetGUID().GetCounter();
if (WorldSession const* sess = p->GetSession(); sess && !sess->IsBot())
{
Services::Owners().NoteOwnerCharacterOffline(sess->GetAccountId(), id);
g_addon_hints.Logout(id);
// Self-bot ends with the session: left marked, the character would be logged in as a bot with its group at the next login.
bool const marked = Services::Lifecycle().is_bot(id);
if (HumanClientAction(marked, false, marked && Services::Accounts().is_pool_account(sess->GetAccountId())) == HumanLoginAction::Release)
{
Services::Lifecycle().unmark_as_bot(id);
Services::Owners().ClearOwner(id);
}
}
if (!Services::Registry().has(id)) return; // not one of ours
// Speak queued chat (the `.bot dismiss` goodbye) while the bot is still in the world.
BotChatter::FlushPendingChat(p);
Services::Scheduler().unregister_bot(id);
ReactionTiming::Instance().Forget(id);
Services::Snapshots().remove(id);
Services::Registry().unregister_bot(id);
// Clean per-bot entries in file-scope maps. These keys are uint64
// player guid_low; without erase, entries accumulate forever on
// populations that rotate bots in/out (BotPopulationManager
// spawning fresh alts as old ones level up). At ~24 bytes/entry
// the leak is slow but real for long-running servers — and the
// entries are dead data (guid_low is unique per character).
g_last_snap_z_ms.erase(id);
g_global_stuck.erase(id);
TC_LOG_INFO("playerbot.v2", "[PlayerbotV2] Bot logic detached: {} (id {})", p->GetName(), id);
}
void Module::OnLevelUp(Player* p, uint8 new_level)
{
if (!initialized_ || !p) return;
// A bot that gains a level by XP learns what its trainer would teach at the new level (all ranks the level opens), the
// class quest abilities and weapon skills, as the setup pipeline does for a bot it levels. Without this a questing bot
// only trains when it happens to stand beside a trainer with a gold in its bag and keeps rank 1 for dozens of levels.
// Talents follow by themselves: idle:apply_context_talents is keyed on the level and spends the new point along the build.
if (!Services::Registry().has(p->GetGUID().GetCounter()))
return;
::Playerbot::API api(p);
api.purge_foreign_class_spells();
uint32 const quest_spells = api.learn_class_quest_spells_free();
uint32 const spells = api.learn_trainer_spells_free();
uint32 const weapon_skills = api.learn_weapon_skills_free();
// Riding (vanilla: apprentice and the race's mount at 40, journeyman and the swift mount at 60). The call does nothing below 40 and
// when the skill and mount are already there, so a level past 40 (or a jump over it) is safe to hand to it.
uint32 const riding = new_level >= 40 ? api.learn_riding_free() : 0;
TC_LOG_INFO("playerbot.v2", "[levelup] {} reached L{} (class {} zone {}): +{} trainer spells, +{} class quest spells, +{} weapon skills, +{} riding",
p->GetName(), uint32(new_level), uint32(p->GetClass()), p->GetZoneId(), spells, quest_spells, weapon_skills, riding);
}
namespace {
// Killing blows on players, stashed by OnDamageTaken and consumed by OnDeath for the [bot_death] line the test-night
// digest reads (scripts/bot-log-digest.ps1). World thread only; one entry per player at most, erased at the death.
struct LethalHit { uint32 spell = 0; int32 amount = 0; int32 overkill = 0; };
std::unordered_map<uint64, LethalHit> g_lethal_hits;
std::mutex g_lethal_hits_mtx; // OnDamageTaken may run on a map thread when MapUpdate.Threads > 1
// Members of a group still alive on this map, `skip` (the one who just died) excluded: the wipe test of tools/Fight-Report.ps1.
void CountGroupAlive(Group const* g, Player const* skip, uint32& alive, uint32& total)
{
alive = total = 0;
if (!g) return;
for (GroupReference const& ref : g->GetMembers())
{
Player const* m = ref.GetSource();
if (!m) continue;
++total;
if (m != skip && m->IsAlive() && !m->HasAuraType(SPELL_AURA_GHOST)) ++alive;
}
}
void LogPlayerDeath(Player* vp, Unit* killer, BotAI const* vai)
{
// Consume the stashed hit first so an early return below cannot leak the entry.
LethalHit hit;
{
std::lock_guard lk(g_lethal_hits_mtx);
if (auto it = g_lethal_hits.find(vp->GetGUID().GetCounter()); it != g_lethal_hits.end())
{
hit = it->second;
g_lethal_hits.erase(it);
}
}
Map* map = vp->GetMap();
if (!vai && !(map && (map->IsDungeon() || map->IsBattlegroundOrArena())))
return; // humans only where it matters: raids, dungeons, battlegrounds
std::string killer_txt = "-";
if (killer)
{
killer_txt = killer->GetName();
if (Creature const* kc = killer->ToCreature())
killer_txt += " (" + std::to_string(kc->GetEntry()) + ")";
else if (killer == vp)
killer_txt = "self";
}
std::string spell_txt = "melee";
if (hit.spell)
{
SpellInfo const* si = sSpellMgr->GetSpellInfo(hit.spell, DIFFICULTY_NONE);
spell_txt = std::to_string(hit.spell) + " " + (si ? std::string(si->SpellName->Str[LOCALE_enUS]) : std::string("?"));
}
else if (hit.amount == 0)
spell_txt = "unknown";
char const* rule = vai ? vai->last_rule_fired() : nullptr;
// Where the killer stood and what the killing spell is in this server's data (first damaging effect's radius and
// base points): settles "out of range but still hit" and "hit far harder than the spell should" from the log alone
// (Molten Giant Smash 2026-10-06: 8-25k at ~23 y, vanilla is 810-990 within 10 y).
std::string killer_at = "-";
if (killer && killer != vp)
killer_at = fmt::format("{:.1f},{:.1f} dist={:.1f}", killer->GetPositionX(), killer->GetPositionY(), vp->GetExactDist2d(killer));
std::string spell_data = "-";
if (hit.spell)
if (SpellInfo const* si = sSpellMgr->GetSpellInfo(hit.spell, DIFFICULTY_NONE))
for (SpellEffectInfo const& eff : si->GetEffects())
if (eff.IsEffect(SPELL_EFFECT_SCHOOL_DAMAGE) || eff.IsEffect(SPELL_EFFECT_WEAPON_DAMAGE) ||
eff.IsEffect(SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL) || eff.IsEffect(SPELL_EFFECT_NORMALIZED_WEAPON_DMG) ||
eff.IsEffect(SPELL_EFFECT_WEAPON_PERCENT_DAMAGE) || eff.IsEffect(SPELL_EFFECT_HEALTH_LEECH) ||
eff.IsEffect(SPELL_EFFECT_ENVIRONMENTAL_DAMAGE))
{
spell_data = fmt::format("eff{} type={} bp={} radius={:.0f}", uint32(eff.EffectIndex), uint32(eff.Effect),
eff.BasePoints, eff.CalcRadius(killer).Max);
break;
}
// A self-inflicted killing blow with no spell is environmental damage (Player::EnvironmentalDamage deals it as the victim on
// itself): drowning, dark-water fatigue, lava or slime, or a fall. Name what the bot stood in so the line says which, instead of
// the bare "killer=self spell=melee" of the Drowned City mage (af47-1, 2026-11-19) and the Molten Core lava (2026-10-11).
std::string env = "-";
if (killer == vp && !hit.spell)
{
ZLiquidStatus const st = vp->GetLiquidStatus();
env = fmt::format("liquid=0x{:X}{}{}{}", uint32(st), vp->IsUnderWater() ? " under_water" : "", vp->IsInWater() ? " in_water" : "",
vp->IsFalling() ? " falling" : "");
}
uint32 grp_alive = 0, grp_total = 0;
CountGroupAlive(vp->GetGroup(), vp, grp_alive, grp_total);
Playerbot::V2::DeathCause::Note(vp->GetGUID().GetCounter(), fmt::format("{} by {} ({} dmg)", spell_txt, killer_txt, hit.amount));
TC_LOG_INFO("playerbot.v2",
"[bot_death] ts={} {} ({}) map={} pos={:.1f},{:.1f},{:.1f} killer={} spell={} dmg={} overkill={} rule={} killer_at={} spell_data={} env={} class={} grp_alive={}/{}",
uint64(GameTime::GetGameTime()), vp->GetName(), vai ? "bot" : "player", vp->GetMapId(),
vp->GetPositionX(), vp->GetPositionY(), vp->GetPositionZ(), killer_txt, spell_txt, hit.amount, hit.overkill,
rule ? rule : "-", killer_at, spell_data, env, uint32(vp->GetClass()), grp_alive, grp_total);
if (vai)
{
// A scenario test in progress takes the same facts as its failure reason.
Diagnostics::ScenarioDeath sd;
sd.name = vp->GetName();
sd.map = vp->GetMapId();
sd.x = vp->GetPositionX(); sd.y = vp->GetPositionY(); sd.z = vp->GetPositionZ();
sd.killer = killer_txt;
sd.spell = spell_txt;
sd.damage = hit.amount;
sd.rule = rule ? rule : "-";
Diagnostics::Scenario::NoteBotDeath(vp->GetGUID().GetCounter(), std::move(sd));
}
}
} // namespace
void Module::OnDeath(Unit* victim, Unit* killer)
{
if (!initialized_ || !victim) return;
// A bot died: stamp the real death time (world thread, the same thread
// OrderRelease latches on). BotAI::tick's death edge, which can lag the
// death by a snapshot or more, keeps a release order latched after this
// instant and drops one from before it (a previous death).
if (Player* vp = victim->ToPlayer())
{
BotAI* vai = Services::Registry().ai(vp->GetGUID().GetCounter());
if (vai)
{
const uint32 now = GameTime::GetGameTimeMS();
vai->set_died_at_ms(now ? now : 1u);
}
// Open-world deaths by a creature feed the hotspot map (quest pursuit avoids places that keep killing peers).
if (vai && killer && killer != vp && killer->GetTypeId() == TYPEID_UNIT && vp->GetMap() && !vp->GetMap()->IsDungeon()
&& !vp->GetMap()->IsBattlegroundOrArena())
DeathHotspots::Instance().NoteDeath(vp->GetMapId(), vp->GetPositionX(), vp->GetPositionY(),
uint8(killer->GetLevel()), uint64(GameTime::GetGameTime()));
LogPlayerDeath(vp, killer, vai);
if (vai)
BotBridge::NoteDeath(vp, killer && killer != vp ? killer->GetName() : std::string());
return;
}
// When a creature dies, queue its corpse on the pending-loot list of
// every registered bot in its tap list. State_Idle walks the bot toward
// the closest entry next tick and emits LootIntent once in range. We
// queue here (rather than emit Intent immediately) because the bot is
// typically still in combat with adds — looting now would interrupt
// their rotation. The queue persists until State_Idle drains it.
Creature* c = victim->ToCreature();
if (c)
Diagnostics::Scenario::NoteCreatureKilled(c->GetMapId(), c->GetEntry(), c->hasLootRecipient());
// The boss-kill moment (IsBossCreature: a scripted boss's BossAI, the world-boss flag or the vanilla boss rank; the core's
// dungeon-boss flag is never set, so 5-man bosses had no moment at all). The player with the credit is the killer itself or
// the owner of the pet, totem or charmed unit that landed the last hit. Companions near it may say "gg" (BotChatter).
bool const boss_kill = c && IsBossCreature(c);
if (boss_kill && killer)
if (Player* credited = killer->GetCharmerOrOwnerPlayerOrPlayerItself())
BotChatter::OnBossKilled(credited, c);
// Boss kill line for tools/Fight-Report.ps1: dungeon and raid bosses, with how many of the tapping group survived.
if (boss_kill && c->GetMap() && c->GetMap()->IsDungeon())
{
Group const* tap_group = nullptr;
Player const* tap_player = nullptr;
for (ObjectGuid const& tg : c->GetTapList())
if (Player const* tp = ObjectAccessor::FindPlayer(tg))
{
tap_player = tp;
if (tp->GetGroup()) { tap_group = tp->GetGroup(); break; }
}
uint32 alive = 0, total = 0;
CountGroupAlive(tap_group, nullptr, alive, total);
TC_LOG_INFO("playerbot.v2", "[boss_kill] ts={} map={} {} ({}) raiders_alive={}/{}", uint64(GameTime::GetGameTime()),
c->GetMapId(), c->GetName(), c->GetEntry(), tap_group ? alive : (tap_player ? 1u : 0u),
tap_group ? total : (tap_player ? 1u : 0u));
}
if (!c || !c->hasLootRecipient()) return;
auto const& tap = c->GetTapList();
if (tap.empty()) return;
auto& reg = Services::Registry();
const ObjectGuid corpse_guid = c->GetGUID();
for (ObjectGuid const& guid : tap)
{
if (!guid.IsPlayer()) continue;
const BotId id = guid.GetCounter();
if (!reg.has(id)) continue;
// Queue the corpse onto the bot's pending-loot list. State_Idle
// walks the bot to the closest entry and emits a LootIntent each
// tick; the entry is popped on success. The push helper is mutex-
// protected since the bot worker drains concurrently.
reg.push_loot(id, corpse_guid);
}
}
void Module::OnResurrect(Player* /*p*/)
{
if (!initialized_) return;
// Intentionally a no-op. BotAI::tick() reacts to `is_alive` going true
// by transitioning Dead → Idle on the next snapshot, which restarts the
// dispatch chain naturally. No event-bus push needed.
}
void Module::OnSpecChanged(Player* /*p*/, uint8 /*new_spec*/)
{
if (!initialized_) return;
// Intentionally a no-op. Rotation lookup is `Combat::GetRotation(cls,
// spec)` every tick, so spec changes pick up the new APL within one
// tick of the snapshot updating `spec`. No state to invalidate.
}
void Module::OnDamageDealt(Unit* /*attacker*/, Unit* /*victim*/, int32 /*amount*/, uint32 /*spell_id*/)
{
if (!initialized_) return;
// Intentionally a no-op. DamageDealt fires every swing/tick/DoT and
// would saturate the 256-slot inbox ring. Snapshot deltas (in_combat,
// victim hp) cover the cases rules actually need without burning event
// capacity. Combat-tier promotion happens in OnWorldUpdate from the
// freshly built snapshot's `in_combat` flag.
}
namespace { thread_local uint32 tl_damage_spell = 0; }
void Module::NoteDamageSpell(uint32 spell_id)
{
tl_damage_spell = spell_id;
}
void Module::OnDamageTaken(Unit* attacker, Unit* victim, int32 amount, uint32 spell_id)
{
// The hook that precedes OnDamage names the spell; consumed here so a later melee swing is not labelled with it.
uint32 const lethal_spell = spell_id ? spell_id : tl_damage_spell;
tl_damage_spell = 0;
if (!initialized_) return;
PveGroupCoordinator::NoteDamageDone(attacker, victim, amount); // the [raid_dps] per-bot tally
GroupSnapshotBuilder::NoteDamageTaken(victim, amount); // a grouped player's damage taken per second, for the raid healers
// The bot's own hit (not its pet's) for the idle watchdog: "did anything land in the last 4 s".
if (attacker && attacker->IsPlayer() && victim && amount > 0 && !victim->IsPlayer())
if (BotAI* ai = Services::Registry().ai(attacker->GetGUID().GetCounter()))
ai->note_damage_done(GameTime::GetGameTimeMS());
Diagnostics::Scenario::NoteDamage(attacker, victim, amount, lethal_spell); // a scenario's boss-pull statistics; one atomic load when none runs
// The core hands this hook spell 0; the spell is the one NoteDamageSpell stashed just before (0 = a melee swing). With 0 every spell and
// DoT tick was tallied as "white swing" and no per-spell row matched its outcome.
Diagnostics::Parity::NoteDamage(attacker, victim, amount, lethal_spell); // the damage_parity tally; one atomic load when none runs
// Killing blow on a player: remember the spell for the [bot_death] line (one compare per hit otherwise).
if (victim && amount > 0 && victim->IsPlayer() && uint64(amount) >= victim->GetHealth())
{
std::lock_guard lk(g_lethal_hits_mtx);
g_lethal_hits[victim->GetGUID().GetCounter()] =
LethalHit{ lethal_spell, amount, int32(uint64(amount) - victim->GetHealth()) };
}
// Intentionally a no-op. The BotEventInbox was retired 2026-05-21
// after audit found zero consumers + the snapshot's in_combat /
// hp / attackers vector already covers every actual rule need.
// V1 had an event bus with real subscribers (174 publishers, 210
// subscribers across 7 domains); V2's snapshot-then-bot-worker
// distribution model fills that role differently. If event-driven
// dispatch becomes needed for a specific feature (e.g. kick-then-
// pause), introduce a focused per-event hook then.
}
void Module::OnHitOutcome(Unit* attacker, Unit* victim, uint32 spell_id, uint8 outcome, uint32 damage)
{
if (!initialized_) return;
Diagnostics::Parity::NoteOutcome(attacker, victim, spell_id, outcome, damage); // one atomic load when no damage_parity leg measures
}
void Module::OnEnergize(Unit* target, uint32 spell_id, int32 gained, uint8 power)
{
if (!initialized_) return;
Diagnostics::Parity::NoteEnergize(target, spell_id, gained, power); // one atomic load when no economy leg measures
}
void Module::OnHealOutcome(Unit* healer, Unit* target, uint32 spell_id, uint32 raw, uint32 effective)
{
if (!initialized_) return;
Diagnostics::Parity::NoteHeal(healer, target, spell_id, raw, effective); // one atomic load when no support scorecard runs
Diagnostics::Scenario::NoteHeal(healer, spell_id, raw, effective); // a scenario's boss-pull heal breakdown
}
void Module::OnHealReceived(Unit* /*healer*/, Unit* /*target*/, int32 /*amount*/, uint32 /*spell_id*/)
{
if (!initialized_) return;
// See OnDamageTaken — same rationale. Snapshot's hp_pct + group
// member HP tracking cover all current consumer cases.
}
void Module::OnAuraApplied(Unit* /*target*/, Aura* /*aura*/)
{
if (!initialized_) return;
// Intentionally a no-op. Aura applications fire constantly during combat
// (every DoT tick stack, every refresh, every proc-aura) and would
// saturate the 256-slot inbox ring. The snapshot's `own_auras` list (with
// `mechanic` populated) lets rules reason about active CC within ~100ms
// of application — fast enough for reactive defensives without the
// event-bus overhead. If a use case ever needs sub-tick latency on a
// specific aura, gate the push on that spell ID here.
}
void Module::OnAuraRemoved(Unit* /*target*/, Aura* /*aura*/)
{
if (!initialized_) return;
// Intentionally a no-op. Same rationale as OnAuraApplied — aura removals
// fire too often to event-bus blindly. Snapshot delta on `own_auras` /
// outbound aura tables covers refresh-detection use cases.
}
void Module::OnGroupMemberJoined(Group* /*g*/, Player* p)
{
if (!initialized_) return;
// A bot in a group rides when its leader does, so it has to hold the riding and mount spell its level allows. Companions get them in
// their upkeep; a world bot a player invited had them only from the gear pass, if that had come round. learn_riding_free is
// idempotent and level-gated (nothing below 40).
if (p && Services::Registry().has(p->GetGUID().GetCounter()))
if (uint32 const changed = ::Playerbot::API(p).learn_riding_free())
TC_LOG_INFO("playerbot.v2", "[mount] {} joined a group: riding and mount brought up to level {} ({} change(s))", p->GetName(),
uint32(p->GetLevel()), changed);
// The rest is a no-op. The world-tick rebuilds GroupSnapshot every tick
// for grouped bots, so newly-joined members appear in the next snapshot
// without a hook-driven invalidation. If we ever cache snapshots across
// ticks, this is the place to invalidate.
}
void Module::OnGroupMemberLeft(Group* /*g*/, Player* /*p*/)
{
if (!initialized_) return;
// Intentionally a no-op. Same rationale as OnGroupMemberJoined — the
// next world-tick GroupSnapshot rebuild reflects departures. Leadership
// re-election lives in TrinityCore's Group code; no V2 action needed.
}
void Module::OnWhisperReceived(Player* sender, Player* receiver, std::string const& msg)
{
if (!initialized_ || !sender || !receiver) return;
const BotId id = receiver->GetGUID().GetCounter();
if (!Services::Registry().has(id)) return; // not one of ours
// Try the command parser first — if it recognized a verb it returns
// true and we're done. Otherwise fall through to the social reactor
// so non-command whispers ("hi", "ty for the help") still get a
// friendly reply. Without this fallthrough, strangers whispering a
// bot get silence, which reads as broken.
// "can I join your guild" / "пригласи в гильдию": a guild invite (or why not). Ahead of the parser: the owner's own
// "invite" (a group invite) has no guild word, so it still reaches the parser.
if (GuildWhisperInvite::TryRequest(sender, receiver, msg)) return;
if (BotCommandParser::Dispatch(sender, receiver, msg)) return;
// A short "yes" / "inv" right after the bot talked about its guild, or after its Trade recruiting line.
if (GuildWhisperInvite::TryFollowUp(sender, receiver, msg)) return;
BotChatReactor::ReactWhisper(sender, receiver, msg);
}
void Module::OnGuildChat(Player* sender, uint64 guild_id, std::string const& msg)
{
// Phase C.3: route to BotChatReactor::ReactGuild. The reactor
// handles all gating (per-guild throttle, bot-self filter, online-
// officer pick).
if (!initialized_ || !sender || guild_id == 0 || msg.empty()) return;
BotChatReactor::ReactGuild(sender, guild_id, msg);
}
void Module::OnChannelChat(Player* sender, Channel* channel, std::string const& msg)
{
if (!initialized_ || !sender || !channel || msg.empty()) return;
V2::ChannelChat::OnPlayerLine(sender, channel, msg);
}
void Module::OnSayChat(Player* sender, std::string const& msg)
{
// SC-P1a: a real player spoke in /say. Let the reactor pick at most one
// nearby bot to answer (range-gated by CONFIG_LISTEN_RANGE_SAY, per-bot
// + per-area cooldown). All gating lives in the reactor.
if (!initialized_ || !sender || msg.empty()) return;
BotChatReactor::ReactSay(sender, msg);
}
void Module::OnYellChat(Player* sender, std::string const& msg)
{
// SC-P1a: /yell variant — wider range (CONFIG_LISTEN_RANGE_YELL) but a
// lower reply chance (the reactor keeps yell answers rarer than say).
if (!initialized_ || !sender || msg.empty()) return;
BotChatReactor::ReactYell(sender, msg);
}
void Module::OnTextEmote(Player* sender, uint32 emote_id, uint32 text_emote_id, ObjectGuid target)
{
// SC-P2c: a player performed a text emote; a nearby bot reciprocates.
if (!initialized_ || !sender) return;
BotChatReactor::ReactEmote(sender, emote_id, text_emote_id, target);
}
void Module::OnItemLooted(Player* p, uint32 item_id)
{
if (!initialized_ || !p || !p->GetSession() || !p->GetSession()->IsBot()) return;
BotChatter::OnItemLooted(p, item_id);
}
void Module::OnGroupMemberRemoved(Group* group, ObjectGuid member, uint8 method, ObjectGuid kicker)
{
// An owned companion the owner removes from the group (or that leaves it) keeps following its owner for as long as the owner binding
// stands (2026-10-18: Veda followed her owner after the uninvite). It is let go like a dismissal: a goodbye and a logout; the owner and the
// saved mark stay, so `.bot call` brings it back. Plain conversions, an owner going offline and map changes never reach this hook.
// A player leaves the group (or is removed): the world bots that were with him and have no owner are left in a group with no player in it.
// They leave it, hand in what they finished beside the giver and drop the rest (BotPopulationManager::DrainLedWorldBotReleases), rather than
// running to a quest giver and back for ever as a group of bots. Owned companions have their own release below.
if (initialized_ && group && member.IsPlayer() && !Services::Registry().has(member.GetCounter()) && !group->isBGGroup() && !group->isBFGroup() && !group->isLFGGroup())
{
std::vector<uint64> bots;
bool other_human = false;
for (GroupReference const& ref : group->GetMembers())
{
Player const* m = ref.GetSource();
if (!m || m->GetGUID() == member) continue;
if (Services::Registry().has(m->GetGUID().GetCounter())) bots.push_back(m->GetGUID().GetCounter());
else other_human = true;
}
if (!other_human)
for (uint64 const b : bots)
if (Services::Owners().GetOwner(b).account_id == 0 && !::Playerbot::Pool::IsScenarioBot(b))
Services::Population().QueueLedWorldBotRelease(b);
}
if (!initialized_ || !group || !member.IsPlayer() || !Services::Registry().has(member.GetCounter()))
return;
auto const owner = Services::Owners().GetOwner(member.GetCounter());
if (owner.account_id == 0)
return;
Player const* bot = ObjectAccessor::FindConnectedPlayer(member);
bool const logging_out = !bot || !bot->IsInWorld() || !bot->GetSession() || bot->GetSession()->PlayerLogout();
Player const* remover = kicker.IsEmpty() ? nullptr : ObjectAccessor::FindConnectedPlayer(kicker);
bool const by_owner = remover && remover->GetSession() && !remover->GetSession()->IsBot() &&
remover->GetSession()->GetAccountId() == owner.account_id;
if (!GroupControl::ReleaseCompanionOnRemoval(method, group->isBGGroup() || group->isBFGroup(), by_owner, logging_out))
return;
std::string owner_name;
if (by_owner)
owner_name = remover->GetName();
else if (owner.player_guid)
sCharacterCache->GetCharacterNameByGuid(ObjectGuid::Create<HighGuid::Player>(owner.player_guid), owner_name);
TC_LOG_INFO("playerbot.v2", "[companion] {} released: removed from {}'s group (method {}, by {})", bot->GetName(), owner_name.empty() ? std::string("its owner") : owner_name,
uint32(method), by_owner ? "the owner" : remover ? remover->GetName() : std::string("no one"));
Services::Population().QueueCompanionRelease(member.GetCounter());
}
void Module::OnMemberRoleSet(Group* group, ObjectGuid setter, ObjectGuid member, uint8 newRole)
{
// The party frame's role menu (Set Role > Tank / Healer / Damage / None) on a bot. The bot used to publish its own duty role back
// over the choice on its next tick (ingroup:set_role_no_lfg), so a warrior set to Tank stayed a damage dealer with damage gear
// (DoctorSun, Discord 2026-11-17). The choice is now the bot's declared role, the same as `/setrole`. Battleground groups pick
// their own roles; a bot setting its own role does not come through here (API::lfg_role_check calls Group::SetLfgRoles).
if (!initialized_ || !group || group->isBGGroup() || group->isBFGroup() || setter == member || !member.IsPlayer())
return;
if (!Services::Lifecycle().is_bot(member.GetCounter()))
return;
Player* bot = ObjectAccessor::FindConnectedPlayer(member);
if (!bot || !bot->IsInWorld())
return;
uint8 const role = newRole & ~uint8(1); // without the leader bit
Role const declared = role == 2 ? Role::Tank : role == 4 ? Role::Healer : role == 8 ? Role::Dps : Role::Unknown;
if (role && declared == Role::Unknown)
return; // more than one role bit: not a choice the menu makes
BotCommandParser::DeclareRole(bot, declared);
TC_LOG_INFO("playerbot.v2", "[set_role] {} set {}'s role to {} from the role menu", setter.ToString(), bot->GetName(),
declared == Role::Tank ? "tank" : declared == Role::Healer ? "healer" : declared == Role::Dps ? "damage" : "none (cleared)");
}
void Module::OnRaidSubgroupChanged(Group* group, ObjectGuid member, uint8 subgroup)
{
// The leader moved a raid member (or a restore did, which is not recorded). An owned companion's new sub-group is stored so a
// relog or `.bot call all` puts it back. Battleground groups have sub-groups of their own and are not the roster.
if (!initialized_ || !group || group->isBGGroup() || group->isBFGroup() || Fleet::g_subgroup_restoring)
return;
BotId const id = member.GetCounter();
if (!member.IsPlayer() || Services::Owners().GetOwner(id).account_id == 0)
return;
Services::Owners().SetStoredSubgroup(id, subgroup);
TC_LOG_INFO("playerbot.v2", "[roster_save] {} moved to raid group {}{}", member.ToString(), uint32(subgroup) + 1u, Services::Owners().GetOwner(id).saved ? " (saved)" : "");
}
void Module::OnSpiritReleased(Player* p)
{
// The owner pressed Release Spirit (ike3 / the owner: companions release with
// their master). Inside a dungeon or raid RepopAtGraveyard revives the
// owner at the graveyard, so the owner is never a ghost there and
// State_Dead's owner-released check (owner->is_ghost) cannot fire; this
// hook is the release itself. Every dead bot of this owner in the group
// on the owner's (death) map takes the same latch as a "release" order.
// Bots release through PlayerbotAPI, never this handler; a bot owner is
// skipped anyway.
if (!initialized_ || !p || !p->GetSession() || p->GetSession()->IsBot()) return;
Group* group = p->GetGroup();
if (!group) return;
uint32 released = 0, considered = 0;
for (GroupReference const& itr : group->GetMembers())
{
Player* member = itr.GetSource();
if (!member || member == p) continue;
BotId const mid = member->GetGUID().GetCounter();
if (!Services::Registry().has(mid) || !BotCommandParser::ControlsForGroupOrder(p, member))
continue;
if (member->IsAlive() || member->GetMapId() != p->GetMapId()) continue;
++considered;
if (BotCommandParser::OrderRelease(p, member, "owner released spirit"))
++released;
}
TC_LOG_INFO("playerbot.v2",
"[death-recovery] {} released spirit on map {}{}: {} of {} dead companion(s) on that map release with them",
p->GetName(), p->GetMapId(), p->GetMap() && p->GetMap()->Instanceable() ? " (instance: owner revives at the graveyard)" : "",
released, considered);
}
void Module::OnGuildMemberAdded(uint64 guild_id, ObjectGuid joiner_guid, std::string const& joiner_name)
{
// SC-P2b: welcome a new guild member from one online bot guildmate.
if (!initialized_ || guild_id == 0) return;
BotChatReactor::ReactGuildJoin(guild_id, joiner_guid, joiner_name);
}
// Runs a recognized bare group order (ClassifyGroupOrder already normalized it) for the sender's bots in the group.
// Shared by party/raid chat and by the addon-message transport.
void Module::RouteGroupOrder(Player* sender, Group* group, std::string order)
{
uint32 const account = sender->GetSession() ? sender->GetSession()->GetAccountId() : 0;
if (account == 0)
return;
// Typed crowd-control orders ("cc off", "no fear") that reach here: the add-on's transport, or a party "cc off" (always a group
// order) from a player who commands no bot in the group. The panel's "crowdcontrol on|off" is not one and goes on below.
if (BotCommandParser::ApplyCcOrder(sender, order, "group"))
return;
if (BotCommandParser::ApplyIgnoreOrder(sender, order, "group"))
return;
if (BotCommandParser::ApplyPlaceOrder(sender, order, "group"))
return;
if (BotCommandParser::ApplyRaidResetOrder(sender, order, "group"))
return;
if (BotCommandParser::ApplyLeadOrder(sender, order, "group"))
return;
// Stay / Hold / Follow / Come while the tank leads pause the lead ("go" picks it up); the order itself goes on below.
BotCommandParser::PauseLeadForOwnerOrder(sender, order, "group");
if (order.rfind("crowdcontrol ", 0) == 0 || order.rfind("crowd control ", 0) == 0 || order == "cc on" || order == "cc off")
{
// A switch of the owner's, not an order to each bot: set once, answer once.
std::string const word = order.substr(order.find_last_of(' ') + 1);
if (word != "on" && word != "off")
return;
bool const on = word == "on";
bool const changed = Services::Owners().SetCcEnabled(account, on);
TC_LOG_INFO("playerbot.v2", "[cc_switch] {} turns crowd control {} ({})", sender->GetName(), word, changed ? "changed" : "already");
Playerbot::BotLanguage::LocalizedHandler(sender->GetSession()).PSendSysMessage("Crowd control for your bots: %s.", on ? "on" : "off");
return;
}
uint32 tanks = 0;
// Raid pull: the main tank (raid-frame flag) pulls alone; with none set, every tank as before.
ObjectGuid pull_main_tank;
char const* pull_why = "every tank (no main tank set)";
// A tank that can pull: one of the sender's own bots, alive, in the tank role.
auto const usable_tank = [&](Player* m)
{
if (!m || m == sender || !m->IsAlive())
return false;
BotId const mid = m->GetGUID().GetCounter();
return Services::Registry().has(mid) && BotCommandParser::ControlsForGroupOrder(sender, m) &&
BotCommandParser::BotRole(m) == Role::Tank;
};
std::string main_tank_skipped;
if ((order == "pull" || order == "pull back") && group->isRaidGroup())
for (Group::MemberSlot const& slot : group->GetMemberSlots())
{
if (!(slot.flags & MEMBER_FLAG_MAINTANK))
continue;
// The flagged main tank pulls only if it can: a dead one, a player, someone else's bot or a bot that is not
// a tank would leave nobody pulling, so the next flagged one or the closest tank takes over.
Player* flagged = ObjectAccessor::FindConnectedPlayer(slot.guid);
if (usable_tank(flagged)) { pull_main_tank = slot.guid; pull_why = "the main tank"; break; }
main_tank_skipped += (main_tank_skipped.empty() ? "" : ", ") + slot.name;
}
if (!main_tank_skipped.empty() && pull_main_tank.IsEmpty())
TC_LOG_INFO("playerbot.v2", "[pull_back] flagged main tank {} cannot pull (dead, not your bot or not a tank): next best tank pulls",
main_tank_skipped);
// A 5-man's Pull sends one tank like Pull back does (two tank-role bots would both run at the pack); a raid's Pull keeps every tank.
if (order == "pull back" || (order == "pull" && !group->isRaidGroup()))
{
// Exactly ONE bot pulls: the tank assigned to the selected mob's raid icon, else the main tank, else the
// owned tank closest to the mob. Every other bot (other tanks included) holds where it stands.
Unit* sel = sender->GetSelectedUnit();
ObjectGuid assigned;
if (sel)
{
auto const marks = Services::Owners().TankMarks(group->GetGUID().GetCounter());
for (uint8 i = 0; i < 8 && assigned.IsEmpty(); ++i)
if (group->GetTargetIcon(i) == sel->GetGUID())
for (auto const& [tank_low, marker] : marks)
if ((marker & 7) == i) { assigned = ObjectGuid::Create<HighGuid::Player>(tank_low); break; }
}
if (!assigned.IsEmpty())
{ pull_main_tank = assigned; pull_why = "the tank assigned to that icon"; }
else if (pull_main_tank.IsEmpty())
{
float best = 1e9f;
for (GroupReference const& itr : group->GetMembers())
{
Player* m = itr.GetSource();
if (!m || m == sender || !m->IsAlive()) continue;
BotId const mid = m->GetGUID().GetCounter();
if (!Services::Registry().has(mid) || !BotCommandParser::ControlsForGroupOrder(sender, m) ||
BotCommandParser::BotRole(m) != Role::Tank)
continue;
float const d = sel ? m->GetDistance(sel) : m->GetDistance(sender);
if (d < best) { best = d; pull_main_tank = m->GetGUID(); }
}
pull_why = "the tank closest to the mob";
}
TC_LOG_INFO("playerbot.v2", "[pull_back] {} pulls: {} chosen as {}; every other bot holds",
sender->GetName(), pull_main_tank.IsEmpty() ? std::string("nobody") : pull_main_tank.ToString(), pull_why);
}
for (GroupReference const& itr : group->GetMembers())
{
Player* member = itr.GetSource();
if (!member || member == sender) continue;
BotId const mid = member->GetGUID().GetCounter();
// Every order reaches the bots the sender owns and the world bots he leads (invited into his group, no owner): they
// obey the group leader exactly like a companion. Quest words also reach an unowned bot of any other member.
bool const quest_word = order == "share" || order == "accept" || order == "accept all" || order == "quests" ||
order == "quest list";
if (!Services::Registry().has(mid) ||
!(BotCommandParser::ControlsForGroupOrder(sender, member) ||
(quest_word && Services::Owners().GetOwner(mid).account_id == 0)))
continue;
if (order == "pull back")
{
// Only the tank pulls and returns (the main tank when a raid flags one); everyone else carries on.
if (BotCommandParser::BotRole(member) != Role::Tank ||
(!pull_main_tank.IsEmpty() && member->GetGUID() != pull_main_tank))
{
// The other tanks come and stand beside the owner, where the mob is brought to (playtest 2026-10-08: "all the
// warriors should be standing beside me when I say pull back"); everyone else holds where they stand
// (like "pull"); "fall back" is the whole-raid version.
if (BotCommandParser::BotRole(member) == Role::Tank && member->IsAlive())
BotCommandParser::DispatchSingle(sender, member, "follow", "party");
else
BotCommandParser::ReleaseOwnerFollowOrder(sender, member, "party pull back");
continue;
}
BotCommandParser::DispatchSingle(sender, member, order, "party");
++tanks;
continue;
}
if (order == "pull")
{
// A tank with an icon assignment goes too, to its own mob (MC Garr 2026-10-08: only the flagged main tank pulled).
bool assigned_tank = false;
for (auto const& [tank_low, marker] : Services::Owners().TankMarks(group->GetGUID().GetCounter()))
if (tank_low == mid) { assigned_tank = true; break; }
if (BotCommandParser::BotRole(member) != Role::Tank ||
(!pull_main_tank.IsEmpty() && member->GetGUID() != pull_main_tank && !assigned_tank))
{
// The rest join once it is engaged: the owner handed the fight back.
if (BotCommandParser::BotRole(member) == Role::Tank)
TC_LOG_INFO("playerbot.v2", "[raid_pull] {} stays back: tank without an icon assignment (main tank {})",
member->GetName(), pull_main_tank.IsEmpty() ? std::string("none") : pull_main_tank.ToString());
BotCommandParser::ReleaseOwnerFollowOrder(sender, member, "party pull");
continue;
}
TC_LOG_INFO("playerbot.v2", "[raid_pull] {} is sent to pull ({})", member->GetName(),
assigned_tank ? "its icon assignment" : "the main tank / no main tank set");
BotCommandParser::DispatchSingle(sender, member, "tank", "party");
++tanks;
continue;
}
BotCommandParser::DispatchSingle(sender, member, order, "party");
}
if ((order == "pull" || order == "pull back") && !tanks)
{
// Say why: the flagged main tank could not pull, or none of the sender's bots in this group is a living tank.
uint32 owned_tanks = 0, dead_tanks = 0;
for (GroupReference const& itr : group->GetMembers())
{
Player* m = itr.GetSource();
if (!m || m == sender) continue;
BotId const mid = m->GetGUID().GetCounter();
if (!Services::Registry().has(mid) || !BotCommandParser::ControlsForGroupOrder(sender, m) ||
BotCommandParser::BotRole(m) != Role::Tank)
continue;
++owned_tanks;
if (!m->IsAlive()) ++dead_tanks;
}
Playerbot::BotLanguage::LocalizedHandler(sender->GetSession()).PSendSysMessage(owned_tanks && dead_tanks == owned_tanks ?
"Your tank bots are all dead; use attack instead." :
"No tank among your bots to pull; use attack instead.");
TC_LOG_INFO("playerbot.v2", "[pull_back] {} '{}': no tank pulled (your tank bots in the group: {}, dead {}; flagged main tank skipped: {})",
sender->GetName(), order, owned_tanks, dead_tanks, main_tank_skipped.empty() ? "none" : main_tank_skipped);
}
return;
}
void Module::OnAddonOrder(Player* sender, Group* group, std::string const& prefix, std::string const& text)
{
if (!initialized_ || !sender || !group) return;
// What the add-on sends to itself goes over the group channel when grouped: client 70658's add-on whispers
// (CMSG_CHAT_ADDON_MESSAGE_TARGETED) never reach the whisper hooks (live 2026-10-28, RC5/RC6), its group add-on messages do.
// The bot manager's PBC frames and the queued ".bot ..." lines are the sender's own, never orders for the group.
if (prefix == AddonControl::kPrefix)
{
if (WorldSession* session = sender->GetSession(); session && !session->IsBot())
AddonControl::OnAddonWhisper(sender, sender, prefix, text);
return;
}
if (prefix == kAddonOrderPrefix && (text == kAddonHelloText || IsAddonBotCommand(text)))
{
OnAddonWhisper(sender, sender, prefix, text);
return;
}
auto const order = ClassifyAddonOrder(prefix, text, Services::Config().behavior_owner_commands());
if (!order) return;
TC_LOG_INFO("playerbot.v2", "[addon_order] {} -> '{}'", sender->GetName(), *order);
RouteGroupOrder(sender, group, *order);
}
void Module::OnGroupPingUnit(Player* sender, Group* group, Unit* target, uint8 type)
{
if (!initialized_ || !sender || !group || !target || type != 0) return; // 0 = PingSubjectType::Attack
if (target->IsPlayer() || !target->IsAlive() || !sender->IsValidAttackTarget(target)) return;
TC_LOG_INFO("playerbot.v2", "[ping] {} pings attack on {}", sender->GetName(), target->GetName());
// "attack" acts on the sender's selection: the ping picks the mob.
sender->SetSelection(target->GetGUID());
RouteGroupOrder(sender, group, "attack");
}
void Module::OnGroupPingPoint(Player* sender, Group* group, Position const& point, uint8 type)
{
if (!initialized_ || !sender || !group) return;
// A "Place at cursor" from the Positions tab waits for this: any ping on the ground is the spot (BotCommandParser::TakePendingPlace).
if (BotCommandParser::TakePendingPlace(sender, point))
{
TC_LOG_INFO("playerbot.v2", "[ping] {} pings ({:.1f},{:.1f}) type {}: the Positions spot", sender->GetName(), point.GetPositionX(),
point.GetPositionY(), type);
return;
}
if (type != 3) return; // 3 = PingSubjectType::OnMyWay
uint32 const account = sender->GetSession() ? sender->GetSession()->GetAccountId() : 0;
if (account == 0) return;
uint32 set = 0;
for (GroupReference const& itr : group->GetMembers())
{
Player* m = itr.GetSource();
if (!m || m == sender || !m->IsAlive() || m->GetMapId() != sender->GetMapId()) continue;
BotId const mid = m->GetGUID().GetCounter();
if (!Services::Registry().has(mid) || !BotCommandParser::ControlsForGroupOrder(sender, m)) continue;
if (BotAI* ai = Services::Registry().ai(mid))
{
ai->set_rally_point(sender->GetMapId(), point.GetPositionX(), point.GetPositionY(), point.GetPositionZ());
++set;
}
}
if (!set) return;
TC_LOG_INFO("playerbot.v2", "[ping] {} pings on my way to ({:.1f},{:.1f}): {} bots fall back there", sender->GetName(),
point.GetPositionX(), point.GetPositionY(), set);
RouteGroupOrder(sender, group, "fallback");
}
void Module::OnWorldMarkerPlaced(Player* sender, Position const& point)
{
if (!initialized_ || !sender) return;
if (BotCommandParser::TakePendingPlace(sender, point))
TC_LOG_INFO("playerbot.v2", "[world_marker] {} marks ({:.1f},{:.1f}): the Positions spot", sender->GetName(), point.GetPositionX(),
point.GetPositionY());
}
void Module::NoteAddonHello(Player* p)
{
if (!initialized_ || !p) return;
if (g_addon_hints.Pending(p->GetGUID().GetCounter()))
TC_LOG_INFO("playerbot.v2", "[addon_hello] {} has the ForeverBots add-on", p->GetName());
g_addon_hints.Hello(p->GetGUID().GetCounter());
}
void Module::SendDueAddonHints()
{
if (!Services::Config().addon_hint_enabled())
return;
for (uint64 guid : g_addon_hints.TakeDue(GameTime::GetGameTimeMS()))
{
Player* p = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(guid));
if (!p || !p->IsInWorld() || !p->GetSession() || p->GetSession()->IsBot())
continue;
Playerbot::BotLanguage::LocalizedHandler(p->GetSession()).PSendSysMessage("WoW Forever Bots add-on (buttons for your bots, Raid ready): %s - unzip it into Interface\\AddOns and type /reload.",
Services::Config().addon_hint_url().c_str());
TC_LOG_INFO("playerbot.v2", "[addon_hint] {} has no ForeverBots add-on: link sent", p->GetName());
}
}
void Module::OnAddonWhisper(Player* sender, Player* receiver, std::string const& prefix, std::string const& text)
{
// The add-on's greeting to itself on login: this client has it, so no hint.
if (initialized_ && sender && sender == receiver && prefix == kAddonOrderPrefix && text == kAddonHelloText)
{
NoteAddonHello(sender);
return;
}
// A ".bot ..." command the add-on could not say (sent from a timer): run it as if the player had typed it.
if (initialized_ && sender && sender == receiver && prefix == kAddonOrderPrefix && IsAddonBotCommand(text))
{
if (WorldSession* session = sender->GetSession(); session && !session->IsBot())
{
NoteAddonHello(sender);
TC_LOG_INFO("playerbot.v2", "[addon_command] {}: {}", sender->GetName(), text);
ChatHandler(session).ParseCommands(text);
}
return;
}
if (!initialized_ || !sender || !receiver || text.empty() || prefix != kAddonOrderPrefix) return;
TC_LOG_INFO("playerbot.v2", "[addon_whisper] {} -> {}", sender->GetName(), receiver->GetName());
OnWhisperReceived(sender, receiver, text);
}
void Module::OnPartyChat(Player* sender, Group* group, std::string const& msg, bool raid_chat)
{
// Squad-chat command surface. Only messages prefixed with `;` are
// routed; every other party-chat line is ignored so we don't spam
// bots with the human chatter that fills /p during dungeons.
//
// The leading `;` is stripped, then the rest is treated like a
// whisper to the first owned bot in the group — the dispatch path
// resolves any address prefix (`tank:`, `mage:`, `Areon:`, ...)
// and applies to all matching bots. The whispered "primary" bot
// is the messenger that emits the single summary reply.
if (!initialized_ || !sender || !group || msg.empty()) return;
// Social reaction layer: lets bots respond to "ty"/"gz"/"lol"/their
// own name with short conversational replies so group chat reads like
// a room of real players. Runs for every message; the reactor itself
// gates on bot verbosity, per-bot throttle, and skips bot-sent lines.
BotChatReactor::React(sender, group, msg, raid_chat);
if (msg.front() != ';')
{
// The owner's crowd-control orders ("no fear", "@warlock no fear", "@mages no sheep", "off cc"): stored once for the
// sender, answered once, from a player who commands a bot in this group (anyone else's "no fear" stays chatter).
// The same for a boss plan ("tactics Lucifron: adds first, no aoe, tank here"; Bot/Raid/BossTactics.h).
bool const cc_order = CcDeny::ParseOrder(msg).has_value();
bool const tactics_order = !cc_order && Raid::ParseTacticsOrder(msg).has_value();
if (cc_order || tactics_order)
{
bool commands_a_bot = false;
for (GroupReference const& itr : group->GetMembers())
if (Player const* m = itr.GetSource(); m && m != sender && Services::Registry().has(m->GetGUID().GetCounter()) &&
BotCommandParser::ControlsForGroupOrder(sender, m))
{ commands_a_bot = true; break; }
if (commands_a_bot && cc_order && BotCommandParser::ApplyCcOrder(sender, msg, "party"))
return;
if (commands_a_bot && tactics_order && BotCommandParser::ApplyTacticsOrder(sender, msg, raid_chat ? "raid" : "party"))
return;
}
// A bare group order ("follow", "stay", ...) in party or raid chat goes to every bot of the sender's in the
// group. A line that starts with the name of a bot in the group, or with "all", is that bot's (every bot's)
// order without the ';' ("Lentha use altar", "all use altar"). Other chatter stays ignored.
// "go" while the tank leads or is paused picks the lead up (BotCommandParser::ApplyLeadOrder); otherwise it is chatter.
if (BotCommandParser::IsLeadGoWord(msg) && BotCommandParser::ApplyLeadOrder(sender, msg, raid_chat ? "raid" : "party"))
return;
if (auto const order = ClassifyGroupOrder(msg, Services::Config().behavior_owner_commands()))
RouteGroupOrder(sender, group, *order);
else if (auto const addressed = AddressedGroupOrder(msg, [group](std::string_view name) { return GroupHasBotNamed(group, name); }))
DispatchSquadLine(sender, group, *addressed, false);
else
BotChatReactor::ReactPartyModel(sender, group, msg, raid_chat); // Playerbot.ChatModel (no-op when off)
return;
}
const std::string body = msg.substr(1);
if (body.empty()) return;
DispatchSquadLine(sender, group, body, true);
}
bool Module::GroupHasBotNamed(Group const* group, std::string_view name)
{
if (!group || name.empty()) return false;
for (GroupReference const& itr : group->GetMembers())
{
Player const* member = itr.GetSource();
if (!member) continue;
if (!Services::Registry().has(member->GetGUID().GetCounter())) continue;
if (StringEqualI(member->GetName(), name)) return true;
}
return false;
}
bool Module::AskModelSquadOrder(Player* sender, Group* group, std::string const& body)
{
if (!sender || !group || body.empty() || !BotBridge::ChatOn())
return false;
// "Name:rest" / "squad:rest" (AddressedGroupOrder), or a ';' line with no name. A role or class prefix ("tank: ...") stays the
// parser's: the program does not pick bots by role.
std::string name, text = body;
if (auto const colon = body.find(':'); colon != std::string::npos && body.find(' ') > colon)
{
name = body.substr(0, colon);
text = body.substr(colon + 1);
if (StringEqualI(name, "squad") || StringEqualI(name, "all") || StringEqualI(name, "everyone"))
name.clear();
else if (!GroupHasBotNamed(group, name))
return false;
}
text.erase(0, text.find_first_not_of(' '));
text.erase(text.find_last_not_of(' ') + 1);
if (text.empty())
return false;
std::vector<Player*> bots;
for (GroupReference const& itr : group->GetMembers())
{
Player* m = itr.GetSource();
if (!m || m == sender || !Services::Registry().has(m->GetGUID().GetCounter()) || !m->IsInWorld())
continue;
if (!name.empty() ? StringEqualI(m->GetName(), name) : BotCommandParser::ControlsForGroupOrder(sender, m))
bots.push_back(m);
}
return BotBridge::AskGroupOrder(sender, bots, group->isRaidGroup() ? "raid" : "party", text);
}
void Module::DispatchSquadLine(Player* sender, Group* group, std::string const& body, bool answer_unknown)
{
if (!sender || !group || body.empty()) return;
// Walk the group for the sender's first owned bot. We need a
// primary bot to attach the reply whisper to; if the owner has
// none in the group, route to the first owned-on-realm fallback.
const uint32 sender_account =
sender->GetSession() ? sender->GetSession()->GetAccountId() : 0;
if (sender_account == 0) return;
Player* primary = nullptr;
// Pass 1: prefer an EXPLICITLY OWNED bot in the group. This is the
// canonical squad-control path — the player has bound bots via
// /squad mark or similar and wants commands routed to their squad.
Player* any_group_bot = nullptr;
for (GroupReference const& itr : group->GetMembers())
{
Player* member = itr.GetSource();
if (!member || member == sender) continue;
const BotId mid = member->GetGUID().GetCounter();
if (!Services::Registry().has(mid)) continue;
if (!any_group_bot) any_group_bot = member; // remember for pass 2
if (Services::Owners().IsOwner(mid, sender_account,
sender->GetGUID().GetCounter()))
{ primary = member; break; }
}
// Pass 2: any owned bot online (off-tank parked elsewhere, etc.).
if (!primary)
{
std::vector<BotId> const owned =
Services::Owners().BotsOwnedBy(sender_account);
for (BotId mid : owned)
{
ObjectGuid g = ObjectGuid::Create<HighGuid::Player>(mid);
if (Player* p = ObjectAccessor::FindConnectedPlayer(g))
{ primary = p; break; }
}
}
// Pass 3: any V2 bot in the group. Covers the LFG-formed-group case
// where the player ran the dungeon-finder UI and joined alongside
// auto-spawned bots they don't formally own — the player is sharing
// the dungeon with them and should be able to drive their behavior
// (run/stop/pull/etc.). Authority is still scoped to the group: a
// bot in someone else's group can't be commanded from outside.
if (!primary)
primary = any_group_bot;
if (!primary) return;
if (!BotCommandParser::Dispatch(sender, primary, body, "party;") && !AskModelSquadOrder(sender, group, body) && answer_unknown)
{
// A ";" line is meant as an order: an unknown or refused one answers instead of vanishing. (A plain whisper falls
// through to the social reactor and a bare party word is chatter, so only this explicit route replies.)
BotId const pid = primary->GetGUID().GetCounter();
if (Services::HasIntents(pid))
{
Intent it{};
it.bot_id = pid;
it.body = ChatIntent{WhisperIntent{
sender->GetName(),
fmt::format("{}: no order '{}' (unknown, or not yours to give)", primary->GetName(), body.substr(0, 40))}};
Services::Intents(pid).push(std::move(it));
}
}
}
void Module::OnPlayerJoinedBgQueue(Player* player, uint32 bg_type_id, uint8 bracket)
{
if (!initialized_ || !player) return;
// SOLO bots ignore (they're queued by Filler intent emit, not by core
// path — re-firing the fill for each would recurse). A bot GROUP LEADER
// is the exception (audit B25): a premade (guild BG-night /
// BgTeamForming) that just queued needs the OPPOSING faction filled or
// the match can never form — the human-player path never fires for it
// and SeedBgMatches only seeds ambient matches when no BG is active.
if (player->GetSession() && player->GetSession()->IsBot())
{
Group const* g = player->GetGroup();
if (!g || g->GetLeaderGUID() != player->GetGUID())
return;
// fall through: bot premade leader — fill both factions below
}
// A real player waiting for a place: MakeRoomForRealPlayers watches the wait.
Fleet::BotQueueFiller::NoteHumanBgQueue(player, bg_type_id, bracket);
// A running battleground of this type that has or can make room: the human joins it (a bot leaves), no second
// instance is opened and no bot fill is queued (Playerbot.Behavior.BgHumanPriority).
uint32 claimed_open_slots = 0;
bool claimed = false;
if (player->GetSession() && !player->GetSession()->IsBot() &&
Fleet::BotQueueFiller::ClaimRunningBgForHuman(player, bg_type_id, bracket, &claimed_open_slots))
{
// Nearly full already: nothing to add. Otherwise the fill below tops the instance up while the human is porting in
// (TC's queue update sends the queued bots to the instance with a human first).
if (claimed_open_slots <= 2)
return;
claimed = true;
}
Fleet::BotQueueFiller filler;
Fleet::BotQueueFiller::FillRequest req{};
req.kind = Fleet::BotQueueFiller::QueueKind::Bg;
req.bracket = bracket;
req.faction = uint32(player->GetTeam());
req.instance_id = bg_type_id;
req.requesting_player = player;
// Override target level to the queuing player's actual level. The
// PVPDifficultyEntry bracket ID is NOT level/10 — for AV bracket 0
// corresponds to L51-60 (or similar), not L0-9 as BracketMidpoint
// would compute. Without this override the filler searches the wrong
// level pool and returns no candidates. Observed 2026-05-13: AV queue
// logged "kind=0 bracket=0 instance=1 needs=3T/3H/30D" with horde
// online_seen=250 but only 7 actually queued because the level-15
// midpoint with ±15 window only matched L0-30 bots, when the user
// (and the bracket's actual range) was L60+. Same issue as LFG —
// same fix.
req.target_level_override = uint8(std::clamp(int(player->GetLevel()), 1, int(MaxPlayerLevel())));
// Cap initial queue fill at max_per_team for the BG so we never
// overpopulate. The BattlegroundTemplate exposes the cap; for a
// 10v10 BG (WSG/TP/AB/etc.) this is 10. Without the cap, the
// default NeedsFor(Bg) of 3T/3H/30D = 36 per side overshot —
// observed WSG with 13 alliance vs 7 horde when surplus invites
// landed before TC's per-team check could reject them.
if (BattlegroundTemplate const* tmpl =
sBattlegroundMgr->GetBattlegroundTemplateByTypeId(BattlegroundTypeId(bg_type_id)))
{
if (uint16 const max_per_team = tmpl->GetMaxPlayersPerTeam())
req.max_total_bots = uint8(std::min<uint16>(max_per_team, 255));
// Look up the actual bracket level range on this BG's map so the
// filler can reject bots whose level is outside [min, max]. Without
// this, the ±15 coarse window pulled in bots (e.g., L10-17 for an
// L22 user) that AV's PVPDifficulty doesn't have a bracket for —
// every queue intent then fails `no_bracket` in API::bg_queue and
// TC's matchmaker never gets enough valid candidates to create
// the BG. Crash 2026-05-13: AV stuck not starting.
if (!tmpl->MapIDs.empty())
{
if (PVPDifficultyEntry const* diff =
DB2Manager::GetBattlegroundBracketById(tmpl->MapIDs.front(),
BattlegroundBracketId(bracket)))
{
req.bracket_min_level = uint8(std::clamp<int32>(diff->MinLevel, 1, int32(MaxPlayerLevel())));
req.bracket_max_level = uint8(std::clamp<int32>(diff->MaxLevel, 1, int32(MaxPlayerLevel())));
}
}
}
if (claimed)
req.max_total_bots = uint8(std::min<uint32>(req.max_total_bots ? req.max_total_bots : 255u, claimed_open_slots));
filler.Fill(req);
// BotCoordinationBus publish — BG team formation signal. Existing
// BotQueueFiller path stays the primary filler (battle-tested);
// bus subscribers (BotGroupBuilder) form an additional premade
// 5-bot squad on the same faction so the queue has a cohesive
// group entry alongside the solo invites.
// Only a human's queue forms the premade: the premade's own leader queues through this hook too, and publishing again for it
// formed a new 5-bot premade per round (same-faction overshoot, audit B 1.1).
bool const queuerIsBot = player->GetSession() && player->GetSession()->IsBot();
if (Services::Initialized() && !queuerIsBot)
{
V2::CoordEvent ev{};
ev.kind = V2::CoordSignal::BgTeamForming;
ev.origin_low = player->GetGUID().GetCounter();
ev.content_id = bg_type_id;
ev.level_min = req.bracket_min_level;
ev.level_max = req.bracket_max_level;
ev.faction_mask = (player->GetTeam() == ALLIANCE) ? 0x1u : 0x2u;
Services::Coordination().Publish(ev);
}
}
bool Module::HoldNewBattleground(uint32 bg_type_id, uint8 bracket)
{
return initialized_ && Fleet::BotQueueFiller::HoldNewBattleground(bg_type_id, bracket);
}
void Module::OnPlayerJoinedLfg(Player* player, uint32 dungeon_id, uint8 /*role_mask*/)
{
if (!initialized_ || !player) return;
if (player->GetSession() && player->GetSession()->IsBot()) return;
Fleet::BotQueueFiller filler;
Fleet::BotQueueFiller::FillRequest req{};
// Detect raid vs 5-man dungeon from the LFGDungeons DB2 entry. LFR/raid
// queues require Raid10 / Raid20 composition (2T/3H/5D or 2T/4H/14D)
// rather than 1T/1H/3D. Pulling group-size from CountTank+CountHealer+
// CountDamage covers any future content without a hard-coded type map.
req.kind = Fleet::BotQueueFiller::QueueKind::Dungeon5;
// Target level: derived from the DUNGEON's level range, falling back
// to the QUEUEING PLAYER's level. Both bypass the coarse
// BracketMidpoint mapping (which rounds level/10 to a midpoint that's
// off by 10+ for low/mid brackets — e.g. L21 → bracket 2 → midpoint
// 35, far too high for L17-24 dungeons). Empirical log confirmation
// from a L21 Balastan in Wailing Caverns:
// [QueueFill] kind=1 bracket=2 needs=1T/1H/3D
// pool faction=HORDE … level=175 → 175 horde bots rejected by ±5
// filter around midpoint 35. ContentTuningID was 0 for that dungeon
// (vanilla data) so the prior ContentTuning lookup didn't fire.
// Use the player's exact level as a robust fallback — level-scaling
// and ±15 LFG window will absorb minor under/over-match.
req.bracket = uint8(player->GetLevel() / 10);
req.target_level_override = uint8(std::clamp(int(player->GetLevel()), 1, int(MaxPlayerLevel())));
if (LFGDungeonsEntry const* d = sLFGDungeonsStore.LookupEntry(dungeon_id))
{
const uint32 size = uint32(d->CountTank) + uint32(d->CountHealer) +
uint32(d->CountDamage);
if (size > 5 && size <= 12) req.kind = Fleet::BotQueueFiller::QueueKind::Raid10;
else if (size > 12) req.kind = Fleet::BotQueueFiller::QueueKind::Raid20;
// size <= 5 keeps Dungeon5 (scenarios use size 3 — still Dungeon5
// composition-wise, the filler clamps to <=5 fill).
if (d->ContentTuningID)
{
if (Optional<ContentTuningLevels> lv =
sDB2Manager.GetContentTuningData(d->ContentTuningID, {}))
{
const int16 mid = (lv->TargetLevelMin > 0 || lv->TargetLevelMax > 0)
? int16((lv->TargetLevelMin + lv->TargetLevelMax) / 2)
: int16((lv->MinLevel + lv->MaxLevel) / 2);
if (mid > 0)
req.target_level_override = uint8(std::clamp(int(mid), 1, int(MaxPlayerLevel())));
}
}
}
req.faction = uint32(player->GetTeam());
req.instance_id = dungeon_id;
req.requesting_player = player;
filler.Fill(req);
// Enqueue for periodic top-up. Subsequent Fill retries strip the
// requesting_player pointer (it could go stale across 30s) and
// also clear the role-deficit at refill time by counting bots
// already in the queue for this dungeon.
{
Fleet::BotQueueFiller::FillRequest persisted = req;
persisted.requesting_player = nullptr;
const uint32 now_ms = getMSTime();
std::lock_guard lk(pending_lfg_refills_mtx_);
// Replace any existing entry for this player (re-queue resets).
const uint64 pguid = player->GetGUID().GetCounter();
std::erase_if(pending_lfg_refills_,
[pguid](PendingLfgRefill const& e){ return e.player_guid_low == pguid; });
pending_lfg_refills_.push_back(PendingLfgRefill{
pguid, std::move(persisted), now_ms, now_ms});
}
// BotCoordinationBus publish — fan out role-need signals so any
// subscriber (BotGroupBuilder, future analytics) can react.
// The original BotQueueFiller path above still handles the
// primary fill; this publishes a coarse "tank/healer/dps needed"
// event with the requesting player's level as the target so role-
// specific subscribers can pick targeted bots.
if (Services::Initialized())
{
V2::CoordEvent ev{};
ev.origin_low = player->GetGUID().GetCounter();
ev.content_id = dungeon_id;
ev.level_min = uint8(std::max(1, int(player->GetLevel()) - 5));
ev.level_max = uint8(std::min(int(MaxPlayerLevel()), int(player->GetLevel()) + 5));
ev.faction_mask = (player->GetTeam() == ALLIANCE) ? 0x1u : 0x2u;
// Publish all three role signals so subscribers can pick what
// they handle. Subscriber filtering happens at the handler
// level — cheap.
ev.kind = V2::CoordSignal::LfgTankNeeded;
Services::Coordination().Publish(ev);
ev.kind = V2::CoordSignal::LfgHealerNeeded;
Services::Coordination().Publish(ev);
ev.kind = V2::CoordSignal::LfgDpsNeeded;
Services::Coordination().Publish(ev);
}
}
namespace
{
// The invite hook passes the battleground's own type, not its queue. BattlegroundMgr::GetBattleground finds the instance through the
// queue's BattlemasterListId only, so a queue id built from that type reaches the same instance.
Battleground* FindInvitedBattleground(uint32 bg_instance_id, uint32 bg_type_id)
{
return sBattlegroundMgr->GetBattleground(bg_instance_id,
BattlegroundMgr::BGQueueTypeId(uint16(bg_type_id), BattlegroundQueueIdType::Battleground, false, 0));
}
}
void Module::OnBGInvitationReceived(Player* player, uint32 bg_instance_id, uint32 bg_type_id)
{
// Synchronous handler fired from BattlegroundQueue::InviteGroupToBG /
// BGQueueInviteEvent::Execute the instant TC sends the
// BattlefieldStatusNeedConfirmation packet. The queue is mid-iteration
// here — we MUST NOT mutate BG queue data, AND we MUST NOT port bots
// in the same tick (or even seconds-later) as the invite if a real
// human is also invited — bots filling the BG can advance its state
// and silently invalidate the human's invite.
//
// Strategy:
// - Human invitee: just bump expected_humans for this instance, return.
// - Bot invitee: enqueue deferred port; FireDueBgPorts (run each tick
// before DrainIntents) fires it once gating passes:
// * 0 expected humans → fire after a 0-1500ms stagger.
// * 1+ expected humans → fire only once a human is in the BG
// instance (sticky bool, then small per-bot stagger).
// * 80s hard expiry → drop (TC's 90s INVITE_ACCEPT_WAIT_TIME -10s).
// - Snapshot-poll idle:bg_port_accept rule is suppressed for bg_type_id
// via BgPort cooldown stamp so it can't preempt the gating.
if (!initialized_ || !player) return;
uint32 const now_ms = GameTime::GetGameTimeMS();
bool const is_bot = player->GetSession() && player->GetSession()->IsBot();
if (!is_bot)
{
std::lock_guard lk(pending_bg_ports_mtx_);
++bg_invite_state_[bg_instance_id].expected_humans;
return;
}
BotId const id = player->GetGUID().GetCounter();
auto& reg = Services::Registry();
if (!reg.has(id)) return;
// CRASH FIX 2026-05-13: TC fires this hook from BOTH
// BattlegroundQueue::InviteGroupToBG (initial) AND BGQueueInviteEvent
// ::Execute (~30s reminder). Without per-(bot,bg_instance) dedup we'd
// queue two staggered ports for the same bot — the second fires after
// the first port succeeded, re-entering API::bg_port while the bot is
// mid-teleport, tripping Map::RemovePlayerFromMap's ASSERT(remove)
// when the bot isn't in any grid (Map.cpp:935). Two guards:
// (a) skip if a pending entry for (bot, bg_instance) already exists
// (b) skip if the bot is ALREADY in the target BG instance (port
// already completed; the reminder is just informational).
if (player->InBattleground())
{
Battleground const* current = player->GetBattleground();
if (current && current->GetInstanceID() == bg_instance_id)
return; // already in this BG — nothing to do
}
{
std::lock_guard lk(pending_bg_ports_mtx_);
for (auto const& p : pending_bg_ports_)
{
if (p.bot_id == uint64(id) && p.bg_instance_id == bg_instance_id)
return; // dedup
}
pending_bg_ports_.push_back({ now_ms, bg_instance_id, uint64(id),
uint16(bg_type_id),
/*bg_template_type_id*/ uint32(bg_type_id) });
}
if (BotAI* ai = reg.ai(id))
ai->note_action_retry(BotAI::ActionKind::BgPort,
uint64(bg_type_id), now_ms);
TC_LOG_INFO("playerbot.v2",
"[OnBGInvitationReceived] {} deferred port bg_inst={} bg_type_id={} (gating on human-first)",
player->GetName(), bg_instance_id, bg_type_id);
}
void Module::FireDueBgPorts()
{
uint32 const now_ms = GameTime::GetGameTimeMS();
std::vector<PendingBgPort> to_fire;
{
std::lock_guard lk(pending_bg_ports_mtx_);
if (pending_bg_ports_.empty()) return;
auto it = pending_bg_ports_.begin();
while (it != pending_bg_ports_.end())
{
// Expire stale entries (TC's INVITE_ACCEPT_WAIT_TIME is 90s).
if (now_ms - it->created_at_ms > 80u * 1000u)
{
it = pending_bg_ports_.erase(it);
continue;
}
auto state_it = bg_invite_state_.find(it->bg_instance_id);
uint32 const expected_humans = (state_it != bg_invite_state_.end())
? state_it->second.expected_humans : 0u;
bool human_in_bg = (state_it != bg_invite_state_.end())
? state_it->second.any_human_seen : false;
// No humans invited → fire after light per-bot stagger so the BG
// doesn't flood-start the same tick.
if (expected_humans == 0)
{
// A human of this type is queued and not invited yet: TC's prep timer starts with the first player inside, so
// bots going in now would burn the human's wait. Hold until the human has an invitation (80 s expiry above).
if (Battleground const* held_bg = FindInvitedBattleground(it->bg_instance_id, it->bg_template_type_id))
if (Fleet::BotQueueFiller::HasWaitingHuman(it->bg_template_type_id, uint8(held_bg->GetBracketId())))
{
++it;
continue;
}
uint32 const stagger_ms = uint32(it->bot_id % 1500u);
if (now_ms - it->created_at_ms >= stagger_ms)
{
to_fire.push_back(*it);
it = pending_bg_ports_.erase(it);
}
else
++it;
continue;
}
// Humans invited → only fire once a human is actually IN the BG.
// Once flagged sticky, the gate stays open for that instance.
if (!human_in_bg)
{
Battleground* bg = FindInvitedBattleground(it->bg_instance_id, it->bg_template_type_id);
if (bg)
{
for (auto const& [guid, _] : bg->GetPlayers())
{
Player* p = ObjectAccessor::FindPlayer(guid);
if (p && p->GetSession() && !p->GetSession()->IsBot())
{
human_in_bg = true;
if (state_it != bg_invite_state_.end())
state_it->second.any_human_seen = true;
break;
}
}
}
if (!human_in_bg) { ++it; continue; } // wait
}
// Gate open. Apply a small 0-1500ms per-bot stagger from the
// moment the gate opened (created_at + 0 if gate already open
// at invite-time, or current tick-time otherwise — using created_at
// here gives a deterministic spread per bot anchored to invite).
uint32 const stagger_ms = uint32(it->bot_id % 1500u);
if (now_ms - it->created_at_ms >= stagger_ms)
{
to_fire.push_back(*it);
it = pending_bg_ports_.erase(it);
}
else
++it;
}
}
if (to_fire.empty()) return;
auto& reg = Services::Registry();
for (auto const& p : to_fire)
{
BotId const id = BotId(p.bot_id);
if (!reg.has(id)) continue;
// CRASH FIX 2026-05-13: belt-and-braces re-check at fire time —
// between push and fire, the bot may have already entered the BG
// (via the snapshot-poll fallback or a prior hook fire). Pushing
// another BgPortIntent in that state re-enters API::bg_port with
// currentBg == target_bg and races the teleport flow, tripping
// Map::RemovePlayerFromMap ASSERT. Skip if already in target.
Player* bot = ObjectAccessor::FindConnectedPlayer(
ObjectGuid::Create<HighGuid::Player>(uint64(p.bot_id)));
if (bot)
{
if (Battleground const* cur = bot->GetBattleground())
if (cur->GetInstanceID() == p.bg_instance_id)
continue;
}
IntentQueue* iq = reg.intents(id);
IntentId* nid = reg.next_intent_id(id);
if (!iq || !nid) continue;
Intent intent;
intent.id = ++(*nid);
intent.bot_id = id;
intent.source_snapshot = 0;
intent.body = QueueIntent{BgPortIntent{p.bg_type_id, /*accept*/ true}};
iq->push(std::move(intent));
if (bot)
{
TC_LOG_INFO("playerbot.v2",
"[BgPortFire] {} faction={} bg_inst={} bg_type={} (deferred {}ms)",
bot->GetName(),
bot->GetTeam() == ALLIANCE ? "ALLIANCE" : "HORDE",
p.bg_instance_id, p.bg_type_id,
now_ms - p.created_at_ms);
}
}
}
void Module::OnLfgProposalReceived(Player* player, uint32 proposal_id)
{
// Synchronous handler fired from LFGMgr::AddProposal. The proposal table
// is mid-iteration here so we MUST NOT call sLFGMgr->UpdateProposal
// directly — push an LfgProposalRespondIntent which drains safely on
// the next world-tick DrainIntents pass.
if (!initialized_ || !player) return;
if (!player->GetSession() || !player->GetSession()->IsBot()) return;
if (proposal_id == 0) return;
BotId const id = player->GetGUID().GetCounter();
auto& reg = Services::Registry();
if (!reg.has(id)) return;
uint32 const now_ms = GameTime::GetGameTimeMS();
{
std::lock_guard lk(pending_lfg_accepts_mtx_);
pending_lfg_accepts_.push_back({ now_ms, uint64(id), proposal_id });
}
// Suppress snapshot-poll idle:lfg_proposal_accept so it can't double-fire.
if (BotAI* ai = reg.ai(id))
ai->set_lfg_proposal_acked_id(proposal_id);
TC_LOG_INFO("playerbot.v2",
"[OnLfgProposalReceived] {} deferred accept proposal_id={}",
player->GetName(), proposal_id);
}
void Module::FireDueLfgAccepts()
{
uint32 const now_ms = GameTime::GetGameTimeMS();
std::vector<PendingLfgAccept> to_fire;
{
std::lock_guard lk(pending_lfg_accepts_mtx_);
if (pending_lfg_accepts_.empty()) return;
auto it = pending_lfg_accepts_.begin();
while (it != pending_lfg_accepts_.end())
{
// Expire after 35s (TC's LFG_TIME_PROPOSAL is 40s).
if (now_ms - it->created_at_ms > 35u * 1000u)
{
it = pending_lfg_accepts_.erase(it);
continue;
}
// 0-800ms per-bot stagger so 5 (or 25) bots don't all fire intents
// the exact same tick. Deterministic per bot_id.
uint32 const stagger_ms = uint32(it->bot_id % 800u);
if (now_ms - it->created_at_ms >= stagger_ms)
{
to_fire.push_back(*it);
it = pending_lfg_accepts_.erase(it);
}
else
++it;
}
}
if (to_fire.empty()) return;
auto& reg = Services::Registry();
for (auto const& p : to_fire)
{
BotId const id = BotId(p.bot_id);
if (!reg.has(id)) continue;
IntentQueue* iq = reg.intents(id);
IntentId* nid = reg.next_intent_id(id);
if (!iq || !nid) continue;
Intent intent;
intent.id = ++(*nid);
intent.bot_id = id;
intent.source_snapshot = 0;
intent.body = QueueIntent{LfgProposalRespondIntent{p.proposal_id, /*accept*/ true}};
iq->push(std::move(intent));
}
}
void Module::TopUpPendingLfg(uint32 now_ms)
{
constexpr uint32 kLfgTopUpIntervalMs = 30u * 1000u;
// Snapshot the entries to refill outside the lock so the Fill calls
// (which may take >1ms each) don't hold the mutex.
std::vector<PendingLfgRefill> to_refill;
{
std::lock_guard lk(pending_lfg_refills_mtx_);
auto it = pending_lfg_refills_.begin();
while (it != pending_lfg_refills_.end())
{
// Drop entries older than 15 min — by then the queue has
// either popped, been canceled, or gone stale (the player
// moved on). Without this entries leak forever on logout.
if (now_ms - it->created_at_ms > 15u * 60u * 1000u)
{
it = pending_lfg_refills_.erase(it);
continue;
}
// Drop entries whose player is no longer in LFG_STATE_QUEUED.
// sLFGMgr->GetState returns LFG_STATE_NONE for offline / not-
// queued, LFG_STATE_PROPOSAL once a match formed, etc. Either
// way the top-up is no longer needed.
ObjectGuid pguid = ObjectGuid::Create<HighGuid::Player>(it->player_guid_low);
lfg::LfgState const st = sLFGMgr->GetState(pguid);
if (st != lfg::LFG_STATE_QUEUED)
{
it = pending_lfg_refills_.erase(it);
continue;
}
// Cron cadence: refill at most every 30s per entry.
if (now_ms - it->last_refill_ms < kLfgTopUpIntervalMs)
{
++it;
continue;
}
it->last_refill_ms = now_ms;
to_refill.push_back(*it);
++it;
}
}
if (to_refill.empty()) return;
auto& reg = Services::Lifecycle();
auto ids = reg.snapshot_ids();
for (auto& entry : to_refill)
{
// Count our online V2 bots already in LFG queue for this dungeon
// by role. Subtract from the kind's default composition to get
// the deficit. The walk is O(online bots) — bounded by what's
// logged in, scales fine.
uint32 q_t = 0, q_h = 0, q_d = 0;
for (BotId id : ids)
{
ObjectGuid g = ObjectGuid::Create<HighGuid::Player>(id);
Player* bot = ObjectAccessor::FindConnectedPlayer(g);
if (!bot) continue;
if (sLFGMgr->GetState(g) != lfg::LFG_STATE_QUEUED) continue;
lfg::LfgDungeonSet const& dgs = sLFGMgr->GetSelectedDungeons(g);
if (!dgs.count(entry.req.instance_id)) continue;
uint8 const roles = sLFGMgr->GetRoles(g);
if (roles & lfg::PLAYER_ROLE_TANK) ++q_t;
else if (roles & lfg::PLAYER_ROLE_HEALER) ++q_h;
else ++q_d;
}
// Composition target. 5-man dungeon default: 1T/1H/3D minus
// human player (-1 to whichever role they queued as — but
// approximate by subtracting from DPS since most humans queue
// DPS; if T/H human, we'd over-spawn by one which the LFG
// queue de-dups anyway). For raids the kind sets bigger values
// via NeedsFor in BotQueueFiller — here we hard-code 5-man
// since that's what dungeon top-ups are about.
uint8 target_t = 1, target_h = 1, target_d = 3;
if (entry.req.kind == Fleet::BotQueueFiller::QueueKind::Raid10)
{ target_t = 2; target_h = 3; target_d = 5; }
else if (entry.req.kind == Fleet::BotQueueFiller::QueueKind::Raid20)
{ target_t = 2; target_h = 4; target_d = 14; }
const uint8 need_t = (q_t >= target_t) ? 0 : uint8(target_t - q_t);
const uint8 need_h = (q_h >= target_h) ? 0 : uint8(target_h - q_h);
const uint8 need_d = (q_d >= target_d) ? 0 : uint8(target_d - q_d);
if (need_t + need_h + need_d == 0)
{
TC_LOG_INFO("playerbot.v2",
"[LfgTopUp] dungeon={} queue full ({}T/{}H/{}D); skipping",
entry.req.instance_id, q_t, q_h, q_d);
continue;
}
Fleet::BotQueueFiller::FillRequest req = entry.req;
req.needs_tank_override = need_t;
req.needs_healer_override = need_h;
req.needs_dps_override = need_d;
// Pass null player — the original is potentially offline / moved
// by now. BotQueueFiller doesn't strictly need the pointer; it
// uses req.faction and target_level_override for filtering.
req.requesting_player = nullptr;
TC_LOG_INFO("playerbot.v2",
"[LfgTopUp] dungeon={} bot-queued so far {}T/{}H/{}D; refilling {}T/{}H/{}D",
entry.req.instance_id, q_t, q_h, q_d, need_t, need_h, need_d);
Fleet::BotQueueFiller filler;
filler.Fill(req);
}
}
void Module::OnBGPortFailed(Player* player, uint8 reason_code, uint32 bg_instance_id)
{
if (!initialized_ || !player) return;
// Inert for bots — they port via the V2 BgPortIntent pathway and don't
// use HandleBattleFieldPortOpcode. Diagnostic is for human players.
if (player->GetSession() && player->GetSession()->IsBot()) return;
char const* reason_name = "unknown";
switch (reason_code)
{
case 1: reason_name = "not_in_queue"; break;
case 2: reason_name = "invalid_queue_slot"; break;
case 3: reason_name = "no_group_info"; break;
case 4: reason_name = "ginfo_not_invited"; break;
case 5: reason_name = "bg_instance_gone"; break;
case 6: reason_name = "no_bracket_entry"; break;
case 7: reason_name = "freeze_debuff"; break;
case 8: reason_name = "player_invite_flag_cleared"; break;
}
TC_LOG_WARN("playerbot.v2",
"[OnBGPortFailed] player={} reason={} ({}) bg_instance={}",
player->GetName(), uint32(reason_code), reason_name, bg_instance_id);
}
void Module::OnPathOutcome(uint8 outcome)
{
if (!initialized_) return;
Services::Perf().record_path_outcome(static_cast<PerfCounters::PathOutcome>(outcome));
}
} // namespace Playerbot::V2