ScummVM API documentation
graph.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 #ifndef TWP_GRAPH_H
23 #define TWP_GRAPH_H
24 
25 #include "common/array.h"
26 #include "math/vector2d.h"
27 #include "twp/util.h"
28 
29 namespace Twp {
30 
32 public:
34 
35  void insert(int index);
36  int pop();
37 
38  void reorderUp();
39  void reorderDown();
40 
41  bool isEmpty();
42 
43 private:
44  Common::Array<float> &_keys;
45  Common::Array<int> _data;
46 };
47 
48 // An edge is a part of a walkable area, it is used by a Graph.
49 // See also:
50 // - PathFinder
51 // - Graph
52 struct GraphEdge {
53  GraphEdge(int start, int to, float cost);
54 
55  int start; // Index of the node in the graph representing the start of the edge.
56  int to; // Index of the node in the graph representing the end of the edge.
57  float cost; // Cost of the edge in the graph.
58 };
59 
60 // A graph helps to find a path between two points.
61 // This class has been ported from http://www.groebelsloot.com/2016/03/13/pathfinding-part-2/
62 // and modified
63 class Graph {
64 public:
65  Graph();
66  void addNode(const Math::Vector2d &node);
67  void addEdge(const GraphEdge &edge);
68  // Gets the edge from 'from' index to 'to' index.
69  GraphEdge *edge(int start, int to);
70  Common::Array<int> getPath(int source, int target);
71 
74  Common::Array<Math::Vector2d> _concaveVertices;
75 };
76 
77 class AStar {
78 public:
79  AStar(Graph *graph);
80  void search(int source, int target);
81 
82  Graph *_graph = nullptr;
83  Common::Array<GraphEdge *> _spt; // The Shortest Path Tree
84  Common::Array<float> _gCost; // This array will store the G cost of each node
85  Common::Array<float> _fCost; // This array will store the F cost of each node
86  Common::Array<GraphEdge *> _sf; // The Search Frontier
87 };
88 
89 // Represents an area where an actor can or cannot walk
90 class Walkbox {
91 public:
92  Walkbox(const Common::Array<Vector2i> &polygon, bool visible = true);
93 
94  // Indicates whether or not the specified position is inside this walkbox.
95  bool contains(const Math::Vector2d &position, bool toleranceOnOutside = true) const;
96  bool concave(int vertex) const;
97  void setVisible(bool visible) { _visible = visible; }
98  bool isVisible() const { return _visible; }
99  const Common::Array<Vector2i> &getPoints() const { return _polygon; }
100  Math::Vector2d getClosestPointOnEdge(const Math::Vector2d &p) const;
101 
102 public:
103  Common::String _name;
104 
105 private:
106  Common::Array<Vector2i> _polygon;
107  bool _visible;
108 };
109 
110 // A PathFinder is used to find a walkable path within one or several walkboxes.
111 class PathFinder {
112 public:
113  void setWalkboxes(const Common::Array<Walkbox> &walkboxes);
114  Common::Array<Walkbox> getWalkboxes() const { return _walkboxes; }
115  Common::Array<Math::Vector2d> calculatePath(const Math::Vector2d &start, const Math::Vector2d &to);
116  void setDirty(bool dirty) { _isDirty = dirty; }
117  bool isDirty() const { return _isDirty; }
118  const Graph &getGraph() const { return _walkgraph; }
119 
120 private:
121  Common::SharedPtr<Graph> createGraph();
122  bool inLineOfSight(const Math::Vector2d &start, const Math::Vector2d &to);
123 
124 private:
125  Common::Array<Walkbox> _walkboxes;
127  Graph _walkgraph;
128  bool _isDirty = true;
129 };
130 
131 } // namespace Twp
132 
133 #endif
Definition: graph.h:90
Definition: str.h:59
Definition: graph.h:111
Definition: graph.h:77
Definition: graph.h:31
Definition: graph.h:52
Definition: ptr.h:159
Definition: graph.h:63
Definition: achievements_tables.h:27