perf(autohunt): avoid discarded A-star searches for attack sight lines
This commit is contained in:
@@ -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.
|
// Empty means unreachable within the loaded data/search budget, never permission to walk straight.
|
||||||
inline std::vector<Point> FindPath(Point start, Point goal, float arrival, const Walkable& walkable,
|
inline std::vector<Point> 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) ||
|
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 {};
|
!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); };
|
auto reaches=[&](Point p) { return Segment(p,Toward(p,goal,body),walkable); };
|
||||||
if (Distance(start,goal) <= arrival && reaches(start)) return {start};
|
if (Distance(start,goal) <= arrival && reaches(start)) return {start};
|
||||||
if (reaches(start)) return {Toward(start,goal,body)};
|
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;
|
constexpr float cell=100.f;
|
||||||
// Grid is relative to the exact starting position, so the first step cannot cross an unchecked edge.
|
// 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;
|
const int extent = int(std::ceil(Distance(start,goal)/cell))+25;
|
||||||
|
|||||||
@@ -757,7 +757,7 @@ class CPythonPlayer : public CSingleton<CPythonPlayer>, public IAbstractPlayer
|
|||||||
bool __AutoHunt_CastSkill(DWORD slot, float now);
|
bool __AutoHunt_CastSkill(DWORD slot, float now);
|
||||||
bool __AutoHunt_UpdateMount(CInstanceBase& main, float now);
|
bool __AutoHunt_UpdateMount(CInstanceBase& main, float now);
|
||||||
bool __AutoHunt_UpdateRecovery(CInstanceBase& main, float now);
|
bool __AutoHunt_UpdateRecovery(CInstanceBase& main, float now);
|
||||||
bool __AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector<TPixelPosition>& path, float body = 0.f);
|
bool __AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector<TPixelPosition>& path, float body = 0.f, bool directOnly = false);
|
||||||
bool __AutoHunt_MoveTo(const TPixelPosition& goal, float arrival, float now, float body = 0.f);
|
bool __AutoHunt_MoveTo(const TPixelPosition& goal, float arrival, float now, float body = 0.f);
|
||||||
void __AutoHunt_ResetMovement();
|
void __AutoHunt_ResetMovement();
|
||||||
void __AutoHunt_WalkAt(CInstanceBase& main, const TPixelPosition& goal, float now);
|
void __AutoHunt_WalkAt(CInstanceBase& main, const TPixelPosition& goal, float now);
|
||||||
|
|||||||
@@ -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.
|
// the player cannot enter (a hydra beside the deck), so there is no line to test.
|
||||||
if (body > 0.f) return true;
|
if (body > 0.f) return true;
|
||||||
std::vector<TPixelPosition> line;
|
std::vector<TPixelPosition> 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)
|
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)
|
bool CPythonPlayer::__AutoHunt_RequestPull(CInstanceBase& main, float now)
|
||||||
{
|
{
|
||||||
const int maxHP = GetStatus(POINT_MAX_HP);
|
const int maxHP = GetStatus(POINT_MAX_HP);
|
||||||
int nearby = 0; __AutoHunt_FindCloseTarget(main, &nearby);
|
if (now < m_fAutoHuntCrowdUntil ||
|
||||||
if (nearby >= 2 || now < m_fAutoHuntCrowdUntil ||
|
|
||||||
m_kAutoHuntConfig.iGatherCount <= 0 || m_isAutoHuntGathering || now < m_fAutoHuntNextPullTime ||
|
m_kAutoHuntConfig.iGatherCount <= 0 || m_isAutoHuntGathering || now < m_fAutoHuntNextPullTime ||
|
||||||
main.IsInSafe() || maxHP <= 0 || GetStatus(POINT_HP) * 100 < maxHP * c_iAutoHuntGatherHPPercent)
|
main.IsInSafe() || maxHP <= 0 || GetStatus(POINT_HP) * 100 < maxHP * c_iAutoHuntGatherHPPercent)
|
||||||
return false;
|
return false;
|
||||||
|
int nearby = 0; __AutoHunt_FindCloseTarget(main, &nearby);
|
||||||
|
if (nearby >= 2) return false;
|
||||||
if (m_kAutoHuntConfig.bAttackStone) {
|
if (m_kAutoHuntConfig.bAttackStone) {
|
||||||
auto* target = NEW_FindActorPtr(__AutoHunt_FindTarget(main, now));
|
auto* target = NEW_FindActorPtr(__AutoHunt_FindTarget(main, now));
|
||||||
if (target && target->IsStone()) return false;
|
if (target && target->IsStone()) return false;
|
||||||
@@ -1153,7 +1154,7 @@ void CPythonPlayer::__AutoHunt_ResetMovement()
|
|||||||
m_fAutoHuntTargetScanTime=0;
|
m_fAutoHuntTargetScanTime=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector<TPixelPosition>& path, float body)
|
bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arrival, std::vector<TPixelPosition>& path, float body, bool directOnly)
|
||||||
{
|
{
|
||||||
auto* main=NEW_GetMainActorPtr();
|
auto* main=NEW_GetMainActorPtr();
|
||||||
auto& background=CPythonBackground::Instance();
|
auto& background=CPythonBackground::Instance();
|
||||||
@@ -1195,7 +1196,7 @@ bool CPythonPlayer::__AutoHunt_BuildPath(const TPixelPosition& goal, float arriv
|
|||||||
probeAt(p.x,p.y,probe);
|
probeAt(p.x,p.y,probe);
|
||||||
return !probe.blocked;
|
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();
|
path.clear();
|
||||||
for (const auto& point:points) {
|
for (const auto& point:points) {
|
||||||
TPixelPosition position=goal; position.x=point.x; position.y=point.y;
|
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;
|
if (!target || !__AutoHunt_IsTargetAllowed(*main,*target)) return false;
|
||||||
TPixelPosition goal; target->NEW_GetPixelPosition(&goal);
|
TPixelPosition goal; target->NEW_GetPixelPosition(&goal);
|
||||||
std::vector<TPixelPosition> line;
|
std::vector<TPixelPosition> 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 (main->IsMountingHorse() && !data->IsHorseSkill()) {
|
||||||
if (m_iAutoHuntMountState || !m_kAutoHuntConfig.bDismountSkill || now<m_fAutoHuntMountNextTime) return false;
|
if (m_iAutoHuntMountState || !m_kAutoHuntConfig.bDismountSkill || now<m_fAutoHuntMountNextTime) return false;
|
||||||
|
|||||||
@@ -42,5 +42,18 @@ int main() {
|
|||||||
assert(Distance(prev,{1000,0})<=450);
|
assert(Distance(prev,{1000,0})<=450);
|
||||||
auto around=FindPath({0,0},{1000,0},450,[&](Point p) { return lake(p) && wall(p); },3500,350);
|
auto around=FindPath({0,0},{1000,0},450,[&](Point p) { return lake(p) && wall(p); },3500,350);
|
||||||
assert(around.size()>1 && Distance(around.back(),{1000,0})<=450);
|
assert(around.size()>1 && 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<normalQueries);
|
||||||
|
assert(FindPath({0,0},{1000,0},0,free,3500,0,true).size()==1);
|
||||||
|
assert(FindPath({0,0},{100,0},150,thin,3500,0,true).empty());
|
||||||
|
assert(FindPath({0,0},{1000,0},450,lake,3500,350,true).size()==1);
|
||||||
|
std::printf("crowd sight probes: %d -> %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");
|
std::puts("autohunt navigation: direct, detour, sealed, thin wall, corners, budget, unloaded, coordinates and large targets passed");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user