WorldSim  inDev
2D tile-based sandbox RPG with procedurally generated fantasy world simulator 🌏
Pathing.hpp
Go to the documentation of this file.
1 #ifndef WORLDSIM_PATHING_HPP
2 #define WORLDSIM_PATHING_HPP
3 
4 /* Pathing.hpp
5  #include"Pathing.hpp"
6 
7  This class handles all pathing within the world and local maps. Pathing objects can automatically update
8  and also be built incrementally over several turns, which can improve performance. Threading will also be an option
9 */
10 
11 #include "World_Local.hpp"
12 
13 #include <math.h>
14 #include "Item.hpp"
15 
16 //#define PATHING_MARKERS
17 
19 {
20 
21  struct Node
22  {
23  int x,y,z;
24  int distanceFromTarget;
25  int distanceFromSource;
26  bool exhausted;
27  bool created;
28 
29  Node* parent;
30  char pathName;
31 
32  Node()
33  {
34  exhausted=false;
35  x=0; y=0;
36  distanceFromTarget=0; distanceFromSource=0;
37  created=false;
38 
39  parent=0;
40  pathName=0;
41  }
42  void findDistance(int _currentX, int _currentY, int _sX, int _sY, int _tX, int _tY)
43  {
44  /*manhattan distance*/
45  distanceFromTarget=abs((_currentX-_tX))+abs((_currentY-_tY));
46  distanceFromSource=abs((_currentX-_sX))+abs((_currentY-_sY));
47  }
48  void setCoords(const int _x, const int _y)
49  {
50  x=_x;
51  y=_y;
52  }
53  };
54 
55  World_Local* map;
56 
57  ArrayS2 <Node*> aNode;
58  Vector <Node*> vNode;
59  /* The last node which was closest to/furthest from the target. Useful for providing incomplete solution. */
60  // For now we find it from the vector. In future we might track it.
62 
63 
64  Node* finalNode;
65 
66  int sourceOriginalX;
67  int sourceOriginalY;
68 
69  int pathSize;
70 
71  int sX;
72  int sY;
73 
74  int tX;
75  int tY;
76 
77 
78  bool finished;
79  bool flipBest; /* Prevent always going a single direction. */
80 
81 
82  public:
83 
84  Vector <char> vPath;
85 
86  Pathing_Local();
88 
89  void init(World_Local* _map);
90 
91  // Do a simple local A* path. _pathSize 0 = unlimited
92  // pathAway true sets best nodes as those furthest away from target.
93  bool pathLocal(int _startX, int _startY, int _endX, int _endY, int _pathSize=50, bool pathAway=false);
94 
95  // Simple path basically just walks in the general direction of the target.
96  // This is done before using the more expensive A*.
97  bool appendSimplePath();
98 
99  // This only expands local tiles for pathing. Path will only be able to consider tiles in the current map.
100  void expandLocal(int _x, int _y);
101 
102  void expandLocal (Node* pNode);
103 
104  bool expandBest(bool pathAway);
105 
106  void getPath(bool pathAway);
107  void printPath();
108 
109 };
110 
111 #endif
void printPath()
Definition: Pathing.cpp:433
bool appendSimplePath()
Definition: Pathing.cpp:161
void getPath(bool pathAway)
Definition: Pathing.cpp:315
Definition: Pathing.hpp:18
Pathing_Local()
Definition: Pathing.cpp:20
void init(World_Local *_map)
Definition: Pathing.cpp:51
Definition: World_Local.hpp:58
Vector< char > vPath
Definition: Pathing.hpp:84
~Pathing_Local()
Definition: Pathing.cpp:40
void expandLocal(int _x, int _y)
Definition: Pathing.cpp:167
bool pathLocal(int _startX, int _startY, int _endX, int _endY, int _pathSize=50, bool pathAway=false)
Definition: Pathing.cpp:103
bool expandBest(bool pathAway)
Definition: Pathing.cpp:248