HOG2
Map2DSectorAbstractionEnvironment.cpp
Go to the documentation of this file.
1 //
2 // Map2DSectorAbstractionEnvironment.cpp
3 // hog2 mac native demos
4 //
5 // Created by Nathan Sturtevant on 5/23/18.
6 // Copyright © 2018 NS Software. All rights reserved.
7 //
8 
10 
12 {
13  return s1.sector == s2.sector && s1.region == s2.region;
14 }
15 
17 {
18  map = m;
19  me = new MapEnvironment(map);
20  msa = new MinimalSectorAbstraction(m, sectorSize);
21 }
22 
24 {
25  delete me;
26  delete msa;
27 }
28 
29 void Map2DSectorAbstraction::GetSuccessors(const abstractGridState &nodeID, std::vector<abstractGridState> &neighbors) const
30 {
31  neighbors.clear();
32  msa->GetNeighbors(nodeID.sector, nodeID.region, actions);
34  for (tempEdgeData &a : actions)
35  {
36  g.sector = msa->GetAdjacentSector(nodeID.sector, a.direction);
37  g.region = a.to;
38  neighbors.push_back(g);
39  }
40 }
41 
42 void Map2DSectorAbstraction::GetActions(const abstractGridState &nodeID, std::vector<abstractMove> &acts) const
43 {
44  acts.clear();
45  msa->GetNeighbors(nodeID.sector, nodeID.region, actions);
46  for (tempEdgeData &a : actions)
47  {
48  acts.push_back({a.direction, a.to});
49  }
50 }
51 
52 
54 {
56  s.region = a.region;
57 }
58 
60 {
61  unsigned int x, y;
62  msa->GetXYLocation(loc.sector, loc.region, x, y);
63  return xyLoc(x, y);
64 }
65 
67 {
69  g.sector = msa->GetSector(loc.x, loc.y);
70  g.region = msa->GetRegion(loc.x, loc.y);
71  return g;
72 }
73 
75 double Map2DSectorAbstraction::HCost(const abstractGridState &node1, const abstractGridState &node2) const
76 {
77  xyLoc l1, l2;
78 
79  l1 = GetState(node1);
80  l2 = GetState(node2);
81 
82  int dx = std::abs(l1.x-l2.x);
83  int dy = std::abs(l1.y-l2.y);
84  return std::max(dx, dy)+std::min(dx, dy)*(ROOT_TWO-1.0);
85 }
86 
87 
88 double Map2DSectorAbstraction::GCost(const abstractGridState &node1, const abstractGridState &node2) const
89 {
90  return HCost(node1, node2);
91 }
92 
94 {
95  assert(!"Not implemented");
96 }
97 
99 {
100  return (node.sector == goal.sector && node.region == goal.region);
101 }
102 
103 
105 {
106  return (node.sector<<16)|node.region;
107 }
108 
109 
111 {
112  return (act.direction<<16)|act.region;
113 }
114 
115 
117 {
118  msa->Draw(display);
119 }
120 
122 {
123  xyLoc l = GetState(g);
124 // me->SetColor(GetColor());
125 // me->Draw(display, l);
126 // rgbColor c = GetColor();
127 
128 
129  GLdouble px, py, t, rad;
130  map->GetOpenGLCoord(l.x, l.y, px, py, t, rad);
131 
132  //if (map->GetTerrainType(l.x, l.y) == kGround)
133  {
134  rad*=3;
135 // Graphics::rect r;
136 // r.left = px-rad;
137 // r.top = py-rad;
138 // r.right = px+rad;
139 // r.bottom = py+rad;
140 
141  //s += SVGDrawCircle(l.x+0.5+1, l.y+0.5+1, 0.5, c);
142 // display.FillCircle(r, GetColor());
143  display.FillCircle({static_cast<float>(px), static_cast<float>(py)}, rad, GetColor());
144  //stroke-width="1" stroke="pink" />
145  }
146 
147  xyLoc curr = GetState(g);
148  xyLoc next;
149  GetSuccessors(g, nbr);
151  for (auto &n : nbr)
152  {
153  next = GetState(n);
154  me->DrawLine(display, curr, next, 3);
155  }
156 }
157 
159 {
160  xyLoc l1 = GetState(x);
161  xyLoc l2 = GetState(y);
162  me->SetColor(GetColor());
163  me->DrawLine(display, l1, l2, width);
164 }
165 
166 
loc::x
int x
Definition: MapGenerators.cpp:296
Map2DSectorAbstraction::me
MapEnvironment * me
Definition: Map2DSectorAbstractionEnvironment.h:69
MinimalSectorAbstraction::GetSector
int GetSector(int x, int y)
MinimalSectorAbstraction::GetSector()
Definition: MinimalSectorAbstraction.cpp:765
Map2DSectorAbstraction::actions
std::vector< tempEdgeData > actions
Definition: Map2DSectorAbstractionEnvironment.h:71
Map2DSectorAbstraction::GCost
virtual double GCost(const abstractGridState &node1, const abstractGridState &node2) const
Definition: Map2DSectorAbstractionEnvironment.cpp:88
tempEdgeData
Definition: MinimalSectorAbstraction.h:72
min
double min(double a, double b)
Definition: FPUtil.h:35
Map2DSectorAbstraction::GetAbstractState
abstractGridState GetAbstractState(const xyLoc &loc) const
Definition: Map2DSectorAbstractionEnvironment.cpp:66
loc::y
int y
Definition: MapGenerators.cpp:296
abstractMove::direction
int direction
Definition: Map2DSectorAbstractionEnvironment.h:23
xyLoc::y
uint16_t y
Definition: Map2DEnvironment.h:42
Map2DSectorAbstraction::DrawLine
virtual void DrawLine(Graphics::Display &display, const abstractGridState &x, const abstractGridState &y, float width=1.0) const
Definition: Map2DSectorAbstractionEnvironment.cpp:158
Map2DSectorAbstraction::msa
MinimalSectorAbstraction * msa
Definition: Map2DSectorAbstractionEnvironment.h:70
Map2DSectorAbstraction::GetActionHash
virtual uint64_t GetActionHash(abstractMove act) const
Definition: Map2DSectorAbstractionEnvironment.cpp:110
Map2DSectorAbstraction::GoalTest
virtual bool GoalTest(const abstractGridState &node, const abstractGridState &goal) const
Definition: Map2DSectorAbstractionEnvironment.cpp:98
xyLoc
Definition: Map2DEnvironment.h:37
width
int width
Definition: SFML_HOG.cpp:54
MinimalSectorAbstraction
MinimalSectorAbstraction.
Definition: MinimalSectorAbstraction.h:92
abstractGridState::sector
int sector
Definition: Map2DSectorAbstractionEnvironment.h:18
Map2DSectorAbstraction::Draw
virtual void Draw(Graphics::Display &display) const
Definition: Map2DSectorAbstractionEnvironment.cpp:116
xyLoc::x
uint16_t x
Definition: Map2DEnvironment.h:41
SearchEnvironment::SetColor
virtual void SetColor(const rgbColor &r) const
Definition: SearchEnvironment.h:102
MapEnvironment
Definition: Map2DEnvironment.h:133
loc
Definition: MapGenerators.cpp:296
Graphics::Display
Definition: Graphics.h:146
Map2DSectorAbstraction::Map2DSectorAbstraction
Map2DSectorAbstraction(Map *m, int sectorSize)
Definition: Map2DSectorAbstractionEnvironment.cpp:16
abstractMove
Definition: Map2DSectorAbstractionEnvironment.h:22
ROOT_TWO
static const double ROOT_TWO
Definition: GLUtil.h:61
MinimalSectorAbstraction::GetXYLocation
void GetXYLocation(unsigned int sector, unsigned int region, unsigned int &x, unsigned int &y) const
MinimalSectorAbstraction::GetXYLocation()
Definition: MinimalSectorAbstraction.cpp:667
Map2DSectorAbstraction::~Map2DSectorAbstraction
~Map2DSectorAbstraction()
Definition: Map2DSectorAbstractionEnvironment.cpp:23
abstractGridState
Definition: Map2DSectorAbstractionEnvironment.h:16
SearchEnvironment< abstractGridState, abstractMove >::GetColor
virtual rgbColor GetColor() const
Definition: SearchEnvironment.h:105
MinimalSectorAbstraction::GetRegion
int GetRegion(int x, int y)
MinimalSectorAbstraction::GetRegion()
Definition: MinimalSectorAbstraction.cpp:789
Map2DSectorAbstraction::GetStateHash
virtual uint64_t GetStateHash(const abstractGridState &node) const
Definition: Map2DSectorAbstractionEnvironment.cpp:104
operator==
bool operator==(const abstractGridState &s1, const abstractGridState &s2)
Definition: Map2DSectorAbstractionEnvironment.cpp:11
max
#define max(a, b)
Definition: MinimalSectorAbstraction.cpp:40
abstractMove::region
int region
Definition: Map2DSectorAbstractionEnvironment.h:24
Map2DSectorAbstraction::ApplyAction
virtual void ApplyAction(abstractGridState &s, abstractMove a) const
Definition: Map2DSectorAbstractionEnvironment.cpp:53
Map2DSectorAbstraction::GetState
xyLoc GetState(const abstractGridState &loc) const
Definition: Map2DSectorAbstractionEnvironment.cpp:59
abstractGridState::region
int region
Definition: Map2DSectorAbstractionEnvironment.h:19
Colors::red
const rgbColor red
Definition: Colors.h:128
Graphics::Display::FillCircle
void FillCircle(rect r, rgbColor c)
Definition: Graphics.cpp:128
MapEnvironment::DrawLine
void DrawLine(Graphics::Display &disp, const xyLoc &x, const xyLoc &y, double width=1.0) const
Definition: Map2DEnvironment.cpp:1430
MinimalSectorAbstraction::Draw
void Draw(Graphics::Display &display) const
Definition: MinimalSectorAbstraction.cpp:556
MinimalSectorAbstraction::GetAdjacentSector
int GetAdjacentSector(unsigned int sector, int direction)
MinimalSectorAbstraction::GetAdjacentSector()
Definition: MinimalSectorAbstraction.cpp:735
Map::GetOpenGLCoord
bool GetOpenGLCoord(int _x, int _y, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &radius) const
Get the openGL coordinates of a given tile.
Definition: Map.cpp:1826
Map2DSectorAbstraction::map
Map * map
Definition: Map2DSectorAbstractionEnvironment.h:68
Map2DSectorAbstraction::nbr
std::vector< abstractGridState > nbr
Definition: Map2DSectorAbstractionEnvironment.h:72
Map2DSectorAbstraction::GetActions
void GetActions(const abstractGridState &nodeID, std::vector< abstractMove > &actions) const
Definition: Map2DSectorAbstractionEnvironment.cpp:42
Map2DSectorAbstractionEnvironment.h
Map2DSectorAbstraction::HCost
virtual double HCost(const abstractGridState &node1, const abstractGridState &node2) const
Heuristic value between two arbitrary nodes.
Definition: Map2DSectorAbstractionEnvironment.cpp:75
Map2DSectorAbstraction::GetSuccessors
void GetSuccessors(const abstractGridState &nodeID, std::vector< abstractGridState > &neighbors) const
Definition: Map2DSectorAbstractionEnvironment.cpp:29
MinimalSectorAbstraction::GetNeighbors
void GetNeighbors(unsigned int sector, unsigned int region, std::vector< tempEdgeData > &edges) const
MinimalSectorAbstraction::GetNeighbors()
Definition: MinimalSectorAbstraction.cpp:691
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142