21constexpr std::array<float, 3> kOver = {-0.5f, 0.0f, 0.5f};
24constexpr float kLook = 0.25f;
26constexpr float kDiagonal = std::numbers::sqrt2_v<float>;
31 : size_(static_cast<int>(std::ceil(2.0f * terrain.half_size() /
kSquare))),
32 half_(terrain.half_size()) {
33 const int squares = size_ * size_;
34 open_.resize(squares);
35 for (
int square = 0; square < squares; ++square) {
36 const Place middle = Middle(square);
38 for (
const float down : kOver) {
39 for (
const float across : kOver) {
46 cost_.resize(squares);
47 came_from_.resize(squares);
48 stamp_.assign(squares, 0);
51int Paths::SquareOf(
Place place)
const {
54 const float i = std::floor((place.
x + half_) /
kSquare);
55 const float j = std::floor((place.
z + half_) /
kSquare);
56 const auto size =
static_cast<float>(size_);
57 if (i >= 0.0f && j >= 0.0f && i < size && j < size) {
58 return static_cast<int>(j) * size_ +
static_cast<int>(i);
63Place Paths::Middle(
int square)
const {
64 const int across = square % size_;
65 const int down = square / size_;
66 return {.x = (
static_cast<float>(across) + 0.5f) *
kSquare - half_,
67 .z = (
static_cast<float>(down) + 0.5f) *
kSquare - half_};
71 const int square = SquareOf(place);
72 return square >= 0 && open_[square];
76 const float length = std::hypot(to.
x - from.
x, to.
z - from.
z);
77 const int points =
static_cast<int>(std::ceil(length / kLook));
78 for (
int point = 1; point <= points; ++point) {
79 const float share =
static_cast<float>(point) /
static_cast<float>(points);
80 if (!
Open({.x = from.
x + (to.
x - from.
x) * share,
81 .z = from.
z + (to.
z - from.
z) * share})) {
89float Paths::Estimate(
int square,
int goal)
const {
90 const int square_down = square / size_;
91 const int goal_down = goal / size_;
93 static_cast<float>(std::abs(square % size_ - goal % size_));
94 const auto down =
static_cast<float>(std::abs(square_down - goal_down));
95 return (std::max(across, down) +
96 (kDiagonal - 1.0f) * std::min(across, down)) *
101 const int start = SquareOf(from);
102 const int goal = SquareOf(to);
104 if (start < 0 || goal < 0 || !open_[goal]) {
108 if (
Clear(from, to)) {
114 using Entry = std::pair<float, int>;
115 std::priority_queue<Entry, std::vector<Entry>, std::greater<>> frontier;
116 stamp_[start] = search_;
118 came_from_[start] = -1;
119 frontier.push({Estimate(start, goal), start});
121 float nearest_left = Estimate(start, goal);
123 const auto [estimate, square] = frontier.top();
125 const float left = Estimate(square, goal);
126 if (estimate > cost_[square] + left + 1e-3f) {
130 if (left < nearest_left) {
134 if (square == goal) {
137 const int i = square % size_;
138 const int j = square / size_;
139 for (
int dj = -1; dj <= 1; ++dj) {
140 for (
int di = -1; di <= 1; ++di) {
141 const int ni = i + di;
142 const int nj = j + dj;
143 if ((di == 0 && dj == 0) || ni < 0 || nj < 0 || ni >= size_ ||
147 const int next = nj * size_ + ni;
150 const bool diagonal = di != 0 && dj != 0;
152 (diagonal && (!open_[j * size_ + ni] || !open_[nj * size_ + i]))) {
156 cost_[square] + (diagonal ? kDiagonal : 1.0f) *
kSquare;
157 if (stamp_[next] != search_ || cost < cost_[next]) {
158 stamp_[next] = search_;
160 came_from_[next] = square;
161 frontier.push({cost + Estimate(next, goal), next});
166 std::vector<Place> squares;
167 for (
int square = nearest; square != start; square = came_from_[square]) {
168 squares.push_back(Middle(square));
170 std::ranges::reverse(squares);
171 if (nearest == goal) {
172 squares.push_back(to);
174 return Straighten(from, squares);
179std::vector<Place> Paths::Straighten(
Place from,
180 const std::vector<Place>& squares)
const {
181 std::vector<Place> way;
183 std::size_t next = 0;
184 while (next < squares.size()) {
185 std::size_t farthest = next;
186 while (farthest + 1 < squares.size() &&
Clear(at, squares[farthest + 1])) {
189 way.push_back(squares[farthest]);
190 at = squares[farthest];
static constexpr float kSquare
In metres along each side.
bool Clear(Place from, Place to) const
std::vector< Place > Find(Place from, Place to)
bool Open(Place place) const
Whether the square the place is in is one a player can walk on.
static constexpr int kMostSearched
A little under a sixth of the island's squares.
Paths(const Terrain &terrain)
bool Walkable(float x, float z) const
In metres from the middle of a land.