Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Pathfinding.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_PATHFINDING
19#define INCLUDED_PATHFINDING
20
21#include "graphics/Terrain.h"
22#include "maths/Fixed.h"
23#include "maths/MathUtil.h"
27
28#include <vector>
29
30class CParamNode;
31template<typename T> class Grid;
32
33typedef std::uint16_t pass_class_t;
34
36{
37 std::uint32_t ticket;
43};
44
46{
47 std::uint32_t ticket;
57};
58
60{
62};
63
64/**
65 * Returned path.
66 * Waypoints are in *reverse* order (the earliest is at the back of the list)
67 */
69{
70 std::vector<Waypoint> m_Waypoints;
71};
72
73/**
74 * Represents the cost of a path consisting of horizontal/vertical and
75 * diagonal movements over a uniform-cost grid.
76 * Maximum path length before overflow is about 45K steps.
77 */
79{
80 PathCost() : data(0) { }
81
82 /// Construct from a number of horizontal/vertical and diagonal steps
83 PathCost(std::uint16_t hv, std::uint16_t d)
84 : data(hv * 65536 + d * 92682) // 2^16 * sqrt(2) == 92681.9
85 {
86 }
87
88 /// Construct for horizontal/vertical movement of given number of steps
89 static PathCost horizvert(std::uint16_t n)
90 {
91 return PathCost(n, 0);
92 }
93
94 /// Construct for diagonal movement of given number of steps
95 static PathCost diag(std::uint16_t n)
96 {
97 return PathCost(0, n);
98 }
99
101 {
102 PathCost c;
103 c.data = data + a.data;
104 return c;
105 }
106
108 {
109 data += a.data;
110 return *this;
111 }
112
113 bool operator<=(const PathCost& b) const { return data <= b.data; }
114 bool operator< (const PathCost& b) const { return data < b.data; }
115 bool operator>=(const PathCost& b) const { return data >= b.data; }
116 bool operator>(const PathCost& b) const { return data > b.data; }
117
118 std::uint32_t ToInt()
119 {
120 return data;
121 }
122
123private:
124 std::uint32_t data;
125};
126
127inline constexpr int PASS_CLASS_BITS = 16;
128typedef std::uint16_t NavcellData; // 1 bit per passability class (up to PASS_CLASS_BITS)
129#define IS_PASSABLE(item, classmask) (((item) & (classmask)) == 0)
130#define PASS_CLASS_MASK_FROM_INDEX(id) ((pass_class_t)(1u << id))
131#define SPECIAL_PASS_CLASS PASS_CLASS_MASK_FROM_INDEX((PASS_CLASS_BITS-1)) // 16th bit, used for special in-place computations
132
133namespace Pathfinding
134{
135 /**
136 * The long-range pathfinder operates primarily over a navigation grid (a uniform-cost
137 * 2D passability grid, with horizontal/vertical (not diagonal) connectivity).
138 * This is based on the terrain tile passability, plus the rasterized shapes of
139 * obstructions, all expanded outwards by the radius of the units.
140 * Since units are much smaller than terrain tiles, the nav grid should be
141 * higher resolution than the tiles.
142 * We therefore split each the world into NxN "nav cells" (for some integer N,
143 * preferably a power of two).
144 */
145 inline constexpr fixed NAVCELL_SIZE = fixed::FromInt(1);
146 inline constexpr int NAVCELL_SIZE_INT = 1;
147 inline constexpr int NAVCELL_SIZE_LOG2 = 0;
148
149 /**
150 * The terrain grid is coarser, and it is often convenient to convert from one to the other.
151 */
153 static_assert(TERRAIN_TILE_SIZE % NAVCELL_SIZE_INT == 0, "Terrain tile size is not a multiple of navcell size");
154
155 /**
156 * To make sure the long-range pathfinder is more strict than the short-range one,
157 * we need to slightly over-rasterize. So we extend the clearance radius by 1.
158 */
160
161 /**
162 * Compute the navcell indexes on the grid nearest to a given point
163 * w, h are the grid dimensions, i.e. the number of navcells per side
164 */
165 inline void NearestNavcell(entity_pos_t x, entity_pos_t z, std::uint16_t& i, std::uint16_t& j,
166 std::uint16_t w, std::uint16_t h)
167 {
168 // Use NAVCELL_SIZE_INT to save the cost of dividing by a fixed
169 i = static_cast<std::uint16_t>(Clamp((x / NAVCELL_SIZE_INT).ToInt_RoundToNegInfinity(), 0,
170 w - 1));
171 j = static_cast<std::uint16_t>(Clamp((z / NAVCELL_SIZE_INT).ToInt_RoundToNegInfinity(), 0,
172 h - 1));
173 }
174
175 /**
176 * Returns the position of the center of the given terrain tile
177 */
178 inline void TerrainTileCenter(std::uint16_t i, std::uint16_t j, entity_pos_t& x, entity_pos_t& z)
179 {
180 static_assert(TERRAIN_TILE_SIZE % 2 == 0);
183 }
184
185 inline void NavcellCenter(std::uint16_t i, std::uint16_t j, entity_pos_t& x, entity_pos_t& z)
186 {
187 x = entity_pos_t::FromInt(i * 2 + 1).Multiply(NAVCELL_SIZE / 2);
188 z = entity_pos_t::FromInt(j * 2 + 1).Multiply(NAVCELL_SIZE / 2);
189 }
190
191 /*
192 * Checks that the line (x0,z0)-(x1,z1) does not intersect any impassable navcells.
193 */
195 pass_class_t passClass, const Grid<NavcellData>& grid);
196}
197
198/*
199 * For efficient pathfinding we want to try hard to minimise the per-tile search cost,
200 * so we precompute the tile passability flags for the various different types of unit.
201 * We also want to minimise memory usage (there can easily be 100K tiles so we don't want
202 * to store many bytes for each).
203 *
204 * To handle passability efficiently, we have a small number of passability classes
205 * (e.g. "infantry", "ship"). Each unit belongs to a single passability class, and
206 * uses that for all its pathfinding.
207 * Passability is determined by water depth, terrain slope, forestness, buildingness.
208 * We need at least one bit per class per tile to represent passability.
209 *
210 * Not all pass classes are used for actual pathfinding. The pathfinder calls
211 * CCmpObstructionManager's Rasterize() to add shapes onto the passability grid.
212 * Which shapes are rasterized depend on the value of the m_Obstructions of each passability
213 * class.
214 *
215 * Passabilities not used for unit pathfinding should not use the Clearance attribute, and
216 * will get a zero clearance value.
217 */
219{
220public:
222
223 bool IsPassable(fixed waterdepth, fixed steepness, fixed shoredist) const
224 {
225 return ((m_MinDepth <= waterdepth && waterdepth <= m_MaxDepth) && (steepness < m_MaxSlope) && (m_MinShore <= shoredist && shoredist <= m_MaxShore));
226 }
227
229
230 fixed m_Clearance; // min distance from static obstructions
231
233 {
237 };
239
240private:
246};
247
248#endif // INCLUDED_PATHFINDING
T Clamp(T value, T min, T max)
Definition: MathUtil.h:34
constexpr int PASS_CLASS_BITS
Definition: Pathfinding.h:127
std::uint16_t pass_class_t
Definition: Pathfinding.h:33
std::uint16_t NavcellData
Definition: Pathfinding.h:128
Entity coordinate types.
const ssize_t TERRAIN_TILE_SIZE
metres [world space units] per tile in x and z
Definition: Terrain.h:40
A simple fixed-point number class.
Definition: Fixed.h:129
CFixed Multiply(CFixed n) const
Multiply by a CFixed.
Definition: Fixed.h:331
static constexpr CFixed FromInt(int n)
Definition: Fixed.h:149
An entity initialisation parameter node.
Definition: ParamNode.h:153
Basic 2D array, intended for storing tile data, plus support for lazy updates by ICmpObstructionManag...
Definition: Grid.h:46
Pathfinder goal.
Definition: PathGoal.h:34
Definition: Pathfinding.h:219
pass_class_t m_Mask
Definition: Pathfinding.h:228
fixed m_MinShore
Definition: Pathfinding.h:244
PathfinderPassability(pass_class_t mask, const CParamNode &node)
Definition: Pathfinding.cpp:127
fixed m_Clearance
Definition: Pathfinding.h:230
fixed m_MaxSlope
Definition: Pathfinding.h:243
ObstructionHandling
Definition: Pathfinding.h:233
@ NONE
Definition: Pathfinding.h:234
@ FOUNDATION
Definition: Pathfinding.h:236
@ PATHFINDING
Definition: Pathfinding.h:235
fixed m_MinDepth
Definition: Pathfinding.h:241
fixed m_MaxShore
Definition: Pathfinding.h:245
fixed m_MaxDepth
Definition: Pathfinding.h:242
ObstructionHandling m_Obstructions
Definition: Pathfinding.h:238
bool IsPassable(fixed waterdepth, fixed steepness, fixed shoredist) const
Definition: Pathfinding.h:223
Definition: Pathfinding.cpp:31
void NavcellCenter(std::uint16_t i, std::uint16_t j, entity_pos_t &x, entity_pos_t &z)
Definition: Pathfinding.h:185
constexpr fixed NAVCELL_SIZE
The long-range pathfinder operates primarily over a navigation grid (a uniform-cost 2D passability gr...
Definition: Pathfinding.h:145
bool CheckLineMovement(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, pass_class_t passClass, const Grid< NavcellData > &grid)
Definition: Pathfinding.cpp:32
constexpr entity_pos_t CLEARANCE_EXTENSION_RADIUS
To make sure the long-range pathfinder is more strict than the short-range one, we need to slightly o...
Definition: Pathfinding.h:159
constexpr int NAVCELL_SIZE_INT
Definition: Pathfinding.h:146
void TerrainTileCenter(std::uint16_t i, std::uint16_t j, entity_pos_t &x, entity_pos_t &z)
Returns the position of the center of the given terrain tile.
Definition: Pathfinding.h:178
constexpr int NAVCELLS_PER_TERRAIN_TILE
The terrain grid is coarser, and it is often convenient to convert from one to the other.
Definition: Pathfinding.h:152
void NearestNavcell(entity_pos_t x, entity_pos_t z, std::uint16_t &i, std::uint16_t &j, std::uint16_t w, std::uint16_t h)
Compute the navcell indexes on the grid nearest to a given point w, h are the grid dimensions,...
Definition: Pathfinding.h:165
constexpr int NAVCELL_SIZE_LOG2
Definition: Pathfinding.h:147
std::uint32_t entity_id_t
Entity ID type.
Definition: Entity.h:29
Definition: Pathfinding.h:36
entity_id_t notify
Definition: Pathfinding.h:42
entity_pos_t x0
Definition: Pathfinding.h:38
std::uint32_t ticket
Definition: Pathfinding.h:37
pass_class_t passClass
Definition: Pathfinding.h:41
PathGoal goal
Definition: Pathfinding.h:40
entity_pos_t z0
Definition: Pathfinding.h:39
Represents the cost of a path consisting of horizontal/vertical and diagonal movements over a uniform...
Definition: Pathfinding.h:79
bool operator>(const PathCost &b) const
Definition: Pathfinding.h:116
PathCost operator+(const PathCost &a) const
Definition: Pathfinding.h:100
std::uint32_t ToInt()
Definition: Pathfinding.h:118
std::uint32_t data
Definition: Pathfinding.h:124
PathCost & operator+=(const PathCost &a)
Definition: Pathfinding.h:107
static PathCost horizvert(std::uint16_t n)
Construct for horizontal/vertical movement of given number of steps.
Definition: Pathfinding.h:89
bool operator>=(const PathCost &b) const
Definition: Pathfinding.h:115
static PathCost diag(std::uint16_t n)
Construct for diagonal movement of given number of steps.
Definition: Pathfinding.h:95
bool operator<=(const PathCost &b) const
Definition: Pathfinding.h:113
PathCost()
Definition: Pathfinding.h:80
PathCost(std::uint16_t hv, std::uint16_t d)
Construct from a number of horizontal/vertical and diagonal steps.
Definition: Pathfinding.h:83
bool operator<(const PathCost &b) const
Definition: Pathfinding.h:114
Definition: Pathfinding.h:46
entity_pos_t clearance
Definition: Pathfinding.h:50
pass_class_t passClass
Definition: Pathfinding.h:53
entity_id_t group
Definition: Pathfinding.h:55
PathGoal goal
Definition: Pathfinding.h:52
entity_id_t notify
Definition: Pathfinding.h:56
bool avoidMovingUnits
Definition: Pathfinding.h:54
entity_pos_t range
Definition: Pathfinding.h:51
entity_pos_t x0
Definition: Pathfinding.h:48
std::uint32_t ticket
Definition: Pathfinding.h:47
entity_pos_t z0
Definition: Pathfinding.h:49
Returned path.
Definition: Pathfinding.h:69
std::vector< Waypoint > m_Waypoints
Definition: Pathfinding.h:70
Definition: Pathfinding.h:60
entity_pos_t x
Definition: Pathfinding.h:61
entity_pos_t z
Definition: Pathfinding.h:61