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