TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
scenarioprovider.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 "game.h"
11 #include "modules/once.h"
12 #include "scenariospecification.h"
13 #include "state.h"
14 
15 namespace cherrypi {
16 
17 /**
18  * Base class for providing scenarios.
19  * Returns a pair of players to be used by the training code.
20  *
21  * Detects game end and cleans up after the scenario.
22  */
24  public:
25  virtual ~ScenarioProvider() = default;
26 
27  /// The maximum number of steps to play before considering a scenario finished
28  /// Defaults to -1, which indicates no maximum.
30  maxFrame_ = value;
31  return *this;
32  }
33 
34  /// Specifies whether to run OpenBW headfully.
35  /// Takes effect only when spawning a new OpenBWProcess; so generally you want
36  /// to invoke this before using the ScenarioProvider.
37  ScenarioProvider& setGui(bool value) {
38  gui_ = value;
39  return *this;
40  }
41 
42  /// Spawns a scenario. It takes as parameters the setup functions for both
43  /// players (this should take care of adding modules), and returns the
44  /// pointers to the created players
45  virtual std::pair<std::shared_ptr<BasePlayer>, std::shared_ptr<BasePlayer>>
47  const std::function<void(BasePlayer*)>& setup1,
48  const std::function<void(BasePlayer*)>& setup2) = 0;
49 
50  /// Check whether the scenario is finished.
51  /// By default, return true whenever the number of frames is exceeded or one
52  /// of the players don't have any units left
53  virtual bool isFinished(int currentStep);
54 
55  protected:
56  template <typename TPlayer>
57  void loadMap(
58  std::string const& map,
59  tc::BW::Race race1,
60  tc::BW::Race race2,
61  GameType gameType,
62  std::string const& replayPath = std::string()) {
63  game_ = std::make_shared<GameMultiPlayer>(
64  map, race1, race2, gameType, replayPath, gui_);
65  player1_ = std::make_shared<TPlayer>(game_->makeClient1());
66  player2_ = std::make_shared<TPlayer>(game_->makeClient2());
67  };
68 
69  // Two hours, which is longer than any reasonable game but short enough to
70  // finish in reasonable time if a scenario goes off the rails.
71  int maxFrame_ = 24 * 60 * 60 * 2;
72  bool gui_ = false;
73 
74  std::shared_ptr<BasePlayer> player1_;
75  std::shared_ptr<BasePlayer> player2_;
76  std::shared_ptr<GameMultiPlayer> game_;
77 };
78 
79 } // namespace cherrypi
GameType
Definition: gametype.h:12
ScenarioProvider & setMaxFrames(int value)
The maximum number of steps to play before considering a scenario finished Defaults to -1...
Definition: scenarioprovider.h:29
std::shared_ptr< BasePlayer > player2_
Definition: scenarioprovider.h:75
bool gui_
Definition: scenarioprovider.h:72
The main bot object.
Definition: baseplayer.h:28
virtual std::pair< std::shared_ptr< BasePlayer >, std::shared_ptr< BasePlayer > > startNewScenario(const std::function< void(BasePlayer *)> &setup1, const std::function< void(BasePlayer *)> &setup2)=0
Spawns a scenario.
std::shared_ptr< GameMultiPlayer > game_
Definition: scenarioprovider.h:76
virtual ~ScenarioProvider()=default
void loadMap(std::string const &map, tc::BW::Race race1, tc::BW::Race race2, GameType gameType, std::string const &replayPath=std::string())
Definition: scenarioprovider.h:57
Base class for providing scenarios.
Definition: scenarioprovider.h:23
Main namespace for bot-related code.
Definition: areainfo.cpp:17
int maxFrame_
Definition: scenarioprovider.h:71
std::shared_ptr< BasePlayer > player1_
Definition: scenarioprovider.h:74
ScenarioProvider & setGui(bool value)
Specifies whether to run OpenBW headfully.
Definition: scenarioprovider.h:37
virtual bool isFinished(int currentStep)
Check whether the scenario is finished.
Definition: scenarioprovider.cpp:13