TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
once.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 "blackboard.h"
11 #include "gameutils/scenariospecification.h"
12 #include "modules/lambda.h"
13 #include "state.h"
14 
15 namespace cherrypi {
16 
17 /**
18  * A simple utility module that runs a user-supplied function once per game.
19  */
20 class OnceModule : public LambdaModule {
21  public:
22  OnceModule(StepFunctionState func, std::string name = std::string());
23  OnceModule(StepFunctionStateModule func, std::string name = std::string());
24  virtual ~OnceModule() = default;
25 
26  /// Spawns ally units.
27  static std::shared_ptr<Module> makeWithSpawns(
28  const std::vector<SpawnPosition>& spawns,
29  std::string name = std::string());
30 
31  /// Similar function for enemy units
32  static std::shared_ptr<Module> makeWithEnemySpawns(
33  const std::vector<SpawnPosition>& spawns,
34  std::string name = std::string());
35 
36  virtual void step(State* state) override;
37 
38  /// Returns a list of commands which spawn units
39  static std::vector<tc::Client::Command> makeSpawnCommands(
40  const std::vector<SpawnPosition>& spawns,
41  State* state,
42  int playerId);
43 
44  protected:
45  /// Convenience function that returns a lambda to spawn units
46  /// If enemy is true, then the units are spawned for the enemy
48  const std::vector<SpawnPosition>& spawns,
49  bool enemy);
50 
51  private:
52  std::string key_;
53 };
54 
55 } // namespace cherrypi
Game state.
Definition: state.h:42
virtual ~OnceModule()=default
virtual void step(State *state) override
Definition: once.cpp:24
static std::vector< tc::Client::Command > makeSpawnCommands(const std::vector< SpawnPosition > &spawns, State *state, int playerId)
Returns a list of commands which spawn units.
Definition: once.cpp:41
std::string name()
Definition: module.cpp:41
static std::shared_ptr< Module > makeWithEnemySpawns(const std::vector< SpawnPosition > &spawns, std::string name=std::string())
Similar function for enemy units.
Definition: once.cpp:81
static LambdaModule::StepFunctionState makeSpawnFn(const std::vector< SpawnPosition > &spawns, bool enemy)
Convenience function that returns a lambda to spawn units If enemy is true, then the units are spawne...
Definition: once.cpp:62
Lets you construct a lightweight module by providing your own step() as an std::function.
Definition: lambda.h:20
A simple utility module that runs a user-supplied function once per game.
Definition: once.h:20
std::function< void(State *, Module *)> StepFunctionStateModule
Definition: lambda.h:23
Main namespace for bot-related code.
Definition: areainfo.cpp:17
std::function< void(State *)> StepFunctionState
Definition: lambda.h:22
OnceModule(StepFunctionState func, std::string name=std::string())
Definition: once.cpp:14
static std::shared_ptr< Module > makeWithSpawns(const std::vector< SpawnPosition > &spawns, std::string name=std::string())
Spawns ally units.
Definition: once.cpp:74