HOG2
UnitGroup.h
Go to the documentation of this file.
1 /*
2  * $Id: unitGroup.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 12/9/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 
13 #ifndef UNITGROUP_H
14 #define UNITGROUP_H
15 
16 #include "BitVector.h"
17 #include "Map.h"
18 //#include "MapAbstraction.h"
19 #include "ReservationProvider.h"
20 //#include "MapProvider.h"
21 #include "UnitSimulation.h"
22 #include "StatCollection.h"
23 
24 template <class state, class action, class environment>
25 class Unit;
26 
27 template<class state, class action, class environment>
28 class SimulationInfo;
29 
30 template <class state, class action, class environment>
31 class UnitGroup {
32 public:
33  virtual ~UnitGroup() {}
34 
35  virtual const char *GetName() { return "defaultUnitGroup"; }
36 
38  {
39  return (u->MakeMove(e, e->GetOccupancyInfo(), si,a));
40  }
41 
42  virtual void UpdateLocation(Unit<state, action, environment> *u, environment *e, state &loc, bool success, SimulationInfo<state,action,environment> *si)
43  {
44  u->UpdateLocation(e, loc, success, si);
45  }
46 
48  {
49  // Check if we already have this unit
50  for (unsigned int x = 0; x < members.size(); x++)
51  if (members[x] == u)
52  return;
53 
54  // If not then add the unit to the group and set the unit's group
55  members.push_back(u);
56  u->SetUnitGroup(this);
57  }
58 
59  virtual bool Done()
60  {
61  for (unsigned int x = 0; x < members.size(); x++)
62  if (!members[x]->Done())
63  {
64  return false;
65  }
66  return true;
67  }
68 
69 
71  {
72  for (unsigned int x = 0; x < members.size(); x++)
73  {
74  if (members[x] == u)
75  {
76  u->SetUnitGroup(0);
77  members[x] = members[members.size()-1];
78  members.pop_back();
79  }
80  }
81  }
82 
83  virtual void StartNewTrial(StatCollection *c)
84  {
85  for (unsigned int x = 0; x < members.size(); x++)
86  {
87  members[x]->StartNewTrial(c);
88  }
89  }
90 
91  virtual void OpenGLDraw(const environment *, const SimulationInfo<state,action,environment> *) const { }
92 
93  virtual std::vector<Unit<state,action,environment> *> GetMembers() {return members;}
94  unsigned int GetNumMembers() { return members.size(); }
95  Unit<state,action,environment> *GetMember(int which) const { return members[which]; }
96 
97 private:
98  std::vector<Unit<state, action, environment> *> members;
99 };
100 
101 
102 //class unit;
103 //class SimulationInfo;
104 //
106 // * A unitGroup provides shared memory and computation for all units within the group.
107 // */
108 //
109 //class unitGroup {
110 //public:
111 // unitGroup(MapProvider *);
112 // virtual ~unitGroup() {}
113 // virtual tDirection makeMove(unit *u, MapProvider *, reservationProvider *, SimulationInfo *simInfo);
114 // /** gives the unit group time to think on a regular basis. In a synchronous simluation
115 // this will be called once at the beginning of each timestep. */
116 // virtual void think(MapProvider *) { }
117 // virtual const char *GetName() { return "UnitGroupx"; }
118 //
119 // virtual void OpenGLDraw(MapProvider *, SimulationInfo *);
120 // virtual void addUnit(unit *);
121 // virtual void removeUnit(unit *);
122 // virtual MapAbstraction *GetMapAbstraction();
123 //
124 // /** Inform the given unit of its current/new location */
125 // virtual void updateLocation(unit *, MapProvider *, int _x, int _y, bool, SimulationInfo *);
126 // /** Is the group done with racing? */
127 // virtual bool done();
128 // /** Lets the unit group do what it needs to reset a trial */
129 // virtual void StartNewTrial(StatCollection *stats);
130 // // print stats; if we don't know any, get the unit to do it
131 // virtual void LogStats(StatCollection *stats);
132 // virtual void LogFinalStats(StatCollection *) {}
133 // //virtual void printRoundStats(unit *u, FILE *f);
134 //
135 // /** lets the user cycle through the display mode by hitting Shift-Tab */
136 // virtual void cycleDisplayMode(void) { }
137 //
138 // /** lets the user cycle through the display mode by hitting [ and ] */
139 // virtual void increaseDisplayALevel(void) { }
140 // virtual void decreaseDisplayALevel(void) { }
141 //
142 // /** allows you to iterate through units in the group */
143 // unit *getUnit(unsigned int which);
144 // int getGroupID() { return id; }
145 //
146 //protected:
147 // //friend void unitSimulation::addUnitGroup(unitGroup *);
148 // ///** This is only for the unit simulation to set when the unitGroup is added. */
149 // //virtual void setUnitSimulation(unitSimulation *_us, Map *m);
150 // //Map *map;
151 // std::vector<unit *> myUnits;
152 // //unitSimulation *us;
153 // int id;
154 // static int groupID;
155 //};
156 //
157 //#include "Unit.h"
158 
159 #endif
160 
UnitSimulation.h
Unit::SetUnitGroup
void SetUnitGroup(UnitGroup< state, action, environment > *_group)
Definition: Unit.h:48
UnitGroup::GetNumMembers
unsigned int GetNumMembers()
Definition: UnitGroup.h:94
UnitGroup
Definition: UnitGroup.h:31
UnitGroup::StartNewTrial
virtual void StartNewTrial(StatCollection *c)
Definition: UnitGroup.h:83
SimulationInfo
Definition: SimulationInfo.h:13
Unit::UpdateLocation
virtual void UpdateLocation(environment *, state &, bool success, SimulationInfo< state, action, environment > *)=0
StatCollection.h
UnitGroup::Done
virtual bool Done()
Definition: UnitGroup.h:59
UnitGroup::GetMembers
virtual std::vector< Unit< state, action, environment > * > GetMembers()
Definition: UnitGroup.h:93
UnitGroup::AddUnit
void AddUnit(Unit< state, action, environment > *u)
Definition: UnitGroup.h:47
UnitGroup::GetMember
Unit< state, action, environment > * GetMember(int which) const
Definition: UnitGroup.h:95
loc
Definition: MapGenerators.cpp:296
UnitGroup::~UnitGroup
virtual ~UnitGroup()
Definition: UnitGroup.h:33
UnitGroup::RemoveUnit
void RemoveUnit(Unit< state, action, environment > *u)
Definition: UnitGroup.h:70
UnitGroup::UpdateLocation
virtual void UpdateLocation(Unit< state, action, environment > *u, environment *e, state &loc, bool success, SimulationInfo< state, action, environment > *si)
Definition: UnitGroup.h:42
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
ReservationProvider.h
UnitGroup::members
std::vector< Unit< state, action, environment > * > members
Definition: UnitGroup.h:98
BitVector.h
UnitGroup::GetName
virtual const char * GetName()
Definition: UnitGroup.h:35
Unit::MakeMove
virtual bool MakeMove(environment *, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *, action &a)=0
Map.h
UnitGroup::MakeMove
virtual bool MakeMove(Unit< state, action, environment > *u, environment *e, SimulationInfo< state, action, environment > *si, action &a)
Definition: UnitGroup.h:37
UnitGroup::OpenGLDraw
virtual void OpenGLDraw(const environment *, const SimulationInfo< state, action, environment > *) const
Definition: UnitGroup.h:91
Unit
Definition: Unit.h:23