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