Dragonfly  4.3
A text-based game engine
PathFind.h
1 ///
2 /// An A* pathfinding class.
3 ///
4 
5 #ifndef __PATHFIND_H__
6 #define __PATHFIND_H__
7 
8 // Engine includes.
9 #include "EventPath.h"
10 #include "Object.h"
11 #include "Vector.h"
12 
13 namespace df {
14 
15 class Object; // Forward reference.
16 
17 // Coordinates for pathfinding.
18 struct coord_struct {
19  int x, y;
20 };
21 
22 // Nodes for pathfinding.
23 struct node_struct {
24  float from_start; // Cost from start.
25  float to_end; // Estimate to end + cost from start.
26  struct coord_struct came_from; // Most efficient path back.
27 };
28 
29 class PathFind {
30 
31  private:
32  Object *m_p_o; ///< Object finding path.
33  Object *m_p_target_o; ///< Object target (optional).
34  coord_struct m_start; ///< Start position.
35  coord_struct m_end; ///< End target position.
36  struct node_struct **m_node; ///< Nodes in graph.
37  std::vector<coord_struct> m_open_set; ///< Set of nodes not yet visited.
38  std::vector<coord_struct> m_closed_set;///< Set of nodes visited.
39  int m_num_plies; ///< Number of plies cycled.
40  bool m_is_path; ///< True if path is complete.
41 
42  public:
43 
44  /// Constructor needs Object doing pathfinding, start and end locations.
45  PathFind(Object *p_o, Vector start, Vector end);
46  ~PathFind();
47 
48  /// Go through one search cycle.
49  /// Returns indicatation if found path {found, not found, incomplete}.
50  SearchResult cycleOnce();
51 
52  /// Return path.
53  Path getPath() const;
54 
55  /// Set object seeking path.
56  void setObject(Object *new_p_o);
57 
58  /// Return object seeking path.
59  Object *getObject() const;
60 
61  /// Return number of plies.
62  int getNumPlies() const;
63 
64  /// Set target object.
65  /// Pathfinding stops when target object discovered.
66  void setTargetObject(Object *p_target_o);
67 
68  /// Get target object.
69  /// Pathfinding stops when target object discovered.
70  Object *getTargetObject();
71 };
72 
73 } // end of namespace df
74 #endif //__PATHFIND_H__
Definition: Path.h:16
std::vector< coord_struct > m_closed_set
Set of nodes visited.
Definition: PathFind.h:38
bool m_is_path
True if path is complete.
Definition: PathFind.h:40
Object * m_p_o
Object finding path.
Definition: PathFind.h:32
A 2-d bounding box.
Definition: Box.h:10
Object * m_p_target_o
Object target (optional).
Definition: PathFind.h:33
Definition: PathFind.h:29
Definition: PathFind.h:23
coord_struct m_start
Start position.
Definition: PathFind.h:34
struct node_struct ** m_node
Nodes in graph.
Definition: PathFind.h:36
Definition: Object.h:59
std::vector< coord_struct > m_open_set
Set of nodes not yet visited.
Definition: PathFind.h:37
int m_num_plies
Number of plies cycled.
Definition: PathFind.h:39
Definition: PathFind.h:18
coord_struct m_end
End target position.
Definition: PathFind.h:35
Definition: Vector.h:10