TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
builderc.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 "controller.h"
11 
12 #include "modules/builder.h"
13 
14 namespace cherrypi {
15 
16 /**
17  * Base class for controllers used by BuilderModule.
18  *
19  * Builder-related tasks will select a unit when they are ready for production
20  * or construction and not upon creation. Hence, relevant controllers will
21  * advertise the units they're using via currentUnits(), and the module that
22  * uses them will need to make sure that proper player-wide allocation is done
23  * after calling step().
24  */
26  public:
28  Module* module,
29  BuildType const* type,
30  std::unordered_map<Unit*, float> unitProbs,
31  std::shared_ptr<BuilderControllerData> bcdata);
32  virtual ~BuilderControllerBase() = default;
33  virtual const char* getName() const override {
34  return "Builder";
35  };
36 
37  protected:
38  virtual bool didSucceed() const override {
39  return succeeded_;
40  }
41  virtual bool didFail() const override {
42  return failed_;
43  }
44 
45  void grabUnit(State* state, Unit* unit);
46  void releaseUnit(State* state, Unit* unit);
47 
48  bool findBuilder(State* state, Position const& pos = Position());
49 
50  auto defaultUnitBuilderScore(State* state);
51  auto larvaBuilderScore(State* state, bool preferSaturation);
52  auto hatcheryTechBuilderScore(State* state);
53 
54  bool cancelled(State* state) const;
55 
56  public:
57  float priority() {
58  return priority_;
59  }
60  void setPriority(float value) {
61  priority_ = value;
62  }
63  BuildType const* type() {
64  return type_;
65  }
66 
67  protected:
68  BuildType const* type_;
69  Unit* builder_ = nullptr;
70  std::unordered_map<Unit*, float> unitProbs_;
71  std::shared_ptr<BuilderControllerData> bcdata_;
72  bool succeeded_ = false;
73  bool failed_ = false;
74  float priority_;
75 };
76 
77 /**
78  * A unit production controller for units that require a worker.
79  *
80  * This is used by BuilderModule.
81  * It is assumed that every type that this controller should produce is a
82  * building.
83  */
85  public:
87  Module* module,
88  BuildType const* type,
89  std::unordered_map<Unit*, float> unitProbs,
90  std::shared_ptr<BuilderControllerData> bcdata,
91  Position pos);
92  virtual ~WorkerBuilderController() = default;
93 
94  virtual void step(State* state) override;
95  virtual void removeUnit(State* state, Unit* unit, UpcId id) override;
96  virtual const char* getName() const override {
97  return "WorkerBuilder";
98  };
99 
100  protected:
101  std::string logPrefix() const;
102 
103  private:
104  Position pos_;
105  Unit* detector_ = nullptr;
106  int lastUpdate_ = 0;
107  bool constructionStarted_ = false;
108  int lastCheckLocation_ = 0;
109  int lastMoveUnitsInTheWay_ = 0;
110  int moveAttempts_ = 0;
111  std::unordered_set<Unit*> movedUnits_;
112  int buildAttempts_ = 0;
113  std::shared_ptr<Tracker> tracker_ = nullptr;
114  TrackerStatus trackerStatus_;
115  bool moving_ = false;
116  bool building_ = false;
117 };
118 
119 /**
120  * A unit production controller for all other units.
121  *
122  * This is used by BuilderModule.
123  */
125  public:
127  Module* module,
128  BuildType const* type,
129  std::unordered_map<Unit*, float> unitProbs,
130  std::shared_ptr<BuilderControllerData> bcdata);
131  virtual ~BuilderController() = default;
132 
133  virtual void step(State* state) override;
134  virtual void removeUnit(State* state, Unit* unit, UpcId id) override;
135 
136  protected:
137  std::string logPrefix() const;
138 
139  private:
140  int lastUpdate_ = 0;
141  bool constructionStarted_ = false;
142  std::shared_ptr<Tracker> tracker_ = nullptr;
143  TrackerStatus trackerStatus_;
144 };
145 
146 } // namespace cherrypi
virtual bool didSucceed() const override
Implement this to return whether your custom Controller did succeed in its mission (if applicable) an...
Definition: builderc.h:38
auto larvaBuilderScore(State *state, bool preferSaturation)
Returns scoring function for selecting a unit to build a Larva-based unit.
Definition: builderc.cpp:141
Game state.
Definition: state.h:42
std::shared_ptr< BuilderControllerData > bcdata_
Definition: builderc.h:71
virtual bool didFail() const override
Implement this to return whether your custom Controller did fail in its mission (if applicable) and c...
Definition: builderc.h:41
void setPriority(float value)
Definition: builderc.h:60
auto hatcheryTechBuilderScore(State *state)
Returns scoring function for selecting a unit to morph a hatchery or lair.
Definition: builderc.cpp:203
virtual const char * getName() const override
A name for this Controller, for debugging purposes.
Definition: builderc.h:96
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
bool cancelled(State *state) const
Definition: builderc.cpp:234
void releaseUnit(State *state, Unit *unit)
Definition: builderc.cpp:89
bool findBuilder(State *state, Position const &pos=Position())
Definition: builderc.cpp:249
BuilderControllerBase(Module *module, BuildType const *type, std::unordered_map< Unit *, float > unitProbs, std::shared_ptr< BuilderControllerData > bcdata)
Definition: builderc.cpp:40
virtual void step(State *state)
Advance controller state and produce UPCs.
Definition: controller.cpp:54
TrackerStatus
Definition: tracker.h:16
Unit * builder_
Definition: builderc.h:69
virtual ~BuilderControllerBase()=default
void grabUnit(State *state, Unit *unit)
Definition: builderc.cpp:50
bool failed_
Definition: builderc.h:73
virtual const char * getName() const override
A name for this Controller, for debugging purposes.
Definition: builderc.h:33
std::unordered_map< Unit *, float > unitProbs_
Definition: builderc.h:70
Represents a unit in the game.
Definition: unitsinfo.h:35
BuildType const * type_
Definition: builderc.h:68
Base class for single-task controllers.
Definition: controller.h:156
A unit production controller for units that require a worker.
Definition: builderc.h:84
A unit production controller for all other units.
Definition: builderc.h:124
float priority()
Definition: builderc.h:57
bool succeeded_
Definition: builderc.h:72
BuildType const * type()
Definition: builderc.h:63
float priority_
Definition: builderc.h:74
Main namespace for bot-related code.
Definition: areainfo.cpp:17
virtual void removeUnit(State *state, Unit *unit, UpcId id)
Remove a unit from this controller.
Definition: controller.cpp:22
int UpcId
Definition: basetypes.h:23
Interface for bot modules.
Definition: module.h:30
Base class for controllers used by BuilderModule.
Definition: builderc.h:25
Vec2T< int > Position
Definition: basetypes.h:178
auto defaultUnitBuilderScore(State *state)
Returns scoring function for selecting a unit to build another (non-building) unit.
Definition: builderc.cpp:116