TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
agent.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 "behavior.h"
12 #include "buildtype.h"
13 #include "movefilters.h"
14 #include "state.h"
15 
16 namespace cherrypi {
17 
18 class SquadTask;
19 
20 /**
21  * An Agent represents the micromanagement state of one of our units.
22  *
23  * Agents belong to a SquadTask, which invokes microFlee() or microDelete().
24  *
25  * From there, the Agent, will forward control to a series of Behaviors,
26  * each of which is given the opportunity to do one of the following:
27  * * Post a sharp UPC (for consumption as an in-game command by UPCToCommand)
28  * * Do nothing (issue no commands to the unit this frame)
29  * * Defer control: give the next Behavior an opportunity to control the unit
30  */
31 class Agent {
32  public:
33  /// To what squad does this unit belong?
34  SquadTask* task;
35 
36  /// What unit is this Agent controlling?
38 
39  /// The current game state
41 
42  /// Behaviors to perform when receiving a Delete UPC
43  std::shared_ptr<Behavior> behaviorDelete;
44 
45  /// Behaviors to perform when receiving a Flee UPC
46  std::shared_ptr<Behavior> behaviorFlee;
47 
48  /// What action has been selected for this unit by a Behavior?
50 
51  /// Who is this unit intended to fight?
52  std::vector<Unit*> legalTargets;
53 
54  /// Who has this unit decided to kill?
55  Unit* target = nullptr;
56 
57  /// Is the target in range right now?
58  /// This accounts for latency and unit/target velocities.
59  bool targetInRange = false;
60  bool prevTargetInRange = false;
61 
62  /// Tracks the last target this unit was commanded to attack.
63  Unit* attacking = nullptr;
64 
65  /// Has this unit joined the vanguard of its squad? Or is it on the way?
66  bool wantsToFight = false;
67 
68  /// On what frame was this unit last micromanaged?
69  int lastMicroFrame = -1;
70 
71  /// On what frame did this unit last choose a target?
72  int lastTarget = -1;
73 
74  /// On what frame did this unit start moving?
75  /// -1 when the unit is attacking, rather than moving.
76  int lastMove = -1;
77 
78  /// On what frame did this unit start attacking?
79  /// -1 when the unit is not attacking.
80  int lastAttack = -1;
81 
82  /// If we attempted to move the unit, the last position to which we attempted
83  /// to move it.
85 
86  /// The unit's position last time we micromanaged it.
88 
89  /// How many consecutive frames has this unit been inadvertently idle?
90  int stuckFrames = 0;
91 
92  /// Is this unit a Mutalisk turning to face a Scourge?
93  /// Used to apply the triangle method for kiting Scourge with Mutalisks.
94  int mutaliskTurning = 0;
95 
96  /// SquadTask organizes its units into a formation for attacking.
97  /// This is the Agent's assigned formation position, which it may use before
98  /// fighting.
100 
101  /// Used by SquadTask in calculating formations
103 
104  /// How many frames of being stuck before we attempt to un-stick a unit.
105  static constexpr int unstickTriggerFrames = 9;
106 
107  public:
108  /// Hand control of the unit over to the Agent for fighting.
109  std::shared_ptr<UPCTuple> microDelete();
110 
111  /// Hand control of the unit over to the Agent for fleeing.
112  std::shared_ptr<UPCTuple> microFlee();
113 
114  /// Issues a command to the Agent's unit by posting it to the Blackboard.
115  void postCommand(tc::BW::UnitCommandType command);
116 
117  std::shared_ptr<UPCTuple> attack(Position const& pos);
118  std::shared_ptr<UPCTuple> attack(Unit* u);
119  std::shared_ptr<UPCTuple> moveTo(Position pos, bool protect = true);
120  std::shared_ptr<UPCTuple> moveTo(Vec2 pos, bool protect = true);
121  std::shared_ptr<UPCTuple> filterMove(const movefilters::PositionFilters& pfs);
122  std::shared_ptr<UPCTuple> smartMove(const Position& tgt);
123  std::shared_ptr<UPCTuple> smartMove(Unit* tgt);
124 
125  /// Attempt to cast a spell targeting a unit.
126  /// Returns a UPC if an acceptable target was found; null otherwise.
127  std::shared_ptr<UPCTuple> tryCastSpellOnUnit(
128  const BuildType* spell,
129  std::function<double(Unit* const)> scoring,
130  double minimumScore);
131 
132  /// Attempt to cast a spell targeting an area.
133  /// Returns a UPC if an acceptable target was found; null otherwise.
134  std::shared_ptr<UPCTuple> tryCastSpellOnArea(
135  const BuildType* spell,
136  double areaWidth,
137  double areaHeight,
138  std::function<double(Unit* const)> scoring,
139  double minimumScore,
140  std::function<Position(Position input)> positionTransform = [](auto p) {
141  return p;
142  });
143 
144  protected:
145  /// Prepare the unit for micro
146  void preMicro();
147 };
148 
149 } // namespace cherrypi
Game state.
Definition: state.h:42
int lastTarget
On what frame did this unit last choose a target?
Definition: agent.h:72
int mutaliskTurning
Is this unit a Mutalisk turning to face a Scourge? Used to apply the triangle method for kiting Scour...
Definition: agent.h:94
State * state
The current game state.
Definition: agent.h:40
bool targetInRange
Is the target in range right now? This accounts for latency and unit/target velocities.
Definition: agent.h:59
bool prevTargetInRange
Definition: agent.h:60
SquadTask * task
To what squad does this unit belong?
Definition: agent.h:34
bool wantsToFight
Has this unit joined the vanguard of its squad? Or is it on the way?
Definition: agent.h:66
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
std::shared_ptr< UPCTuple > moveTo(Position pos, bool protect=true)
Convenience method for issuing a move UPC.
Definition: agent.cpp:104
Position lastPosition
The unit&#39;s position last time we micromanaged it.
Definition: agent.h:87
int formationCounter
Used by SquadTask in calculating formations.
Definition: agent.h:102
Unit * target
Who has this unit decided to kill?
Definition: agent.h:55
static constexpr int unstickTriggerFrames
How many frames of being stuck before we attempt to un-stick a unit.
Definition: agent.h:105
std::vector< PPositionFilter > PositionFilters
Definition: movefilters.h:31
std::shared_ptr< UPCTuple > smartMove(const Position &tgt)
Convenience method for issuing a threat-aware move UPC.
Definition: agent.cpp:135
MicroAction currentAction
What action has been selected for this unit by a Behavior?
Definition: agent.h:49
void preMicro()
Prepare the unit for micro.
Definition: agent.cpp:14
An Agent represents the micromanagement state of one of our units.
Definition: agent.h:31
std::shared_ptr< UPCTuple > attack(Position const &pos)
Convenience method for issuing an attack-move UPC.
Definition: agent.cpp:70
Represents a decision of how to control a unit.
Definition: upc.h:135
std::vector< Unit * > legalTargets
Who is this unit intended to fight?
Definition: agent.h:52
std::shared_ptr< UPCTuple > tryCastSpellOnUnit(const BuildType *spell, std::function< double(Unit *const)> scoring, double minimumScore)
Attempt to cast a spell targeting a unit.
Definition: agent.cpp:144
Represents a unit in the game.
Definition: unitsinfo.h:35
std::shared_ptr< UPCTuple > microDelete()
Hand control of the unit over to the Agent for fighting.
Definition: agent.cpp:45
std::shared_ptr< UPCTuple > filterMove(const movefilters::PositionFilters &pfs)
Convenience method for issuing a move UPC using movefilters.
Definition: agent.cpp:129
std::shared_ptr< UPCTuple > tryCastSpellOnArea(const BuildType *spell, double areaWidth, double areaHeight, std::function< double(Unit *const)> scoring, double minimumScore, std::function< Position(Position input)> positionTransform=[](auto p){return p;})
Attempt to cast a spell targeting an area.
Definition: agent.cpp:213
int lastMove
On what frame did this unit start moving? -1 when the unit is attacking, rather than moving...
Definition: agent.h:76
std::shared_ptr< UPCTuple > microFlee()
Hand control of the unit over to the Agent for fleeing.
Definition: agent.cpp:52
constexpr Position kInvalidPosition
Definition: basetypes.h:179
int lastMicroFrame
On what frame was this unit last micromanaged?
Definition: agent.h:69
Unit * attacking
Tracks the last target this unit was commanded to attack.
Definition: agent.h:63
int lastAttack
On what frame did this unit start attacking? -1 when the unit is not attacking.
Definition: agent.h:80
Unit * unit
What unit is this Agent controlling?
Definition: agent.h:37
void postCommand(tc::BW::UnitCommandType command)
Issues a command to the Agent&#39;s unit by posting it to the Blackboard.
Definition: agent.cpp:59
Main namespace for bot-related code.
Definition: areainfo.cpp:17
std::shared_ptr< Behavior > behaviorFlee
Behaviors to perform when receiving a Flee UPC.
Definition: agent.h:46
int stuckFrames
How many consecutive frames has this unit been inadvertently idle?
Definition: agent.h:90
std::shared_ptr< Behavior > behaviorDelete
Behaviors to perform when receiving a Delete UPC.
Definition: agent.h:43
Position formationPosition
SquadTask organizes its units into a formation for attacking.
Definition: agent.h:99
Vec2T< int > Position
Definition: basetypes.h:178
Position lastMoveTo
If we attempted to move the unit, the last position to which we attempted to move it...
Definition: agent.h:84