HOG2
RandomUnits.cpp
Go to the documentation of this file.
1 /*
2  * RandomUnits.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 4/23/07.
6  * Copyright 2007 Nathan Sturtevant, University of Alberta. All rights reserved.
7  *
8  */
9 
10 #include "RandomUnits.h"
11 #include "FPUtil.h"
12 
20 {
21  dir = possibleDir[lastIndex];
22  return true;
23 }
24 
32 {
33  MapUnit::UpdateLocation(me, l, success, si);
34  if (success)
35  { // I moved successfully
36  if (random()%20 == 7)
37  lastIndex = random()%9;
38  }
39  else {
40  lastIndex = random()%9;
41  }
42 }
43 
51 {
52  dir = possibleDir[random()%9];
53  return true;
54 }
55 
63 {
64  dir = possibleDir[lastIndex];
65  return true;
66 }
67 
75 {
76  MapUnit::UpdateLocation(me, l, success, si);
77  if (success)
78  {
79  // I moved successfully
80  // If I was staying put then I should start moving after the coolOffPeriod expires
81  if (lastIndex == kStayIndex && --collisionStatus <= 0)
82  lastIndex = random()%9;
83  // If I was moving then I may change my mind with probability probDirChange
84  if (lastIndex != kStayIndex && fless(random()%10000,probDirChange*10000.0))
85  lastIndex = random()%9;
86  }
87  else {
88  // I was not successful in my move (i.e., I collided)
89  // start resting
91  lastIndex = coolOffPeriod > 0 ? kStayIndex : random()%9;
92  }
93 }
94 
102 {
103  if (timer == 0)
104  {
105  timer = stayTime;
106  loc.x = random()%me->GetMap()->GetMapWidth();
107  loc.y = random()%me->GetMap()->GetMapHeight();
108  dir = kTeleport;
109  return true;
110  }
111  timer--;
112  dir = kStay;
113  return true;
114 }
115 
loc::x
int x
Definition: MapGenerators.cpp:296
MapUnit::UpdateLocation
virtual void UpdateLocation(MapEnvironment *, xyLoc &l, bool, MapSimulationInfo *)
Definition: MapUnit.h:27
TeleportUnit::MakeMove
bool MakeMove(MapEnvironment *, OccupancyInterface< xyLoc, tDirection > *, MapSimulationInfo *, tDirection &dir)
make teleport move.
Definition: RandomUnits.cpp:101
RandomUnit::MakeMove
virtual bool MakeMove(environment *theEnv, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *, action &a)
Definition: RandomUnit.h:22
SimulationInfo
Definition: SimulationInfo.h:13
RandomUnits.h
kTeleport
@ kTeleport
Definition: Map2DEnvironment.h:79
loc::y
int y
Definition: MapGenerators.cpp:296
BilliardBallUnit::MakeMove
bool MakeMove(MapEnvironment *, OccupancyInterface< xyLoc, tDirection > *, MapSimulationInfo *, tDirection &dir)
Make a move.
Definition: RandomUnits.cpp:62
MapEnvironment::GetMap
Map * GetMap() const
Definition: Map2DEnvironment.h:200
RandomUnit::lastIndex
int lastIndex
Definition: RandomUnits.h:31
RandomerUnit::MakeMove
bool MakeMove(MapEnvironment *, OccupancyInterface< xyLoc, tDirection > *, MapSimulationInfo *, tDirection &dir)
Make a random move.
Definition: RandomUnits.cpp:50
BilliardBallUnit::coolOffPeriod
int coolOffPeriod
Definition: RandomUnits.h:81
TeleportUnit::timer
int timer
Definition: RandomUnits.h:46
RandomUnit::UpdateLocation
virtual void UpdateLocation(environment *, state &newloc, bool success, SimulationInfo< state, action, environment > *)
Definition: RandomUnit.h:29
BilliardBallUnit::lastIndex
int lastIndex
Definition: RandomUnits.h:78
FPUtil.h
xyLoc
Definition: Map2DEnvironment.h:37
TeleportUnit::stayTime
int stayTime
Definition: RandomUnits.h:45
BilliardBallUnit::UpdateLocation
void UpdateLocation(MapEnvironment *, xyLoc &, bool, MapSimulationInfo *)
Set location after last move.
Definition: RandomUnits.cpp:74
kStayIndex
const int kStayIndex
Definition: Map2DEnvironment.h:105
kStay
@ kStay
Definition: Map2DEnvironment.h:79
possibleDir
const tDirection possibleDir[numActions]
Definition: Map2DEnvironment.h:104
MapEnvironment
Definition: Map2DEnvironment.h:133
loc
Definition: MapGenerators.cpp:296
BilliardBallUnit::collisionStatus
int collisionStatus
Definition: RandomUnits.h:79
fless
bool fless(double a, double b)
Definition: FPUtil.h:28
Map::GetMapWidth
long GetMapWidth() const
return the width of the map
Definition: Map.h:163
tDirection
tDirection
Definition: Map2DEnvironment.h:77
Map::GetMapHeight
long GetMapHeight() const
return the height of the map
Definition: Map.h:165
BilliardBallUnit::probDirChange
double probDirChange
Definition: RandomUnits.h:80
OccupancyInterface< xyLoc, tDirection >