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_ICMPPATHFINDER
19 : #define INCLUDED_ICMPPATHFINDER
20 :
21 : #include "simulation2/components/ICmpObstruction.h"
22 : #include "simulation2/helpers/Pathfinding.h"
23 : #include "simulation2/helpers/Position.h"
24 : #include "simulation2/system/Component.h"
25 : #include "simulation2/system/Entity.h"
26 : #include "simulation2/system/Interface.h"
27 :
28 : #include <js/Value.h>
29 : #include <map>
30 : #include <string>
31 : #include <vector>
32 :
33 : class CFixedVector2D;
34 : class IObstructionTestFilter;
35 : class PathGoal;
36 : struct GridUpdateInformation;
37 : template<typename T> class Grid;
38 :
39 1908 : struct PathResult
40 : {
41 954 : 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;
45 : entity_id_t notify;
46 : WaypointPath path;
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 : */
62 1 : class ICmpPathfinder : public IComponent
63 : {
64 : public:
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 : */
74 : virtual void GetPassabilityClasses(
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 :
91 : virtual const Grid<NavcellData>& GetPassabilityGrid() = 0;
92 :
93 : /**
94 : * Get the accumulated dirtiness information since the last time the AI accessed and flushed it.
95 : */
96 : virtual const GridUpdateInformation& GetAIPathfinderDirtinessInformation() const = 0;
97 : virtual void FlushAIPathfinderDirtinessInformation() = 0;
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 : */
169 : 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;
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 : */
178 : 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;
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 17522 : DECLARE_INTERFACE_TYPE(Pathfinder)
217 : };
218 :
219 : #endif // INCLUDED_ICMPPATHFINDER
|