TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
frame.h
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  */
9 
10 #pragma once
11 
12 #include <cassert>
13 #include <cstdio>
14 #include <iostream>
15 #include <sstream>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include <torchcraft/refcount.h>
20 #include "messages_generated.h"
21 
22 #ifdef _MSC_VER
23 typedef unsigned int uint32_t;
24 typedef unsigned short uint16_t;
25 typedef int int32_t;
26 #endif
27 
28 namespace torchcraft {
29 namespace replayer {
30 
31 struct Unit;
32 struct Resources;
33 struct Bullet;
34 struct Action;
35 class Frame;
36 class FrameDiff;
37 namespace detail { class UnitDiff; }
38 
39 std::ostream& operator<<(std::ostream& out, const Frame& o);
40 std::istream& operator>>(std::istream& in, Frame& o);
41 std::ostream& operator<<(std::ostream& out, const FrameDiff& o);
42 std::istream& operator>>(std::istream& in, FrameDiff& o);
43 
44 struct Bullet {
45  int32_t type, x, y;
46 };
47 
48 struct Action {
49  std::vector<int32_t> action;
50  int32_t uid;
51  int32_t aid;
52 };
53 
54 struct Order {
55  int32_t first_frame; // first frame number where order appeared
56  int32_t type; // see BWAPI::Orders::Enum
57  int32_t targetId;
58  int32_t targetX, targetY;
59 
60  bool operator==(const Order& o) const {
61  // Ignore first_frame
62  return type == o.type && targetId == o.targetId && targetX == o.targetX &&
63  targetY == o.targetY;
64  }
65 };
66 
67 struct UnitCommand {
68  int32_t frame;
69  int32_t type; // see BWAPI::UnitCommandType::Enum
70  int32_t targetId;
71  int32_t targetX, targetY;
72  int32_t extra;
73 
74  bool operator==(const UnitCommand& c) const {
75  // Ignore frame
76  return type == c.type && targetId == c.targetId && targetX == c.targetX &&
77  targetY == c.targetY && extra == c.extra;
78  }
79 };
80 
81 struct Unit {
82  int32_t id, x, y;
83  int32_t health, max_health, shield, max_shield, energy;
84  int32_t maxCD, groundCD, airCD;
85  uint64_t flags;
86  int32_t visible;
87  int32_t type, armor, shieldArmor, size;
88  int32_t pixel_x, pixel_y;
89  int32_t pixel_size_x, pixel_size_y;
90  int32_t groundATK, airATK;
91  int32_t groundDmgType, airDmgType;
92  int32_t groundRange, airRange;
93 
94  std::vector<Order> orders;
96 
97  double velocityX, velocityY;
98 
99  int32_t playerId;
100  int32_t resources;
104  int32_t spellCD;
105  int32_t associatedUnit; // addOn, nydusExit, transport, hatchery
106  int32_t associatedCount; // spiderMines, scarabs, interceptors, nuke
107 
108  enum Flags : uint64_t {
109  // clang-format off
110  Accelerating = 1ll << 0,
111  Attacking = 1ll << 1,
112  AttackFrame = 1ll << 2,
113  BeingConstructed = 1ll << 3,
114  BeingGathered = 1ll << 4,
115  BeingHealed = 1ll << 5,
116  Blind = 1ll << 6,
117  Braking = 1ll << 7,
118  Burrowed = 1ll << 8,
119  CarryingGas = 1ll << 9,
120  CarryingMinerals = 1ll << 10,
121  Cloaked = 1ll << 11,
122  Completed = 1ll << 12,
123  Constructing = 1ll << 13,
124  DefenseMatrixed = 1ll << 14,
125  Detected = 1ll << 15,
126  Ensnared = 1ll << 16,
127  Flying = 1ll << 17,
128  Following = 1ll << 18,
129  GatheringGas = 1ll << 19,
130  GatheringMinerals = 1ll << 20,
131  Hallucination = 1ll << 21,
132  HoldingPosition = 1ll << 22,
133  Idle = 1ll << 23,
134  Interruptible = 1ll << 24,
135  Invincible = 1ll << 25,
136  Irradiated = 1ll << 26,
137  Lifted = 1ll << 27,
138  Loaded = 1ll << 28,
139  LockedDown = 1ll << 29,
140  Maelstrommed = 1ll << 30,
141  Morphing = 1ll << 31,
142  Moving = 1ll << 32,
143  Parasited = 1ll << 33,
144  Patrolling = 1ll << 34,
145  Plagued = 1ll << 35,
146  Powered = 1ll << 36,
147  Repairing = 1ll << 37,
148  Researching = 1ll << 38,
149  Selected = 1ll << 39,
150  Sieged = 1ll << 40,
151  StartingAttack = 1ll << 41,
152  Stasised = 1ll << 42,
153  Stimmed = 1ll << 43,
154  Stuck = 1ll << 44,
155  Targetable = 1ll << 45,
156  Training = 1ll << 46,
157  UnderAttack = 1ll << 47,
158  UnderDarkSwarm = 1ll << 48,
159  UnderDisruptionWeb = 1ll << 49,
160  UnderStorm = 1ll << 50,
161  Upgrading = 1ll << 51,
162  // clang-format on
163  };
164 };
165 
166 struct Resources {
167  int32_t ore;
168  int32_t gas;
169  int32_t used_psi;
170  int32_t total_psi;
171  uint64_t upgrades;
172  uint64_t upgrades_level;
173  uint64_t techs;
174 
175  // clang-format off
176  enum Upgrades : uint64_t {
177  Terran_Infantry_Armor = 1ll << 0,
178  Terran_Vehicle_Plating = 1ll << 1,
179  Terran_Ship_Plating = 1ll << 2,
180  Zerg_Carapace = 1ll << 3,
181  Zerg_Flyer_Carapace = 1ll << 4,
182  Protoss_Ground_Armor = 1ll << 5,
183  Protoss_Air_Armor = 1ll << 6,
184  Terran_Infantry_Weapons = 1ll << 7,
185  Terran_Vehicle_Weapons = 1ll << 8,
186  Terran_Ship_Weapons = 1ll << 9,
187  Zerg_Melee_Attacks = 1ll << 10,
188  Zerg_Missile_Attacks = 1ll << 11,
189  Zerg_Flyer_Attacks = 1ll << 12,
190  Protoss_Ground_Weapons = 1ll << 13,
191  Protoss_Air_Weapons = 1ll << 14,
192  Protoss_Plasma_Shields = 1ll << 15,
193  U_238_Shells = 1ll << 16,
194  Ion_Thrusters = 1ll << 17,
195  Titan_Reactor = 1ll << 19,
196  Ocular_Implants = 1ll << 20,
197  Moebius_Reactor = 1ll << 21,
198  Apollo_Reactor = 1ll << 22,
199  Colossus_Reactor = 1ll << 23,
200  Ventral_Sacs = 1ll << 24,
201  Antennae = 1ll << 25,
202  Pneumatized_Carapace = 1ll << 26,
203  Metabolic_Boost = 1ll << 27,
204  Adrenal_Glands = 1ll << 28,
205  Muscular_Augments = 1ll << 29,
206  Grooved_Spines = 1ll << 30,
207  Gamete_Meiosis = 1ll << 31,
208  Metasynaptic_Node = 1ll << 32,
209  Singularity_Charge = 1ll << 33,
210  Leg_Enhancements = 1ll << 34,
211  Scarab_Damage = 1ll << 35,
212  Reaver_Capacity = 1ll << 36,
213  Gravitic_Drive = 1ll << 37,
214  Sensor_Array = 1ll << 38,
215  Gravitic_Boosters = 1ll << 39,
216  Khaydarin_Amulet = 1ll << 40,
217  Apial_Sensors = 1ll << 41,
218  Gravitic_Thrusters = 1ll << 42,
219  Carrier_Capacity = 1ll << 43,
220  Khaydarin_Core = 1ll << 44,
221  Argus_Jewel = 1ll << 47,
222  Argus_Talisman = 1ll << 49,
223  Caduceus_Reactor = 1ll << 51,
224  Chitinous_Plating = 1ll << 52,
225  Anabolic_Synthesis = 1ll << 53,
226  Charon_Boosters = 1ll << 54,
227  Upgrade_60 = 1ll << 60,
228  Unknow = 1ll << 62,
229  };
230  enum UpgradesLevel : uint64_t { // could be uint32_t
231  Terran_Infantry_Armor_2 = 1ll << 0,
232  Terran_Vehicle_Plating_2 = 1ll << 1,
233  Terran_Ship_Plating_2 = 1ll << 2,
234  Terran_Infantry_Weapons_2 = 1ll << 7,
235  Terran_Vehicle_Weapons_2 = 1ll << 8,
236  Terran_Ship_Weapons_2 = 1ll << 9,
237  Zerg_Carapace_2 = 1ll << 3,
238  Zerg_Flyer_Carapace_2 = 1ll << 4,
239  Protoss_Ground_Armor_2 = 1ll << 5,
240  Protoss_Air_Armor_2 = 1ll << 6,
241  Zerg_Melee_Attacks_2 = 1ll << 10,
242  Zerg_Missile_Attacks_2 = 1ll << 11,
243  Zerg_Flyer_Attacks_2 = 1ll << 12,
244  Protoss_Ground_Weapons_2 = 1ll << 13,
245  Protoss_Air_Weapons_2 = 1ll << 14,
246  Protoss_Plasma_Shields_2 = 1ll << 15,
247  Terran_Infantry_Armor_3 = 1ll << 16,
248  Terran_Vehicle_Plating_3 = 1ll << 17,
249  Terran_Ship_Plating_3 = 1ll << 18,
250  Terran_Infantry_Weapons_3 = 1ll << 23,
251  Terran_Vehicle_Weapons_3 = 1ll << 24,
252  Terran_Ship_Weapons_3 = 1ll << 25,
253  Zerg_Carapace_3 = 1ll << 19,
254  Zerg_Flyer_Carapace_3 = 1ll << 20,
255  Protoss_Ground_Armor_3 = 1ll << 21,
256  Protoss_Air_Armor_3 = 1ll << 22,
257  Zerg_Melee_Attacks_3 = 1ll << 26,
258  Zerg_Missile_Attacks_3 = 1ll << 27,
259  Zerg_Flyer_Attacks_3 = 1ll << 28,
260  Protoss_Ground_Weapons_3 = 1ll << 29,
261  Protoss_Air_Weapons_3 = 1ll << 30,
262  Protoss_Plasma_Shields_3 = 1ll << 31,
263  };
264  enum Techs : uint64_t {
265  Stim_Packs = 1ll << 0,
266  Lockdown = 1ll << 1,
267  EMP_Shockwave = 1ll << 2,
268  Spider_Mines = 1ll << 3,
269  Scanner_Sweep = 1ll << 4,
270  Tank_Siege_Mode = 1ll << 5,
271  Defensive_Matrix = 1ll << 6,
272  Irradiate = 1ll << 7,
273  Yamato_Gun = 1ll << 8,
274  Cloaking_Field = 1ll << 9,
275  Personnel_Cloaking = 1ll << 10,
276  Burrowing = 1ll << 11,
277  Infestation = 1ll << 12,
278  Spawn_Broodlings = 1ll << 13,
279  Dark_Swarm = 1ll << 14,
280  Plague = 1ll << 15,
281  Consume = 1ll << 16,
282  Ensnare = 1ll << 17,
283  Parasite = 1ll << 18,
284  Psionic_Storm = 1ll << 19,
285  Hallucination = 1ll << 20,
286  Recall = 1ll << 21,
287  Stasis_Field = 1ll << 22,
288  Archon_Warp = 1ll << 23,
289  Restoration = 1ll << 24,
290  Disruption_Web = 1ll << 25,
291  Unused_26 = 1ll << 26,
292  Mind_Control = 1ll << 27,
293  Dark_Archon_Meld = 1ll << 28,
294  Feedback = 1ll << 29,
295  Optical_Flare = 1ll << 30,
296  Maelstrom = 1ll << 31,
297  Lurker_Aspect = 1ll << 32,
298  Unused_33 = 1ll << 33,
299  Healing = 1ll << 34,
300  Nuclear_Strike = 1ll << 45,
301  Unknown = 1ll << 46,
302  };
303  // clang-format on
304 };
305 
306 class Frame : public RefCounted {
307  public:
308  // The keys of these hash tables are the players' ids.
309  std::unordered_map<int32_t, std::vector<Unit>> units;
310  std::unordered_map<int32_t, std::vector<Action>> actions;
311  std::unordered_map<int32_t, Resources> resources;
312  std::vector<Bullet> bullets;
313  std::vector<uint8_t> creep_map; // Do not access directly
314  uint32_t width, height;
315  int reward;
317 
318  Frame();
319  Frame(Frame&& o);
320  Frame(const Frame& o);
321  Frame(const Frame* o);
322  Frame& operator=(Frame other) noexcept;
323 
324  void swap(Frame& a, Frame& b);
325  void clear();
326  void filter(int32_t x, int32_t y, Frame& o) const;
327  void combine(const Frame& next_frame);
328  bool getCreepAt(uint32_t x, uint32_t y);
329 
330  flatbuffers::Offset<fbs::Frame> addToFlatBufferBuilder(flatbuffers::FlatBufferBuilder& builder) const;
331  void readFromFlatBufferTable(const fbs::Frame& table);
332 }; // class Frame
333 
334 // Frame diffs
335 class FrameDiff;
336 
337 namespace detail {
338  class UnitDiff {
339  public:
340  int id;
341  std::vector<int32_t> var_ids;
342  std::vector<int32_t> var_diffs;
343  std::vector<int32_t> order_ids;
344  std::vector<int32_t> order_diffs;
345  int32_t order_size;
346  double velocityX, velocityY;
347  int64_t flags;
348  };
349 
350  Frame* add(Frame* frame, FrameDiff* diff);
351  void add(Frame* res, Frame* frame, FrameDiff* diff);
352 
353  inline bool orderUnitByiD(const Unit& a, const Unit& b) {
354  return (a.id < b.id);
355  }
356 
357  bool frameEq(Frame* f1, Frame* f2, bool debug = true);
358 } // namespace detail
359 
360 class FrameDiff {
361  public:
362  std::vector<int32_t> pids;
363  std::vector<std::vector<detail::UnitDiff>> units;
364  // These are unlikely to be the same, so we just copy.
365  std::unordered_map<int32_t, std::vector<Action>> actions;
366  std::unordered_map<int32_t, Resources> resources;
367  std::vector<Bullet> bullets;
368  std::unordered_map<uint32_t, uint32_t> creep_map;
369  // Width and height never changes, so we don't diff them
370  int reward;
372 
373  flatbuffers::Offset<fbs::FrameDiff> addToFlatBufferBuilder(flatbuffers::FlatBufferBuilder& builder) const;
374  void readFromFlatBufferTable(const fbs::FrameDiff& fbsFrameDiff);
375 };
376 
377 void writeTail(
378  std::ostream& out,
379  const std::unordered_map<int32_t, std::vector<replayer::Action>>& actions,
380  const std::unordered_map<int32_t, replayer::Resources>& resources,
381  const std::vector<replayer::Bullet>& bullets);
382 
383 void readTail(
384  std::istream& in,
385  std::unordered_map<int32_t, std::vector<replayer::Action>>& actions,
386  std::unordered_map<int32_t, replayer::Resources>& resources,
387  std::vector<replayer::Bullet>& bullets);
388 
389 // These diffing functions will order the IDs of units in each frame, and thus
390 // is not const.
395 void frame_undiff(Frame* result, FrameDiff*, Frame*);
396 void frame_undiff(Frame* result, Frame*, FrameDiff*);
397 
398 } // namespace replayer
399 } // namespace torchcraft
int id
Definition: frame.h:340
int32_t visible
Definition: frame.h:86
std::vector< int32_t > order_diffs
Definition: frame.h:344
uint64_t upgrades_level
Definition: frame.h:172
int32_t y
Definition: frame.h:82
std::vector< int32_t > var_diffs
Definition: frame.h:342
Copyright (c) 2015-present, Facebook, Inc.
Definition: openbwprocess.h:17
Definition: frame.h:48
std::unordered_map< int32_t, std::vector< Unit > > units
Definition: frame.h:309
int32_t used_psi
Definition: frame.h:169
int32_t targetX
Definition: frame.h:71
int32_t groundDmgType
Definition: frame.h:91
int32_t remainingBuildTrainTime
Definition: frame.h:102
int32_t targetId
Definition: frame.h:70
int32_t pixel_size_y
Definition: frame.h:89
Definition: frame.h:306
int32_t spellCD
Definition: frame.h:104
Frame * add(Frame *frame, FrameDiff *diff)
int32_t shield
Definition: frame.h:83
int reward
Definition: frame.h:315
int reward
Definition: frame.h:370
Upgrades
Definition: frame.h:176
std::vector< int32_t > order_ids
Definition: frame.h:343
int32_t gas
Definition: frame.h:168
UnitCommand command
Definition: frame.h:95
uint64_t techs
Definition: frame.h:173
Definition: frame.h:54
int32_t type
Definition: frame.h:56
bool orderUnitByiD(const Unit &a, const Unit &b)
Definition: frame.h:353
int32_t pixel_y
Definition: frame.h:88
int is_terminal
Definition: frame.h:316
int32_t extra
Definition: frame.h:72
int32_t uid
Definition: frame.h:50
int32_t groundRange
Definition: frame.h:92
Definition: frame.h:166
replayer::Unit Unit
Definition: state.h:36
std::vector< int32_t > var_ids
Definition: frame.h:341
int32_t type
Definition: frame.h:87
Frame * frame_undiff(FrameDiff *, Frame *)
int32_t ore
Definition: frame.h:167
void readTail(std::istream &in, std::unordered_map< int32_t, std::vector< replayer::Action >> &actions, std::unordered_map< int32_t, replayer::Resources > &resources, std::vector< replayer::Bullet > &bullets)
std::unordered_map< int32_t, Resources > resources
Definition: frame.h:311
std::istream & operator>>(std::istream &in, Frame &o)
std::unordered_map< int32_t, std::vector< Action > > actions
Definition: frame.h:310
Definition: frame.h:81
int32_t targetY
Definition: frame.h:71
Definition: frame.h:360
int32_t aid
Definition: frame.h:51
int32_t playerId
Definition: frame.h:99
FrameDiff frame_diff(Frame &, Frame &)
int32_t frame
Definition: frame.h:68
std::ostream & operator<<(std::ostream &out, const Frame &o)
std::vector< Bullet > bullets
Definition: frame.h:312
replayer::Resources Resources
Definition: state.h:38
replayer::Frame Frame
Definition: state.h:41
int32_t targetX
Definition: frame.h:58
replayer::Bullet Bullet
Definition: state.h:39
int32_t resources
Definition: frame.h:100
int32_t maxCD
Definition: frame.h:84
bool frameEq(Frame *f1, Frame *f2, bool debug=true)
UpgradesLevel
Definition: frame.h:230
std::unordered_map< uint32_t, uint32_t > creep_map
Definition: frame.h:368
Copyright (c) 2015-present, Facebook, Inc.
Definition: refcount.h:23
std::vector< Bullet > bullets
Definition: frame.h:367
Techs
Definition: frame.h:264
bool operator==(const Order &o) const
Definition: frame.h:60
int32_t targetId
Definition: frame.h:57
int32_t type
Definition: frame.h:69
std::vector< int32_t > action
Definition: frame.h:49
std::vector< uint8_t > creep_map
Definition: frame.h:313
Flags
Definition: frame.h:108
int32_t id
Definition: frame.h:82
Definition: frame.h:44
int32_t y
Definition: frame.h:45
int32_t targetY
Definition: frame.h:58
int32_t first_frame
Definition: frame.h:55
std::vector< std::vector< detail::UnitDiff > > units
Definition: frame.h:363
bool operator==(const UnitCommand &c) const
Definition: frame.h:74
int32_t associatedUnit
Definition: frame.h:105
std::vector< int32_t > pids
Definition: frame.h:362
double velocityY
Definition: frame.h:346
uint64_t upgrades
Definition: frame.h:171
int32_t total_psi
Definition: frame.h:170
Definition: frame.h:67
std::vector< Order > orders
Definition: frame.h:94
int32_t buildTechUpgradeType
Definition: frame.h:101
int32_t order_size
Definition: frame.h:345
void writeTail(std::ostream &out, const std::unordered_map< int32_t, std::vector< replayer::Action >> &actions, const std::unordered_map< int32_t, replayer::Resources > &resources, const std::vector< replayer::Bullet > &bullets)
int32_t remainingUpgradeResearchTime
Definition: frame.h:103
std::unordered_map< int32_t, Resources > resources
Definition: frame.h:366
int64_t flags
Definition: frame.h:347
uint64_t flags
Definition: frame.h:85
uint32_t width
Definition: frame.h:314
int is_terminal
Definition: frame.h:371
int32_t groundATK
Definition: frame.h:90
replayer::Action Action
Definition: state.h:40
std::unordered_map< int32_t, std::vector< Action > > actions
Definition: frame.h:365
double velocityY
Definition: frame.h:97
int32_t associatedCount
Definition: frame.h:106