HOG2
Racetrack.h
Go to the documentation of this file.
1 //
2 // Racetrack.h
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 7/5/21.
6 //
7 
8 #ifndef RACETRACK_H
9 #define RACETRACK_H
10 
11 #include <stdio.h>
12 #include <cstdint>
13 #include <math.h>
14 #include "SearchEnvironment.h"
15 #include "Map2DEnvironment.h"
16 #include "Map.h"
17 
18 // TODO: Need to move code into its own namespace
19 
20 struct RacetrackMove {
21  RacetrackMove(int x=0, int y=0):xDelta(x), yDelta(y), hitGoal(false) {};
22  int xDelta;
23  int yDelta;
24  bool hitGoal;
25  int xGoal, yGoal;
26 };
27 
28 
30  int xLoc, yLoc; // was using xyLoc from Map2DEnvironment, but can't use unsigned for racetracks
32 };
33 
34 const int maxVelocity = 4;
35 
36 std::ostream &operator<<(std::ostream &out, const RacetrackState &s);
37 bool operator==(const RacetrackState &l1, const RacetrackState &l2);
38 bool operator!=(const RacetrackState &l1, const RacetrackState &l2);
39 std::ostream &operator<<(std::ostream &out, const RacetrackMove &m);
40 bool operator==(const RacetrackMove &m1, const RacetrackMove &m2); // comparing
41 
42 //Different types of domain
46 
47 class Racetrack : public SearchEnvironment<RacetrackState, RacetrackMove> {
48 public:
49  Racetrack(Map *map);
50  ~Racetrack();
51  void UpdateMap(Map *map);
52  void GetSuccessors(const RacetrackState &nodeID, std::vector<RacetrackState> &neighbors) const; //current state --> pass in a vector (array lists) __> fill in -- no modification
53  void GetActions(const RacetrackState &nodeID, std::vector<RacetrackMove> &actions) const; // no modification
54  int GetNumSuccessors(const RacetrackState &stateID) const
55  { std::vector<RacetrackState> neighbors; GetSuccessors(stateID, neighbors); return (int)neighbors.size(); }
56 
57  void Reset(RacetrackState &s) const;
58 
59 
60  void ApplyAction(RacetrackState &s, RacetrackMove a) const;// a = action s(state) gets changed
61  bool InvertAction(RacetrackMove &a) const;
63 
64  void Boundaries(RacetrackState &s, RacetrackMove &v) const;
65  bool Legal(const RacetrackState &node1, RacetrackMove &act) const;
66 
68  double HCost(const RacetrackState &node1, const RacetrackState &node2) const;
71  double HCost(const RacetrackState &node) const;
72 
73  double GCost(const RacetrackState &node1, const RacetrackState &node2) const { return 1; };
74  double GCost(const RacetrackState &node, const RacetrackMove &act) const { return 1; };
75  bool GoalTest(const RacetrackState &node, const RacetrackState &goal) const; // Goal reached?
76  //
77  uint64_t GetStateHash(const RacetrackState &node) const; // turn into a number
78  uint64_t GetActionHash(RacetrackMove act) const;
79 
80  // Deprecated
81  void OpenGLDraw() const {};
82  void OpenGLDraw(const RacetrackState&) const {};
83  void OpenGLDraw(const RacetrackState&, const RacetrackMove&) const {};
84 
85  void Draw(Graphics::Display &display) const;
86  void Draw(Graphics::Display &display, const RacetrackState &s) const;
87  void Draw(Graphics::Display &display, const RacetrackState&, RacetrackMove&) const;
88  void Draw(Graphics::Display &display, const RacetrackState &l1, const RacetrackState &l2, float v) const;
89 
90  void DrawLine(Graphics::Display &display, const RacetrackState &x, const RacetrackState &y, float width) const;
91 protected: // take two states and draw a line
92 private:
94  Map *map;
95  std::vector<int> heuristic;
96  int GetIndex(int x, int y) const {return static_cast<int>(map->GetMapWidth()*y+x);}
98 };
99 
100 
101 #endif /* TOH_hpp */
Graphics::point
Definition: Graphics.h:32
Racetrack::GetActions
void GetActions(const RacetrackState &nodeID, std::vector< RacetrackMove > &actions) const
Definition: Racetrack.cpp:122
Racetrack::UpdateMap
void UpdateMap(Map *map)
Definition: Racetrack.cpp:52
maxVelocity
const int maxVelocity
Definition: Racetrack.h:34
RacetrackMove::xDelta
int xDelta
Definition: Racetrack.h:21
operator!=
bool operator!=(const RacetrackState &l1, const RacetrackState &l2)
Definition: Racetrack.cpp:25
Racetrack::OpenGLDraw
void OpenGLDraw(const RacetrackState &, const RacetrackMove &) const
Definition: Racetrack.h:83
kTrees
@ kTrees
Definition: Map.h:59
Racetrack::Racetrack
Racetrack(Map *map)
Definition: Racetrack.cpp:40
Racetrack::Legal
bool Legal(const RacetrackState &node1, RacetrackMove &act) const
Definition: Racetrack.cpp:260
Racetrack::GetCarCoordinates
void GetCarCoordinates(const RacetrackState &s, Graphics::point &center, Graphics::point &p1, Graphics::point &p2, Graphics::point &p3) const
Definition: Racetrack.cpp:399
Racetrack::GetIndex
int GetIndex(int x, int y) const
Definition: Racetrack.h:96
width
int width
Definition: SFML_HOG.cpp:54
Map2DEnvironment.h
Racetrack::OpenGLDraw
void OpenGLDraw() const
Definition: Racetrack.h:81
RacetrackMove::xGoal
int xGoal
Definition: Racetrack.h:25
RacetrackState::xVelocity
int xVelocity
Definition: Racetrack.h:31
operator==
bool operator==(const RacetrackState &l1, const RacetrackState &l2)
Definition: Racetrack.cpp:17
Racetrack::DrawLine
void DrawLine(Graphics::Display &display, const RacetrackState &x, const RacetrackState &y, float width) const
Definition: Racetrack.cpp:463
kObstacle
const tTerrain kObstacle
Definition: Racetrack.h:45
RacetrackMove::hitGoal
bool hitGoal
Definition: Racetrack.h:24
Racetrack::map
Map * map
Definition: Racetrack.h:94
kSwamp
@ kSwamp
Definition: Map.h:56
MapEnvironment
Definition: Map2DEnvironment.h:133
Racetrack::GCost
double GCost(const RacetrackState &node, const RacetrackMove &act) const
Definition: Racetrack.h:74
Racetrack::GetStateHash
uint64_t GetStateHash(const RacetrackState &node) const
Definition: Racetrack.cpp:345
RacetrackMove::RacetrackMove
RacetrackMove(int x=0, int y=0)
Definition: Racetrack.h:21
Racetrack::GCost
double GCost(const RacetrackState &node1, const RacetrackState &node2) const
Definition: Racetrack.h:73
Graphics::Display
Definition: Graphics.h:146
RacetrackMove::yGoal
int yGoal
Definition: Racetrack.h:25
Map::GetMapWidth
long GetMapWidth() const
return the width of the map
Definition: Map.h:163
Racetrack::Boundaries
void Boundaries(RacetrackState &s, RacetrackMove &v) const
Definition: Racetrack.cpp:186
Racetrack::GoalTest
bool GoalTest(const RacetrackState &node, const RacetrackState &goal) const
Definition: Racetrack.cpp:247
Racetrack::heuristic
std::vector< int > heuristic
Definition: Racetrack.h:95
Racetrack::GetActionHash
uint64_t GetActionHash(RacetrackMove act) const
Definition: Racetrack.cpp:350
Racetrack::Reset
void Reset(RacetrackState &s) const
Definition: Racetrack.cpp:87
Racetrack::OpenGLDraw
void OpenGLDraw(const RacetrackState &) const
Definition: Racetrack.h:82
RacetrackState
Definition: Racetrack.h:29
Racetrack::HCost
double HCost(const RacetrackState &node1, const RacetrackState &node2) const
Heuristic value between two arbitrary nodes.
Definition: Racetrack.cpp:324
tTerrain
tTerrain
Definition: Map.h:51
Racetrack::Draw
void Draw(Graphics::Display &display) const
Definition: Racetrack.cpp:356
Racetrack::GetNumSuccessors
int GetNumSuccessors(const RacetrackState &stateID) const
Definition: Racetrack.h:54
Racetrack::GetSuccessors
void GetSuccessors(const RacetrackState &nodeID, std::vector< RacetrackState > &neighbors) const
Definition: Racetrack.cpp:108
RacetrackMove
Definition: Racetrack.h:20
Racetrack::InvertAction
bool InvertAction(RacetrackMove &a) const
Definition: Racetrack.cpp:222
Racetrack::me
MapEnvironment * me
Definition: Racetrack.h:93
Map.h
kStartTerrain
const tTerrain kStartTerrain
Definition: Racetrack.h:43
Racetrack::~Racetrack
~Racetrack()
Definition: Racetrack.cpp:47
kGrass
@ kGrass
Definition: Map.h:57
RacetrackState::xLoc
int xLoc
Definition: Racetrack.h:30
operator<<
std::ostream & operator<<(std::ostream &out, const RacetrackState &s)
Definition: Racetrack.cpp:11
Racetrack::ApplyAction
void ApplyAction(RacetrackState &s, RacetrackMove a) const
Definition: Racetrack.cpp:165
RacetrackMove::yDelta
int yDelta
Definition: Racetrack.h:23
RacetrackState::yVelocity
int yVelocity
Definition: Racetrack.h:31
SearchEnvironment
Definition: SearchEnvironment.h:30
kEndTerrain
const tTerrain kEndTerrain
Definition: Racetrack.h:44
Racetrack
Definition: Racetrack.h:47
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
SearchEnvironment.h
RacetrackState::yLoc
int yLoc
Definition: Racetrack.h:30
Racetrack::GetAction
RacetrackMove GetAction(const RacetrackState &s1, RacetrackState &s2) const
Definition: Racetrack.cpp:228