HOG2
SharedAMapGroup.cpp
Go to the documentation of this file.
1 /*
2  * $Id: SharedAMapGroup.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 12/16/04.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #include "SharedAMapGroup.h"
13 #include "Unit.h"
14 #include "UnitGroup.h"
15 #include "MapCliqueAbstraction.h"
16 #include "MapFlatAbstraction.h"
17 
18 using namespace GraphAbstractionConstants;
19 
21 :aMap(0), seen(0)
22 {
23  visRadius = 2;
24  sawNewLand = true;
25  newTileCount = 0;
27 
28  Map *m = mp->GetMap();
29  map = new Map(m->GetMapWidth(), m->GetMapHeight());
31  //aMap = new MapFlatAbstraction(map);
32  seen = new BitVector(m->GetMapWidth() * m->GetMapHeight());
33 
34 }
35 
37 {
38  delete aMap;
39  delete seen;
40 }
41 
42 //void SharedAMapGroup::updateLocation(BaseAbsMapUnit *u, MapProvider *mp, int _x, int _y, bool success, AbsMapSimulationInfo *simInfo)
44 {
45  Map *worldMap = mp->GetMap();
46  int rad = visRadius; // how far we can see -- square
47  int x, y, num;
48  u->UpdateLocation(mp, loc, success, si);
49  xyLoc l;
50  u->GetLocation(l);
51  x = l.x; y = l.y;
52  bool changed = false;
53  for (int x1 = x-rad; x1 <= x+rad; x1++)
54  {
55  if ((x1 < 0) || (x1 >= map->GetMapWidth()))
56  continue;
57  for (int y1 = y-rad; y1 <= y+rad; y1++)
58  {
59  if ((y1 < 0) || (y1 >= map->GetMapHeight()))
60  continue;
61 
62  // if we haven't observed a particular tile, we need to look at it
63  if (!seen->Get(y1*map->GetMapWidth()+x1))
64  {
65  // count a new tile!
66  newTileCount++;
67  // if it's different that we think, we just remove it.
68  // in the future we'll need to handle water & splits here
69  num = map->GetNodeNum(x1, y1);
70  Graph *g = aMap->GetAbstractGraph(0);
71  if (!worldMap->AdjacentEdges(x1, y1, kLeftEdge))
72  { edge *e = g->FindEdge(num, map->GetNodeNum(x1-1, y1)); aMap->RemoveEdge(e, 0); }
73  if (!worldMap->AdjacentEdges(x1, y1, kRightEdge))
74  { edge *e = g->FindEdge(num, map->GetNodeNum(x1+1, y1)); aMap->RemoveEdge(e, 0); }
75  if (!worldMap->AdjacentEdges(x1, y1, kTopEdge))
76  { edge *e = g->FindEdge(num, map->GetNodeNum(x1, y1-1)); aMap->RemoveEdge(e, 0); }
77  if (!worldMap->AdjacentEdges(x1, y1, kBottomEdge))
78  { edge *e = g->FindEdge(num, map->GetNodeNum(x1, y1+1)); aMap->RemoveEdge(e, 0); }
79 
80  if (!worldMap->AdjacentCorners(x1, y1, kTopLeft))
81  { edge *e = g->FindEdge(num, map->GetNodeNum(x1-1, y1-1)); aMap->RemoveEdge(e, 0); }
82  if (!worldMap->AdjacentCorners(x1, y1, kTopRight))
83  { edge *e = g->FindEdge(num, map->GetNodeNum(x1+1, y1-1)); aMap->RemoveEdge(e, 0); }
84  if (!worldMap->AdjacentCorners(x1, y1, kBottomLeft))
85  { edge *e = g->FindEdge(num, map->GetNodeNum(x1-1, y1+1)); aMap->RemoveEdge(e, 0); }
86  if (!worldMap->AdjacentCorners(x1, y1, kBottomRight))
87  { edge *e = g->FindEdge(num, map->GetNodeNum(x1+1, y1+1)); aMap->RemoveEdge(e, 0); }
88 
89  //aMap->RemoveNode(me);
90  map->SetTerrainType(x1, y1, (tTerrain)worldMap->GetTerrainType(x1, y1));
91  changed = true;
92 
93  sawNewLand = true;
94  seen->Set(y1*map->GetMapWidth()+x1, true);
95  }
96  }
97  }
98  if (changed)
99  aMap->RepairAbstraction();
100 }
101 
102 void SharedAMapGroup::OpenGLDraw(const AbsMapEnvironment *, const AbsMapSimulationInfo *) const
103 {
104  glBegin(GL_QUADS);
105  glColor3f(.25, .25, .25); // kOutOfBounds
106  glNormal3f(0, 0, 1);
107  GLdouble coverage = 0.9;
108  for (int x = 0; x < map->GetMapWidth(); x++)
109  {
110  for (int y = 0; y < map->GetMapHeight(); y++)
111  {
112  GLdouble a, b, c, radius;
113  if (!seen->Get(y*map->GetMapWidth()+x))
114  {
115  map->GetOpenGLCoord(x, y, a, b, c, radius);
116  glVertex3f(a+coverage*radius, b+coverage*radius, c-4*radius);
117  glVertex3f(a+coverage*radius, b-coverage*radius, c-4*radius);
118  glVertex3f(a-coverage*radius, b-coverage*radius, c-4*radius);
119  glVertex3f(a-coverage*radius, b+coverage*radius, c-4*radius);
120  }
121  }
122  }
123  glEnd();
124 }
125 
127 {
128  return aMap;
129 }
130 
132 {
133  return map;
134 }
135 
138 {
139  return (!sawNewLand);
140 }
141 
143 //void SharedAMapGroup::printRoundStats(unit *, FILE *f)
144 {
145  if (newTileCount != 0)
146  {
147  stats->SumStat("newTilesExplored", GetName(), (long)newTileCount);
149  newTileCount = 0;
150  }
151  //fprintf(f,"%8.2f", (float)newTileCount);
152 }
153 
156 {
157  sawNewLand = false;
158  newTileCount = 0;
160  stats->AddStat("newTilesExplored", GetName(), (long)newTileCount);
161 }
162 
164 {
165  visRadius = _visibility;
166 }
167 
169 {
170  return visRadius;
171 }
172 
173 bool SharedAMapGroup::Explored(int x, int y)
174 {
175  return seen->Get(y*map->GetMapWidth()+x);
176 }
177 
178 bool SharedAMapGroup::Explored(unsigned int _node)
179 {
180  node *loc = aMap->GetAbstractGraph(0)->GetNode(_node);
181  return Explored((unsigned int)loc->GetLabelL(kFirstData),
182  (unsigned int)loc->GetLabelL(kFirstData+1));
183 }
SharedAMapGroup::GetMap
virtual Map * GetMap() const
Definition: SharedAMapGroup.cpp:131
GraphAbstractionConstants
Definition: GraphAbstraction.h:22
MapProvider::GetMap
virtual Map * GetMap() const =0
Map::GetNodeNum
int GetNodeNum(int x, int y, tCorner c=kNone)
Gets the abstract Graph node number for this tile.
Definition: Map.cpp:2339
kRightEdge
@ kRightEdge
Definition: Map.h:83
SimulationInfo
Definition: SimulationInfo.h:13
Graph::FindEdge
edge * FindEdge(unsigned int from, unsigned int to)
Finds an edge between nodes with ids from and to, no matter which direction.
Definition: Graph.cpp:230
Unit::UpdateLocation
virtual void UpdateLocation(environment *, state &, bool success, SimulationInfo< state, action, environment > *)=0
MapProvider
Definition: MapProvider.h:23
SharedAMapGroup.h
SharedAMapGroup::newTileCount
int newTileCount
Definition: SharedAMapGroup.h:63
SharedAMapGroup::GetMapAbstraction
virtual MapAbstraction * GetMapAbstraction()
Definition: SharedAMapGroup.cpp:126
SharedAMapGroup::map
Map * map
Definition: SharedAMapGroup.h:59
xyLoc::y
uint16_t y
Definition: Map2DEnvironment.h:42
kTopRight
@ kTopRight
Definition: Map.h:99
SharedAMapGroup::UpdateLocation
void UpdateLocation(Unit< xyLoc, tDirection, AbsMapEnvironment > *u, AbsMapEnvironment *, xyLoc &loc, bool success, AbsMapSimulationInfo *)
reset the location of a given unit
Definition: SharedAMapGroup.cpp:43
xyLoc
Definition: Map2DEnvironment.h:37
kTopLeft
@ kTopLeft
Definition: Map.h:98
Map::AdjacentCorners
bool AdjacentCorners(long x, long y, tCorner corner) const
Definition: Map.cpp:1661
Graph
A generic Graph class.
Definition: Graph.h:66
xyLoc::x
uint16_t x
Definition: Map2DEnvironment.h:41
SharedAMapGroup::~SharedAMapGroup
~SharedAMapGroup()
Definition: SharedAMapGroup.cpp:36
kBottomLeft
@ kBottomLeft
Definition: Map.h:100
loc
Definition: MapGenerators.cpp:296
SharedAMapGroup::SetVisibilityRadius
void SetVisibilityRadius(int _visibility)
Definition: SharedAMapGroup.cpp:163
StatCollection::SumStat
void SumStat(const char *category, const char *owner, double value)
Given stats for the category and owner, find an existing stat (chronologically backwards search) with...
Definition: StatCollection.cpp:109
BitVector
Definition: BitVector.h:34
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
BitVector::Set
void Set(uint64_t index, bool value)
Definition: BitVector.cpp:88
Unit::GetLocation
virtual void GetLocation(state &)=0
SharedAMapGroup::GetName
virtual const char * GetName()
Definition: SharedAMapGroup.h:30
SharedAMapGroup::newTileCountPerTrial
int newTileCountPerTrial
Definition: SharedAMapGroup.h:64
SharedAMapGroup::aMap
MapAbstraction * aMap
Definition: SharedAMapGroup.h:58
Map::GetMapWidth
long GetMapWidth() const
return the width of the map
Definition: Map.h:163
SharedAMapGroup::StartNewTrial
void StartNewTrial(StatCollection *stats)
Lets the unit group do what it needs to reset a trial.
Definition: SharedAMapGroup.cpp:155
Map::GetTerrainType
long GetTerrainType(long x, long y, tSplitSide split=kWholeTile) const
Get the terrain type of the (split) tile at x, y.
Definition: Map.cpp:1028
MapCliqueAbstraction
A map abstraction based on the reduction of cliques.
Definition: MapCliqueAbstraction.h:21
SharedAMapGroup::OpenGLDraw
virtual void OpenGLDraw(const AbsMapEnvironment *, const AbsMapSimulationInfo *) const
Definition: SharedAMapGroup.cpp:102
MapCliqueAbstraction.h
SharedAMapGroup::GetVisibilityRadius
int GetVisibilityRadius()
Definition: SharedAMapGroup.cpp:168
SharedAMapGroup::seen
BitVector * seen
Definition: SharedAMapGroup.h:60
tTerrain
tTerrain
Definition: Map.h:51
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
SharedAMapGroup::Explored
bool Explored(int x, int y)
Definition: SharedAMapGroup.cpp:173
SharedAMapGroup::SharedAMapGroup
SharedAMapGroup(MapProvider *)
Definition: SharedAMapGroup.cpp:20
SharedAMapGroup::Done
virtual bool Done()
Is the group done with their exploration?
Definition: SharedAMapGroup.cpp:137
SharedAMapGroup::LogStats
void LogStats(StatCollection *stats)
Definition: SharedAMapGroup.cpp:142
Map::GetMapHeight
long GetMapHeight() const
return the height of the map
Definition: Map.h:165
UnitGroup.h
kBottomRight
@ kBottomRight
Definition: Map.h:101
StatCollection::AddStat
void AddStat(const char *category, const char *owner, double value)
Add a new stat entry for the given category, owner and value.
Definition: StatCollection.cpp:67
kTopEdge
@ kTopEdge
Definition: Map.h:84
Map::AdjacentEdges
bool AdjacentEdges(long x, long y, tEdge edge) const
Is the tile at x, y adjacent across the edge?
Definition: Map.cpp:1602
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
SharedAMapGroup::visRadius
int visRadius
Definition: SharedAMapGroup.h:61
kLeftEdge
@ kLeftEdge
Definition: Map.h:82
Map::SetTerrainType
void SetTerrainType(int32_t x1, int32_t y1, int32_t x2, int32_t y2, tTerrain t)
Map::SetTerrainType()
Definition: Map.cpp:1049
kBottomEdge
@ kBottomEdge
Definition: Map.h:85
SharedAMapGroup::sawNewLand
bool sawNewLand
Definition: SharedAMapGroup.h:62
BitVector::Get
bool Get(uint64_t index) const
Definition: BitVector.h:54
MapFlatAbstraction.h
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
Unit.h
Unit
Definition: Unit.h:23
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129