nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
paths.cc
Go to the documentation of this file.
1#include "terrain/paths.h"
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstddef>
7#include <functional>
8#include <numbers>
9#include <queue>
10#include <utility>
11#include <vector>
12
13namespace nanommo {
14
15namespace {
16
17// A square is open when a player can stand all over it: on its middle, its
18// corners and the middles of its sides, as shares of a square from its middle.
19// Then a straight walk over open squares has ground under it all the way, and
20// a way past a tree keeps clear of its trunk.
21constexpr std::array<float, 3> kOver = {-0.5f, 0.0f, 0.5f};
22
23// A straight walk is checked at points this far apart.
24constexpr float kLook = 0.25f;
25
26constexpr float kDiagonal = std::numbers::sqrt2_v<float>;
27
28} // namespace
29
30Paths::Paths(const Terrain& terrain)
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);
37 bool open = true;
38 for (const float down : kOver) {
39 for (const float across : kOver) {
40 open = open && terrain.Walkable(middle.x + across * kSquare,
41 middle.z + down * kSquare);
42 }
43 }
44 open_[square] = open;
45 }
46 cost_.resize(squares);
47 came_from_.resize(squares);
48 stamp_.assign(squares, 0);
49}
50
51int Paths::SquareOf(Place place) const {
52 // Compared while they're floats: a place far off the land, or not a number,
53 // is off it, and isn't made an int it doesn't fit in.
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);
59 }
60 return -1;
61}
62
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_};
68}
69
70bool Paths::Open(Place place) const {
71 const int square = SquareOf(place);
72 return square >= 0 && open_[square];
73}
74
75bool Paths::Clear(Place from, Place to) const {
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})) {
82 return false;
83 }
84 }
85 return true;
86}
87
88// Octile distance: diagonally as far as it can, then straight.
89float Paths::Estimate(int square, int goal) const {
90 const int square_down = square / size_;
91 const int goal_down = goal / size_;
92 const auto across =
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)) *
97 kSquare;
98}
99
100std::vector<Place> Paths::Find(Place from, Place to) {
101 const int start = SquareOf(from);
102 const int goal = SquareOf(to);
103 searched_ = 0;
104 if (start < 0 || goal < 0 || !open_[goal]) {
105 return {};
106 }
107 // Most walks have nothing in the way.
108 if (Clear(from, to)) {
109 return {to};
110 }
111 ++search_;
112 // A square can go into the queue more than once, as a shorter way to it
113 // turns up; the longer ones come out later, and are skipped.
114 using Entry = std::pair<float, int>;
115 std::priority_queue<Entry, std::vector<Entry>, std::greater<>> frontier;
116 stamp_[start] = search_;
117 cost_[start] = 0.0f;
118 came_from_[start] = -1;
119 frontier.push({Estimate(start, goal), start});
120 int nearest = start;
121 float nearest_left = Estimate(start, goal);
122 while (!frontier.empty() && searched_ < kMostSearched) {
123 const auto [estimate, square] = frontier.top();
124 frontier.pop();
125 const float left = Estimate(square, goal);
126 if (estimate > cost_[square] + left + 1e-3f) {
127 continue;
128 }
129 ++searched_;
130 if (left < nearest_left) {
131 nearest = square;
132 nearest_left = left;
133 }
134 if (square == goal) {
135 break;
136 }
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_ ||
144 nj >= size_) {
145 continue;
146 }
147 const int next = nj * size_ + ni;
148 // Not between two squares that touch at a corner only, which would
149 // cut through the corner of what's in the way.
150 const bool diagonal = di != 0 && dj != 0;
151 if (!open_[next] ||
152 (diagonal && (!open_[j * size_ + ni] || !open_[nj * size_ + i]))) {
153 continue;
154 }
155 const float cost =
156 cost_[square] + (diagonal ? kDiagonal : 1.0f) * kSquare;
157 if (stamp_[next] != search_ || cost < cost_[next]) {
158 stamp_[next] = search_;
159 cost_[next] = cost;
160 came_from_[next] = square;
161 frontier.push({cost + Estimate(next, goal), next});
162 }
163 }
164 }
165 }
166 std::vector<Place> squares;
167 for (int square = nearest; square != start; square = came_from_[square]) {
168 squares.push_back(Middle(square));
169 }
170 std::ranges::reverse(squares);
171 if (nearest == goal) {
172 squares.push_back(to);
173 }
174 return Straighten(from, squares);
175}
176
177// From where we are, on to the farthest square still in sight, and again from
178// there: the way turns only where something's in the way.
179std::vector<Place> Paths::Straighten(Place from,
180 const std::vector<Place>& squares) const {
181 std::vector<Place> way;
182 Place at = from;
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])) {
187 ++farthest;
188 }
189 way.push_back(squares[farthest]);
190 at = squares[farthest];
191 next = farthest + 1;
192 }
193 return way;
194}
195
196} // namespace nanommo
static constexpr float kSquare
In metres along each side.
Definition paths.h:20
bool Clear(Place from, Place to) const
Definition paths.cc:75
std::vector< Place > Find(Place from, Place to)
Definition paths.cc:100
bool Open(Place place) const
Whether the square the place is in is one a player can walk on.
Definition paths.cc:70
static constexpr int kMostSearched
A little under a sixth of the island's squares.
Definition paths.h:23
Paths(const Terrain &terrain)
Definition paths.cc:30
bool Walkable(float x, float z) const
Definition terrain.cc:123
Definition chat.cc:9
In metres from the middle of a land.
Definition lands.h:13
float z
Definition lands.h:15
float x
Definition lands.h:14