ScummVM API documentation
PathfindingAgent.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * This code is based on the CRAB engine
24  *
25  * Copyright (c) Arvind Raja Yadav
26  *
27  * Licensed under MIT
28  *
29  */
30 
31 #ifndef CRAB_PATHFINDINGAGENT_H
32 #define CRAB_PATHFINDINGAGENT_H
33 
34 #include "common/stablemap.h"
35 #include "crab/PathfindingGrid.h"
36 #include "crab/PriorityQueue.h"
37 #include "crab/vectors.h"
38 
39 namespace Crab {
40 
41 // This class represents the actual pathfinding and following agent that utilizes
42 // the pathfinding grid
43 class PlannerNode {
44  PlannerNode *_parent;
45  PlannerNode *_child;
46 
47  double _cost; // Heuristic cost equivalent to cost to reach goal from planner node's position.
48  double _finalCost; // Final cost of route through the planner node. Used to determine optimal path.
49  double _givenCost; // The current distance of the route.
50 
51 public:
52  PathfindingGraphNode *_location;
53 
54  PlannerNode() {
55  _location = nullptr;
56  _parent = nullptr;
57  _child = nullptr;
58  _cost = 0;
59  _finalCost = 0;
60  _givenCost = 0;
61  }
62  ~PlannerNode() {}
63 
64  PathfindingGraphNode *getLocation() {
65  return _location;
66  }
67 
68  PlannerNode *getParent() {
69  return _parent;
70  }
71 
72  PlannerNode *getChild() {
73  return _child;
74  }
75 
76  double getHCost() const {
77  return _cost;
78  }
79 
80  double getFinalCost() const {
81  return _finalCost;
82  }
83 
84  double getGivenCost() const {
85  return _givenCost;
86  }
87 
88  void setLocation(PathfindingGraphNode *loc) {
89  _location = loc;
90  }
91 
92  void setParent(PlannerNode *p) {
93  _parent = p;
94  }
95 
96  void setChild(PlannerNode *c) {
97  _child = c;
98  }
99 
100  void setHCost(double c) {
101  _cost = c;
102  }
103 
104  void setFinalCost(double c) {
105  _finalCost = c;
106  }
107 
108  void setGivenCost(double c) {
109  _givenCost = c;
110  }
111 };
112 
114  Vector2f _position;
115  Vector2f _prevPosition; // Used to determine that we are making progress toward the goal
116  Vector2i _immediateDest; // The next stop on the AI's path
117 
118  bool _destinationSet; // Was a destination specified.
119  bool _destinationReachable; // Can the agent actually get to the destination?
120 
121  float _nodeBufferDistance; // How much leeway is there for reaching the destination
122 
123 public:
125  ~PathfindingAgent();
126 
127  PathfindingGrid *_grid;
128 
129  Vector2f _destination;
130 
131  PathfindingGraphNode *_startTile; // The system originally used tiles, but this one uses discreet points.
132  PathfindingGraphNode *_goalTile; // The tile we are trying to reach. May not be the tile that was clicked if the clicked tile is blocked.
133  PathfindingGraphNode *_clickedTile; // The tile that was clicked. If it is open, it will be the goal node.
134 
135  bool _solutionFound;
136  bool _noSolution;
137 
139 
140  void setNodeBufferDistance(float w) {
141  _nodeBufferDistance = w;
142  }
143 
144  float getNodeBufferDistance() {
145  return _nodeBufferDistance;
146  }
147 
148  // Added for Greedy search
149  double distSquared(PathfindingGraphNode *tileA, PathfindingGraphNode *tileB);
150  // Added for A* search
151  double distExact(PathfindingGraphNode *tileA, PathfindingGraphNode *tileB);
152 
153  PriorityQueue<PlannerNode> _nodeQueue;
154 
156 
157  // void SetSprite(pyrodactyl::anim::Sprite* s){entitySprite = s;}
158 
165  void initialize(PathfindingGrid *g);
166 
167  void setDestination(Vector2f d);
168  void setDestination(Vector2f d, bool r);
169  void setDestination(Vector2i d);
170  void setDestination(Vector2i d, bool r);
171 
172  void setPosition(Vector2f p) {
173  _position = p;
174  }
175 
176  void setPrevPosition(Vector2f p) {
177  _prevPosition = p;
178  }
179 
180  Vector2f getPosition() {
181  return _position;
182  }
183 
184  bool positionChanged() {
185  return _position != _prevPosition;
186  }
187 
188  Vector2i getImmediateDest() {
189  return _immediateDest;
190  }
191 
194  void update(uint32 timeslice);
195 
199  bool isDone() const;
200 
202  Common::Array<PathfindingGraphNode const *> const getSolution(PathfindingGraphNode *destNode) const;
203 
204  // Get the solution removing any nodes that are completely surrounded by open space.
205  // This will result in a more linear path to the goal.
206  Common::Array<PathfindingGraphNode const *> const getPrunedSolution(PathfindingGraphNode *destNode);
207 
209  void reset();
210 
212  void shutdown();
213 
214  // Returns true if the node connects to the goal node
215  bool adjacentToGoal(PathfindingGraphNode *node);
216 };
217 
218 } // End of namespace Crab
219 
220 #endif // CRAB_PATHFINDINGAGENT_H
Definition: array.h:52
Definition: PathfindingGrid.h:43
The open heap used by all cost-based search algorithms.
Definition: PriorityQueue.h:42
Definition: PathfindingAgent.h:113
Definition: stablemap.h:43
Definition: moveeffect.h:37
Definition: PathfindingAgent.h:43
Definition: PathfindingGraphNode.h:41