Line data Source code
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"
24 : #include "simulation2/helpers/PathGoal.h"
25 : #include "simulation2/helpers/Position.h"
26 : #include "simulation2/system/Entity.h"
27 :
28 : #include <vector>
29 :
30 : class CParamNode;
31 : template<typename T> class Grid;
32 :
33 : typedef std::uint16_t pass_class_t;
34 :
35 0 : struct LongPathRequest
36 : {
37 : std::uint32_t ticket;
38 : entity_pos_t x0;
39 : entity_pos_t z0;
40 : PathGoal goal;
41 : pass_class_t passClass;
42 : entity_id_t notify;
43 : };
44 :
45 0 : struct ShortPathRequest
46 : {
47 : std::uint32_t ticket;
48 : entity_pos_t x0;
49 : entity_pos_t z0;
50 : entity_pos_t clearance;
51 : entity_pos_t range;
52 : PathGoal goal;
53 : pass_class_t passClass;
54 : bool avoidMovingUnits;
55 : entity_id_t group;
56 : entity_id_t notify;
57 : };
58 :
59 0 : struct Waypoint
60 : {
61 : entity_pos_t x, z;
62 : };
63 :
64 : /**
65 : * Returned path.
66 : * Waypoints are in *reverse* order (the earliest is at the back of the list)
67 : */
68 4779 : struct WaypointPath
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 : */
78 : struct PathCost
79 : {
80 537211 : PathCost() : data(0) { }
81 :
82 : /// Construct from a number of horizontal/vertical and diagonal steps
83 18175 : PathCost(std::uint16_t hv, std::uint16_t d)
84 9549 : : 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 4759 : static PathCost horizvert(std::uint16_t n)
90 : {
91 4759 : return PathCost(n, 0);
92 : }
93 :
94 : /// Construct for diagonal movement of given number of steps
95 3867 : static PathCost diag(std::uint16_t n)
96 : {
97 3837 : return PathCost(0, n);
98 : }
99 :
100 17192 : PathCost operator+(const PathCost& a) const
101 : {
102 17192 : PathCost c;
103 17192 : c.data = data + a.data;
104 17192 : return c;
105 : }
106 :
107 30 : PathCost& operator+=(const PathCost& a)
108 : {
109 30 : data += a.data;
110 30 : return *this;
111 : }
112 :
113 : bool operator<=(const PathCost& b) const { return data <= b.data; }
114 23851 : 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 :
123 : private:
124 : std::uint32_t data;
125 : };
126 :
127 : inline constexpr int PASS_CLASS_BITS = 16;
128 : typedef 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 :
133 : namespace 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 : */
152 : inline constexpr int NAVCELLS_PER_TERRAIN_TILE = TERRAIN_TILE_SIZE / NAVCELL_SIZE_INT;
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 : */
159 : inline constexpr entity_pos_t CLEARANCE_EXTENSION_RADIUS = fixed::FromInt(1);
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 25631 : 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 25631 : i = static_cast<std::uint16_t>(Clamp((x / NAVCELL_SIZE_INT).ToInt_RoundToNegInfinity(), 0,
170 : w - 1));
171 51262 : 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 331776 : inline void TerrainTileCenter(std::uint16_t i, std::uint16_t j, entity_pos_t& x, entity_pos_t& z)
179 : {
180 331776 : static_assert(TERRAIN_TILE_SIZE % 2 == 0);
181 331776 : x = entity_pos_t::FromInt(i*(int)TERRAIN_TILE_SIZE + (int)TERRAIN_TILE_SIZE / 2);
182 331776 : z = entity_pos_t::FromInt(j*(int)TERRAIN_TILE_SIZE + (int)TERRAIN_TILE_SIZE / 2);
183 : }
184 :
185 5311307 : inline void NavcellCenter(std::uint16_t i, std::uint16_t j, entity_pos_t& x, entity_pos_t& z)
186 : {
187 5311307 : x = entity_pos_t::FromInt(i * 2 + 1).Multiply(NAVCELL_SIZE / 2);
188 5311307 : 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 : */
194 : bool CheckLineMovement(entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1,
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 : */
218 : class PathfinderPassability
219 : {
220 : public:
221 : PathfinderPassability(pass_class_t mask, const CParamNode& node);
222 :
223 47775744 : bool IsPassable(fixed waterdepth, fixed steepness, fixed shoredist) const
224 : {
225 47775744 : return ((m_MinDepth <= waterdepth && waterdepth <= m_MaxDepth) && (steepness < m_MaxSlope) && (m_MinShore <= shoredist && shoredist <= m_MaxShore));
226 : }
227 :
228 : pass_class_t m_Mask;
229 :
230 : fixed m_Clearance; // min distance from static obstructions
231 :
232 : enum ObstructionHandling
233 : {
234 : NONE,
235 : PATHFINDING,
236 : FOUNDATION
237 : };
238 : ObstructionHandling m_Obstructions;
239 :
240 : private:
241 : fixed m_MinDepth;
242 : fixed m_MaxDepth;
243 : fixed m_MaxSlope;
244 : fixed m_MinShore;
245 : fixed m_MaxShore;
246 : };
247 :
248 : #endif // INCLUDED_PATHFINDING
|