TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
behavior.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 "state.h"
12 
13 namespace cherrypi {
14 
15 class Agent;
16 
17 /**
18  * A Behavior is a self-contained situational micro rule.
19  */
20 class Behavior {
21  public:
22  /// Checks if a unit still needs a micro decision. If so, invokes onPerform()
23  void perform(Agent& agent);
24 
25  virtual ~Behavior() = default;
26 
27  protected:
28  /// Convenience method: Form a MicroAction reflecting a decision to
29  /// issue a UPC for this unit.
30  MicroAction doAction(std::shared_ptr<UPCTuple> upc) const {
31  return MicroAction{true, upc};
32  }
33  /// Convenience: A MicroAction reflecting a decision to
34  /// ignore this unit and let another Behavior control it.
36 
37  /// Convenience: A MicroAction reflecting a decision to
38  /// do nothing with this unit and let no other Behavior control it.
39  const MicroAction doNothing = doAction(nullptr);
40 
41  /// Decide what to do with a unit that has not yet been controlled by a
42  /// Behavior.
43  virtual MicroAction onPerform(Agent&) = 0;
44 };
45 
46 // Class declarations for micro Behaviors
47 #define CPI_DEFINE_BEHAVIOR(NAME) \
48  class Behavior##NAME : public Behavior { \
49  public: \
50  virtual MicroAction onPerform(Agent&) override; \
51  };
52 
53 // Class declarations for micro Behaviors
54 #define CPI_DEFINE_INHERIT_BEHAVIOR(NAME, NAMEP) \
55  class Behavior##NAME : public Behavior##NAMEP { \
56  public: \
57  virtual MicroAction onPerform(Agent&) override; \
58  };
59 
60 CPI_DEFINE_BEHAVIOR(ML)
61 CPI_DEFINE_BEHAVIOR(Unstick)
62 CPI_DEFINE_BEHAVIOR(IfIrradiated)
63 CPI_DEFINE_BEHAVIOR(IfStormed)
64 CPI_DEFINE_BEHAVIOR(VsScarab)
65 CPI_DEFINE_BEHAVIOR(Formation)
66 CPI_DEFINE_BEHAVIOR(Detect)
67 CPI_DEFINE_BEHAVIOR(AsZergling)
68 CPI_DEFINE_BEHAVIOR(AsMutaliskVsScourge)
69 CPI_DEFINE_BEHAVIOR(AsMutaliskMicro)
70 CPI_DEFINE_BEHAVIOR(AsScourge)
71 CPI_DEFINE_BEHAVIOR(AsLurker)
72 CPI_DEFINE_BEHAVIOR(AsHydralisk)
73 CPI_DEFINE_BEHAVIOR(AsOverlord)
74 CPI_DEFINE_BEHAVIOR(AsDefilerConsumeOnly)
75 CPI_DEFINE_INHERIT_BEHAVIOR(AsDefiler, AsDefilerConsumeOnly)
76 CPI_DEFINE_BEHAVIOR(Chase)
77 CPI_DEFINE_BEHAVIOR(Kite)
78 CPI_DEFINE_BEHAVIOR(EngageCooperatively)
79 CPI_DEFINE_BEHAVIOR(Engage)
80 CPI_DEFINE_BEHAVIOR(Leave)
81 CPI_DEFINE_BEHAVIOR(Travel)
82 
83 /**
84  * Gives a series of Behaviors the option of issuing a UPC for the unit.
85  * Continues until a Behavior either:
86  * * Issues a UPC, indicating a command for the unit
87  * * Issues a null UPC, indicating that the unit should be left alone
88  */
89 typedef std::vector<std::shared_ptr<Behavior>> BehaviorList;
90 class BehaviorSeries : public Behavior {
91  protected:
92  const BehaviorList behaviors_ = {};
93  virtual MicroAction onPerform(Agent& agent) override;
94 
95  public:
96  BehaviorSeries(BehaviorList behaviors) : behaviors_(behaviors){};
97 };
98 
99 } // namespace cherrypi
const MicroAction pass
Convenience: A MicroAction reflecting a decision to ignore this unit and let another Behavior control...
Definition: behavior.h:35
A Behavior is a self-contained situational micro rule.
Definition: behavior.h:20
STL namespace.
const MicroAction doNothing
Convenience: A MicroAction reflecting a decision to do nothing with this unit and let no other Behavi...
Definition: behavior.h:39
std::vector< std::shared_ptr< Behavior > > BehaviorList
Gives a series of Behaviors the option of issuing a UPC for the unit.
Definition: behavior.h:89
An Agent represents the micromanagement state of one of our units.
Definition: agent.h:31
virtual ~Behavior()=default
BehaviorSeries(BehaviorList behaviors)
Definition: behavior.h:96
Represents a decision of how to control a unit.
Definition: upc.h:135
virtual MicroAction onPerform(Agent &)=0
Decide what to do with a unit that has not yet been controlled by a Behavior.
Definition: behavior.h:90
MicroAction doAction(std::shared_ptr< UPCTuple > upc) const
Convenience method: Form a MicroAction reflecting a decision to issue a UPC for this unit...
Definition: behavior.h:30
Main namespace for bot-related code.
Definition: areainfo.cpp:17
void perform(Agent &agent)
Checks if a unit still needs a micro decision. If so, invokes onPerform()
Definition: behavior.cpp:17