Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ICmpPathfinder.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_ICMPPATHFINDER
19#define INCLUDED_ICMPPATHFINDER
20
27
28#include <js/Value.h>
29#include <map>
30#include <string>
31#include <vector>
32
33class CFixedVector2D;
35class PathGoal;
37template<typename T> class Grid;
38
40{
41 PathResult() = default;
42 PathResult(std::uint32_t t, entity_id_t n, WaypointPath p) : ticket(t), notify(n), path(p) {};
43
44 std::uint32_t ticket;
47};
48
49/**
50 * Pathfinder algorithms.
51 *
52 * There are two different modes: a tile-based pathfinder that works over long distances and
53 * accounts for terrain costs but ignore units, and a 'short' vertex-based pathfinder that
54 * provides precise paths and avoids other units.
55 *
56 * Both use the same concept of a PathGoal: either a point, circle or square.
57 * (If the starting point is inside the goal shape then the path will move outwards
58 * to reach the shape's outline.)
59 *
60 * The output is a list of waypoints.
61 */
63{
64public:
65
66 /**
67 * Get the list of all known passability classes.
68 */
69 virtual void GetPassabilityClasses(std::map<std::string, pass_class_t>& passClasses) const = 0;
70
71 /**
72 * Get the list of passability classes, separating pathfinding classes and others.
73 */
75 std::map<std::string, pass_class_t>& nonPathfindingPassClasses,
76 std::map<std::string, pass_class_t>& pathfindingPassClasses) const = 0;
77
78 /**
79 * Get the tag for a given passability class name.
80 * Logs an error and returns something acceptable if the name is unrecognised.
81 */
82 virtual pass_class_t GetPassabilityClass(const std::string& name) const = 0;
83
84 virtual entity_pos_t GetClearance(pass_class_t passClass) const = 0;
85
86 /**
87 * Get the larger clearance in all passability classes.
88 */
89 virtual entity_pos_t GetMaximumClearance() const = 0;
90
92
93 /**
94 * Get the accumulated dirtiness information since the last time the AI accessed and flushed it.
95 */
98
99 /**
100 * Get a grid representing the distance to the shore of the terrain tile.
101 */
102 virtual Grid<std::uint16_t> ComputeShoreGrid(bool expandOnWater = false) = 0;
103
104 /**
105 * Asynchronous version of ComputePath.
106 * Request a long path computation, asynchronously.
107 * The result will be sent as CMessagePathResult to 'notify'.
108 * Returns a unique non-zero number, which will match the 'ticket' in the result,
109 * so callers can recognise each individual request they make.
110 */
111 virtual std::uint32_t ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, entity_id_t notify) = 0;
112
113 /*
114 * Request a long-path computation immediately
115 */
116 virtual void ComputePathImmediate(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass, WaypointPath& ret) const = 0;
117
118 /**
119 * Request a short path computation, asynchronously.
120 * The result will be sent as CMessagePathResult to 'notify'.
121 * Returns a unique non-zero number, which will match the 'ticket' in the result,
122 * so callers can recognise each individual request they make.
123 */
124 virtual std::uint32_t ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t clearance, entity_pos_t range, const PathGoal& goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t controller, entity_id_t notify) = 0;
125
126 /*
127 * Request a short-path computation immediately.
128 */
129 virtual WaypointPath ComputeShortPathImmediate(const ShortPathRequest& request) const = 0;
130
131 /**
132 * If the debug overlay is enabled, render the path that will computed by ComputePath.
133 */
134 virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass) = 0;
135
136 /**
137 * Distribute units around a point, returning a list of positions.
138 */
139 virtual std::vector<CFixedVector2D> DistributeAround(std::vector<entity_id_t> units, entity_pos_t x, entity_pos_t z) const = 0;
140
141 /**
142 * @return true if the goal is reachable from (x0, z0) for the given passClass, false otherwise.
143 * Warning: this is synchronous, somewhat expensive and not should not be called too liberally.
144 */
145 virtual bool IsGoalReachable(entity_pos_t x0, entity_pos_t z0, const PathGoal& goal, pass_class_t passClass) = 0;
146
147 /**
148 * Check whether the given movement line is valid and doesn't hit any obstructions
149 * or impassable terrain.
150 * Returns true if the movement is okay.
151 */
152 virtual bool CheckMovement(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, pass_class_t passClass) const = 0;
153
154 /**
155 * Check whether a unit placed here is valid and doesn't hit any obstructions
156 * or impassable terrain.
157 * When onlyCenterPoint = true, only check the center tile of the unit
158 * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
159 * a value describing the type of failure.
160 */
161 virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass, bool onlyCenterPoint = false) const = 0;
162
163 /**
164 * Check whether a building placed here is valid and doesn't hit any obstructions
165 * or impassable terrain.
166 * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
167 * a value describing the type of failure.
168 */
170
171 /**
172 * Check whether a building placed here is valid and doesn't hit any obstructions
173 * or impassable terrain.
174 * when onlyCenterPoint = true, only check the center tile of the building
175 * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
176 * a value describing the type of failure.
177 */
179
180
181 /**
182 * Toggle the storage and rendering of debug info.
183 */
184 virtual void SetDebugOverlay(bool enabled) = 0;
185
186 /**
187 * Toggle the storage and rendering of debug info for the hierarchical pathfinder.
188 */
189 virtual void SetHierDebugOverlay(bool enabled) = 0;
190
191 /**
192 * Finish computing asynchronous path requests and send the CMessagePathResult messages.
193 */
194 virtual void SendRequestedPaths() = 0;
195
196 /**
197 * Tell asynchronous pathfinder threads that they can begin computing paths.
198 */
199 virtual void StartProcessingMoves(bool useMax) = 0;
200
201 /**
202 * Regenerates the grid based on the current obstruction list, if necessary
203 */
204 virtual void UpdateGrid() = 0;
205
206 /**
207 * Returns some stats about the last ComputePath.
208 */
209 virtual void GetDebugData(std::uint32_t& steps, double& time, Grid<std::uint8_t>& grid) const = 0;
210
211 /**
212 * Sets up the pathfinder passability overlay in Atlas.
213 */
214 virtual void SetAtlasOverlay(bool enable, pass_class_t passClass = 0) = 0;
215
216 DECLARE_INTERFACE_TYPE(Pathfinder)
217};
218
219#endif // INCLUDED_ICMPPATHFINDER
std::uint16_t pass_class_t
Definition: Pathfinding.h:33
Entity coordinate types.
Definition: FixedVector2D.h:25
A simple fixed-point number class.
Definition: Fixed.h:129
Basic 2D array, intended for storing tile data, plus support for lazy updates by ICmpObstructionManag...
Definition: Grid.h:46
EFoundationCheck
Definition: ICmpObstruction.h:42
Pathfinder algorithms.
Definition: ICmpPathfinder.h:63
virtual void FlushAIPathfinderDirtinessInformation()=0
virtual std::uint32_t ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t clearance, entity_pos_t range, const PathGoal &goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t controller, entity_id_t notify)=0
Request a short path computation, asynchronously.
virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const PathGoal &goal, pass_class_t passClass)=0
If the debug overlay is enabled, render the path that will computed by ComputePath.
virtual ICmpObstruction::EFoundationCheck CheckBuildingPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, entity_id_t id, pass_class_t passClass, bool onlyCenterPoint) const =0
Check whether a building placed here is valid and doesn't hit any obstructions or impassable terrain.
virtual void GetDebugData(std::uint32_t &steps, double &time, Grid< std::uint8_t > &grid) const =0
Returns some stats about the last ComputePath.
virtual std::vector< CFixedVector2D > DistributeAround(std::vector< entity_id_t > units, entity_pos_t x, entity_pos_t z) const =0
Distribute units around a point, returning a list of positions.
virtual bool CheckMovement(const IObstructionTestFilter &filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t x1, entity_pos_t z1, entity_pos_t r, pass_class_t passClass) const =0
Check whether the given movement line is valid and doesn't hit any obstructions or impassable terrain...
virtual Grid< std::uint16_t > ComputeShoreGrid(bool expandOnWater=false)=0
Get a grid representing the distance to the shore of the terrain tile.
virtual WaypointPath ComputeShortPathImmediate(const ShortPathRequest &request) const =0
virtual bool IsGoalReachable(entity_pos_t x0, entity_pos_t z0, const PathGoal &goal, pass_class_t passClass)=0
virtual const GridUpdateInformation & GetAIPathfinderDirtinessInformation() const =0
Get the accumulated dirtiness information since the last time the AI accessed and flushed it.
virtual void SetDebugOverlay(bool enabled)=0
Toggle the storage and rendering of debug info.
virtual entity_pos_t GetMaximumClearance() const =0
Get the larger clearance in all passability classes.
virtual void SetHierDebugOverlay(bool enabled)=0
Toggle the storage and rendering of debug info for the hierarchical pathfinder.
virtual ICmpObstruction::EFoundationCheck CheckBuildingPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, entity_id_t id, pass_class_t passClass) const =0
Check whether a building placed here is valid and doesn't hit any obstructions or impassable terrain.
virtual const Grid< NavcellData > & GetPassabilityGrid()=0
virtual void SetAtlasOverlay(bool enable, pass_class_t passClass=0)=0
Sets up the pathfinder passability overlay in Atlas.
virtual void GetPassabilityClasses(std::map< std::string, pass_class_t > &nonPathfindingPassClasses, std::map< std::string, pass_class_t > &pathfindingPassClasses) const =0
Get the list of passability classes, separating pathfinding classes and others.
virtual pass_class_t GetPassabilityClass(const std::string &name) const =0
Get the tag for a given passability class name.
virtual entity_pos_t GetClearance(pass_class_t passClass) const =0
virtual void SendRequestedPaths()=0
Finish computing asynchronous path requests and send the CMessagePathResult messages.
virtual void StartProcessingMoves(bool useMax)=0
Tell asynchronous pathfinder threads that they can begin computing paths.
virtual void UpdateGrid()=0
Regenerates the grid based on the current obstruction list, if necessary.
virtual std::uint32_t ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const PathGoal &goal, pass_class_t passClass, entity_id_t notify)=0
Asynchronous version of ComputePath.
virtual void GetPassabilityClasses(std::map< std::string, pass_class_t > &passClasses) const =0
Get the list of all known passability classes.
virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass, bool onlyCenterPoint=false) const =0
Check whether a unit placed here is valid and doesn't hit any obstructions or impassable terrain.
virtual void ComputePathImmediate(entity_pos_t x0, entity_pos_t z0, const PathGoal &goal, pass_class_t passClass, WaypointPath &ret) const =0
Definition: IComponent.h:33
Interface for ICmpObstructionManager Test functions to filter out unwanted shapes.
Definition: ICmpObstructionManager.h:357
Pathfinder goal.
Definition: PathGoal.h:34
std::uint32_t entity_id_t
Entity ID type.
Definition: Entity.h:29
#define DECLARE_INTERFACE_TYPE(iname)
Definition: Interface.h:23
Structure holding grid dirtiness informations, for clever updates.
Definition: Grid.h:372
Definition: ICmpPathfinder.h:40
entity_id_t notify
Definition: ICmpPathfinder.h:45
WaypointPath path
Definition: ICmpPathfinder.h:46
std::uint32_t ticket
Definition: ICmpPathfinder.h:44
PathResult()=default
PathResult(std::uint32_t t, entity_id_t n, WaypointPath p)
Definition: ICmpPathfinder.h:42
Definition: Pathfinding.h:46
Returned path.
Definition: Pathfinding.h:69