HOG2
RewardUnit.cpp
Go to the documentation of this file.
1 /*
2  * $Id: rewardUnit.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 2/16/05.
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 "RewardUnit.h"
13 
14 rewardUnit::rewardUnit(int _x, int _y)
15 :unit(_x, _y)
16 {
17  setObjectType(kIncidentalUnit);
18 }
19 
20 void rewardUnit::OpenGLDraw(const MapProvider *mp, const SimulationInfo *) const
21 {
22  Map *map = mp->GetMap();
23  GLdouble xx, yy, zz, rad;
24  map->GetOpenGLCoord(x, y, xx, yy, zz, rad);
25  glColor3f(r, g, b);
26  drawTriangle(xx, yy, zz, rad);
27 }
28 
29 
30 
32 :rewardUnit(_x, _y)
33 {
34  setObjectType(kWorldObject);
35 }
36 
38 {
39  printf("%s got reward %3.2f!!\n", GetName(), amount);
40 }
41 
43 {
44  MapAbstraction *aMap = mp->GetMapAbstraction();
45  if (moves.size() > 0)
46  {
47  tDirection dir = moves.back();
48  moves.pop_back();
49  // if (verbose)
50  // printf("SU %p: returning cached move 0x%X\n", this, (int)dir);
51  return dir;
52  }
53 
54  // add your logic code here...
55  static int nextTarget = 0;
56  goToRewardLoc(aMap, nextTarget);
57  nextTarget = (nextTarget+1)%rewardLocs.size();
58  return kStay;
59 }
60 
61 double rewardSeekingUnit::goToRewardLoc(MapAbstraction *aMap, int which)
62 {
63  double pathCost=-1;
64  path *p;
65  node *from, *to;
66  from = aMap->GetNodeFromMap(x, y); // gets my location
67  int tox, toy;
68  rewardLocs[which]->getLocation(tox, toy);
69  to = aMap->GetNodeFromMap(tox, toy);
70  p = a.GetPath(aMap, from, to);
71  if (p)
72  {
73  pathCost = aMap->distance(p);
74  addPathToCache(p);
75  }
76  return pathCost;
77 }
78 
80 {
81  GLdouble xx, yy, zz, rad;
82  Map *map = mp->GetMap();
83  int posx = x, posy = y;
84  map->GetOpenGLCoord(posx, posy, xx, yy, zz, rad);
85  glColor3f(r, g, b);
86  glBegin(GL_LINE_STRIP);
87  glVertex3f(xx, yy+rad/2, zz);
88  for (int t = moves.size()-1; t >= 0; t--)
89  {
90  posx += ((moves[t]&kE)?1:0) - ((moves[t]&kW)?1:0);
91  posy += ((moves[t]&kS)?1:0) - ((moves[t]&kN)?1:0);
92 
93  map->GetOpenGLCoord(posx, posy, xx, yy, zz, rad);
94 
95  glVertex3f(xx, yy+rad/2, zz);
96  }
97  glEnd();
98 
99  map->GetOpenGLCoord(x, y, xx, yy, zz, rad);
100  glColor3f(r, g, b);
101  drawSphere(xx, yy, zz, rad);
102 }
103 
105 {
106  rewardLocs.push_back(ru);
107 }
108 
110 {
111  // we are at the last move; abort recursion
112  if ((p == NULL) || (p->next == NULL))
113  return;
114  // there is another move; add it first to cache
115  if (p->next->next)
116  addPathToCache(p->next);
117 
118  // ----- Ok, we have a path starting at (x,y) [the current location] and
119  // having at least one more state ----------------------------------------
120 
121  // Take the first move off the path and execute it
122  int result = kStay;
123 
124  // Decide on the horizontal move
125  switch ((p->n->GetLabelL(kFirstData)-p->next->n->GetLabelL(kFirstData)))
126  {
127  case -1: result = kE; break;
128  case 0: break;
129  case 1: result = kW; break;
130  default :
131  printf("SU: %s : The (x) nodes in the path are not next to each other!\n",
132  this->GetName());
133  printf("Distance is %ld\n",
135  std::cout << *p->n << "\n" << *p->next->n << "\n";
136  exit(10); break;
137  }
138 
139  // Tack the vertical move onto it
140  // Notice the exploit of particular encoding of kStay, kE, etc. labels
141  switch ((p->n->GetLabelL(kFirstData+1)-p->next->n->GetLabelL(kFirstData+1)))
142  {
143  case -1: result = result|kS; break;
144  case 0: break;
145  case 1: result = result|kN; break;
146  default :
147  printf("SU: %s : The (y) nodes in the path are not next to each other!\n",
148  this->GetName());
149  printf("Distance is %ld\n",
150  p->n->GetLabelL(kFirstData+1)-p->next->n->GetLabelL(kFirstData+1));
151  std::cout << *p->n << "\n" << *p->next->n << "\n";
152  exit(10); break;
153  }
154  moves.push_back((tDirection)result);
155 }
MapProvider::GetMap
virtual Map * GetMap() const =0
rewardSeekingUnit::OpenGLDraw
void OpenGLDraw(const MapProvider *, const SimulationInfo *) const
Definition: RewardUnit.cpp:79
MapProvider::GetMapAbstraction
virtual MapAbstraction * GetMapAbstraction()=0
SimulationInfo
Definition: SimulationInfo.h:13
MapProvider
Definition: MapProvider.h:23
rewardSeekingUnit::GetName
virtual const char * GetName()
Definition: RewardUnit.h:31
rewardUnit::rewardUnit
rewardUnit(int x, int y)
Definition: RewardUnit.cpp:14
kW
@ kW
Definition: Map2DEnvironment.h:78
path::n
node * n
Definition: Path.h:22
rewardSeekingUnit::a
aStar a
Definition: RewardUnit.h:41
kStay
@ kStay
Definition: Map2DEnvironment.h:79
rewardSeekingUnit::makeMove
virtual tDirection makeMove(MapProvider *, reservationProvider *, SimulationInfo *simInfo)
Definition: RewardUnit.cpp:42
kE
@ kE
Definition: Map2DEnvironment.h:78
rewardSeekingUnit::goToRewardLoc
double goToRewardLoc(MapAbstraction *aMap, int which)
Definition: RewardUnit.cpp:61
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
rewardSeekingUnit::receiveReward
virtual void receiveReward(double)
Definition: RewardUnit.cpp:37
tDirection
tDirection
Definition: Map2DEnvironment.h:77
rewardSeekingUnit::rewardLocs
std::vector< rewardUnit * > rewardLocs
Definition: RewardUnit.h:40
kN
@ kN
Definition: Map2DEnvironment.h:78
rewardUnit
Definition: RewardUnit.h:19
rewardSeekingUnit::addRewardLocation
void addRewardLocation(rewardUnit *)
Definition: RewardUnit.cpp:104
rewardSeekingUnit::rewardSeekingUnit
rewardSeekingUnit(int x, int y)
Definition: RewardUnit.cpp:31
kS
@ kS
Definition: Map2DEnvironment.h:78
rewardUnit::OpenGLDraw
void OpenGLDraw(const MapProvider *, const SimulationInfo *) const
Definition: RewardUnit.cpp:20
reservationProvider
Definition: ReservationProvider.h:33
rewardSeekingUnit::moves
std::vector< tDirection > moves
Definition: RewardUnit.h:39
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
rewardSeekingUnit::addPathToCache
void addPathToCache(path *p)
Definition: RewardUnit.cpp:109
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
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
RewardUnit.h