RoboSim Class Reference

#include <RoboSim.hpp>

Collaboration diagram for RoboSim:

Classes

class  RoboAPIImplementor
 
class  SimGridAllyDeterminant
 

Public Member Functions

vector< vector< GridCell > > & getWorldGrid ()
 
int getOccupantPlayer (const GridCell &cell) const
 
 RoboSim (int initial_robots_per_combatant, int skill_points, int length, int width, int obstacles)
 
int executeSingleTimeStep ()
 

Private Member Functions

vector< vector< GridCell > > getSanitizedSubGrid (int x_left, int y_up, int x_right, int y_down, int player) const
 

Static Private Member Functions

static Robot_Specs checkSpecsValid (Robot_Specs proposed, int player, int skill_points)
 

Private Attributes

vector< vector< GridCell > > worldGrid
 
vector< RobotDataturnOrder
 
int turnOrder_pos
 

Static Private Attributes

static const int WALL_HEALTH = 10
 
static const int WALL_DEFENSE = 10
 

Detailed Description

Definition at line 21 of file RoboSim.hpp.

Constructor & Destructor Documentation

RoboSim::RoboSim ( int  initial_robots_per_combatant,
int  skill_points,
int  length,
int  width,
int  obstacles 
)

Constructor for RoboSim:

Parameters
initial_robots_per_combatanthow many robots each team starts out with
skill_pointsskill points per combatant
lengthlength of arena
widthwidth of arena
obstaclesnumber of obstacles on battlefield

Definition at line 66 of file RoboSim.cpp.

References robot_api::RobotData::assoc_cell, robot_api::Robot_Specs::charge, robot_api::Robot_Status::charge, checkSpecsValid(), Robot::createRobot(), robot_api::Robot_Status::defense_boost, robot_api::EMPTY, robot_api::Robot_Status::health, robot_api::RobotData::invested_assoc_cell, robot_api::RobotData::investedPower, robot_api::NOTHING, robot_api::RobotData::player, RBP_CALL_CONSTRUCTOR, RBP_NUM_PLAYERS, robot_api::RobotData::robot, robot_api::SELF, robot_api::RobotData::specs, robot_api::RobotData::status, turnOrder, turnOrder_pos, robot_api::WALL, WALL_HEALTH, robot_api::RobotData::whatBuilding, and worldGrid.

66  :
67  worldGrid(length,vector<GridCell>(width)), turnOrder(RBP_NUM_PLAYERS*initial_robots_per_combatant),
68  turnOrder_pos(0)
69 {
70  for(int i=0; i<length; i++)
71  for(int j=0; j<width; j++)
72  {
73  worldGrid[i][j].x_coord = i;
74  worldGrid[i][j].y_coord = j;
75  worldGrid[i][j].contents = EMPTY;
76  }
77 
78  //Add robots for each combatant
79  for(int player=1; player<=RBP_NUM_PLAYERS; player++)
80  {
81  for(int i=0; i<initial_robots_per_combatant; i++)
82  {
83  int x_pos,y_pos;
84  do
85  {
86  x_pos = rand()%length;
87  y_pos = rand()%width;
88  } while(worldGrid[x_pos][y_pos].contents!=EMPTY);
89 
90  worldGrid[x_pos][y_pos].contents = SELF;
91  RobotData& data = turnOrder[(player-1)*initial_robots_per_combatant+i];
92  worldGrid[x_pos][y_pos].occupant_data = &data;
93  data.assoc_cell = &worldGrid[x_pos][y_pos];
94  data.robot = RBP_CALL_CONSTRUCTOR(player);
95  data.player = player;
96  vector<uint8_t> creation_message(64);
97  creation_message[1] = turnOrder_pos % 256;
98  creation_message[0] = turnOrder_pos / 256;
99  data.specs = checkSpecsValid(data.robot->createRobot(NULL, skill_points, creation_message), player, skill_points);
100  data.status.charge = data.status.health = data.specs.charge*10;
101 
102  data.status.defense_boost = 0;
103  data.whatBuilding = NOTHING;
104  data.investedPower = 0;
105  data.invested_assoc_cell = NULL;
106  }
107  }
108 
109  //Add obstacles to battlefield
110  for(int i=0; i<obstacles; i++)
111  {
112  int x_pos, y_pos;
113  do
114  {
115  x_pos = rand()%length;
116  y_pos = rand()%width;
117  } while(worldGrid[x_pos][y_pos].contents!=EMPTY);
118  worldGrid[x_pos][y_pos].contents = WALL;
119  worldGrid[x_pos][y_pos].wallforthealth = WALL_HEALTH;
120  }
121 }
virtual Robot_Specs createRobot(WorldAPI *api, int skill_points, std::vector< std::uint8_t > message)=0
#define RBP_CALL_CONSTRUCTOR(x)
int turnOrder_pos
Definition: RoboSim.hpp:45
#define RBP_NUM_PLAYERS
Robot_Status status
Definition: robot_api.hpp:109
static const int WALL_HEALTH
Definition: RoboSim.hpp:39
BuildStatus whatBuilding
Definition: robot_api.hpp:114
vector< RobotData > turnOrder
Definition: RoboSim.hpp:44
static Robot_Specs checkSpecsValid(Robot_Specs proposed, int player, int skill_points)
Definition: RoboSim.cpp:58
GridCell * assoc_cell
Definition: robot_api.hpp:107
vector< vector< GridCell > > worldGrid
Definition: RoboSim.hpp:43
GridCell * invested_assoc_cell
Definition: robot_api.hpp:116

Here is the call graph for this function:

Member Function Documentation

Robot_Specs RoboSim::checkSpecsValid ( Robot_Specs  proposed,
int  player,
int  skill_points 
)
staticprivate

Definition at line 58 of file RoboSim.cpp.

References robot_api::Robot_Specs::attack, robot_api::Robot_Specs::charge, robot_api::Robot_Specs::defense, and robot_api::Robot_Specs::power.

Referenced by RoboSim::RoboAPIImplementor::finalizeBuilding(), and RoboSim().

59 {
60  if(proposed.attack + proposed.defense + proposed.power + proposed.charge != skill_points)
61  throw RoboSimExecutionException("attempted to create invalid robot!",player);
62  else
63  return proposed;
64 }

Here is the caller graph for this function:

int RoboSim::executeSingleTimeStep ( )
inline

Executes one timestep of the simulation.

Returns
the winner, if any, or -1

Definition at line 332 of file RoboSim.hpp.

References Robot::act(), robot_api::RobotData::buffered_radio, robot_api::Robot_Specs::charge, robot_api::Robot_Status::charge, robot_api::Robot_Status::defense_boost, robot_api::Robot_Specs::power, robot_api::Robot_Status::power, robot_api::RobotData::robot, robot_api::RobotData::specs, and robot_api::RobotData::status.

333  {
335  {
336  //References to robot's data
338  RoboAPIImplementor student_api(*this,data);
339 
340  //Charge robot an amount of charge equal to charge skill
341  data.status.charge = min(data.status.charge + data.specs.charge, data.specs.charge*10);
342 
343  /*We can spend up to status.power power this turn, but
344  *no more than our current charge level*/
345  data.status.power = min(data.specs.power, data.status.charge);
346 
347  //Defense boost reset to zero at beginning of turn
348  data.status.defense_boost = 0;
349 
350  //Clone status for student
351  Robot_Status clonedStatus = data.status;
352 
353  //Run student code
354  data.robot->act(student_api,clonedStatus,data.buffered_radio);
355  data.buffered_radio.clear();
356  }
357 
358  int player = turnOrder[0].player;
359  for(int i=1; i<turnOrder.size(); i++)
360  if(turnOrder[i].player!=player)
361  return -1;
362  return player;
363  }
vector< vector< uint8_t > > buffered_radio
Definition: robot_api.hpp:119
int turnOrder_pos
Definition: RoboSim.hpp:45
Robot_Status status
Definition: robot_api.hpp:109
vector< RobotData > turnOrder
Definition: RoboSim.hpp:44
virtual void act(WorldAPI &api, Robot_Status status, std::vector< std::vector< std::uint8_t > > received_radio)=0

Here is the call graph for this function:

int RoboSim::getOccupantPlayer ( const GridCell cell) const
inline

SimulatorGUI needs to see who owns the robots in the cells This is a hack to allow this by downcasting the passed GridCell to SimGridCell and extracting the data.

Definition at line 54 of file RoboSim.hpp.

References robot_api::GridCell::occupant_data, and robot_api::RobotData::player.

Referenced by SimulatorGUI::do_timestep().

55  {
56  return cell.occupant_data->player;
57  }
RobotData * occupant_data
Definition: robot_api.hpp:96

Here is the caller graph for this function:

vector< vector< GridCell > > RoboSim::getSanitizedSubGrid ( int  x_left,
int  y_up,
int  x_right,
int  y_down,
int  player 
) const
private

Helper method to retrieve a sanitized subgrid of the world grid

Parameters
x_leftleft x coordinate (inclusive)
y_upsmaller y coordinate (inclusive)
x_rightright x coordinate (inclusive)
y_downlarger y coordinate (inclusive)
playerplayer number
Returns
subgrid of the world grid (NOT copied or sanitized)

Definition at line 34 of file RoboSim.cpp.

References robot_api::ALLY, robot_api::GridCell::contents, robot_api::ENEMY, robot_api::GridCell::has_private_members, robot_api::GridCell::occupant_data, robot_api::RobotData::player, robot_api::SELF, and robot_api::GridCell::wallforthealth.

Referenced by RoboSim::RoboAPIImplementor::getVisibleNeighborhood(), and RoboSim::RoboAPIImplementor::getWorld().

35 {
36  const int x_length = x_right - x_left + 1;
37  const int y_height = y_down - y_up + 1;
38 
39  vector<vector<GridCell> > to_return(x_length,vector<GridCell>(y_height));
40  for(int i=x_left; i<=x_right; i++)
41  for(int j=y_up; j<=y_down; j++)
42  {
43  GridCell& sanitized = to_return[i-x_left][j-y_up];
44  sanitized = worldGrid[i][j];
45  if(sanitized.contents==SELF)
46  if(sanitized.occupant_data->player==player)
47  sanitized.contents = ALLY;
48  else
49  sanitized.contents = ENEMY;
50  sanitized.has_private_members = false;
51  sanitized.occupant_data = NULL;
52  sanitized.wallforthealth = 0;
53  }
54 
55  return to_return;
56 }
RobotData * occupant_data
Definition: robot_api.hpp:96
GridObject contents
Definition: robot_api.hpp:83
vector< vector< GridCell > > worldGrid
Definition: RoboSim.hpp:43

Here is the caller graph for this function:

vector<vector<GridCell> >& RoboSim::getWorldGrid ( )
inline

This is so SimulatorGUI can get a copy of world

Definition at line 49 of file RoboSim.hpp.

Referenced by SimulatorGUI::do_timestep().

49 { return worldGrid; }
vector< vector< GridCell > > worldGrid
Definition: RoboSim.hpp:43

Here is the caller graph for this function:

Member Data Documentation

vector<RobotData> RoboSim::turnOrder
private
int RoboSim::turnOrder_pos
private

Definition at line 45 of file RoboSim.hpp.

Referenced by RoboSim::RoboAPIImplementor::processAttack(), and RoboSim().

const int RoboSim::WALL_DEFENSE = 10
staticprivate

Definition at line 40 of file RoboSim.hpp.

const int RoboSim::WALL_HEALTH = 10
staticprivate

Definition at line 39 of file RoboSim.hpp.

Referenced by RoboSim::RoboAPIImplementor::finalizeBuilding(), and RoboSim().


The documentation for this class was generated from the following files: