TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
strategy.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 <functional>
11 #include <list>
12 
13 #include "autobuild.h"
14 #include "cherrypi.h"
15 #include "models/bandit.h"
16 #include "module.h"
17 #include "state.h"
18 
19 #include "models/bos/runner.h"
20 
21 #include <common/flags.h>
22 
23 // When turned off, no game history will be read or written
24 DECLARE_bool(game_history);
25 
26 namespace cherrypi {
27 
28 class StrategyModule : public Module {
29  public:
30  enum Duty {
31  None = 0,
32  BuildOrder = 1 << 0,
33  Scouting = 1 << 1,
34  Harassment = 1 << 2,
35  All = 0xFFFF,
36  };
37 
38  StrategyModule(Duty duties = Duty::All);
39  virtual ~StrategyModule() = default;
40 
41  virtual void step(State* state) override;
42  virtual void onGameStart(State* s) override;
43  virtual void onGameEnd(State* s) override;
44 
45  protected:
46  /**
47  * Chooses the build order.
48  */
49  virtual void stepBuildOrder(State* state);
50  virtual void stepScouting(State* state);
51  virtual void stepHarassment(State* state);
52 
53  std::shared_ptr<ProxyTask> getProxyTaskWithCommand(
54  State* state,
55  Command command);
56 
57  std::string currentBuildOrder_;
58 
59  std::string getOpeningBuildOrder(State* s);
61  State* state,
62  UpcId originUpcId,
63  std::string const& buildorder);
64 
65  private:
66  /**
67  * Selects which initial build order to use,
68  * using either the default set of builds, or FLAGS_build.
69  * Uses multi-armed bandit selection to pick the build we think most likely
70  * to beat our current opponent.
71  */
72  std::string selectBO(
73  State* state,
74  tc::BW::Race ourRace,
75  tc::BW::Race enemyRace,
76  const std::string& mapName,
77  const std::string& enemyName);
78  std::unique_ptr<bos::ModelRunner> makeBosRunner(State* state);
79  std::string stepBos(State* state);
80  bool shouldListenToBos(State* state);
81 
82  Duty duties_;
83  int nbScoutingOverlords_ = 0;
84  int nbScoutingExplorers_ = 0;
85  int nbScoutingWorkers_ = 0;
86 
87  std::unique_ptr<bos::ModelRunner> bosRunner_ = nullptr;
88  FrameNum nextBosForwardFrame_ = 0;
89  float bosStartTime_ = 0;
90  bool bosMapVerified_ = false;
91 };
92 
94 
95 } // namespace cherrypi
Game state.
Definition: state.h:42
virtual ~StrategyModule()=default
virtual void step(State *state) override
Definition: strategy.cpp:101
std::string getOpeningBuildOrder(State *s)
Definition: strategy.cpp:351
int FrameNum
Definition: basetypes.h:22
Command
Abstract "meta" commands for UPCTuples.
Definition: basetypes.h:314
Definition: strategy.h:33
DEFINE_FLAG_OPERATORS(StrategyModule::Duty)
Duty
Definition: strategy.h:30
virtual void stepScouting(State *state)
Definition: strategy.cpp:164
std::string currentBuildOrder_
Definition: strategy.h:57
Definition: strategy.h:31
std::shared_ptr< ProxyTask > getProxyTaskWithCommand(State *state, Command command)
Definition: strategy.cpp:649
Definition: strategy.h:34
Definition: strategy.h:35
virtual void stepHarassment(State *state)
Definition: strategy.cpp:249
Definition: strategy.h:32
virtual void onGameEnd(State *s) override
Definition: strategy.cpp:382
Definition: strategy.h:28
virtual void onGameStart(State *s) override
Definition: strategy.cpp:316
void spawnBuildOrderTask(State *state, UpcId originUpcId, std::string const &buildorder)
Definition: strategy.cpp:141
StrategyModule(Duty duties=Duty::All)
Definition: strategy.cpp:99
Main namespace for bot-related code.
Definition: areainfo.cpp:17
int UpcId
Definition: basetypes.h:23
virtual void stepBuildOrder(State *state)
Chooses the build order.
Definition: strategy.cpp:113
Interface for bot modules.
Definition: module.h:30