HOG2
Unit.h
Go to the documentation of this file.
1 /*
2  * $Id: unit.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 9/28/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 #ifndef UNITS_H
13 #define UNITS_H
14 
15 #include "UnitGroup.h"
16 #include "UnitSimulation.h"
17 #include "OccupancyInterface.h"
18 
19 template<class state, class action, class environment>
20 class SimulationInfo;
21 
22 template <class state, class action, class environment>
23 class Unit {
24 public:
25  // Unit(state s, Unit<state, action, env> *target);
26  Unit() :speed(0), group(0) { SetColor(1.0, 0.0, 0.0); priority = 999; }
27  virtual ~Unit() { SetUnitGroup(0); }
28  virtual const char *GetName() = 0;
29  virtual bool MakeMove(environment *, OccupancyInterface<state,action> *, SimulationInfo<state,action,environment> *, action& a) = 0;
30  virtual void UpdateLocation(environment *, state &, bool success, SimulationInfo<state,action,environment> *) = 0;
31  virtual void GetLocation(state &) = 0;
32  virtual void OpenGLDraw(const environment *, const SimulationInfo<state,action,environment> *) const = 0;
33  virtual void GetGoal(state &s) {};
34  virtual bool Done() { return true;}
35 
36  virtual double GetSpeed() { return speed; }
37  void SetSpeed(double s) { speed = s; }
38 
40  virtual void LogStats(StatCollection *) {}
42  virtual void LogFinalStats(StatCollection *) {}
43 
44  virtual void SetColor(GLfloat _r, GLfloat _g, GLfloat _b) { r=_r; g=_g; b=_b; }
45  virtual void GetColor(GLfloat& _r, GLfloat& _g, GLfloat& _b) const { _r=r; _g=g; _b=b; }
46 
49  {
50  // If we're already set to the given group then do nothing
51  if (_group == group)
52  return;
53 
55 
56  group = 0;
57  if (tmp)
58  tmp->RemoveUnit(this);
59 
60  // Set the back pointer
61  group = _group;
62 
63  if (_group != 0)
64  _group->AddUnit(this);
65 
66  // OLD CODE
67 /* // If we had a group before then move
68  if (tmp != 0)
69  {
70  tmp->RemoveUnit(this);
71  if (_group)
72  _group->AddUnit(this);
73  } */
74  }
75 
76  virtual unsigned int GetNum() { return unitid; }
77  virtual void SetNum( unsigned int num ) { unitid = num; return; }
78  virtual void StartNewTrial(StatCollection *) {}
79 
80  virtual int GetPriority() { return priority; }
81  virtual void SetPriority(int val) { priority = val; }
82 private:
83  double speed;
84  int priority;
86  GLfloat r, g, b;
87  unsigned int unitid;
88 };
89 
90 
91 #endif
Unit::SetPriority
virtual void SetPriority(int val)
Definition: Unit.h:81
UnitSimulation.h
Unit::SetUnitGroup
void SetUnitGroup(UnitGroup< state, action, environment > *_group)
Definition: Unit.h:48
UnitGroup
Definition: UnitGroup.h:31
SimulationInfo
Definition: SimulationInfo.h:13
Unit::UpdateLocation
virtual void UpdateLocation(environment *, state &, bool success, SimulationInfo< state, action, environment > *)=0
Unit::GetPriority
virtual int GetPriority()
Definition: Unit.h:80
Unit::SetColor
virtual void SetColor(GLfloat _r, GLfloat _g, GLfloat _b)
Definition: Unit.h:44
Unit::StartNewTrial
virtual void StartNewTrial(StatCollection *)
Definition: Unit.h:78
Unit::GetSpeed
virtual double GetSpeed()
Definition: Unit.h:36
Unit::SetNum
virtual void SetNum(unsigned int num)
Definition: Unit.h:77
Unit::r
GLfloat r
Definition: Unit.h:86
OccupancyInterface.h
Unit::g
GLfloat g
Definition: Unit.h:86
Unit::GetName
virtual const char * GetName()=0
Unit::priority
int priority
Definition: Unit.h:84
Unit::GetColor
virtual void GetColor(GLfloat &_r, GLfloat &_g, GLfloat &_b) const
Definition: Unit.h:45
UnitGroup::AddUnit
void AddUnit(Unit< state, action, environment > *u)
Definition: UnitGroup.h:47
Unit::Done
virtual bool Done()
Definition: Unit.h:34
Unit::~Unit
virtual ~Unit()
Definition: Unit.h:27
Unit::GetLocation
virtual void GetLocation(state &)=0
Unit::SetSpeed
void SetSpeed(double s)
Definition: Unit.h:37
Unit::unitid
unsigned int unitid
Definition: Unit.h:87
UnitGroup::RemoveUnit
void RemoveUnit(Unit< state, action, environment > *u)
Definition: UnitGroup.h:70
Unit::speed
double speed
Definition: Unit.h:83
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
Unit::LogFinalStats
virtual void LogFinalStats(StatCollection *)
log any final one-time stats before a simulation is ended
Definition: Unit.h:42
Unit::GetUnitGroup
UnitGroup< state, action, environment > * GetUnitGroup()
Definition: Unit.h:47
Unit::LogStats
virtual void LogStats(StatCollection *)
log an stats that may have been computed during the last run
Definition: Unit.h:40
UnitGroup.h
Unit::MakeMove
virtual bool MakeMove(environment *, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *, action &a)=0
Unit::GetGoal
virtual void GetGoal(state &s)
Definition: Unit.h:33
Unit::group
UnitGroup< state, action, environment > * group
Definition: Unit.h:85
Unit::OpenGLDraw
virtual void OpenGLDraw(const environment *, const SimulationInfo< state, action, environment > *) const =0
Unit::b
GLfloat b
Definition: Unit.h:86
Unit::GetNum
virtual unsigned int GetNum()
Definition: Unit.h:76
Unit::Unit
Unit()
Definition: Unit.h:26
Unit
Definition: Unit.h:23
OccupancyInterface
Definition: OccupancyInterface.h:36