TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
state.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <list>
11 
12 #include "areainfo.h"
13 #include "blackboard.h"
14 #include "cherrypi.h"
15 #include "tilesinfo.h"
16 #include "tracker.h"
17 #include "unitsinfo.h"
18 
19 #include <chrono>
20 #include <utility>
21 
22 namespace BWEM {
23 class Map;
24 }
25 namespace tcbwapi {
26 class TCGame;
27 }
28 
29 namespace cherrypi {
30 
31 /// Type to represent upgrade level values
32 typedef int UpgradeLevel;
33 
34 /**
35  * Game state.
36  *
37  * The game state serves as the main input and output for bot modules. It
38  * provides a global (player-wide) blackboard and access to the
39  * TorchCraft::State object in the form of wrapper functions and data
40  * structures.
41  */
42 class State {
43  public:
44  explicit State(std::shared_ptr<tc::Client> client);
45  State(State const&) = delete;
46  virtual ~State();
47 
48  // Don't keep unit pointers around. They'll be invalidated on torchcraft state
49  // updates.
50  std::unordered_map<int32_t, tc::Unit*> const& units() const {
51  return units_;
52  }
53  tc::Unit* unit(int32_t id) const {
54  return units_.find(id) == units_.end() ? nullptr : units_.at(id);
55  }
56 
58  return currentFrame_;
59  }
60 
61  /// Current game time in seconds, assuming "fastest" speed
62  float currentGameTime() const {
63  return (currentFrame_ * 42.0f) / 1000;
64  }
65 
67  return tcstate_->lag_frames;
68  }
69 
70  PlayerId playerId() const {
71  return playerId_;
72  }
73 
74  PlayerId neutralId() const {
75  return neutralId_;
76  }
77 
78  /// For replays, treat units from this player as allied units.
79  void setPerspective(PlayerId id);
80 
81  int mapWidth() const {
82  return mapWidth_;
83  }
84  int mapHeight() const {
85  return mapHeight_;
86  }
87  const std::string mapName() const {
88  return tcstate_->map_name;
89  }
90  const std::string mapTitle() const {
91  return tcstate_->map_title;
92  }
93  Rect mapRect() const {
94  return Rect(0, 0, mapWidth_, mapHeight_);
95  }
96 
97  tc::Resources resources() const;
98 
99  Blackboard* board() const {
100  return board_;
101  }
102  BWEM::Map* map() const {
103  return map_.get();
104  }
105 
106  template <typename T, typename... Args>
107  std::shared_ptr<T> addTracker(Args&&... args) {
108  auto tracker = std::make_shared<T>(std::forward<Args>(args)...);
109  trackers_.emplace_back(tracker);
110  return tracker;
111  }
112  std::list<std::shared_ptr<Tracker>> const& trackers() const {
113  return trackers_;
114  }
115 
117  return unitsInfo_;
118  }
120  return tilesInfo_;
121  }
123  return areaInfo_;
124  }
125 
126  std::vector<std::pair<std::string, std::chrono::milliseconds>>
128  std::vector<std::pair<std::string, std::chrono::milliseconds>>
129  stateUpdateTimes;
130  for (auto nameTime : stateUpdateTimeSpent_) {
131  auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
132  nameTime.second);
133  stateUpdateTimes.push_back(std::make_pair(nameTime.first, ms));
134  }
135  return stateUpdateTimes;
136  }
137 
138  /// Get the current level of a given upgrade
139  /// @param upgrade Upgrade for which to check the level
140  /// @return Level of a given upgrade
141  UpgradeLevel getUpgradeLevel(const BuildType* upgrade) const;
142 
143  /// Check whether a given technology has been researched
144  /// @param tech Technology to check for
145  /// @return true, if the technology has been researched
146  bool hasResearched(const BuildType* tech) const;
147 
148  /// Updates internal mappings after the torchcraft state has been updated.
149  void update();
150 
151  /// Get my race as reported by the game
152  tc::BW::Race myRace() const;
153 
154  /// Get the player id of the first opponent
155  PlayerId firstOpponent() const;
156 
157  /// Get the race that the game returns, for a given player
158  tc::BW::Race raceFromClient(PlayerId playerId) const;
159 
160  /// Returns true if the game has ended.
161  /// This will be the case if either player doesn't control any units for a
162  /// some frames or if TorchCraft signals that the game has ended.
163  bool gameEnded() const;
164  bool won() const;
165  bool lost() const;
166 
167  /// The underlying TorchCraft state.
168  /// It's recommended to access the game state via the other functions of this
169  /// class instead.
171  return tcstate_;
172  }
173 
174  void setCollectTimers(bool collect);
175 
177  return &unitsInfo_;
178  }
180  return &tilesInfo_;
181  }
183  return &areaInfo_;
184  }
185 
186  void setMapHack(bool h) {
187  mapHack_ = h;
188  }
189  bool mapHack() {
190  return mapHack_;
191  }
192 
193  private:
194  typedef std::unordered_map<int, bool> Tech2StatusMap;
195  typedef std::unordered_map<int, UpgradeLevel> Upgrade2LevelMap;
196 
197  void initTechnologyStatus();
198  void initUpgradeStatus();
199  void updateBWEM();
200  void updateTechnologyStatus();
201  void updateUpgradeStatus();
202  void updateTrackers();
203  void updateFirstToLeave();
204  void findEnemyInfo();
205 
206  std::shared_ptr<tc::Client> client_;
207  tc::State* tcstate_;
208  std::unordered_map<int32_t, tc::Unit*> units_;
209 
210  std::unique_ptr<BWEM::Map> map_;
211  std::unique_ptr<tcbwapi::TCGame> tcbGame_;
212 
213  Blackboard* board_;
214  std::list<std::shared_ptr<Tracker>> trackers_;
215 
216  FrameNum currentFrame_ = 0;
217  PlayerId playerId_ = 0;
218  PlayerId neutralId_ = 0;
219  PlayerId firstToLeave_ = -1;
220  int mapWidth_ = 0;
221  int mapHeight_ = 0;
222 
223  UnitsInfo unitsInfo_{this};
224  TilesInfo tilesInfo_{this};
225  AreaInfo areaInfo_{this};
226 
227  bool sawFirstEnemyUnit_ = false;
228  bool collectTimers_ = false;
229 
230  Tech2StatusMap tech2StatusMap_;
231  Upgrade2LevelMap upgrade2LevelMap_;
232 
233  std::vector<std::pair<std::string, Duration>> stateUpdateTimeSpent_;
234 
235  bool mapHack_ = false;
236 };
237 
238 } // namespace cherrypi
Game state.
Definition: state.h:42
AreaInfo * areaInfoPtr()
Definition: state.h:182
int FrameNum
Definition: basetypes.h:22
PlayerId playerId() const
Definition: state.h:70
TilesInfo * tilesInfoPtr()
Definition: state.h:179
Rect mapRect() const
Definition: state.h:93
FrameNum currentFrame() const
Definition: state.h:57
int mapHeight() const
Definition: state.h:84
UnitsInfo * unitsInfoPtr()
Definition: state.h:176
AreaInfo & areaInfo()
Definition: state.h:122
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
Definition: tcgame.h:17
std::vector< std::pair< std::string, std::chrono::milliseconds > > getStateUpdateTimes() const
Definition: state.h:127
std::list< std::shared_ptr< Tracker > > const & trackers() const
Definition: state.h:112
Definition: state.h:25
UnitsInfo & unitsInfo()
Definition: state.h:116
Definition: frame.h:166
std::shared_ptr< T > addTracker(Args &&...args)
Definition: state.h:107
PlayerId neutralId() const
Definition: state.h:74
Definition: frame.h:81
Map features from StaticData.
Updates and organizes information about all the units in the game.
Definition: unitsinfo.h:294
int mapWidth() const
Definition: state.h:81
const std::string mapName() const
Definition: state.h:87
tc::State * tcstate()
The underlying TorchCraft state.
Definition: state.h:170
Blackboard * board() const
Definition: state.h:99
Definition: state.h:43
An access-aware blackboard.
Definition: blackboard.h:88
int UpgradeLevel
Type to represent upgrade level values.
Definition: state.h:32
TilesInfo & tilesInfo()
Definition: state.h:119
Manages and updates per-tile data.
Definition: tilesinfo.h:81
tc::Unit * unit(int32_t id) const
Definition: state.h:53
const std::string mapTitle() const
Definition: state.h:90
float currentGameTime() const
Current game time in seconds, assuming "fastest" speed.
Definition: state.h:62
FrameNum latencyFrames() const
Definition: state.h:66
int PlayerId
Definition: basetypes.h:21
Definition: areainfo.h:17
void setMapHack(bool h)
Definition: state.h:186
std::unordered_map< int32_t, tc::Unit * > const & units() const
Definition: state.h:50
Main namespace for bot-related code.
Definition: areainfo.cpp:17
Access point for area and base information.
Definition: areainfo.h:112
bool mapHack()
Definition: state.h:189
BWEM::Map * map() const
Definition: state.h:102