SuperTuxKart
arena_graph.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2016 SuperTuxKart Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_ARENA_GRAPH_HPP
20 #define HEADER_ARENA_GRAPH_HPP
21 
22 #include "tracks/graph.hpp"
23 #include "utils/cpp2011.hpp"
24 
25 #include <cstdint>
26 #include <set>
27 
28 class ArenaNode;
29 class XMLNode;
30 
35 class ArenaGraph : public Graph
36 {
37 private:
39  std::vector<std::vector<float>> m_distance_matrix;
40 
42  std::vector<std::vector<int16_t>> m_parent_node;
43 
45  std::set<int> m_red_node;
46 
47  std::set<int> m_blue_node;
48 
49  // ------------------------------------------------------------------------
50  void loadGoalNodes(const XMLNode *node);
51  // ------------------------------------------------------------------------
52  void loadNavmesh(const std::string &navmesh);
53  // ------------------------------------------------------------------------
54  void buildGraph();
55  // ------------------------------------------------------------------------
56  void setNearbyNodesOfAllNodes();
57  // ------------------------------------------------------------------------
58  void computeDijkstra(int n);
59  // ------------------------------------------------------------------------
60  void computeFloydWarshall();
61  // ------------------------------------------------------------------------
62  static std::vector<int16_t> getPathFromTo(int from, int to,
63  const std::vector< std::vector< int16_t > >& parent_node);
64  // ------------------------------------------------------------------------
65  virtual bool hasLapLine() const OVERRIDE { return false; }
66  // ------------------------------------------------------------------------
67  virtual void differentNodeColor(int n, video::SColor* c) const OVERRIDE;
68 
69 public:
70  static ArenaGraph* get() { return dynamic_cast<ArenaGraph*>(m_graph); }
71  // ------------------------------------------------------------------------
72  static void unitTesting();
73  // ------------------------------------------------------------------------
74  ArenaGraph(const std::string &navmesh, const XMLNode *node = NULL);
75  // ------------------------------------------------------------------------
76  virtual ~ArenaGraph() {}
77  // ------------------------------------------------------------------------
78  ArenaNode* getNode(unsigned int i) const;
79  // ------------------------------------------------------------------------
84  int getNextNode(int i, int j) const
85  {
86  if (i == Graph::UNKNOWN_SECTOR || j == Graph::UNKNOWN_SECTOR)
87  return Graph::UNKNOWN_SECTOR;
88  return (int)(m_parent_node[j][i]);
89  }
90  // ------------------------------------------------------------------------
92  float getDistance(int from, int to) const
93  {
94  if (from == Graph::UNKNOWN_SECTOR || to == Graph::UNKNOWN_SECTOR)
95  return 99999.0f;
96  return m_distance_matrix[from][to];
97  }
98 
99 }; // ArenaGraph
100 
101 #endif
A graph made from navmesh.
Definition: arena_graph.hpp:36
static std::vector< int16_t > getPathFromTo(int from, int to, const std::vector< std::vector< int16_t > > &parent_node)
Determines the full path from 'from' to 'to' and returns it in a std::vector (in reverse order).
Definition: arena_graph.cpp:373
static void unitTesting()
Unit testing for arena graph distance and parent node computation.
Definition: arena_graph.cpp:393
int getNextNode(int i, int j) const
Returns the next node on the shortest path from i to j.
Definition: arena_graph.hpp:84
std::vector< std::vector< float > > m_distance_matrix
The actual graph data structure, it is an adjacency matrix.
Definition: arena_graph.hpp:39
std::set< int > m_red_node
Used in soccer mode to colorize the goal lines in minimap.
Definition: arena_graph.hpp:45
std::vector< std::vector< int16_t > > m_parent_node
The matrix that is used to store computed shortest paths.
Definition: arena_graph.hpp:42
float getDistance(int from, int to) const
Returns the distance between any two nodes.
Definition: arena_graph.hpp:92
void computeFloydWarshall()
THIS FUNCTION IS ONLY USED FOR UNIT-TESTING, to verify that the new Dijkstra algorithm gives the same...
Definition: arena_graph.cpp:277
void computeDijkstra(int n)
Dijkstra shortest path computation.
Definition: arena_graph.cpp:220
Definition: arena_node.hpp:32
This class stores a graph of quads.
Definition: graph.hpp:53
utility class used to parse XML files
Definition: xml_node.hpp:48