TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
state.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 <map>
13 #include <set>
14 #include <string>
15 #include <vector>
16 
17 #include <torchcraft/constants.h>
18 #include <torchcraft/frame.h>
19 #include <torchcraft/refcount.h>
20 
21 // Flatbuffer messages
22 namespace torchcraft {
23 namespace fbs {
24 struct HandshakeServer;
25 struct StateUpdate;
26 struct EndGame;
27 struct PlayerLeft;
28 struct Error;
29 enum class FrameOrFrameDiff : uint8_t;
30 } // namespace fbs
31 } // namespace torchcraft
32 
33 namespace torchcraft {
34 
35 // Aliases for basic replayer types provided for convenience
42 
43 class State : public RefCounted {
44  public:
45  struct Position {
46  int x, y;
47  Position() : x(0), y(0) {}
48  Position(int x, int y) : x(x), y(y) {}
49  };
50 
51  struct PlayerInfo {
52  int id;
53  BW::Race race = BW::Race::Unknown;
54  std::string name;
55  bool is_enemy;
56  bool has_left;
57  };
58 
59  // setup
60  int lag_frames; // number of frames from order to execution
61  int map_size[2]; // map size in walk tiles
62  std::vector<uint8_t> ground_height_data; // 2D, walk tile resolution
63  std::vector<uint8_t> walkable_data; // 2D, walk tile resolution
64  std::vector<uint8_t> buildable_data; // 2D, walk tile resolution
65  std::string map_name; // File name of the current map or replay; derives from BWAPI::Game::mapFileName()
66  std::string map_title; // Name embedded into the current map; derives from BWAPI::Game::mapName()
67  std::vector<Position> start_locations;
68  std::map<int, PlayerInfo> player_info;
69  int player_id;
71  bool replay;
72 
73  // game state
74  Frame* frame; // this will allow for easy reset (XXX)
75  std::vector<int> deaths;
77  int battle_frame_count; // if micro mode
78 
79  bool game_ended; // did the game end?
80  bool game_won;
81 
82  // if micro mode
84  bool battle_won;
87 
88  // if with image
89  std::string img_mode;
90  int screen_position[2]; // position of screen {x, y} in pixels. {0, 0} is
91  // top-left
92  std::vector<uint8_t> visibility;
93  int visibility_size[2];
94  std::vector<uint8_t> image; // RGB
95  int image_size[2];
96 
97  // Alive units in this frame. Used to detect end-of-battle in micro mode. If
98  // the current frame is the end of a battle, this will contain all units that
99  // were alive when the battle ended (which is not necessarily the current
100  // frame due to frame skipping on the serv side). Note that this map ignores
101  // onlyConsiderUnits_.
102  // Maps unit id to player id
103  std::unordered_map<int32_t, int32_t> aliveUnits;
104 
105  // Like aliveUnits, but containing only units of types in onlyConsiderUnits.
106  // If onlyConsiderUnits is empty, this map is invalid.
107  std::unordered_map<int32_t, int32_t> aliveUnitsConsidered;
108 
109  // Bots might want to use this map instead of frame->units because:
110  // - Unknown unit types are not present (e.g. map revealers)
111  // - Units reported as dead are not present (important if the server performs
112  // frame skipping. In that case, frame->units will still contain all units
113  // that have died since the last update.
114  // - In micro mode and with frame skipping, deaths are only applied until the
115  // battle is considered finished, i.e. it corresponds to aliveUnits.
116  std::unordered_map<int32_t, std::vector<Unit>> units;
117 
118  // Total number of updates received since creation (resets are counted as
119  // well).
120  uint64_t numUpdates = 0;
121 
122  explicit State(
123  bool microBattles = false,
124  std::set<BW::UnitType> onlyConsiderTypes = std::set<BW::UnitType>());
125  State(const State& other);
126  State(State&& other);
127  ~State();
128  State& operator=(State other);
129  friend void swap(State& a, State& b);
130 
131  bool microBattles() const {
132  return microBattles_;
133  }
134  const std::set<BW::UnitType>& onlyConsiderTypes() const {
135  return onlyConsiderTypes_;
136  }
137  void setMicroBattles(bool microBattles) {
138  microBattles_ = microBattles;
139  }
140  void setOnlyConsiderTypes(std::set<BW::UnitType> types) {
141  onlyConsiderTypes_ = std::move(types);
142  aliveUnitsConsidered.clear();
143  }
144 
145  void reset();
146  std::vector<std::string> update(const fbs::HandshakeServer* handshake);
147  std::vector<std::string> update(const fbs::StateUpdate* stateUpdate);
148  std::vector<std::string> update(const fbs::EndGame* end);
149  std::vector<std::string> update(const fbs::PlayerLeft* left);
150  void trackAliveUnits(
151  std::vector<std::string>& upd,
152  const std::set<BW::UnitType>& considered);
153 
154  int getUpgradeLevel(BW::UpgradeType ut) {
155  if (!(frame->resources[player_id].upgrades & (1ll << ut))) {
156  return 0;
157  }
158  auto constexpr NB_LVLABLE_UPGRADES = 16;
159  if (ut >= NB_LVLABLE_UPGRADES) {
160  return 1;
161  }
162  uint64_t lvls = frame->resources[player_id].upgrades_level;
163  if (lvls & (1ll << ut)) {
164  return 2;
165  }
166  if (lvls & (1ll << (ut + NB_LVLABLE_UPGRADES))) {
167  return 3;
168  }
169  return 1;
170  }
171 
172  bool hasResearched(BW::TechType tt) {
173  if (frame->resources[player_id].techs & (1ll << tt)) {
174  return true;
175  }
176  return false;
177  }
178 
179  private:
180  bool setRawImage(const fbs::StateUpdate* frame);
181  void preUpdate();
182  void postUpdate(std::vector<std::string>& upd);
183  bool checkBattleFinished(std::vector<std::string>& upd);
184  bool update_frame(const void* flatBuffer, const fbs::FrameOrFrameDiff type);
185 
186  bool microBattles_;
187  std::set<BW::UnitType> onlyConsiderTypes_;
188 };
189 
190 } // namespace torchcraft
std::vector< uint8_t > image
Definition: state.h:94
int getUpgradeLevel(BW::UpgradeType ut)
Definition: state.h:154
Copyright (c) 2015-present, Facebook, Inc.
Definition: openbwprocess.h:17
Definition: frame.h:48
std::vector< uint8_t > walkable_data
Definition: state.h:63
bool has_left
Definition: state.h:56
std::string name
Definition: state.h:54
int lag_frames
Definition: state.h:60
std::string map_title
Definition: state.h:66
int frame_from_bwapi
Definition: state.h:76
Definition: frame.h:306
bool game_ended
Definition: state.h:79
std::map< int, PlayerInfo > player_info
Definition: state.h:68
bool game_won
Definition: state.h:80
std::vector< Position > start_locations
Definition: state.h:67
std::unordered_map< int32_t, int32_t > aliveUnits
Definition: state.h:103
Definition: frame.h:54
std::vector< uint8_t > ground_height_data
Definition: state.h:62
bool microBattles() const
Definition: state.h:131
Definition: frame.h:166
replayer::Unit Unit
Definition: state.h:36
Position()
Definition: state.h:47
std::unordered_map< int32_t, Resources > resources
Definition: frame.h:311
std::string img_mode
Definition: state.h:89
bool hasResearched(BW::TechType tt)
Definition: state.h:172
Definition: frame.h:81
Definition: state.h:51
Frame * frame
Definition: state.h:74
int player_id
Definition: state.h:69
replayer::Order Order
Definition: state.h:37
replayer::Frame Frame
Definition: state.h:41
replayer::Bullet Bullet
Definition: state.h:39
void setOnlyConsiderTypes(std::set< BW::UnitType > types)
Definition: state.h:140
bool replay
Definition: state.h:71
Copyright (c) 2015-present, Facebook, Inc.
Definition: refcount.h:23
bool waiting_for_restart
Definition: state.h:85
std::vector< uint8_t > visibility
Definition: state.h:92
int y
Definition: state.h:46
int battle_frame_count
Definition: state.h:77
Definition: state.h:43
std::string map_name
Definition: state.h:65
Definition: frame.h:44
bool is_enemy
Definition: state.h:55
std::unordered_map< int32_t, int32_t > aliveUnitsConsidered
Definition: state.h:107
std::vector< int > deaths
Definition: state.h:75
int id
Definition: state.h:52
bool battle_just_ended
Definition: state.h:83
int last_battle_ended
Definition: state.h:86
std::vector< uint8_t > buildable_data
Definition: state.h:64
Definition: state.h:45
bool battle_won
Definition: state.h:84
std::unordered_map< int32_t, std::vector< Unit > > units
Definition: state.h:116
Position(int x, int y)
Definition: state.h:48
const std::set< BW::UnitType > & onlyConsiderTypes() const
Definition: state.h:134
void setMicroBattles(bool microBattles)
Definition: state.h:137
replayer::Action Action
Definition: state.h:40
int neutral_id
Definition: state.h:70