TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
commandtrackers.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 "basetypes.h"
11 #include "tracker.h"
12 
13 namespace cherrypi {
14 
15 struct BuildType;
16 struct Unit;
17 
18 /**
19  * Tracks movement of a set of units to a target location.
20  *
21  * Ongoing is defined as at least one unit being in movement. The tracker might
22  * switch back to Pending if no unit is moving.
23  * Success is defined in terms of units reaching the target location with the
24  * center of units being within `mind` distance.
25  */
26 class MovementTracker : public Tracker {
27  public:
29  std::unordered_set<Unit*> units,
30  int targetX,
31  int targetY,
32  float mind = 4.0f * 2,
33  int timeout = 15);
34 
35  protected:
36  virtual bool updateNotTracking(State* s) override {
37  return false;
38  }
39  virtual bool updatePending(State* s) override;
40  virtual bool updateOngoing(State* s) override;
41 
42  private:
43  std::unordered_set<Unit*> units_;
44  Position target_;
45  float mind_;
46 };
47 
48 /**
49  * Tracks construction of a building or training of a unit.
50  */
51 class BuildTracker : public Tracker {
52  public:
53  BuildTracker(Unit* unit, BuildType const* type, int timeout = 15 * 4);
54 
55  protected:
56  virtual bool updateNotTracking(State* s) override {
57  return false;
58  }
59  virtual bool updatePending(State* s) override;
60  virtual bool updateOngoing(State* s) override;
61 
62  private:
63  void findTarget(State* s);
64  void findTargetForDrone(State* s);
65 
66  static constexpr FrameNum kMorphTimeout = 4;
67  static constexpr float kMorphDistanceThreshold = 4.0f;
68  static constexpr FrameNum kNotBuildingTimeout = 4;
69  Unit* unit_;
70  Unit* target_ = nullptr;
71  BuildType const* type_;
72  FrameNum startedPendingFrame_ = -1;
73 };
74 
75 /**
76  * Tracks upgrade development.
77  */
78 class UpgradeTracker : public Tracker {
79  public:
80  UpgradeTracker(Unit* unit, BuildType const* type, int timeout = 15 * 4);
81 
82  protected:
83  virtual bool updateNotTracking(State* s) override {
84  return false;
85  }
86  virtual bool updatePending(State* s) override;
87  virtual bool updateOngoing(State* s) override;
88 
89  private:
90  Unit* unit_;
91  BuildType const* type_;
92 };
93 
94 /**
95  * Tracks research progress
96  */
97 class ResearchTracker : public Tracker {
98  public:
99  ResearchTracker(Unit* unit, BuildType const* type, int timeout = 15 * 4);
100 
101  protected:
102  virtual bool updateNotTracking(State* s) override {
103  return false;
104  }
105  virtual bool updatePending(State* s) override;
106  virtual bool updateOngoing(State* s) override;
107 
108  private:
109  Unit* unit_;
110  BuildType const* type_;
111 };
112 
113 /**
114  * Tracks a set of units attacking enemy units.
115  *
116  * Ongoing is defined as at least one unit firing bullets. The tracker might
117  * switch back to Pending if no unit is firing.
118  * Success is defined in terms of all enemy units being dead.
119  */
120 class AttackTracker : public Tracker {
121  public:
123  std::unordered_set<Unit*> units,
124  std::unordered_set<Unit*> enemies,
125  int timeout = 30);
126  virtual ~AttackTracker() {}
127 
128  void setUnits(std::unordered_set<Unit*> units) {
129  units_ = std::move(units);
130  }
131 
132  protected:
133  virtual bool updateNotTracking(State* s);
134  virtual bool updatePending(State* s);
135  virtual bool updateOngoing(State* s);
136 
137  void updateEnemies();
138 
139  std::unordered_set<Unit*> units_;
140  std::unordered_set<Unit*> enemies_;
141 };
142 
143 } // namespace cherrypi
Game state.
Definition: state.h:42
Tracks movement of a set of units to a target location.
Definition: commandtrackers.h:26
int FrameNum
Definition: basetypes.h:22
virtual bool updateNotTracking(State *s) override
Updates the tracker if its status is NotTracking.
Definition: commandtrackers.h:56
MovementTracker(std::unordered_set< Unit * > units, int targetX, int targetY, float mind=4.0f *2, int timeout=15)
Definition: commandtrackers.cpp:21
virtual bool updateNotTracking(State *s) override
Updates the tracker if its status is NotTracking.
Definition: commandtrackers.h:83
virtual bool updatePending(State *s) override
Updates the tracker if its status is Pending.
Definition: commandtrackers.cpp:34
std::unordered_set< Unit * > enemies_
Definition: commandtrackers.h:140
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
Tracks a set of units attacking enemy units.
Definition: commandtrackers.h:120
replayer::Unit Unit
Definition: state.h:36
Tracks research progress.
Definition: commandtrackers.h:97
void setUnits(std::unordered_set< Unit * > units)
Definition: commandtrackers.h:128
Represents a unit in the game.
Definition: unitsinfo.h:35
Abstract base class for Trackers.
Definition: tracker.h:47
const BuildType * timeout
Definition: autobuild.cpp:348
virtual bool updateNotTracking(State *s) override
Updates the tracker if its status is NotTracking.
Definition: commandtrackers.h:102
std::unordered_set< Unit * > units_
Definition: commandtrackers.h:139
virtual bool updateNotTracking(State *s) override
Updates the tracker if its status is NotTracking.
Definition: commandtrackers.h:36
Main namespace for bot-related code.
Definition: areainfo.cpp:17
Tracks upgrade development.
Definition: commandtrackers.h:78
virtual ~AttackTracker()
Definition: commandtrackers.h:126
virtual bool updateOngoing(State *s) override
Updates the tracker if its status is Ongoing.
Definition: commandtrackers.cpp:62
Tracks construction of a building or training of a unit.
Definition: commandtrackers.h:51