TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
task.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 <set>
11 
12 #include "cherrypi.h"
13 #include "tracker.h"
14 
15 namespace cherrypi {
16 
17 class State;
18 struct Unit;
19 
20 /**
21  * Describes the current status of a task.
22  */
23 enum class TaskStatus {
24  Unknown = -1,
25  /// Task is currently ongoing
26  Ongoing,
27  /// Task finished successfully
28  Success,
29  /// Task Canceled
30  Cancelled,
31  /// Task failed or was aborted
32  Failure
33 };
34 
35 /**
36  * The primary way for modules to publish their activity.
37  *
38  * A task describes a particular activity of a module, likely spanning multiple
39  * frames in the game. Tasks are meant for publishing module activity to the
40  * blackboard. A typical use case would be that a given Module B acts on a
41  * UPCTuple from Module A on the blackboard (i.e. it consumes it) and spawns a
42  * corresponding task and possible further UPCTuples for lower-level Module
43  * objects. Tasks are publicly available on the blackboard, which allows Module
44  * A to track the execution of the UPCTuple that it spawned previously.
45  *
46  * Modules are encouraged to sub-class Task and add custom, task-specific data.
47  * This way, module state is stored on the blackboard instead of in the modules
48  * themselves.
49  */
50 class Task {
51  public:
52  explicit Task(UpcId upcId, std::unordered_set<Unit*> units = {})
53  : upcId_(upcId), units_(std::move(units)) {}
54  virtual ~Task() {}
55 
56  virtual void update(State*) {}
57 
58  /*
59  * The cancel method is an external signal that can be sent
60  * by the module that owns the task, or by other tasks that are proxies
61  * of the upc treated in the current task. It should be understood as a
62  * means to mark the task as failed from the bot itself, rather than from
63  * external conditions. Cancel signals should be propagated from ProxyTasks
64  * to the tasks that execute the proxied UPC.
65  *
66  * The cancel method of a task should typically free the resources
67  * reserved for it, and essentially set the overall state as if the
68  * task naturally failed, including the removal of the task from the
69  * blackboard in case it was not posted with auroremove=true.
70  *
71  * It *does not* mean that cancelling the task should cancel commands
72  * sent to the game (this is usually impossible) or anything.
73  */
74  virtual void cancel(State*);
75 
76  TaskStatus status() const {
77  return status_;
78  }
79 
80  void setStatus(TaskStatus status) {
81  status_ = status;
82  }
83 
84  bool finished() const {
85  return status_ == TaskStatus::Success || status_ == TaskStatus::Failure ||
86  status_ == TaskStatus::Cancelled;
87  }
88 
89  /// UPC id in Blackboard that caused this Task to be spawned
90  UpcId upcId() const {
91  return upcId_;
92  }
93  /// A set of units occupied performing this task
94  std::unordered_set<Unit*> const& units() const {
95  return units_;
96  }
97  void removeUnit(Unit* unit);
98  /// A set of units occupied performing this task. If this is a ProxyTask, this
99  /// will reflect the units of the targeted task.
100  virtual std::unordered_set<Unit*> const& proxiedUnits() const {
101  return units_;
102  }
103 
104  /// A name for this task, for debugging purposes
105  virtual const char* getName() const {
106  return "Task";
107  };
108 
109  protected:
110  std::unordered_set<Unit*>& units() {
111  return units_;
112  }
113  /// Remove units that have been assigned to another task and units that have
114  /// died.
115  virtual void removeDeadOrReassignedUnits(State* state);
116 
117  private:
119  UpcId upcId_ = -1;
120  std::unordered_set<Unit*> units_;
121 };
122 
123 /**
124  * A task that tracks execution of another task (for another UPCTuple).
125  *
126  * This is a simple wrapper task that can be used for modules that are merely
127  * interested in the outcome of any action triggered by a posted UPCTuple.
128  */
129 class ProxyTask : public Task {
130  public:
131  ProxyTask(UpcId targetUpcId, UpcId upcId);
132  virtual ~ProxyTask() = default;
133 
134  virtual void update(State* state) override;
135  virtual void cancel(State* state) override;
136  virtual std::unordered_set<Unit*> const& proxiedUnits() const override;
137 
138  std::shared_ptr<Task> target() const {
139  return target_;
140  }
141 
142  protected:
144  std::shared_ptr<Task> target_;
145 };
146 
147 /// Policies for tracking multiple tasks.
148 enum class ProxyPolicy {
149  /// One task has a given status
150  ANY,
151  /// Most tasks (i.e. more than half) have a given status
152  MOST,
153  /// All tasks have a given status
154  ALL,
155 };
156 
157 /**
158  * A task that tracks execution of multiple other tasks.
159  *
160  * This is similar to ProxyTask but supports tracking of multiple tasks. Note
161  * that this is slightly heavier in terms of processing needs than ProxyTask.
162  * The status of the underlying tasks is mirrored according to the given policy.
163  * These are the default settings:
164  * - ProxyPolicy::ALL for TaskStatus::Success
165  * - ProxyPolicy::ANY for TaskStatus::Failure
166  * - ProxyPolicy::ANY for TaskStatus::Ongoing
167  * - ProxyPolicy::ALL for TaskStatus::Unknown
168  *
169  * Status checks according to policies are evaluated in the following order:
170  * TaskStatus::Success, TaskStatus::Failure, TaskStatus::Ongoing and
171  * TaskStatus::Unknown.
172  */
173 class MultiProxyTask : public Task {
174  public:
175  MultiProxyTask(std::vector<UpcId> targetUpcIds, UpcId upcId);
176  virtual ~MultiProxyTask() = default;
177 
178  void setPolicyForStatus(TaskStatus status, ProxyPolicy policy);
179 
180  virtual void update(State* state) override;
181  virtual void cancel(State* state) override;
182  virtual std::unordered_set<Unit*> const& proxiedUnits() const override;
183 
184  std::vector<std::shared_ptr<Task>> targets() const {
185  return targets_;
186  }
187 
188  protected:
189  bool matchStatus(TaskStatus status);
190 
191  std::vector<UpcId> targetUpcIds_;
192  std::vector<std::shared_ptr<Task>> targets_;
193  std::unordered_set<Unit*> proxiedUnits_;
194  std::map<TaskStatus, ProxyPolicy> policy_;
195  TaskStatus defaultTargetStatus_ = TaskStatus::Unknown;
196 };
197 
198 } // namespace cherrypi
Game state.
Definition: state.h:42
virtual std::unordered_set< Unit * > const & proxiedUnits() const
A set of units occupied performing this task.
Definition: task.h:100
Most tasks (i.e. more than half) have a given status.
UpcId upcId() const
UPC id in Blackboard that caused this Task to be spawned.
Definition: task.h:90
std::unordered_set< Unit * > const & units() const
A set of units occupied performing this task.
Definition: task.h:94
One task has a given status.
The primary way for modules to publish their activity.
Definition: task.h:50
Task is currently ongoing.
std::unordered_set< Unit * > & units()
Definition: task.h:110
Task(UpcId upcId, std::unordered_set< Unit * > units={})
Definition: task.h:52
replayer::Unit Unit
Definition: state.h:36
bool finished() const
Definition: task.h:84
All tasks have a given status.
std::shared_ptr< Task > target_
Definition: task.h:144
A task that tracks execution of multiple other tasks.
Definition: task.h:173
Represents a unit in the game.
Definition: unitsinfo.h:35
std::vector< std::shared_ptr< Task > > targets_
Definition: task.h:192
UpcId targetUpcId_
Definition: task.h:143
Task finished successfully.
TaskStatus status() const
Definition: task.h:76
TaskStatus
Describes the current status of a task.
Definition: task.h:23
std::map< TaskStatus, ProxyPolicy > policy_
Definition: task.h:194
A task that tracks execution of another task (for another UPCTuple).
Definition: task.h:129
ProxyPolicy
Policies for tracking multiple tasks.
Definition: task.h:148
virtual const char * getName() const
A name for this task, for debugging purposes.
Definition: task.h:105
std::vector< UpcId > targetUpcIds_
Definition: task.h:191
std::unordered_set< Unit * > proxiedUnits_
Definition: task.h:193
Task failed or was aborted.
Main namespace for bot-related code.
Definition: areainfo.cpp:17
std::shared_ptr< Task > target() const
Definition: task.h:138
void removeUnit(BuildState &st, BuildStateUnit &u)
Definition: autobuild.cpp:182
int UpcId
Definition: basetypes.h:23
virtual ~Task()
Definition: task.h:54
void setStatus(TaskStatus status)
Definition: task.h:80
virtual void update(State *)
Definition: task.h:56
std::vector< std::shared_ptr< Task > > targets() const
Definition: task.h:184