TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
tracker.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 "cherrypi.h"
11 
12 namespace cherrypi {
13 
14 class State;
15 
16 enum class TrackerStatus {
17  /// Haven't found the target that should be tracker yet
19  /// Corresponding order picked up but not being actually executed yet (e.g.
20  /// worker moving to building location)
21  Pending,
22  /// NotTracking or Pending for too long
23  Timeout,
24  /// Corresponding target is being executed
25  Ongoing,
26  /// Corresponding target finished succesfully
27  Success,
28  /// Corresponding target aborted and no chance of automatic recovery
29  Failure,
30  /// Tracker was cancelled externally
31  Cancelled,
32 };
33 
34 /**
35  * Abstract base class for Trackers.
36  *
37  * A tracker monitors the execution of a given target. For example, this could
38  * be the command issued in the game (via CommandTracker) or the implementation
39  * of an UPCSTuple (via TaskTracker). At any given time, the tracker will
40  * provide a status assumption regarding the target. It will end up in either
41  * `Timeout`, `Success` or `Failure`. See TrackerStatus for a description of the
42  * individual modes.
43  *
44  * Tracker implementations provide update functions for the NotTracking, Pending
45  * and Ongoing status. Timeouts are handled by the base class.
46  */
47 class Tracker {
48  public:
49  Tracker(int timeout) : timeout_(timeout) {}
50  virtual ~Tracker() {}
51 
53  return status_;
54  }
55 
56  void cancel() {
57  status_ = TrackerStatus::Cancelled;
58  }
59  bool failed() const {
60  return status_ == TrackerStatus::Failure ||
61  status_ == TrackerStatus::Timeout;
62  }
63  bool succeeded() const {
64  return status_ == TrackerStatus::Success;
65  }
66 
67  /// Updates the tracker.
68  /// Returns true if status has changed
69  virtual bool update(State* s);
70 
71  protected:
72  /// Updates the tracker if its status is NotTracking.
73  /// Returns true if status has changed
74  virtual bool updateNotTracking(State* s) = 0;
75  /// Updates the tracker if its status is Pending.
76  /// Returns true if status has changed
77  virtual bool updatePending(State* s) = 0;
78  /// Updates the tracker if its status is Ongoing.
79  /// Returns true if status has changed
80  virtual bool updateOngoing(State* s) = 0;
81 
83  int time_ = 0;
84  int timeout_;
85 };
86 
87 } // namespace cherrypi
Game state.
Definition: state.h:42
bool succeeded() const
Definition: tracker.h:63
Task is currently ongoing.
virtual ~Tracker()
Definition: tracker.h:50
TrackerStatus
Definition: tracker.h:16
Corresponding order picked up but not being actually executed yet (e.g.
TrackerStatus status() const
Definition: tracker.h:52
void cancel()
Definition: tracker.h:56
bool failed() const
Definition: tracker.h:59
Task finished successfully.
Abstract base class for Trackers.
Definition: tracker.h:47
Haven't found the target that should be tracker yet.
NotTracking or Pending for too long.
Tracker(int timeout)
Definition: tracker.h:49
Task failed or was aborted.
Main namespace for bot-related code.
Definition: areainfo.cpp:17
int timeout_
Definition: tracker.h:84