From 1f3d11d1c44e72251e90185010c5e71211a8fc29 Mon Sep 17 00:00:00 2001 From: Alex Dcnh <140754794+Wishmaster117@users.noreply.github.com> Date: Mon, 23 Feb 2026 22:19:56 +0100 Subject: [PATCH] Stage2 refactor switch custom calculations by core helpers clean (#2127) # Pull Request This change replaces a few manual distance calculations in `WorldPosition` with AzerothCore distance helpers. The goal is to reduce duplicated math, keep behavior consistent with core utilities, and avoid reimplementing logic that already exists in the core. --- ## Design Philosophy We prioritize **stability, performance, and predictability** over behavioral realism. Complex player-mimicking logic is intentionally limited due to its negative impact on scalability, maintainability, and long-term robustness. Excessive processing overhead can lead to server hiccups, increased CPU usage, and degraded performance for all participants. Because every action and decision tree is executed **per bot and per trigger**, even small increases in logic complexity can scale poorly and negatively affect both players and world (random) bots. Bots are not expected to behave perfectly, and perfect simulation of human decision-making is not a project goal. Increased behavioral realism often introduces disproportionate cost, reduced predictability, and significantly higher maintenance overhead. Every additional branch of logic increases long-term responsibility. All decision paths must be tested, validated, and maintained continuously as the system evolves. If advanced or AI-intensive behavior is introduced, the **default configuration must remain the lightweight decision model**. More complex behavior should only be available as an **explicit opt-in option**, clearly documented as having a measurable performance cost. Principles: - **Stability before intelligence** A stable system is always preferred over a smarter one. - **Performance is a shared resource** Any increase in bot cost affects all players and all bots. - **Simple logic scales better than smart logic** Predictable behavior under load is more valuable than perfect decisions. - **Complexity must justify itself** If a feature cannot clearly explain its cost, it should not exist. - **Defaults must be cheap** Expensive behavior must always be optional and clearly communicated. - **Bots should look reasonable, not perfect** The goal is believable behavior, not human simulation. Before submitting, confirm that this change aligns with those principles. --- ## Feature Evaluation Please answer the following: - Describe the **minimum logic** required to achieve the intended behavior? Use existing core distance helpers instead of manual math, keeping the logic localized to `WorldPosition`. - Describe the **cheapest implementation** that produces an acceptable result? Directly call `GetExactDist`, `GetExactDist2d`, and `GetExactDist2dSq` where appropriate. - Describe the **runtime cost** when this logic executes across many bots? No additional cost; the helper calls replace equivalent math and avoid extra intermediate objects. --- ## How to Test the Changes - Step-by-step instructions to test the change - Build the module and run existing bot scenarios that rely on `WorldPosition` distance checks. - Verify no behavioral regressions in travel-related logic. - Any required setup (e.g. multiple players, bots, specific configuration) - Standard server + mod-playerbots setup. - Expected behavior and how to verify it - Distances computed in travel logic remain identical; no gameplay change expected. ## Complexity & Impact Does this change add new decision branches? - - [x] No - - [ ] Yes (**explain below**) Does this change increase per-bot or per-tick processing? - - [x] No - - [ ] Yes (**describe and justify impact**) Could this logic scale poorly under load? - - [x] No - - [ ] Yes (**explain why**) --- ## Defaults & Configuration Does this change modify default bot behavior? - - [x] No - - [ ] Yes (**explain why**) If this introduces more advanced or AI-heavy logic: - - [x] Lightweight mode remains the default - - [x] More complex behavior is optional and thereby configurable --- ## AI Assistance Was AI assistance (e.g. ChatGPT or similar tools) used while working on this change? - - [x] No - - [ ] Yes (**explain below**) --- ## Final Checklist - - [x] Stability is not compromised - - [x] Performance impact is understood, tested, and acceptable - - [x] Added logic complexity is justified and explained - - [ ] Documentation updated if needed --- ## Notes for Reviewers This is a localized refactor that replaces manual distance math with core helpers for consistency and maintainability. No behavioral change is expected. --------- Co-authored-by: Keleborn <22352763+Celandriel@users.noreply.github.com> --- src/Ai/Base/Actions/RpgSubActions.cpp | 15 ++++++------ .../PitOfSaron/Action/PitOfSaronActions.cpp | 9 +++---- .../GruulsLair/Util/RaidGruulsLairHelpers.cpp | 6 ++--- .../Raid/Icecrown/Action/RaidIccActions.cpp | 24 +++++++------------ .../Util/RaidMagtheridonHelpers.cpp | 4 ++-- src/Mgr/Travel/TravelMgr.cpp | 6 ++--- src/Mgr/Travel/TravelMgr.h | 6 ++--- 7 files changed, 30 insertions(+), 40 deletions(-) diff --git a/src/Ai/Base/Actions/RpgSubActions.cpp b/src/Ai/Base/Actions/RpgSubActions.cpp index bc16ee75..727f67a8 100644 --- a/src/Ai/Base/Actions/RpgSubActions.cpp +++ b/src/Ai/Base/Actions/RpgSubActions.cpp @@ -52,14 +52,15 @@ GuidPosition RpgHelper::guidP() { return AI_VALUE(GuidPosition, "rpg target"); } ObjectGuid RpgHelper::guid() { return (ObjectGuid)guidP(); } -bool RpgHelper::InRange() -{ - GuidPosition gp = guidP(); - if (!gp) - return false; + bool RpgHelper::InRange() + { + GuidPosition targetGuid = guidP(); + if (!targetGuid) + return false; - return gp.sqDistance2d(bot) < INTERACTION_DISTANCE * INTERACTION_DISTANCE; -} + return bot->GetExactDist2dSq(targetGuid.GetPositionX(), targetGuid.GetPositionY()) < + INTERACTION_DISTANCE * INTERACTION_DISTANCE; + } void RpgHelper::setFacingTo(GuidPosition guidPosition) { diff --git a/src/Ai/Dungeon/PitOfSaron/Action/PitOfSaronActions.cpp b/src/Ai/Dungeon/PitOfSaron/Action/PitOfSaronActions.cpp index 577575d4..27546e79 100644 --- a/src/Ai/Dungeon/PitOfSaron/Action/PitOfSaronActions.cpp +++ b/src/Ai/Dungeon/PitOfSaron/Action/PitOfSaronActions.cpp @@ -201,8 +201,7 @@ bool IckAndKrickAction::ExplosiveBarrage(bool explosiveBarrage, Unit* boss) continue; // Check if position is within maximum allowed distance from boss - if (boss && sqrt(pow(potentialPos.GetPositionX() - boss->GetPositionX(), 2) + - pow(potentialPos.GetPositionY() - boss->GetPositionY(), 2)) > MAX_BOSS_DISTANCE) + if (boss && boss->GetDistance2d(potentialPos.GetPositionX(), potentialPos.GetPositionY()) > MAX_BOSS_DISTANCE) continue; // Score this position based on: @@ -215,8 +214,7 @@ bool IckAndKrickAction::ExplosiveBarrage(bool explosiveBarrage, Unit* boss) float minOrbDist = std::numeric_limits::max(); for (Unit* orb : orbs) { - float orbDist = sqrt(pow(potentialPos.GetPositionX() - orb->GetPositionX(), 2) + - pow(potentialPos.GetPositionY() - orb->GetPositionY(), 2)); + float orbDist = orb->GetDistance2d(potentialPos.GetPositionX(), potentialPos.GetPositionY()); minOrbDist = std::min(minOrbDist, orbDist); } score += minOrbDist * 2.0f; // Weight orb distance more heavily @@ -232,8 +230,7 @@ bool IckAndKrickAction::ExplosiveBarrage(bool explosiveBarrage, Unit* boss) // Factor in proximity to boss (closer is better, as long as we're safe from orbs) if (boss) { - float bossDist = sqrt(pow(potentialPos.GetPositionX() - boss->GetPositionX(), 2) + - pow(potentialPos.GetPositionY() - boss->GetPositionY(), 2)); + float bossDist = boss->GetDistance2d(potentialPos.GetPositionX(), potentialPos.GetPositionY()); // Add points for being closer to boss (inverse relationship) // but only if we're safely away from orbs if (minOrbDist > SAFE_DISTANCE) diff --git a/src/Ai/Raid/GruulsLair/Util/RaidGruulsLairHelpers.cpp b/src/Ai/Raid/GruulsLair/Util/RaidGruulsLairHelpers.cpp index 7ed82f19..b29549b8 100644 --- a/src/Ai/Raid/GruulsLair/Util/RaidGruulsLairHelpers.cpp +++ b/src/Ai/Raid/GruulsLair/Util/RaidGruulsLairHelpers.cpp @@ -128,7 +128,7 @@ namespace GruulsLairHelpers Unit* krosh = botAI->GetAiObjectContext()->GetValue("find target", "krosh firehand")->Get(); if (krosh && krosh->IsAlive()) { - float dist = sqrt(pow(pos.GetPositionX() - krosh->GetPositionX(), 2) + pow(pos.GetPositionY() - krosh->GetPositionY(), 2)); + float dist = krosh->GetDistance2d(pos.GetPositionX(), pos.GetPositionY()); if (dist < KROSH_SAFE_DISTANCE) isSafe = false; } @@ -136,7 +136,7 @@ namespace GruulsLairHelpers Unit* maulgar = botAI->GetAiObjectContext()->GetValue("find target", "high king maulgar")->Get(); if (botAI->IsRanged(bot) && maulgar && maulgar->IsAlive()) { - float dist = sqrt(pow(pos.GetPositionX() - maulgar->GetPositionX(), 2) + pow(pos.GetPositionY() - maulgar->GetPositionY(), 2)); + float dist = maulgar->GetDistance2d(pos.GetPositionX(), pos.GetPositionY()); if (dist < MAULGAR_SAFE_DISTANCE) isSafe = false; } @@ -182,7 +182,7 @@ namespace GruulsLairHelpers if (IsPositionSafe(botAI, bot, candidatePos)) { - float movementDistance = sqrt(pow(destX - bot->GetPositionX(), 2) + pow(destY - bot->GetPositionY(), 2)); + float movementDistance = bot->GetDistance2d(destX, destY); if (movementDistance < bestScore) { bestScore = movementDistance; diff --git a/src/Ai/Raid/Icecrown/Action/RaidIccActions.cpp b/src/Ai/Raid/Icecrown/Action/RaidIccActions.cpp index 95f5bc3b..762f0287 100644 --- a/src/Ai/Raid/Icecrown/Action/RaidIccActions.cpp +++ b/src/Ai/Raid/Icecrown/Action/RaidIccActions.cpp @@ -1800,8 +1800,7 @@ bool IccRotfaceTankPositionAction::HandleBigOozePositioning(Unit*) Unit* puddle = botAI->GetUnit(puddleGuid); if (puddle && botAI->GetAura("Ooze Flood", puddle)) { - float puddleDistance = std::sqrt(std::pow(newX - puddle->GetPositionX(), 2) + - std::pow(newY - puddle->GetPositionY(), 2)); + float puddleDistance = puddle->GetDistance2d(newX, newY); if (puddleDistance < puddleSafeDistance) { isSafeFromPuddles = false; @@ -1921,8 +1920,7 @@ bool IccRotfaceGroupPositionAction::MoveAwayFromPuddle(Unit* boss, Unit* puddle, float moveZ = bot->GetPositionZ(); // Check distances and line of sight - float newPuddleDistance = - sqrt(pow(moveX - puddle->GetPositionX(), 2) + pow(moveY - puddle->GetPositionY(), 2)); + float newPuddleDistance = puddle->GetDistance2d(moveX, moveY); float newCenterDistance = sqrt(pow(moveX - ICC_ROTFACE_CENTER_POSITION.GetPositionX(), 2) + pow(moveY - ICC_ROTFACE_CENTER_POSITION.GetPositionY(), 2)); @@ -2125,8 +2123,7 @@ bool IccRotfaceGroupPositionAction::FindAndMoveFromClosestMember(Unit* boss, Uni // Ensure the target position is at least 30 yards away from the puddle if (puddle) { - float puddleDistance = std::sqrt(std::pow(targetX - puddle->GetPositionX(), 2) + - std::pow(targetY - puddle->GetPositionY(), 2)); + float puddleDistance = puddle->GetDistance2d(targetX, targetY); if (puddleDistance < puddleSafeDistance) { // Adjust the target position to move further away from the puddle @@ -2203,8 +2200,7 @@ bool IccRotfaceMoveAwayFromExplosionAction::MoveToRandomSafeLocation() if (!puddle || !botAI->HasAura("Ooze Flood", puddle)) continue; - float puddleDistance = - std::sqrt(std::pow(moveX - puddle->GetPositionX(), 2) + std::pow(moveY - puddle->GetPositionY(), 2)); + float puddleDistance = puddle->GetDistance2d(moveX, moveY); if (puddleDistance < 30.0f) { // Adjust the position to move further away from the puddle @@ -2407,7 +2403,7 @@ bool IccPutricideGrowingOozePuddleAction::IsPositionTooCloseToOtherPuddles(float if (Aura* grow = unit->GetAura(SPELL_GROW_AURA)) safeDistance += (grow->GetStackAmount() * STACK_MULTIPLIER); - float dist = sqrt(pow(x - unit->GetPositionX(), 2) + pow(y - unit->GetPositionY(), 2)); + float dist = unit->GetDistance2d(x, y); if (dist < safeDistance) return true; } @@ -2650,7 +2646,7 @@ bool IccPutricideGasCloudAction::HandleGaseousBloatMovement(Unit* gasCloud) float minGasBombDist = FLT_MAX; for (Unit* bomb : gasBombs) { - float bombDist = sqrt(pow(testX - bomb->GetPositionX(), 2) + pow(testY - bomb->GetPositionY(), 2)); + float bombDist = bomb->GetDistance2d(testX, testY); if (bombDist < minGasBombDist) minGasBombDist = bombDist; } @@ -2717,8 +2713,7 @@ bool IccPutricideGasCloudAction::HandleGaseousBloatMovement(Unit* gasCloud) float minEmergencyGasBombDist = FLT_MAX; for (Unit* bomb : gasBombs) { - float bombDist = sqrt(pow(emergencyPos.GetPositionX() - bomb->GetPositionX(), 2) + - pow(emergencyPos.GetPositionY() - bomb->GetPositionY(), 2)); + float bombDist = bomb->GetDistance2d(emergencyPos.GetPositionX(), emergencyPos.GetPositionY()); if (bombDist < minEmergencyGasBombDist) minEmergencyGasBombDist = bombDist; } @@ -4487,8 +4482,7 @@ bool IccBqlGroupPositionAction::HandleGroupPosition(Unit* boss, Aura* frenzyAura // Maintain minimum distance from center position (if too close to center, move out) float centerX = ICC_BQL_CENTER_POSITION.GetPositionX(); float centerY = ICC_BQL_CENTER_POSITION.GetPositionY(); - float centerDist = - std::sqrt(std::pow(bot->GetPositionX() - centerX, 2) + std::pow(bot->GetPositionY() - centerY, 2)); + float centerDist = bot->GetDistance2d(centerX, centerY); if (centerDist < MIN_CENTER_DISTANCE && !((boss->GetPositionZ() - bot->GetPositionZ()) > 5.0f)) { float dx = bot->GetPositionX() - centerX; @@ -6904,7 +6898,7 @@ bool IccLichKingShadowTrapAction::Execute(Event /*event*/) Unit* trap = botAI->GetUnit(trapGuid); if (!trap) continue; - float distToTrap = sqrt(pow(testX - trap->GetPositionX(), 2) + pow(testY - trap->GetPositionY(), 2)); + float distToTrap = trap->GetDistance2d(testX, testY); if (distToTrap < SAFE_DISTANCE) { isSafe = false; diff --git a/src/Ai/Raid/Magtheridon/Util/RaidMagtheridonHelpers.cpp b/src/Ai/Raid/Magtheridon/Util/RaidMagtheridonHelpers.cpp index a56d4b85..344dda5b 100644 --- a/src/Ai/Raid/Magtheridon/Util/RaidMagtheridonHelpers.cpp +++ b/src/Ai/Raid/Magtheridon/Util/RaidMagtheridonHelpers.cpp @@ -132,7 +132,7 @@ namespace MagtheridonHelpers } for (Unit* hazard : debrisHazards) { - float dist = std::sqrt(std::pow(x - hazard->GetPositionX(), 2) + std::pow(y - hazard->GetPositionY(), 2)); + float dist = hazard->GetDistance2d(x, y); if (dist < 9.0f) return false; } @@ -145,7 +145,7 @@ namespace MagtheridonHelpers if (!go || go->GetEntry() != GO_BLAZE) continue; - float dist = std::sqrt(std::pow(x - go->GetPositionX(), 2) + std::pow(y - go->GetPositionY(), 2)); + float dist = go->GetDistance2d(x, y); if (dist < 5.0f) return false; } diff --git a/src/Mgr/Travel/TravelMgr.cpp b/src/Mgr/Travel/TravelMgr.cpp index 2909df5e..be17e28b 100644 --- a/src/Mgr/Travel/TravelMgr.cpp +++ b/src/Mgr/Travel/TravelMgr.cpp @@ -228,13 +228,13 @@ WorldPosition WorldPosition::offset(WorldPosition* center) float WorldPosition::size() { - return sqrt(pow(GetPositionX(), 2.0) + pow(GetPositionY(), 2.0) + pow(GetPositionZ(), 2.0)); + return GetExactDist(0.0f, 0.0f, 0.0f); } float WorldPosition::distance(WorldPosition* center) { if (GetMapId() == center->GetMapId()) - return relPoint(center).size(); + return GetExactDist(center->GetPositionX(), center->GetPositionY(), center->GetPositionZ()); // this -> mapTransfer | mapTransfer -> center return TravelMgr::instance().mapTransDistance(*this, *center); @@ -243,7 +243,7 @@ float WorldPosition::distance(WorldPosition* center) float WorldPosition::fDist(WorldPosition* center) { if (GetMapId() == center->GetMapId()) - return sqrt(sqDistance2d(center)); + return GetExactDist2d(center->GetPositionX(), center->GetPositionY()); // this -> mapTransfer | mapTransfer -> center return TravelMgr::instance().fastMapTransDistance(*this, *center); diff --git a/src/Mgr/Travel/TravelMgr.h b/src/Mgr/Travel/TravelMgr.h index 7e2a40b1..1f5f848c 100644 --- a/src/Mgr/Travel/TravelMgr.h +++ b/src/Mgr/Travel/TravelMgr.h @@ -179,8 +179,7 @@ public: // Quick square distance in 2d plane. float sqDistance2d(WorldPosition center) { - return (GetPositionX() - center.GetPositionX()) * (GetPositionX() - center.GetPositionX()) + - (GetPositionY() - center.GetPositionY()) * (GetPositionY() - center.GetPositionY()); + return GetExactDist2dSq(center.GetPositionX(), center.GetPositionY()); } // Quick square distance calculation without map check. Used for getting the minimum distant points. @@ -193,8 +192,7 @@ public: float sqDistance2d(WorldPosition* center) { - return (GetPositionX() - center->GetPositionX()) * (GetPositionX() - center->GetPositionX()) + - (GetPositionY() - center->GetPositionY()) * (GetPositionY() - center->GetPositionY()); + return GetExactDist2dSq(center->GetPositionX(), center->GetPositionY()); } float sqDistance(WorldPosition* center)