HOG2
PatrolUnit.cpp
Go to the documentation of this file.
1 /*
2  * $Id: patrolUnit.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 3/23/06.
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 "PatrolUnit.h"
13 
14 
15 patrolUnit::patrolUnit(int _x, int _y)
16 :unit(_x, _y)
17 {
18  setObjectType(kWorldObject);
19  currTarget = -1;
21 }
22 
24 // The last location is the unit's starting position to create a loop */
25 //patrolUnit::patrolUnit(int _x, int _y, int numPLocations, unitSimulation* us)
26 //:unit(_x, _y)
27 //{
28 // setObjectType(kWorldObject);
29 // currTarget = -1;
30 // nodesExpanded = nodesTouched = 0;
31 // for (int i=1; i<numPLocations; i++)
32 // {
33 // int xx, yy;
34 // us->getRandomLocation(_x,_y,xx,yy);
35 // addPatrolLocation(new unit(xx, yy));
36 // }
37 // addPatrolLocation(new unit(_x, _y));
38 //}
39 
41 {
42  MapAbstraction *aMap = mp->GetMapAbstraction();
43  if (moves.size() > 0)
44  {
45  tDirection dir = moves.back();
46  moves.pop_back();
47  return dir;
48  }
49 
50  if (currTarget != -1)
51  {
52  goToLoc(aMap, currTarget);
53  currTarget = (currTarget+1)%Locs.size();
54  if (moves.size() > 0)
55  {
56  tDirection dir = moves.back();
57  moves.pop_back();
58  return dir;
59  }
60  }
61  return kStay;
62 }
63 
64 double patrolUnit::goToLoc(MapAbstraction *aMap, int which)
65 {
66  double pathCost=-1;
67  path *p;
68  node *from, *to;
69  from = aMap->GetNodeFromMap(x, y); // gets my location
70  int tox, toy;
71  Locs[which]->getLocation(tox, toy);
72 // printf("Patrol unit going to %d, %d\n", tox, toy);
73  to = aMap->GetNodeFromMap(tox, toy);
74  p = a.GetPath(aMap, from, to);
77  if (p)
78  {
79  pathCost = aMap->distance(p);
80  addPathToCache(p);
81  }
82  return pathCost;
83 }
84 
85 void patrolUnit::OpenGLDraw(const MapProvider *mp, const SimulationInfo *) const
86 {
87  GLdouble xx, yy, zz, rad;
88  Map *map = mp->GetMap();
89  int posx = x, posy = y;
90  map->GetOpenGLCoord(posx, posy, xx, yy, zz, rad);
91  glColor3f(r, g, b);
92  glBegin(GL_LINE_STRIP);
93 // glVertex3f(xx, yy+rad/2, zz);
94  glVertex3f(xx, yy, zz-rad/2);
95  for (int t = moves.size()-1; t >= 0; t--)
96  {
97  posx += ((moves[t]&kE)?1:0) - ((moves[t]&kW)?1:0);
98  posy += ((moves[t]&kS)?1:0) - ((moves[t]&kN)?1:0);
99 
100  map->GetOpenGLCoord(posx, posy, xx, yy, zz, rad);
101 
102 // glVertex3f(xx, yy+rad/2, zz);
103  glVertex3f(xx, yy, zz-rad/2);
104  }
105  glEnd();
106 
107  map->GetOpenGLCoord(x, y, xx, yy, zz, rad);
108  glColor3f(r, g, b);
109  drawSphere(xx, yy, zz, rad);
110 }
111 
113 {
114  currTarget = 0;
115  Locs.push_back(ru);
116 }
117 
119 {
120  if (currTarget == -1)
121  return 0;
122  return Locs[currTarget];
123 }
124 
126 {
127  // we are at the last move; abort recursion
128  if ((p == NULL) || (p->next == NULL))
129  return;
130  // there is another move; add it first to cache
131  if (p->next->next)
132  addPathToCache(p->next);
133 
134  // ----- Ok, we have a path starting at (x,y) [the current location] and
135  // having at least one more state ----------------------------------------
136 
137  // Take the first move off the path and execute it
138  int result = kStay;
139 
140  // Decide on the horizontal move
141  switch ((p->n->GetLabelL(kFirstData)-p->next->n->GetLabelL(kFirstData)))
142  {
143  case -1: result = kE; break;
144  case 0: break;
145  case 1: result = kW; break;
146  default :
147  printf("SU: %s : The (x) nodes in the path are not next to each other!\n",
148  this->GetName());
149  printf("Distance is %ld\n",
151  std::cout << *p->n << "\n" << *p->next->n << "\n";
152  exit(10); break;
153  }
154 
155  // Tack the vertical move onto it
156  // Notice the exploit of particular encoding of kStay, kE, etc. labels
157  switch ((p->n->GetLabelL(kFirstData+1)-p->next->n->GetLabelL(kFirstData+1)))
158  {
159  case -1: result = result|kS; break;
160  case 0: break;
161  case 1: result = result|kN; break;
162  default :
163  printf("SU: %s : The (y) nodes in the path are not next to each other!\n",
164  this->GetName());
165  printf("Distance is %ld\n",
166  p->n->GetLabelL(kFirstData+1)-p->next->n->GetLabelL(kFirstData+1));
167  std::cout << *p->n << "\n" << *p->next->n << "\n";
168  exit(10); break;
169  }
170  moves.push_back((tDirection)result);
171 }
172 
174 {
175  if (((nodesExpanded == 0) && (nodesTouched != 0)) ||
176  ((nodesExpanded != 0) && (nodesTouched == 0)))
177  {
178  printf("Error; somehow nodes touched/expanded are inconsistent. t:%d e:%d\n",
180  }
181  // printf("SearchUnit::LogStats(nodesExpanded=%d, nodesTouched=%d)\n",nodesExpanded,nodesTouched);
182  if (nodesExpanded != 0)
183  stats->AddStat("nodesExpanded", GetName(), (long)nodesExpanded);
184  if (nodesTouched != 0)
185  stats->AddStat("nodesTouched", GetName(), (long)nodesTouched);
187 }
188 
190 {
191  a.LogFinalStats(stats);
192 }
MapProvider::GetMap
virtual Map * GetMap() const =0
SearchAlgorithm::LogFinalStats
virtual void LogFinalStats(StatCollection *)
Definition: SearchAlgorithm.h:33
MapProvider::GetMapAbstraction
virtual MapAbstraction * GetMapAbstraction()=0
SimulationInfo
Definition: SimulationInfo.h:13
MapProvider
Definition: MapProvider.h:23
patrolUnit::goToLoc
double goToLoc(MapAbstraction *aMap, int which)
Definition: PatrolUnit.cpp:64
patrolUnit::OpenGLDraw
void OpenGLDraw(const MapProvider *, const SimulationInfo *) const
Definition: PatrolUnit.cpp:85
Unit::r
GLfloat r
Definition: Unit.h:86
kW
@ kW
Definition: Map2DEnvironment.h:78
path::n
node * n
Definition: Path.h:22
Unit::g
GLfloat g
Definition: Unit.h:86
aStar::GetNodesTouched
uint64_t GetNodesTouched()
Definition: AStar.h:93
patrolUnit::makeMove
virtual tDirection makeMove(MapProvider *, reservationProvider *, SimulationInfo *simInfo)
‍** Creates a patrol unit and assigns it numPLocations random locations to patrol.
Definition: PatrolUnit.cpp:40
patrolUnit::addPathToCache
void addPathToCache(path *p)
Definition: PatrolUnit.cpp:125
patrolUnit::nodesExpanded
uint64_t nodesExpanded
Definition: PatrolUnit.h:40
patrolUnit::GetGoal
unit * GetGoal()
Definition: PatrolUnit.cpp:118
patrolUnit::patrolUnit
patrolUnit(int x, int y)
Definition: PatrolUnit.cpp:15
kStay
@ kStay
Definition: Map2DEnvironment.h:79
patrolUnit::moves
std::vector< tDirection > moves
Definition: PatrolUnit.h:35
patrolUnit::LogFinalStats
void LogFinalStats(StatCollection *stats)
log any final one-time stats before a simulation is ended
Definition: PatrolUnit.cpp:189
kE
@ kE
Definition: Map2DEnvironment.h:78
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
patrolUnit::a
aStar a
Definition: PatrolUnit.h:37
PatrolUnit.h
tDirection
tDirection
Definition: Map2DEnvironment.h:77
kN
@ kN
Definition: Map2DEnvironment.h:78
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
patrolUnit::nodesTouched
uint64_t nodesTouched
Definition: PatrolUnit.h:41
kS
@ kS
Definition: Map2DEnvironment.h:78
patrolUnit::addPatrolLocation
void addPatrolLocation(unit *)
Definition: PatrolUnit.cpp:112
reservationProvider
Definition: ReservationProvider.h:33
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
aStar::GetPath
path * GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp=0)
Definition: AStar.cpp:31
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
path::next
path * next
Definition: Path.h:23
patrolUnit::Locs
std::vector< unit * > Locs
Definition: PatrolUnit.h:36
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
patrolUnit::LogStats
void LogStats(StatCollection *stats)
log an stats that may have been computed during the last run
Definition: PatrolUnit.cpp:173
patrolUnit::currTarget
int currTarget
Definition: PatrolUnit.h:39
aStar::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: AStar.h:92
patrolUnit::GetName
virtual const char * GetName()
Definition: PatrolUnit.h:23
Unit::b
GLfloat b
Definition: Unit.h:86
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142