Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ICmpPathfinder.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 "maths/FixedVector2D.h"
27 
28 #include <map>
29 #include <vector>
30 
32 
33 template<typename T> class Grid;
34 
35 /**
36  * Pathfinder algorithms.
37  *
38  * There are two different modes: a tile-based pathfinder that works over long distances and
39  * accounts for terrain costs but ignore units, and a 'short' vertex-based pathfinder that
40  * provides precise paths and avoids other units.
41  *
42  * Both use the same concept of a Goal: either a point, circle or square.
43  * (If the starting point is inside the goal shape then the path will move outwards
44  * to reach the shape's outline.)
45  *
46  * The output is a list of waypoints.
47  */
48 class ICmpPathfinder : public IComponent
49 {
50 public:
51  typedef u16 pass_class_t;
52  typedef u8 cost_class_t;
53 
54  struct Goal
55  {
56  enum Type {
60  } type;
61  entity_pos_t x, z; // position of center
62  CFixedVector2D u, v; // if SQUARE, then orthogonal unit axes
63  entity_pos_t hw, hh; // if SQUARE, then half width & height; if CIRCLE, then hw is radius
64  };
65 
66  struct Waypoint
67  {
69  };
70 
71  /**
72  * Returned path.
73  * Waypoints are in *reverse* order (the earliest is at the back of the list)
74  */
75  struct Path
76  {
77  std::vector<Waypoint> m_Waypoints;
78  };
79 
80  /**
81  * Get the list of all known passability classes.
82  */
83  virtual std::map<std::string, pass_class_t> GetPassabilityClasses() = 0;
84 
85  /**
86  * Get the tag for a given passability class name.
87  * Logs an error and returns something acceptable if the name is unrecognised.
88  */
89  virtual pass_class_t GetPassabilityClass(const std::string& name) = 0;
90 
91  /**
92  * Get the tag for a given movement cost class name.
93  * Logs an error and returns something acceptable if the name is unrecognised.
94  */
95  virtual cost_class_t GetCostClass(const std::string& name) = 0;
96 
97  virtual const Grid<u16>& GetPassabilityGrid() = 0;
98 
99  /**
100  * Compute a tile-based path from the given point to the goal, and return the set of waypoints.
101  * The waypoints correspond to the centers of horizontally/vertically adjacent tiles
102  * along the path.
103  */
104  virtual void ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal& goal, pass_class_t passClass, cost_class_t costClass, Path& ret) = 0;
105 
106  /**
107  * Asynchronous version of ComputePath.
108  * The result will be sent as CMessagePathResult to 'notify'.
109  * Returns a unique non-zero number, which will match the 'ticket' in the result,
110  * so callers can recognise each individual request they make.
111  */
112  virtual u32 ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const Goal& goal, pass_class_t passClass, cost_class_t costClass, entity_id_t notify) = 0;
113 
114  /**
115  * If the debug overlay is enabled, render the path that will computed by ComputePath.
116  */
117  virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const Goal& goal, pass_class_t passClass, cost_class_t costClass) = 0;
118 
119  /**
120  * Compute a precise path from the given point to the goal, and return the set of waypoints.
121  * The path is based on the full set of obstructions that pass the filter, such that
122  * a unit of radius 'r' will be able to follow the path with no collisions.
123  * The path is restricted to a box of radius 'range' from the starting point.
124  */
125  virtual void ComputeShortPath(const IObstructionTestFilter& filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal& goal, pass_class_t passClass, Path& ret) = 0;
126 
127  /**
128  * Asynchronous version of ComputeShortPath (using ControlGroupObstructionFilter).
129  * The result will be sent as CMessagePathResult to 'notify'.
130  * Returns a unique non-zero number, which will match the 'ticket' in the result,
131  * so callers can recognise each individual request they make.
132  */
133  virtual u32 ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal& goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t group, entity_id_t notify) = 0;
134 
135  /**
136  * Find the speed factor (typically around 1.0) for a unit of the given cost class
137  * at the given position.
138  */
139  virtual fixed GetMovementSpeed(entity_pos_t x0, entity_pos_t z0, cost_class_t costClass) = 0;
140 
141  /**
142  * Returns the coordinates of the point on the goal that is closest to pos in a straight line.
143  */
144  virtual CFixedVector2D GetNearestPointOnGoal(CFixedVector2D pos, const Goal& goal) = 0;
145 
146  /**
147  * Check whether the given movement line is valid and doesn't hit any obstructions
148  * or impassable terrain.
149  * Returns true if the movement is okay.
150  */
151  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) = 0;
152 
153  /**
154  * Check whether a unit placed here is valid and doesn't hit any obstructions
155  * or impassable terrain.
156  * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
157  * a value describing the type of failure.
158  */
160 
161  /**
162  * Check whether a unit placed here is valid and doesn't hit any obstructions
163  * or impassable terrain.
164  * When onlyCenterPoint = true, only check the center tile of the unit
165  * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
166  * a value describing the type of failure.
167  */
168  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) = 0;
169 
170  /**
171  * Check whether a building placed here is valid and doesn't hit any obstructions
172  * or impassable terrain.
173  * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
174  * a value describing the type of failure.
175  */
177 
178  /**
179  * Check whether a building placed here is valid and doesn't hit any obstructions
180  * or impassable terrain.
181  * when onlyCenterPoint = true, only check the center tile of the building
182  * @return ICmpObstruction::FOUNDATION_CHECK_SUCCESS if the placement is okay, else
183  * a value describing the type of failure.
184  */
186 
187 
188  /**
189  * Toggle the storage and rendering of debug info.
190  */
191  virtual void SetDebugOverlay(bool enabled) = 0;
192 
193  /**
194  * Finish computing asynchronous path requests and send the CMessagePathResult messages.
195  */
196  virtual void FinishAsyncRequests() = 0;
197 
198  /**
199  * Process moves during the same turn they were created in to improve responsiveness.
200  */
201  virtual void ProcessSameTurnMoves() = 0;
202 
203  DECLARE_INTERFACE_TYPE(Pathfinder)
204 };
205 
206 #endif // INCLUDED_ICMPPATHFINDER
virtual pass_class_t GetPassabilityClass(const std::string &name)=0
Get the tag for a given passability class name.
virtual std::map< std::string, pass_class_t > GetPassabilityClasses()=0
Get the list of all known passability classes.
#define u8
Definition: types.h:39
A simple fixed-point number class.
Definition: Fixed.h:115
Interface for ICmpObstructionManager Test functions to filter out unwanted shapes.
virtual const Grid< u16 > & GetPassabilityGrid()=0
CFixedVector2D v
virtual u32 ComputeShortPathAsync(entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal &goal, pass_class_t passClass, bool avoidMovingUnits, entity_id_t group, entity_id_t notify)=0
Asynchronous version of ComputeShortPath (using ControlGroupObstructionFilter).
virtual void ProcessSameTurnMoves()=0
Process moves during the same turn they were created in to improve responsiveness.
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)=0
Check whether a building placed here is valid and doesn&#39;t hit any obstructions or impassable terrain...
virtual void FinishAsyncRequests()=0
Finish computing asynchronous path requests and send the CMessagePathResult messages.
virtual fixed GetMovementSpeed(entity_pos_t x0, entity_pos_t z0, cost_class_t costClass)=0
Find the speed factor (typically around 1.0) for a unit of the given cost class at the given position...
Returned path.
std::vector< Waypoint > m_Waypoints
virtual cost_class_t GetCostClass(const std::string &name)=0
Get the tag for a given movement cost class name.
Basic 2D array, intended for storing tile data, plus support for lazy updates by ICmpObstructionManag...
Pathfinder algorithms.
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)=0
Check whether the given movement line is valid and doesn&#39;t hit any obstructions or impassable terrain...
virtual ICmpObstruction::EFoundationCheck CheckUnitPlacement(const IObstructionTestFilter &filter, entity_pos_t x, entity_pos_t z, entity_pos_t r, pass_class_t passClass)=0
Check whether a unit placed here is valid and doesn&#39;t hit any obstructions or impassable terrain...
CFixedVector2D u
#define DECLARE_INTERFACE_TYPE(iname)
Definition: Interface.h:23
virtual u32 ComputePathAsync(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass, entity_id_t notify)=0
Asynchronous version of ComputePath.
#define u16
Definition: types.h:40
virtual void SetDebugPath(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass)=0
If the debug overlay is enabled, render the path that will computed by ComputePath.
enum ICmpPathfinder::Goal::Type type
#define u32
Definition: types.h:41
virtual void ComputePath(entity_pos_t x0, entity_pos_t z0, const Goal &goal, pass_class_t passClass, cost_class_t costClass, Path &ret)=0
Compute a tile-based path from the given point to the goal, and return the set of waypoints...
virtual void ComputeShortPath(const IObstructionTestFilter &filter, entity_pos_t x0, entity_pos_t z0, entity_pos_t r, entity_pos_t range, const Goal &goal, pass_class_t passClass, Path &ret)=0
Compute a precise path from the given point to the goal, and return the set of waypoints.
virtual void SetDebugOverlay(bool enabled)=0
Toggle the storage and rendering of debug info.
virtual CFixedVector2D GetNearestPointOnGoal(CFixedVector2D pos, const Goal &goal)=0
Returns the coordinates of the point on the goal that is closest to pos in a straight line...
Entity coordinate types.
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24