Cuberite
A lightweight, fast and extensible game server for Minecraft
Path.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 /*
5 // Needed Fwds: cPath
6 enum class ePathFinderStatus;
7 class cPath;
8 */
9 
10 
11 #include "../FastRandom.h"
12 #ifdef COMPILING_PATHFIND_DEBUGGER
13  /* Note: the COMPILING_PATHFIND_DEBUGGER flag is used by Native / WiseOldMan95 to debug
14  this class outside of Cuberite. This preprocessor flag is never set when compiling Cuberite. */
15  #include "PathFinderIrrlicht_Head.h"
16 #endif
17 
18 #include <unordered_map>
19 
20 //fwd: ../Chunk.h
21 class cChunk;
22 
23 
24 /* Various little structs and classes */
42 struct cPathCell
43 {
44  Vector3i m_Location; // Location of the cell in the world.
45  int m_F, m_G, m_H; // F, G, H as defined in regular A*.
46  eCellStatus m_Status; // Which list is the cell in? Either non, open, or closed.
47  cPathCell * m_Parent; // Cell's parent, as defined in regular A*.
48  bool m_IsSolid; // Is the cell an air or a solid? Partial solids are considered solids. If m_IsSpecial is true, this is always true.
49  bool m_IsSpecial; // The cell is special - it acts as "solid" or "air" depending on direction, e.g. door or top of fence.
52 };
53 
54 
55 
56 
58 {
59 public:
60  bool operator()(cPathCell * & a_V1, cPathCell * & a_V2);
61 };
62 
63 
64 
65 
66 
67 class cPath
68 {
69 public:
79  cPath(
80  cChunk & a_Chunk,
81  const Vector3d & a_StartingPoint, const Vector3d & a_EndingPoint, int a_MaxSteps,
82  double a_BoundingBoxWidth, double a_BoundingBoxHeight
83  );
84 
86  cPath();
87 
89  cPath(const cPath & a_other) = delete;
90  cPath(cPath && a_other) = delete;
91 
92  cPath & operator=(const cPath & a_other) = delete;
93  cPath & operator=(cPath && a_other) = delete;
94 
101  ePathFinderStatus CalculationStep(cChunk & a_Chunk);
102 
106  Vector3i AcceptNearbyPath();
107 
108  // Point retrieval functions, inlined for performance:
109 
112  {
114  ASSERT(m_CurrentPoint < m_PathPoints.size());
115  Vector3i Point = m_PathPoints[m_PathPoints.size() - 1 - m_CurrentPoint];
116  ++m_CurrentPoint;
117  return Vector3d(Point.x + m_HalfWidth, Point.y, Point.z + m_HalfWidth);
118  }
119 
120 
122  inline bool NoMoreWayPoints() const
123  {
125  return (m_CurrentPoint == m_PathPoints.size());
126  }
127 
129  inline bool IsFirstPoint() const
130  {
132  return (m_CurrentPoint == 0);
133  }
134 
138  inline bool IsValid() const
139  {
140  return m_IsValid;
141  }
142 
144  inline size_t WayPointsLeft() const
145  {
147  return m_PathPoints.size() - m_CurrentPoint;
148  }
149 
150 
151 
152 
153 private:
154 
155  /* General */
156  bool StepOnce(); // The public version just calls this version * CALCULATIONS_PER_CALL times.
157  void FinishCalculation(); // Clears the memory used for calculating the path.
158  void FinishCalculation(ePathFinderStatus a_NewStatus); // Clears the memory used for calculating the path and changes the status.
159  void AttemptToFindAlternative();
160  void BuildPath();
161 
162  /* Openlist and closedlist management */
163  void OpenListAdd(cPathCell * a_Cell);
164  cPathCell * OpenListPop();
165  bool ProcessIfWalkable(const Vector3i & a_Location, cPathCell * a_Source, int a_Cost);
166 
167  /* Map management */
168  void ProcessCell(cPathCell * a_Cell, cPathCell * a_Caller, int a_GDelta);
169  cPathCell * GetCell(const Vector3i & a_location);
170 
171  /* Pathfinding fields */
172  std::priority_queue<cPathCell *, std::vector<cPathCell *>, compareHeuristics> m_OpenList;
173  std::unordered_map<Vector3i, cPathCell, VectorHasher<int>> m_Map;
178  double m_HalfWidth;
181 
182  /* Control fields */
184  bool m_IsValid;
185 
186  /* Final path fields */
188  std::vector<Vector3i> m_PathPoints;
189 
190  /* Interfacing with the world */
191  void FillCellAttributes(cPathCell & a_Cell); // Query our hosting world and fill the cell with info
192  cChunk * m_Chunk; // Only valid inside Step()!
194 
195  /* High level world queries */
196  bool IsWalkable(const Vector3i & a_Location, const Vector3i & a_Source);
197  bool BodyFitsIn(const Vector3i & a_Location, const Vector3i & a_Source);
198  bool BlockTypeIsSpecial(BLOCKTYPE a_Type);
199  bool SpecialIsSolidFromThisDirection(BLOCKTYPE a_Type, NIBBLETYPE a_Meta, const Vector3i & a_Direction);
200  bool HasSolidBelow(const Vector3i & a_Location);
201  #ifdef COMPILING_PATHFIND_DEBUGGER
202  #include "../path_irrlicht.cpp"
203  #endif
204 };
int m_BoundingBoxWidth
Definition: Path.h:176
bool m_IsValid
Definition: Path.h:184
int m_StepsLeft
Definition: Path.h:179
bool m_IsSpecial
Definition: Path.h:49
double m_HalfWidth
Definition: Path.h:178
unsigned char BLOCKTYPE
The datatype used by blockdata.
Definition: ChunkDef.h:42
NIBBLETYPE m_BlockMeta
Definition: Path.h:51
Definition: Path.h:67
size_t WayPointsLeft() const
The amount of waypoints left to return.
Definition: Path.h:144
cChunk * m_Chunk
Definition: Path.h:192
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
Definition: ChunkDef.h:45
Definition: Chunk.h:49
std::unordered_map< Vector3i, cPathCell, VectorHasher< int > > m_Map
Definition: Path.h:173
Vector3i m_Destination
Definition: Path.h:174
int m_H
Definition: Path.h:45
bool m_IsSolid
Definition: Path.h:48
Vector3i m_Location
Definition: Path.h:44
ePathFinderStatus m_Status
Definition: Path.h:183
std::priority_queue< cPathCell *, std::vector< cPathCell * >, compareHeuristics > m_OpenList
Definition: Path.h:172
Vector3< double > Vector3d
Definition: Vector3.h:445
#define ASSERT(x)
Definition: Globals.h:335
size_t m_CurrentPoint
Definition: Path.h:187
int m_BoundingBoxHeight
Definition: Path.h:177
ePathFinderStatus
Definition: Path.h:25
BLOCKTYPE m_BlockType
Definition: Path.h:50
bool IsValid() const
Returns true if this path is properly initialized.
Definition: Path.h:138
cPathCell * m_NearestPointToTarget
Definition: Path.h:180
Vector3d GetNextPoint()
Returns the next point in the path.
Definition: Path.h:111
cPathCell * m_Parent
Definition: Path.h:47
eCellStatus m_Status
Definition: Path.h:46
eCellStatus
Definition: Path.h:26
bool m_BadChunkFound
Definition: Path.h:193
The pathfinder has 3 types of cells (cPathCell).
Definition: Path.h:42
Vector3i m_Source
Definition: Path.h:175
bool NoMoreWayPoints() const
Checks if we have no more waypoints to return.
Definition: Path.h:122
std::vector< Vector3i > m_PathPoints
Definition: Path.h:188
bool IsFirstPoint() const
Returns true if GetNextPoint() was never called for this Path.
Definition: Path.h:129