Improve auto-hunt navigation, recovery and mounted skill casting
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
#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;
|
||||
}
|
||||
// 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) {
|
||||
if (!std::isfinite(start.x) || !std::isfinite(start.y) || !std::isfinite(goal.x) ||
|
||||
!std::isfinite(goal.y) || arrival < 0 || budget <= 0 || Distance(start,goal)>40000.f) return {};
|
||||
if (Distance(start,goal) <= arrival && Segment(start,goal,walkable)) return {start};
|
||||
if (Segment(start,goal,walkable)) return {goal};
|
||||
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) &&
|
||||
Segment(p,goal,walkable)) { 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(goal);
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user