TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
snapshotter.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 <cereal/archives/binary.hpp>
11 
12 #include "fmt/format.h"
13 #include "gameutils/scenariospecification.h"
14 #include "module.h"
15 #include "state.h"
16 
17 namespace cherrypi {
18 
19 /// A low-resolution snapshot of a unit state, used as a component of Snapshots.
20 struct SnapshotUnit {
21  int type;
22  int x;
23  int y;
24  int health;
25  int shields;
26  int energy;
27  // It would be nice to also capture [cooldown, angle, velocity] but we can't
28  // modify those properties via OpenBW
29 
30  template <class Archive>
31  void serialize(Archive& ar) {
32  ar(type, x, y, health, shields, energy);
33  }
34 };
35 
36 /// A low-resolution snapshot of a player state, used as a component of
37 /// Snapshots.
39  // Upgrade/Tech serialization format taken from
40  // torchcraft::replayer::Frame::Resourcs
41  int64_t upgrades;
42  int64_t upgradeLevels;
43  int64_t techs;
44  std::vector<SnapshotUnit> units;
45 
46  template <class Archive>
47  void serialize(Archive& ar) {
48  ar(upgradeLevels, techs, units);
49  }
50 
51  /// Convenience method for accessing TC-formatted upgradeLevels.
52  /// @param upgradeId
53  /// BWAPI/buildtype->upgrade ID of the upgrade to retrieve
54  int getUpgradeLevel(int upgradeId) const;
55 
56  /// Convenience method for accessing TC-formatted techs.
57  /// @param upgradeId
58  /// BWAPI/buildtype->tech ID of the tech to retrieve
59  bool hasTech(int techId) const;
60 };
61 
62 /// A low-resolution snapshot of a game state,
63 /// intended for producing micro training scenarios
64 struct Snapshot {
65  std::vector<SnapshotPlayer> players = {{}, {}};
68  std::string mapTitle;
69 
70  template <class Archive>
71  void serialize(Archive& ar) {
72  ar(players);
73  }
74 };
75 
78 void saveSnapshot(const Snapshot& snapshot, const std::string& path);
79 Snapshot loadSnapshot(const std::string& path);
80 
81 /// Records "snapshots" -- low-fidelity recordings of game state which can be
82 /// loaded as micro scenarios.
83 class Snapshotter {
84  public:
85  /// Is the current game state appropriate for taking a snapshot?
87  return false;
88  };
89 
90  /// Minimum number of frames in between taking snapshots
91  int cooldownFramesMax = 24 * 10;
92 
93  /// Stop snapshotting a game after this many snapshots have been taken
94  int maxSnapshots = 20;
95 
96  virtual std::string outputDirectory();
97  std::string snapshotName = "snapshot";
98 
99  void step(torchcraft::State*);
100 
101  protected:
102  int lastFrame_ = 0;
103  int cooldownFrames_ = 0;
104  int snapshots_ = 0;
105 };
106 
107 } // namespace cherrypi
void serialize(Archive &ar)
Definition: snapshotter.h:31
A low-resolution snapshot of a player state, used as a component of Snapshots.
Definition: snapshotter.h:38
int mapBuildTileHeight
Definition: snapshotter.h:67
std::vector< SnapshotUnit > units
Definition: snapshotter.h:44
Records "snapshots" – low-fidelity recordings of game state which can be loaded as micro scenarios...
Definition: snapshotter.h:83
std::string mapTitle
Definition: snapshotter.h:68
int64_t upgrades
Definition: snapshotter.h:41
int y
Definition: snapshotter.h:23
int x
Definition: snapshotter.h:22
int64_t upgradeLevels
Definition: snapshotter.h:42
void saveSnapshot(const Snapshot &snapshot, const std::string &path)
Definition: snapshotter.cpp:164
bool hasTech(const BuildState &st, const BuildType *type)
Returns true if we have this tech in this BuildState.
Definition: autobuild.cpp:73
int health
Definition: snapshotter.h:24
A low-resolution snapshot of a game state, intended for producing micro training scenarios.
Definition: snapshotter.h:64
Definition: state.h:43
FixedScenario snapshotToScenario(const Snapshot &snapshot)
Definition: snapshotter.cpp:13
int mapBuildTileWidth
Definition: snapshotter.h:66
void serialize(Archive &ar)
Definition: snapshotter.h:47
Definition: scenariospecification.h:57
virtual bool isCameraReady(torchcraft::State *)
Is the current game state appropriate for taking a snapshot?
Definition: snapshotter.h:86
Main namespace for bot-related code.
Definition: areainfo.cpp:17
A low-resolution snapshot of a unit state, used as a component of Snapshots.
Definition: snapshotter.h:20
int64_t techs
Definition: snapshotter.h:43
Snapshot stateToSnapshot(torchcraft::State *state)
Definition: snapshotter.cpp:77
Snapshot loadSnapshot(const std::string &path)
Definition: snapshotter.cpp:170
void serialize(Archive &ar)
Definition: snapshotter.h:71
int energy
Definition: snapshotter.h:26
int type
Definition: snapshotter.h:21
int shields
Definition: snapshotter.h:25