SuperTuxKart
drive_graph.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2009-2015 Joerg Henrichs
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, B
18 
19 #ifndef HEADER_DRIVE_GRAPH_HPP
20 #define HEADER_DRIVE_GRAPH_HPP
21 
22 #include <vector>
23 #include <string>
24 
25 #include "tracks/graph.hpp"
26 #include "utils/aligned_array.hpp"
27 #include "utils/cpp2011.hpp"
28 
29 #include "LinearMath/btTransform.h"
30 
31 class DriveNode;
32 class XMLNode;
33 
38 class DriveGraph : public Graph
39 {
40 private:
42  float m_lap_length;
43 
45  std::string m_quad_filename;
46 
48  bool m_reverse;
49 
50  // ------------------------------------------------------------------------
51  void setDefaultSuccessors();
52  // ------------------------------------------------------------------------
53  void computeChecklineRequirements(DriveNode* node, int latest_checkline);
54  // ------------------------------------------------------------------------
55  void computeDirectionData();
56  // ------------------------------------------------------------------------
57  void determineDirection(unsigned int current, unsigned int succ_index);
58  // ------------------------------------------------------------------------
59  float normalizeAngle(float f);
60  // ------------------------------------------------------------------------
61  void addSuccessor(unsigned int from, unsigned int to);
62  // ------------------------------------------------------------------------
63  void load(const std::string &quad_file_name, const std::string &filename);
64  // ------------------------------------------------------------------------
65  void getPoint(const XMLNode *xml, const std::string &attribute_name,
66  Vec3 *result) const;
67  // ------------------------------------------------------------------------
68  void computeDistanceFromStart(unsigned int start_node, float distance);
69  // ------------------------------------------------------------------------
70  unsigned int getStartNode() const;
71  // ------------------------------------------------------------------------
72  virtual bool hasLapLine() const OVERRIDE;
73  // ------------------------------------------------------------------------
74  virtual void differentNodeColor(int n, video::SColor* c) const OVERRIDE;
75 
76 public:
77  static DriveGraph* get() { return dynamic_cast<DriveGraph*>(m_graph); }
78  // ------------------------------------------------------------------------
79  DriveGraph(const std::string &quad_file_name,
80  const std::string &graph_file_name, const bool reverse);
81  // ------------------------------------------------------------------------
82  virtual ~DriveGraph() {}
83  // ------------------------------------------------------------------------
84  void getSuccessors(int node_number, std::vector<unsigned int>& succ,
85  bool for_ai=false) const;
86  // ------------------------------------------------------------------------
87  void spatialToTrack(Vec3 *dst, const Vec3& xyz, const int sector) const;
88  // ------------------------------------------------------------------------
89  void setDefaultStartPositions(AlignedArray<btTransform> *start_transforms,
90  unsigned int karts_per_row,
91  float forwards_distance = 1.5f,
92  float sidewards_distance = 1.5f,
93  float upwards_distance=0.0f) const;
94  // ------------------------------------------------------------------------
95  void updateDistancesForAllSuccessors(unsigned int indx, float delta,
96  unsigned int count);
97  // ------------------------------------------------------------------------
98  void setupPaths();
99  // ------------------------------------------------------------------------
101  // ------------------------------------------------------------------------
103  float getDistanceToNext(int n, int j) const;
104  // ------------------------------------------------------------------------
107  float getAngleToNext(int n, int j) const;
108  // ------------------------------------------------------------------------
110  int getNumberOfSuccessors(int n) const;
111  // ------------------------------------------------------------------------
113  DriveNode* getNode(unsigned int j) const;
114  // ------------------------------------------------------------------------
116  float getDistanceFromStart(int j) const;
117  // ------------------------------------------------------------------------
119  float getLapLength() const { return m_lap_length; }
120  // ------------------------------------------------------------------------
121  bool isReverse() const { return m_reverse; }
122 
123 }; // DriveGraph
124 
125 #endif
void computeDirectionData()
Computes the direction (straight, left, right) of all graph nodes and the lastest graph node that is ...
Definition: drive_graph.cpp:578
void getPoint(const XMLNode *xml, const std::string &attribute_name, Vec3 *result) const
This function interprets a point specification as an attribute in the xml quad file.
Definition: drive_graph.cpp:66
void determineDirection(unsigned int current, unsigned int succ_index)
Determines the direction of the drive graph when driving to the specified successor.
Definition: drive_graph.cpp:618
void setDefaultStartPositions(AlignedArray< btTransform > *start_transforms, unsigned int karts_per_row, float forwards_distance=1.5f, float sidewards_distance=1.5f, float upwards_distance=0.0f) const
Sets all start positions depending on the drive graph.
Definition: drive_graph.cpp:384
float getLapLength() const
Returns the length of the main driveline.
Definition: drive_graph.hpp:119
A wrapper around bullets btVector3 to include conventient conversion functions (e....
Definition: vec3.hpp:34
unsigned int getStartNode() const
Returns the index of the first graph node (i.e.
Definition: drive_graph.cpp:274
void updateDistancesForAllSuccessors(unsigned int indx, float delta, unsigned int count)
Increases the distance from start for all nodes that are directly or indirectly a successor of the gi...
Definition: drive_graph.cpp:529
This class stores a graph of quads.
Definition: graph.hpp:52
void setupPaths()
This function defines the "path-to-nodes" for each graph node that has more than one successor.
Definition: drive_graph.cpp:341
A graph made from driveline.
Definition: drive_graph.hpp:38
void computeDistanceFromStart(unsigned int start_node, float distance)
Recursively determines the distance the beginning (lower end) of the quads have from the start of the...
Definition: drive_graph.cpp:480
float m_lap_length
The length of the first loop.
Definition: drive_graph.hpp:42
int getNumberOfSuccessors(int n) const
Returns the number of successors of a node n.
Definition: drive_graph.cpp:702
float getAngleToNext(int n, int j) const
Returns the angle of the line between node n and its j-th.
Definition: drive_graph.cpp:696
DriveGraph(const std::string &quad_file_name, const std::string &graph_file_name, const bool reverse)
Constructor, loads the graph information for a given set of quads from a graph file.
Definition: drive_graph.cpp:40
This class stores a node of the drive graph, i.e.
Definition: drive_node.hpp:31
void load(const std::string &quad_file_name, const std::string &filename)
Loads a drive graph from a file.
Definition: drive_graph.cpp:92
std::string m_quad_filename
Stores the filename - just used for error messages.
Definition: drive_graph.hpp:45
float getDistanceToNext(int n, int j) const
Return the distance to the j-th successor of node n.
Definition: drive_graph.cpp:690
void computeChecklineRequirements()
Sets the checkline requirements for all nodes in the graph.
Definition: drive_graph.cpp:283
void setDefaultSuccessors()
This function sets a default successor for all graph nodes that currently don't have a successor defi...
Definition: drive_graph.cpp:353
utility class used to parse XML files
Definition: xml_node.hpp:47
void spatialToTrack(Vec3 *dst, const Vec3 &xyz, const int sector) const
This function takes absolute coordinates (coordinates in OpenGL space) and transforms them into coord...
Definition: drive_graph.cpp:677
void getSuccessors(int node_number, std::vector< unsigned int > &succ, bool for_ai=false) const
Returns the list of successors or a node.
Definition: drive_graph.cpp:460
float normalizeAngle(float f)
Adjust the given angle to be in [-PI, PI].
Definition: drive_graph.cpp:595
bool m_reverse
Wether the graph should be reverted or not.
Definition: drive_graph.hpp:48
DriveNode * getNode(unsigned int j) const
Returns the quad that belongs to a graph node.
Definition: drive_graph.cpp:727
float getDistanceFromStart(int j) const
Returns the distance from the start to the beginning of a quad.
Definition: drive_graph.cpp:708