SC2API
An API for AI for StarCraft II
sc2_unit.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include "sc2_proto_interface.h"
7 #include "sc2_gametypes.h"
8 #include "sc2_common.h"
9 #include "sc2_typeenums.h"
10 #include <vector>
11 #include <unordered_map>
12 #include <string>
13 #include <stdint.h>
14 
15 namespace sc2 {
16 
17 class ObservationInterface;
18 
20 struct UnitOrder {
28  float progress;
29 
30  UnitOrder() :
31  ability_id(0),
32  target_unit_tag(NullTag),
33  progress(0.0f) {
34  }
35 };
36 
38 struct PassengerUnit {
40  Tag tag;
42  float health;
44  float health_max;
46  float shield;
48  float shield_max;
50  float energy;
52  float energy_max;
55 
56  PassengerUnit() :
57  tag(NullTag),
58  health(0.0f),
59  health_max(0.0f),
60  shield(0.0f),
61  shield_max(0.0f),
62  energy(0.0f),
63  energy_max(0.0f),
64  unit_type(0) {
65  }
66 };
67 
69 class Unit {
70 public:
72  enum DisplayType {
74  Visible = 1,
77  Snapshot = 2,
79  Hidden = 3,
80  };
81 
83  enum Alliance {
85  Self = 1,
87  Ally = 2,
89  Neutral = 3,
91  Enemy = 4
92  };
93 
95  enum CloakState {
97  Cloaked = 1,
99  CloakedDetected = 2,
101  NotCloaked = 3,
103  Unknown = 4
104  };
105 
110 
112  Tag tag;
116  int owner;
117 
121  float facing;
123  float radius;
126 
129 
133  float radar_range;
134 
140  bool is_blip;
141 
142  // Not populated for snapshots
143 
145  float health;
147  float health_max;
149  float shield;
151  float shield_max;
153  float energy;
155  float energy_max;
161  bool is_flying;
166 
167  // Not populated for enemies/snapshots
168 
170  std::vector<UnitOrder> orders;
174  std::vector<PassengerUnit> passengers;
186  std::vector<BuffID> buffs;
189 
191  bool is_alive;
194 
195  Unit();
196 };
197 
198 typedef std::vector<const Unit*> Units;
199 typedef std::unordered_map<Tag, size_t> UnitIdxMap;
200 
201 class UnitPool {
202 public:
203  Unit* CreateUnit(Tag tag);
204  Unit* GetUnit(Tag tag) const;
205  Unit* GetExistingUnit(Tag tag) const;
206  void MarkDead(Tag tag);
207 
208  //TODO: Change alive -> Exist
209  void ForEachExistingUnit(const std::function<void(Unit& unit)>& functor) const;
210  void ClearExisting();
211  bool UnitExists(Tag tag);
212 
213 private:
214  void IncrementIndex();
215 
216  static const size_t ENTRY_SIZE = 1000;
217  typedef std::pair<size_t, size_t> PoolIndex;
218  // std::array<Unit, ENTRY_SIZE>
219  std::vector<std::vector<Unit> > unit_pool_;
220  PoolIndex available_index_;
221  std::unordered_map<Tag, Unit*> tag_to_unit_;
222  std::unordered_map<Tag, Unit*> tag_to_existing_unit_;
223 };
224 
226 struct IsUnit {
227  IsUnit(UNIT_TYPEID type) : type_(type) {};
228  UNIT_TYPEID type_;
229  bool operator()(const Unit& unit) { return unit.unit_type == type_; };
230 };
231 
233 struct IsUnits {
234  IsUnits(std::vector<UNIT_TYPEID> types) : types_(types) {};
235  std::vector<UNIT_TYPEID> types_;
236  bool operator()(const Unit& unit) {
237  bool included = false;
238  for (const auto& type : types_) {
239  included = included || (unit.unit_type == type);
240  }
241  return included;
242  };
243 };
244 
245 }
uint32_t last_seen_game_loop
The last time the unit was seen.
Definition: sc2_unit.h:193
bool is_on_screen
Visible and within the camera frustum.
Definition: sc2_unit.h:138
float energy_max
Max energy of the unit. Not set for snapshots.
Definition: sc2_unit.h:155
Tag tag
The tag of the unit in the transport.
Definition: sc2_unit.h:40
float health
The health of the unit in the transport.
Definition: sc2_unit.h:42
CloakState
Unit cloak state.
Definition: sc2_unit.h:95
Common data types, including points, rectangles and colors.
A unit. Could be a structure, a worker or a military unit.
Definition: sc2_unit.h:69
Alliance
Relationship to this player.
Definition: sc2_unit.h:83
std::vector< UnitOrder > orders
Orders on a unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:170
Definition: sc2_common.h:14
int owner
Which player owns a unit.
Definition: sc2_unit.h:116
bool is_blip
Detected by sensor tower.
Definition: sc2_unit.h:140
Definition: sc2_common.h:49
float radius
Radius of the unit.
Definition: sc2_unit.h:123
Determines if the unit matches the unit type.
Definition: sc2_unit.h:226
bool is_alive
Whether the unit is alive or not.
Definition: sc2_unit.h:191
Definition: sc2_action.h:9
float energy_max
The max possible energy of the unit in the transport.
Definition: sc2_unit.h:52
UnitTypeID unit_type
An identifier of the type of unit.
Definition: sc2_unit.h:114
std::vector< PassengerUnit > passengers
Passengers in this transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:174
CloakState cloak
If the unit is cloaked.
Definition: sc2_unit.h:128
A list of enums provided for your convenience.
float shield
The shield of the unit in the transport.
Definition: sc2_unit.h:46
float shield_max
The max possible shield of the unit in the transport.
Definition: sc2_unit.h:48
float health
Health of the unit. Not set for snapshots.
Definition: sc2_unit.h:145
float radar_range
Range of radar for units that are radar units.
Definition: sc2_unit.h:133
UnitTypeID unit_type
The type of unit in the transport.
Definition: sc2_unit.h:54
float facing
Direction the unit faces in radians (1 radian == 57.296 degrees)
Definition: sc2_unit.h:121
A passenger on a transport.
Definition: sc2_unit.h:38
float progress
Progress of the order.
Definition: sc2_unit.h:28
int ideal_harvesters
Number of harvesters that can be assigned to a town hall (e.g., Command Center). Only valid for this ...
Definition: sc2_unit.h:182
int cargo_space_taken
Number of cargo slots used in the transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:176
Tag target_unit_tag
Target unit of the order, if there is one.
Definition: sc2_unit.h:24
Determines if units matches the unit type.
Definition: sc2_unit.h:233
float energy
The energy of the unit in the transport.
Definition: sc2_unit.h:50
Definition: sc2_unit.h:201
float shield_max
Max shield of the unit. Not set for snapshots.
Definition: sc2_unit.h:151
Point3D pos
Position of the unit in the world.
Definition: sc2_unit.h:119
float build_progress
Gives progress under construction. Range: [0.0, 1.0]. 1.0 == finished.
Definition: sc2_unit.h:125
Tag add_on_tag
Add-on like a tech lab or reactor. Only valid for this player&#39;s units.
Definition: sc2_unit.h:172
float weapon_cooldown
Time remaining for a weapon on cooldown. Not set for snapshots.
Definition: sc2_unit.h:165
Types used in setting up a game.
float energy
Energy of the unit. Not set for snapshots.
Definition: sc2_unit.h:153
AbilityID ability_id
Ability ID that triggered the order.
Definition: sc2_unit.h:22
Point2D target_pos
Target position of the order, if there is one.
Definition: sc2_unit.h:26
An order that is active on a unit.
Definition: sc2_unit.h:20
Alliance alliance
Relationship of the unit to this player.
Definition: sc2_unit.h:109
int mineral_contents
Amount of minerals if the unit is a mineral field. Not set for snapshots.
Definition: sc2_unit.h:157
std::vector< BuffID > buffs
Buffs on this unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:186
float shield
Shield of the unit. Not set for snapshots.
Definition: sc2_unit.h:149
int vespene_contents
Amount of vespene if the unit is a geyser. Not set for snapshots.
Definition: sc2_unit.h:159
float health_max
The max possible health of the unit in the transport.
Definition: sc2_unit.h:44
float health_max
Max health for the unit. Not set for snapshots.
Definition: sc2_unit.h:147
bool is_burrowed
If the unit is burrowed. Not set for snapshots.
Definition: sc2_unit.h:163
Tag engaged_target_tag
Target unit of a unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:184
bool is_flying
If the unit is flying. Not set for snapshots.
Definition: sc2_unit.h:161
int assigned_harvesters
Number of harvesters associated with a town hall (e.g., Command Center). Only valid for this player&#39;s...
Definition: sc2_unit.h:180
float detect_range
Range of detector for detector units.
Definition: sc2_unit.h:131
bool is_powered
Whether the unit is powered by a pylon.
Definition: sc2_unit.h:188
int cargo_space_max
Number of cargo slots available for a transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:178
Tag tag
A unique identifier for the instance of a unit.
Definition: sc2_unit.h:112
bool is_selected
If the unit is in the current selection of the player.
Definition: sc2_unit.h:136
DisplayType display_type
If the unit is shown on screen or not.
Definition: sc2_unit.h:107
DisplayType
If the unit is shown on screen or not.
Definition: sc2_unit.h:72