HOG2
Map3DGrid.h
Go to the documentation of this file.
1 /*
2  * Map3DGrid.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 5/6/11.
6  * Copyright 2011 University of Denver. All rights reserved.
7  *
8  */
9 
10 
11 #ifndef MAP3DGRID_H
12 #define MAP3DGRID_H
13 
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <iostream>
17 #include <cassert>
18 #include "SearchEnvironment.h"
19 #include "UnitSimulation.h"
20 #include "ReservationProvider.h"
21 #include "BitVector.h"
22 #include "GraphEnvironment.h"
23 
24 extern int gSectorSize;
25 
26 inline int indexFromXY(int x, int y)
27 { return y*gSectorSize+x; }
28 
29 inline void xyFromIndex(int index, int &x, int &y)
30 { x = index%gSectorSize; y = index/gSectorSize; }
31 
32 enum moveDir {
33  kWest = 0x1,
34  kEast = 0x2,
35  kNorth = 0x4,
36  kSouth = 0x8,
37  kNorthEast = 0x10,
38  kNorthWest = 0x20,
39  kSouthEast = 0x40,
40  kSouthWest = 0x80
41 };
42 
43 
44 // 8 bits - movable directions
45 // 5 bits - base height offset [0xF0]
46 // 3 bits - blockage
47 class GridData {
48 public:
50  {
51  points = new uint32_t[gSectorSize*gSectorSize];
52  for (int x = 0; x < gSectorSize*gSectorSize; x++)
53  points[x] = 0xFF;
54  }
55  GridData(const GridData& copy_from_me)
56  {
57  points = new uint32_t[gSectorSize*gSectorSize];
58  for (int x = 0; x < gSectorSize*gSectorSize; x++)
59  points[x] = copy_from_me.points[x];
60  }
61  ~GridData() {delete [] points; }
62  int AddMove(unsigned index, moveDir dir, bool local);
63  void AddMove(unsigned int x, unsigned int y, moveDir dir, bool local);
64  int ReMove(unsigned index, moveDir dir);
65  void ReMove(unsigned int x, unsigned int y, moveDir dir);
66  uint8_t GetMoves(int x, int y) const;
67  uint8_t GetMoves(int offset) const;
68  uint8_t GetMoveLocality(int x, int y) const;
69  uint8_t GetMoveLocality(int offset) const;
70  void ClearMoves(int x, int y);
71 
72  int GetHeightOffset(unsigned int x, unsigned int y) const;
73  int GetHeightOffset(int offset) const;
74  void SetHeightOffset(int x, int y, int height);
75  void SetHeightOffset(int offset, int height);
76  bool AddPoint(int x, int y, int height);
77  void RemovePoint(int x, int y);
78  bool IsPointPassable(unsigned int x, unsigned int y) const;
79  bool CanPass(unsigned int x, unsigned int y, int offsetx, int offsety) const;
80  int GetBlocked(int x, int y);
81  void AddBlocked(int x, int y);
82  void SubtractBlocked(int x, int y);
83  // neighbors: 8+2xladder+1x connection
84  int LabelAreas(std::vector<int> &labels, std::vector<int> &counts, std::vector<int> &heights);
85 private:
86  int BFS(std::vector<int> &labels, int offset, int label);
87  void AddIntraRegionEdges(int xRegLoc, int yRegLoc);
88  void RemoveIntraRegionEdges(int xRegLoc, int yRegLoc);
89  uint32_t *points;//[gSectorSize*gSectorSize];
90 };
91 
92 // at the top of regions we have:
93 // [8 bits] last edge
94 // [8 bits] center location
95 // [16 bits] grid location
96 // [8 bits] base height
97 // [8 bits] # points in region
98 // [8 bits] # number partially blocked points
99 
100 class EdgeData
101 {
102 public:
103 // void GetDestSector(int &sectorX, int &sectorY)
104 // {
105 // switch ((theEdge>>5)&0x3)
106 // {
107 // case 0: sectorX += 1; break;
108 // case 1: sectorX -= 1; break;
109 // case 2: sectorY += 1; break;
110 // case 3: sectorY -= 1; break;
111 // case 4: sectorX += 1; sectorY += 1; break;
112 // case 5: sectorX += 1; sectorY -= 1; break;
113 // case 6: sectorX -= 1; sectorY += 1; break;
114 // case 7: sectorX -= 1; sectorY -= 1; break;
115 // }
116 // }
117 // int GetDestRegion() { return theEdge&0x1F; }
118 //private:
120  :sector(-1), region(-1), support(-1) {}
121  EdgeData(int sec, int reg, int sup)
122  :sector(sec), region(reg), support(sup) {}
123  uint16_t sector;
124  uint8_t region;
125  uint8_t support; // underlying edges supporting this edge
126 };
127 
128 
129 class RegionData {
130 public:
132  :baseHeight(0), pointCount(0) {}
133 // int GetRegionGridIndex();
134 // int GetRegionEdgeCount();
135 // //int GetRegionEdge(int edge);
136 // //int GetRegionBaseHeight();
137 // void GetRegionCenter(int &x, int &y);
138 // int GetRegionPoints();
139 // int GetRegionBlocked();
140 
141  // points passed in local region coordinates
142  bool CanAddPoint(int x, int y, int z);
143  bool AddPoint(int x, int y, int z);
144  void AddEdge(EdgeData &e);
145  void RemoveEdge(EdgeData &e);
146  bool LowerBase();
147  void RedoCenterLocation();
148 //private:
149  uint8_t centerLocation;
150  uint8_t baseHeight;
151  uint8_t pointCount;
153  std::vector<EdgeData> edges;
155 };
156 
157 class SectorData {
158 public:
159  bool AddPoint(int x, int y, int z);
160  bool RemovePoint(int x, int y, int z);
161  // num regions implicit
162  std::vector<RegionData> regions;
163 };
164 
165 // either sector/region
166 // or x/y/z location
167 class state3d
168 {
169 public:
170  state3d() { sector = -1; region = -1; offset = -1; }
171  state3d(int s, int r, int o) :sector(s), region(r), offset(o) { }
172  void Init(int sector, int region, int offset)
173  { this->sector = sector; this->region = region; this->offset = offset; }
174  int GetSector() const { return sector; }
175  int GetRegion() const { return region; }
176  int GetOffset() const { return offset; }
177  void SetOffset(int off) { offset = off; }
178 //private:
179  uint16_t sector;
180  uint8_t region, offset;
181 };
182 
183 static bool operator==(const state3d &l1, const state3d &l2)
184 {
185  return ((l1.GetSector() == l2.GetSector()) &&
186  (l1.GetRegion() == l2.GetRegion()) &&
187  (l1.GetOffset() == l2.GetOffset()));
188 }
189 
190 // either direction (x/y offset?)
191 // or edge [direction + region]
192 class action3d
193 {
194 public:
195  uint8_t direction;
196  uint8_t destRegion;
197  uint8_t destSector;
198 };
199 
200 class Map3DGrid : public SearchEnvironment<state3d, action3d> {
201 public:
202  Map3DGrid(int width, int height, int theSectorSize);
203  Map3DGrid(Map *map, int theSectorSize);
204  int GetWidth() { return mWidth; }
205  int GetHeight() { return mHeight; }
206  void PrintStats();
208  int GetGridBytesUsed();
209 
210  void AddMap(Map *map, int elevation);
211  void GetSuccessors(const state3d &nodeID, std::vector<state3d> &neighbors) const;
212  void GetActions(const state3d &nodeID, std::vector<action3d> &actions) const;
213  void ApplyAction(state3d &s, action3d a) const;
214  bool InvertAction(action3d &a) const { return false; }
215  action3d GetAction(const state3d &s1, const state3d &s2) const { return action3d(); }
217  double HCost(const state3d &node1, const state3d &node2) const;
218  double GCost(const state3d &node1, const state3d &node2) const;
219  double GCost(const state3d &node, const action3d &act) const;
220  bool GoalTest(const state3d &node, const state3d &goal) const;
221 
222  uint64_t GetStateHash(const state3d &node) const;
223  uint64_t GetActionHash(action3d act) const;
224 
225  int FindNearState(int x, int y, int z, state3d &s) const;
226 
227  void OpenGLDraw() const;
228  void OpenGLDraw(const state3d&) const;
229  void OpenGLDraw(const state3d&, const action3d&) const { }
230  void GLDrawLine(const state3d &x, const state3d &y) const;
231 
232 
233  bool AddPoint(int x, int y, int z);
234  bool RemovePoint(int x, int y, int z, bool split = true);
235  void SetDrawGrid(bool draw) { drawGrid = draw; }
236  bool GetDrawGrid() { return drawGrid; }
237  void GetPointFromCoordinate(point3d loc, int &px, int &py, int &pz) const;
238 private:
239  void AddEdge(state3d &from, state3d &to);
240  int AddGridEdge(state3d &from, state3d &to, bool local);
241  int AddSectorEdge(state3d &from, state3d &to);
242  void AddEdge(int sec1, int reg1, int sec2, int reg2, int weight);
243  void AddMapPoints(Map *map, std::vector<bool> &visited, int x, int y, int elevation);
244 
245  int InternalRemovePoint(int x, int y, int z);
246  void RemoveEdge(int sec1, int reg1, int sec2, int reg2, int weight);
247  void RemoveEdge(state3d &from, state3d &to);
248  int RemoveGridEdge(state3d &from, state3d &to);
249  int RemoveSectorEdge(state3d &from, state3d &to);
250 
251 
252  //AddIntraRegionEdges(int xRegLoc, int yRegLoc);
253  int GetSector(int x, int y) const { return (y/gSectorSize)*mXSectors +(x/gSectorSize); }
254  void GetXYFromState(const state3d &s, int &x, int &y) const;
255  void GetXYZFromState(const state3d &s, int &x, int &y, int &z) const;
256  void GetStateFromXYZ(state3d &s, int x, int y, int z) const;
257  void GetOpenGLCoord(int xLoc, int yLoc, int zLoc,
258  GLdouble &x, GLdouble &y, GLdouble &z,
259  GLdouble &r, GLdouble &h) const;
262  std::vector<SectorData> sectors;
263  bool drawGrid;
264 };
265 
266 #endif
267 
Map3DGrid::sectors
std::vector< SectorData > sectors
Definition: Map3DGrid.h:262
Map3DGrid
Definition: Map3DGrid.h:200
Map3DGrid::mXSectors
int mXSectors
Definition: Map3DGrid.h:261
UnitSimulation.h
Map3DGrid::GetActionHash
uint64_t GetActionHash(action3d act) const
Definition: Map3DGrid.cpp:861
moveDir
moveDir
Definition: Map3DGrid.h:32
RegionData::RemoveEdge
void RemoveEdge(EdgeData &e)
Definition: Map3DGrid.cpp:391
GridData::GetHeightOffset
int GetHeightOffset(unsigned int x, unsigned int y) const
Definition: Map3DGrid.cpp:123
Map3DGrid::mHeight
int mHeight
Definition: Map3DGrid.h:260
state3d::GetRegion
int GetRegion() const
Definition: Map3DGrid.h:175
Map3DGrid::GetSector
int GetSector(int x, int y) const
Definition: Map3DGrid.h:253
GridData::SetHeightOffset
void SetHeightOffset(int x, int y, int height)
Definition: Map3DGrid.cpp:137
kWest
@ kWest
Definition: Map3DGrid.h:33
GridData::AddBlocked
void AddBlocked(int x, int y)
Definition: Map3DGrid.cpp:149
GridData::GridData
GridData(const GridData &copy_from_me)
Definition: Map3DGrid.h:55
RegionData::LowerBase
bool LowerBase()
Definition: Map3DGrid.cpp:277
kEast
@ kEast
Definition: Map3DGrid.h:34
GridData::GetMoves
uint8_t GetMoves(int x, int y) const
Definition: Map3DGrid.cpp:66
Map3DGrid::mWidth
int mWidth
Definition: Map3DGrid.h:260
kSouthEast
@ kSouthEast
Definition: Map3DGrid.h:39
Map3DGrid::GetWidth
int GetWidth()
Definition: Map3DGrid.h:204
EdgeData::sector
uint16_t sector
Definition: Map3DGrid.h:123
Map3DGrid::AddEdge
void AddEdge(state3d &from, state3d &to)
Definition: Map3DGrid.cpp:1029
RegionData::centerLocation
uint8_t centerLocation
Definition: Map3DGrid.h:149
RegionData::blockedPointCount
uint8_t blockedPointCount
Definition: Map3DGrid.h:152
EdgeData::region
uint8_t region
Definition: Map3DGrid.h:124
width
int width
Definition: SFML_HOG.cpp:54
indexFromXY
int indexFromXY(int x, int y)
Definition: Map3DGrid.h:26
GridData
Definition: Map3DGrid.h:47
operator==
static bool operator==(const state3d &l1, const state3d &l2)
Definition: Map3DGrid.h:183
state3d::sector
uint16_t sector
Definition: Map3DGrid.h:179
GridData::AddPoint
bool AddPoint(int x, int y, int height)
Definition: Map3DGrid.cpp:92
Map3DGrid::GetPointFromCoordinate
void GetPointFromCoordinate(point3d loc, int &px, int &py, int &pz) const
Definition: Map3DGrid.cpp:1429
RegionData::baseHeight
uint8_t baseHeight
Definition: Map3DGrid.h:150
GridData::RemovePoint
void RemovePoint(int x, int y)
Definition: Map3DGrid.cpp:98
GridData::ClearMoves
void ClearMoves(int x, int y)
Definition: Map3DGrid.cpp:87
GridData::~GridData
~GridData()
Definition: Map3DGrid.h:61
Map3DGrid::OpenGLDraw
void OpenGLDraw() const
Definition: Map3DGrid.cpp:1442
Map3DGrid::AddPoint
bool AddPoint(int x, int y, int z)
Definition: Map3DGrid.cpp:867
Map3DGrid::Map3DGrid
Map3DGrid(int width, int height, int theSectorSize)
Definition: Map3DGrid.cpp:459
Map3DGrid::AddMapPoints
void AddMapPoints(Map *map, std::vector< bool > &visited, int x, int y, int elevation)
Definition: Map3DGrid.cpp:483
Map3DGrid::SetDrawGrid
void SetDrawGrid(bool draw)
Definition: Map3DGrid.h:235
kNorth
@ kNorth
Definition: Map3DGrid.h:35
RegionData::edges
std::vector< EdgeData > edges
Definition: Map3DGrid.h:153
SectorData::RemovePoint
bool RemovePoint(int x, int y, int z)
Definition: Map3DGrid.cpp:431
GridData::GetBlocked
int GetBlocked(int x, int y)
Definition: Map3DGrid.cpp:144
gSectorSize
int gSectorSize
Definition: Map3DGrid.cpp:12
Map3DGrid::drawGrid
bool drawGrid
Definition: Map3DGrid.h:263
RegionData
Definition: Map3DGrid.h:129
Map3DGrid::GetGridBytesUsed
int GetGridBytesUsed()
Definition: Map3DGrid.cpp:1638
state3d::SetOffset
void SetOffset(int off)
Definition: Map3DGrid.h:177
loc
Definition: MapGenerators.cpp:296
action3d::destSector
uint8_t destSector
Definition: Map3DGrid.h:197
SectorData::regions
std::vector< RegionData > regions
Definition: Map3DGrid.h:162
state3d::region
uint8_t region
Definition: Map3DGrid.h:180
Map3DGrid::RemovePoint
bool RemovePoint(int x, int y, int z, bool split=true)
Definition: Map3DGrid.cpp:1141
Map3DGrid::RemoveEdge
void RemoveEdge(int sec1, int reg1, int sec2, int reg2, int weight)
Definition: Map3DGrid.cpp:1271
kNorthEast
@ kNorthEast
Definition: Map3DGrid.h:37
point3d
#define point3d
Definition: GLUtil.h:123
action3d::direction
uint8_t direction
Definition: Map3DGrid.h:195
Map3DGrid::GoalTest
bool GoalTest(const state3d &node, const state3d &goal) const
Definition: Map3DGrid.cpp:849
GridData::ReMove
int ReMove(unsigned index, moveDir dir)
Definition: Map3DGrid.cpp:48
state3d::state3d
state3d(int s, int r, int o)
Definition: Map3DGrid.h:171
GridData::CanPass
bool CanPass(unsigned int x, unsigned int y, int offsetx, int offsety) const
Definition: Map3DGrid.cpp:111
Map3DGrid::GetActions
void GetActions(const state3d &nodeID, std::vector< action3d > &actions) const
Definition: Map3DGrid.cpp:785
Map3DGrid::GetXYZFromState
void GetXYZFromState(const state3d &s, int &x, int &y, int &z) const
Definition: Map3DGrid.cpp:1411
Map3DGrid::FindNearState
int FindNearState(int x, int y, int z, state3d &s) const
Definition: Map3DGrid.cpp:1383
RegionData::AddEdge
void AddEdge(EdgeData &e)
Definition: Map3DGrid.cpp:378
Map3DGrid::GLDrawLine
void GLDrawLine(const state3d &x, const state3d &y) const
Definition: Map3DGrid.cpp:1565
GridData::AddMove
int AddMove(unsigned index, moveDir dir, bool local)
Definition: Map3DGrid.cpp:23
kSouthWest
@ kSouthWest
Definition: Map3DGrid.h:40
EdgeData
Definition: Map3DGrid.h:100
height
int height
Definition: SFML_HOG.cpp:54
GridData::SubtractBlocked
void SubtractBlocked(int x, int y)
Definition: Map3DGrid.cpp:156
GridData::points
uint32_t * points
Definition: Map3DGrid.h:89
Map3DGrid::ApplyAction
void ApplyAction(state3d &s, action3d a) const
Definition: Map3DGrid.cpp:806
Map3DGrid::AddGridEdge
int AddGridEdge(state3d &from, state3d &to, bool local)
Definition: Map3DGrid.cpp:1059
SectorData
Definition: Map3DGrid.h:157
kNorthWest
@ kNorthWest
Definition: Map3DGrid.h:38
Map3DGrid::GetAbstractionBytesUsed
int GetAbstractionBytesUsed()
Definition: Map3DGrid.cpp:1625
Map3DGrid::HCost
double HCost(const state3d &node1, const state3d &node2) const
Heuristic value between two arbitrary nodes.
Definition: Map3DGrid.cpp:831
Map3DGrid::GetStateHash
uint64_t GetStateHash(const state3d &node) const
Definition: Map3DGrid.cpp:855
Map3DGrid::RemoveGridEdge
int RemoveGridEdge(state3d &from, state3d &to)
Definition: Map3DGrid.cpp:1297
xyFromIndex
void xyFromIndex(int index, int &x, int &y)
Definition: Map3DGrid.h:29
GridData::BFS
int BFS(std::vector< int > &labels, int offset, int label)
Definition: Map3DGrid.cpp:184
action3d
Definition: Map3DGrid.h:192
GridData::GetMoveLocality
uint8_t GetMoveLocality(int x, int y) const
Definition: Map3DGrid.cpp:76
state3d
Definition: Map3DGrid.h:167
Map3DGrid::GetDrawGrid
bool GetDrawGrid()
Definition: Map3DGrid.h:236
Map3DGrid::RemoveSectorEdge
int RemoveSectorEdge(state3d &from, state3d &to)
Definition: Map3DGrid.cpp:1330
ReservationProvider.h
GridData::RemoveIntraRegionEdges
void RemoveIntraRegionEdges(int xRegLoc, int yRegLoc)
RegionData::pointCount
uint8_t pointCount
Definition: Map3DGrid.h:151
Map3DGrid::GetXYFromState
void GetXYFromState(const state3d &s, int &x, int &y) const
Definition: Map3DGrid.cpp:1403
Map3DGrid::AddSectorEdge
int AddSectorEdge(state3d &from, state3d &to)
Definition: Map3DGrid.cpp:1090
RegionData::grid
GridData grid
Definition: Map3DGrid.h:154
RegionData::RegionData
RegionData()
Definition: Map3DGrid.h:131
state3d::Init
void Init(int sector, int region, int offset)
Definition: Map3DGrid.h:172
Map3DGrid::AddMap
void AddMap(Map *map, int elevation)
Definition: Map3DGrid.cpp:470
Map3DGrid::GetStateFromXYZ
void GetStateFromXYZ(state3d &s, int x, int y, int z) const
Definition: Map3DGrid.cpp:1418
RegionData::AddPoint
bool AddPoint(int x, int y, int z)
Definition: Map3DGrid.cpp:332
SectorData::AddPoint
bool AddPoint(int x, int y, int z)
Definition: Map3DGrid.cpp:409
BitVector.h
EdgeData::EdgeData
EdgeData(int sec, int reg, int sup)
Definition: Map3DGrid.h:121
action3d::destRegion
uint8_t destRegion
Definition: Map3DGrid.h:196
split
std::vector< std::string > split(const std::string &s, char delim)
Splits a string into elements.
Definition: StringUtils.cpp:16
Map3DGrid::GetHeight
int GetHeight()
Definition: Map3DGrid.h:205
Map3DGrid::InternalRemovePoint
int InternalRemovePoint(int x, int y, int z)
Definition: Map3DGrid.cpp:1198
GridData::LabelAreas
int LabelAreas(std::vector< int > &labels, std::vector< int > &counts, std::vector< int > &heights)
Definition: Map3DGrid.cpp:163
GridData::GridData
GridData()
Definition: Map3DGrid.h:49
Map3DGrid::GetOpenGLCoord
void GetOpenGLCoord(int xLoc, int yLoc, int zLoc, GLdouble &x, GLdouble &y, GLdouble &z, GLdouble &r, GLdouble &h) const
Definition: Map3DGrid.cpp:1591
Map3DGrid::OpenGLDraw
void OpenGLDraw(const state3d &, const action3d &) const
Definition: Map3DGrid.h:229
state3d::state3d
state3d()
Definition: Map3DGrid.h:170
EdgeData::support
uint8_t support
Definition: Map3DGrid.h:125
RegionData::CanAddPoint
bool CanAddPoint(int x, int y, int z)
Definition: Map3DGrid.cpp:301
Map3DGrid::GetAction
action3d GetAction(const state3d &s1, const state3d &s2) const
Definition: Map3DGrid.h:215
Map3DGrid::PrintStats
void PrintStats()
Definition: Map3DGrid.cpp:1606
kSouth
@ kSouth
Definition: Map3DGrid.h:36
RegionData::RedoCenterLocation
void RedoCenterLocation()
Definition: Map3DGrid.cpp:360
Map3DGrid::GCost
double GCost(const state3d &node1, const state3d &node2) const
Definition: Map3DGrid.cpp:837
state3d::GetSector
int GetSector() const
Definition: Map3DGrid.h:174
Map3DGrid::InvertAction
bool InvertAction(action3d &a) const
Definition: Map3DGrid.h:214
state3d::offset
uint8_t offset
Definition: Map3DGrid.h:180
SearchEnvironment
Definition: SearchEnvironment.h:30
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
state3d::GetOffset
int GetOffset() const
Definition: Map3DGrid.h:176
GraphEnvironment.h
EdgeData::EdgeData
EdgeData()
Definition: Map3DGrid.h:119
Map3DGrid::GetSuccessors
void GetSuccessors(const state3d &nodeID, std::vector< state3d > &neighbors) const
Definition: Map3DGrid.cpp:531
GridData::IsPointPassable
bool IsPointPassable(unsigned int x, unsigned int y) const
Definition: Map3DGrid.cpp:104
GridData::AddIntraRegionEdges
void AddIntraRegionEdges(int xRegLoc, int yRegLoc)
Map3DGrid::mYSectors
int mYSectors
Definition: Map3DGrid.h:261