From ae0fc25a8117c3fb62ee85ea2c2ed5277402dcf5 Mon Sep 17 00:00:00 2001 From: shen <> Date: Fri, 16 Oct 2026 23:14:48 -0700 Subject: [PATCH] perf(autohunt): avoid discarded A-star searches for attack sight lines --- src/port/UserInterface/AutoHuntNavigation.h | 5 ++++- src/port/UserInterface/PythonPlayer.h | 2 +- src/port/UserInterface/PythonPlayerAutoHunt.cpp | 13 +++++++------ tests/host/autohunt_navigation_test.cpp | 13 +++++++++++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/port/UserInterface/AutoHuntNavigation.h b/src/port/UserInterface/AutoHuntNavigation.h index 8829b912..896b9e55 100644 --- a/src/port/UserInterface/AutoHuntNavigation.h +++ b/src/port/UserInterface/AutoHuntNavigation.h @@ -35,12 +35,15 @@ inline Point Toward(Point p, Point goal, float body) { } // Empty means unreachable within the loaded data/search budget, never permission to walk straight. inline std::vector FindPath(Point start, Point goal, float arrival, const Walkable& walkable, - int budget=3500, float body=0.f) { + int budget=3500, float body=0.f, bool directOnly=false) { if (!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(goal.x) || !std::isfinite(goal.y) || arrival < 0 || body < 0 || budget <= 0 || Distance(start,goal)>40000.f) return {}; auto reaches=[&](Point p) { return Segment(p,Toward(p,goal,body),walkable); }; if (Distance(start,goal) <= arrival && reaches(start)) return {start}; if (reaches(start)) return {Toward(start,goal,body)}; + // Attack/skill line checks reject a detour anyway. Do not run a 3500-node + // A* search for each monster in a crowded room just to discard that route. + if (directOnly) return {}; constexpr float cell=100.f; // Grid is relative to the exact starting position, so the first step cannot cross an unchecked edge. const int extent = int(std::ceil(Distance(start,goal)/cell))+25; diff --git a/src/port/UserInterface/PythonPlayer.h b/src/port/UserInterface/PythonPlayer.h index 7789e153..f50457d0 100644 --- a/src/port/UserInterface/PythonPlayer.h +++ b/src/port/UserInterface/PythonPlayer.h @@ -757,7 +757,7 @@ class CPythonPlayer : public CSingleton, public IAbstractPlayer bool __AutoHunt_CastSkill(DWORD slot, float now); bool __AutoHunt_UpdateMount(CInstanceBase& main, float now); bool __AutoHunt_UpdateRecovery(CInstanceBase& main, float now); - bool __AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector& path, float body = 0.f); + bool __AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector& path, float body = 0.f, bool directOnly = false); bool __AutoHunt_MoveTo(const TPixelPosition& goal, float arrival, float now, float body = 0.f); void __AutoHunt_ResetMovement(); void __AutoHunt_WalkAt(CInstanceBase& main, const TPixelPosition& goal, float now); diff --git a/src/port/UserInterface/PythonPlayerAutoHunt.cpp b/src/port/UserInterface/PythonPlayerAutoHunt.cpp index 72d94648..1476558b 100644 --- a/src/port/UserInterface/PythonPlayerAutoHunt.cpp +++ b/src/port/UserInterface/PythonPlayerAutoHunt.cpp @@ -389,7 +389,7 @@ bool CPythonPlayer::__AutoHunt_CanAttackHere(CInstanceBase& main, CInstanceBase& // the player cannot enter (a hydra beside the deck), so there is no line to test. if (body > 0.f) return true; std::vector line; - return __AutoHunt_BuildPath(goal, 0.f, line, body) && line.size() == 1 && PixelDistance(line.back(), goal) <= body + 1.f; + return __AutoHunt_BuildPath(goal, 0.f, line, body, true) && line.size() == 1 && PixelDistance(line.back(), goal) <= body + 1.f; } DWORD CPythonPlayer::__AutoHunt_FindCloseTarget(CInstanceBase& main, int* count) @@ -419,11 +419,12 @@ DWORD CPythonPlayer::__AutoHunt_FindCloseTarget(CInstanceBase& main, int* count) bool CPythonPlayer::__AutoHunt_RequestPull(CInstanceBase& main, float now) { const int maxHP = GetStatus(POINT_MAX_HP); - int nearby = 0; __AutoHunt_FindCloseTarget(main, &nearby); - if (nearby >= 2 || now < m_fAutoHuntCrowdUntil || + if (now < m_fAutoHuntCrowdUntil || m_kAutoHuntConfig.iGatherCount <= 0 || m_isAutoHuntGathering || now < m_fAutoHuntNextPullTime || main.IsInSafe() || maxHP <= 0 || GetStatus(POINT_HP) * 100 < maxHP * c_iAutoHuntGatherHPPercent) return false; + int nearby = 0; __AutoHunt_FindCloseTarget(main, &nearby); + if (nearby >= 2) return false; if (m_kAutoHuntConfig.bAttackStone) { auto* target = NEW_FindActorPtr(__AutoHunt_FindTarget(main, now)); if (target && target->IsStone()) return false; @@ -1153,7 +1154,7 @@ void CPythonPlayer::__AutoHunt_ResetMovement() m_fAutoHuntTargetScanTime=0; } -bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector& path, float body) +bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector& path, float body, bool directOnly) { auto* main=NEW_GetMainActorPtr(); auto& background=CPythonBackground::Instance(); @@ -1195,7 +1196,7 @@ bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arriv probeAt(p.x,p.y,probe); return !probe.blocked; }; - const auto points=AutoHuntNavigation::FindPath({start.x,start.y},{goal.x,goal.y},arrival,walkable,3500,body); + const auto points=AutoHuntNavigation::FindPath({start.x,start.y},{goal.x,goal.y},arrival,walkable,3500,body,directOnly); path.clear(); for (const auto& point:points) { TPixelPosition position=goal; position.x=point.x; position.y=point.y; @@ -1290,7 +1291,7 @@ bool CPythonPlayer::__AutoHunt_CastSkill(DWORD slot, float now) if (!target || !__AutoHunt_IsTargetAllowed(*main,*target)) return false; TPixelPosition goal; target->NEW_GetPixelPosition(&goal); std::vector line; - if (!__AutoHunt_BuildPath(goal,0.f,line) || line.size()!=1 || PixelDistance(line.back(),goal)>1.f) return false; + if (!__AutoHunt_BuildPath(goal,0.f,line,0.f,true) || line.size()!=1 || PixelDistance(line.back(),goal)>1.f) return false; } if (main->IsMountingHorse() && !data->IsHorseSkill()) { if (m_iAutoHuntMountState || !m_kAutoHuntConfig.bDismountSkill || now1 && Distance(around.back(),{1000,0})<=450); + // A blocked combat sight line must not explore the room looking for a + // detour that the caller immediately rejects. Simulate 240 crowded targets. + int normalQueries=0, sightQueries=0; + for (int i=0;i<240;++i) { + auto full=FindPath({0,0},{1000,float(i%5)},0,[&](Point p){++normalQueries;return wall(p);}); + auto sight=FindPath({0,0},{1000,float(i%5)},0,[&](Point p){++sightQueries;return wall(p);},3500,0,true); + assert((full.size()==1)==(sight.size()==1)); + } + assert(sightQueries*10 %d (same direct-line decisions)\n",normalQueries,sightQueries); std::puts("autohunt navigation: direct, detour, sealed, thin wall, corners, budget, unloaded, coordinates and large targets passed"); }