Files
metin2-client/src/port/UserInterface/AutoHuntNavigation.h
T

108 lines
5.1 KiB
C++

#pragma once
// PORT: bounded A* for auto hunt only. Queries loaded terrain and scene collision through a callback.
#include <algorithm>
#include <cmath>
#include <functional>
#include <limits>
#include <queue>
#include <unordered_map>
#include <vector>
namespace AutoHuntNavigation {
struct Point { float x, y; };
inline float Distance(Point a, Point b) { return std::hypot(a.x-b.x, a.y-b.y); }
using Walkable = std::function<bool(Point)>;
inline bool Segment(Point a, Point b, const Walkable& walkable) {
const int samples = std::max(1, int(std::ceil(Distance(a,b) / 40.f)));
Point previous=a;
for (int i=1; i<=samples; ++i) {
const float t = float(i)/samples;
const Point next{a.x+(b.x-a.x)*t, a.y+(b.y-a.y)*t};
if (!walkable(next)) return false;
if (next.x!=previous.x && next.y!=previous.y &&
(!walkable({next.x,previous.y}) || !walkable({previous.x,next.y}))) return false;
previous=next;
}
return true;
}
// The point `body` short of goal on the way from p: a large target's own cells (a hydra on the
// water) need not be walkable, only the line up to its edge.
inline Point Toward(Point p, Point goal, float body) {
const float d=Distance(p,goal);
if (body<=0 || d<=0) return goal;
if (d<=body) return p;
return {goal.x+(p.x-goal.x)*body/d, goal.y+(p.y-goal.y)*body/d};
}
// 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,
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;
const int width = extent*2+1;
auto key = [=](int x,int y) { return (y+extent)*width+x+extent; };
auto point = [=](int x,int y) -> Point { return {start.x+x*cell,start.y+y*cell}; };
struct Node { float cost=std::numeric_limits<float>::infinity(); int parent=-1; bool closed=false; };
struct Entry { float score; int x,y; bool operator<(const Entry& b) const { return score>b.score; } };
std::unordered_map<int,Node> nodes;
std::unordered_map<int,bool> passable;
auto canWalk = [&](int x,int y) {
const int id=key(x,y);
auto i=passable.find(id);
if (i!=passable.end()) return i->second;
return passable[id]=walkable(point(x,y));
};
std::priority_queue<Entry> open;
const int first=key(0,0);
nodes[first].cost=0;
open.push({Distance(start,goal),0,0});
int finish=-1;
while (!open.empty() && budget>0) {
const auto e=open.top(); open.pop();
const int id=key(e.x,e.y);
if (nodes[id].closed) continue;
nodes[id].closed=true;
--budget;
const float cost=nodes[id].cost; // inserts below may rehash the map
const Point p=point(e.x,e.y);
if (Distance(p,goal)<=std::max(arrival,cell) && reaches(p)) { finish=id; break; }
for (int dy=-1;dy<=1;++dy) for (int dx=-1;dx<=1;++dx) {
if (!dx && !dy) continue;
const int x=e.x+dx,y=e.y+dy;
if (std::abs(x)>extent || std::abs(y)>extent || !canWalk(x,y)) continue;
if (dx && dy && (!canWalk(e.x+dx,e.y) || !canWalk(e.x,e.y+dy))) continue;
const int next=key(x,y);
const float nextCost=cost+((dx && dy)?141.42136f:100.f);
if (nodes[next].closed || nextCost>=nodes[next].cost || !Segment(p,point(x,y),walkable)) continue;
nodes[next].cost=nextCost;
nodes[next].parent=id;
open.push({nextCost+std::max(0.f,Distance(point(x,y),goal)-arrival),x,y});
}
}
if (finish<0) return {};
std::vector<Point> reverse;
for (int id=finish;id!=first;id=nodes[id].parent)
reverse.push_back(point(id%width-extent,id/width-extent));
std::reverse(reverse.begin(),reverse.end());
if (reverse.empty()) reverse.push_back(start);
if (Distance(reverse.back(),goal)>arrival) reverse.push_back(Toward(reverse.back(),goal,body));
// Greedy line-of-sight smoothing, retaining every necessary turn.
std::vector<Point> result;
Point from=start;
for (size_t i=0;i<reverse.size();) {
size_t end=i;
while (end+1<reverse.size() && Segment(from,reverse[end+1],walkable)) ++end;
result.push_back(reverse[end]); from=reverse[end]; i=end+1;
}
return result;
}
}