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 <unordered_set>
13 #include <string>
14 #include <stdint.h>
15 
16 namespace sc2 {
17 
18 class ObservationInterface;
19 
21 struct UnitOrder {
29  float progress;
30 
31  UnitOrder() :
32  ability_id(0),
33  target_unit_tag(NullTag),
34  progress(0.0f) {
35  }
36 };
37 
39 struct PassengerUnit {
41  Tag tag;
43  float health;
45  float health_max;
47  float shield;
49  float shield_max;
51  float energy;
53  float energy_max;
56 
57  PassengerUnit() :
58  tag(NullTag),
59  health(0.0f),
60  health_max(0.0f),
61  shield(0.0f),
62  shield_max(0.0f),
63  energy(0.0f),
64  energy_max(0.0f),
65  unit_type(0) {
66  }
67 };
68 
70 class Unit {
71 public:
73  enum DisplayType {
75  Visible = 1,
78  Snapshot = 2,
80  Hidden = 3,
81  // Building that hasn't started construction.
82  Placeholder = 4,
83  };
84 
86  enum Alliance {
88  Self = 1,
90  Ally = 2,
92  Neutral = 3,
94  Enemy = 4
95  };
96 
98  enum CloakState {
100  CloakedUnknown = 0,
102  Cloaked = 1,
104  CloakedDetected = 2,
106  NotCloaked = 3,
108  CloakedAllied = 4,
109  };
110 
115 
117  Tag tag;
121  int owner;
122 
126  float facing;
128  float radius;
131 
134 
138  float radar_range;
139 
145  bool is_blip;
146 
147  // Not populated for snapshots
148 
150  float health;
152  float health_max;
154  float shield;
156  float shield_max;
158  float energy;
160  float energy_max;
166  bool is_flying;
171 
172  // Not populated for enemies/snapshots
173 
175  std::vector<UnitOrder> orders;
179  std::vector<PassengerUnit> passengers;
191  std::vector<BuffID> buffs;
194 
196  bool is_alive;
199 
202 
205 
208 
209  Unit();
210 };
211 
212 typedef std::vector<const Unit*> Units;
213 typedef std::unordered_map<Tag, size_t> UnitIdxMap;
214 
215 struct UnitDamage {
216  const Unit* unit;
217  float health;
218  float shields;
219 };
220 
221 typedef std::vector<UnitDamage> UnitsDamaged;
222 
223 class UnitPool {
224 public:
225  Unit* CreateUnit(Tag tag);
226  Unit* GetUnit(Tag tag) const;
227  Unit* GetExistingUnit(Tag tag) const;
228  void MarkDead(Tag tag);
229 
230  //TODO: Change alive -> Exist
231  void ForEachExistingUnit(const std::function<void(Unit& unit)>& functor) const;
232  void ClearExisting();
233  bool UnitExists(Tag tag);
234 
235  const Units& GetNewUnits() const noexcept { return units_newly_created_; };
236  const Units& GetUnitsEnteringVision() const noexcept { return units_entering_vision_; };
237  const Units& GetCompletedBuildings() const noexcept { return buildings_constructed_; };
238  const UnitsDamaged& GetDamagedUnits() const noexcept { return units_damaged_; };
239  const std::unordered_set<const Unit*>& GetIdledUnits() const noexcept { return units_idled_; };
240 
241  void AddNewUnit(const Unit* u) { units_newly_created_.push_back(u); };
242  void AddUnitEnteredVision(const Unit* u) { units_entering_vision_.push_back(u); }
243  void AddCompletedBuilding(const Unit* u) { buildings_constructed_.push_back(u); }
244  void AddUnitIdled(const Unit* u) {
245  if (u->alliance == Unit::Alliance::Self) units_idled_.insert(u);
246  }
247  void AddUnitDamaged(const Unit* u, float health, float shield) { units_damaged_.push_back({u, health, shield}); }
248 
249 private:
250  void IncrementIndex();
251 
252  static const size_t ENTRY_SIZE = 1000;
253  typedef std::pair<size_t, size_t> PoolIndex;
254  // std::array<Unit, ENTRY_SIZE>
255  std::vector<std::vector<Unit> > unit_pool_;
256  PoolIndex available_index_;
257  std::unordered_map<Tag, Unit *> tag_to_unit_;
258  std::unordered_map<Tag, Unit *> tag_to_existing_unit_;
259  Units units_newly_created_;
260  Units units_entering_vision_;
261  Units buildings_constructed_;
262  UnitsDamaged units_damaged_;
263  std::unordered_set<const Unit*> units_idled_;
264 };
265 
266 }
uint32_t last_seen_game_loop
The last time the unit was seen.
Definition: sc2_unit.h:198
bool is_on_screen
Visible and within the camera frustum.
Definition: sc2_unit.h:143
float energy_max
Max energy of the unit. Not set for snapshots.
Definition: sc2_unit.h:160
Tag tag
The tag of the unit in the transport.
Definition: sc2_unit.h:41
float health
The health of the unit in the transport.
Definition: sc2_unit.h:43
CloakState
Unit cloak state.
Definition: sc2_unit.h:98
Common data types, including points, rectangles and colors.
A unit. Could be a structure, a worker or a military unit.
Definition: sc2_unit.h:70
Alliance
Relationship to this player.
Definition: sc2_unit.h:86
std::vector< UnitOrder > orders
Orders on a unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:175
Definition: sc2_common.h:14
int owner
Which player owns a unit.
Definition: sc2_unit.h:121
bool is_blip
Detected by sensor tower.
Definition: sc2_unit.h:145
Definition: sc2_common.h:49
float radius
Radius of the unit.
Definition: sc2_unit.h:128
bool is_alive
Whether the unit is alive or not.
Definition: sc2_unit.h:196
Definition: sc2_action.h:9
float energy_max
The max possible energy of the unit in the transport.
Definition: sc2_unit.h:53
UnitTypeID unit_type
An identifier of the type of unit.
Definition: sc2_unit.h:119
std::vector< PassengerUnit > passengers
Passengers in this transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:179
CloakState cloak
If the unit is cloaked.
Definition: sc2_unit.h:133
A list of enums provided for your convenience.
float shield
The shield of the unit in the transport.
Definition: sc2_unit.h:47
float shield_max
The max possible shield of the unit in the transport.
Definition: sc2_unit.h:49
float health
Health of the unit. Not set for snapshots.
Definition: sc2_unit.h:150
float radar_range
Range of radar for units that are radar units.
Definition: sc2_unit.h:138
UnitTypeID unit_type
The type of unit in the transport.
Definition: sc2_unit.h:55
float facing
Direction the unit faces in radians (1 radian == 57.296 degrees)
Definition: sc2_unit.h:126
A passenger on a transport.
Definition: sc2_unit.h:39
float progress
Progress of the order.
Definition: sc2_unit.h:29
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:187
int cargo_space_taken
Number of cargo slots used in the transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:181
Tag target_unit_tag
Target unit of the order, if there is one.
Definition: sc2_unit.h:25
float energy
The energy of the unit in the transport.
Definition: sc2_unit.h:51
Definition: sc2_unit.h:223
float shield_max
Max shield of the unit. Not set for snapshots.
Definition: sc2_unit.h:156
Point3D pos
Position of the unit in the world.
Definition: sc2_unit.h:124
float build_progress
Gives progress under construction. Range: [0.0, 1.0]. 1.0 == finished.
Definition: sc2_unit.h:130
Tag add_on_tag
Add-on like a tech lab or reactor. Only valid for this player&#39;s units.
Definition: sc2_unit.h:177
float weapon_cooldown
Time remaining for a weapon on cooldown. Not set for snapshots.
Definition: sc2_unit.h:170
Types used in setting up a game.
float energy
Energy of the unit. Not set for snapshots.
Definition: sc2_unit.h:158
AbilityID ability_id
Ability ID that triggered the order.
Definition: sc2_unit.h:23
Point2D target_pos
Target position of the order, if there is one.
Definition: sc2_unit.h:27
An order that is active on a unit.
Definition: sc2_unit.h:21
Alliance alliance
Relationship of the unit to this player.
Definition: sc2_unit.h:114
int mineral_contents
Amount of minerals if the unit is a mineral field. Not set for snapshots.
Definition: sc2_unit.h:162
int32_t attack_upgrade_level
Level of weapon upgrades.
Definition: sc2_unit.h:201
std::vector< BuffID > buffs
Buffs on this unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:191
int32_t shield_upgrade_level
Level of shield upgrades.
Definition: sc2_unit.h:207
float shield
Shield of the unit. Not set for snapshots.
Definition: sc2_unit.h:154
int vespene_contents
Amount of vespene if the unit is a geyser. Not set for snapshots.
Definition: sc2_unit.h:164
float health_max
The max possible health of the unit in the transport.
Definition: sc2_unit.h:45
float health_max
Max health for the unit. Not set for snapshots.
Definition: sc2_unit.h:152
bool is_burrowed
If the unit is burrowed. Not set for snapshots.
Definition: sc2_unit.h:168
Tag engaged_target_tag
Target unit of a unit. Only valid for this player&#39;s units.
Definition: sc2_unit.h:189
bool is_flying
If the unit is flying. Not set for snapshots.
Definition: sc2_unit.h:166
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:185
Definition: sc2_unit.h:215
float detect_range
Range of detector for detector units.
Definition: sc2_unit.h:136
bool is_powered
Whether the unit is powered by a pylon.
Definition: sc2_unit.h:193
int cargo_space_max
Number of cargo slots available for a transport. Only valid for this player&#39;s units.
Definition: sc2_unit.h:183
int32_t armor_upgrade_level
Level of armor upgrades.
Definition: sc2_unit.h:204
Tag tag
A unique identifier for the instance of a unit.
Definition: sc2_unit.h:117
bool is_selected
If the unit is in the current selection of the player.
Definition: sc2_unit.h:141
DisplayType display_type
If the unit is shown on screen or not.
Definition: sc2_unit.h:112
DisplayType
If the unit is shown on screen or not.
Definition: sc2_unit.h:73