TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
squadtask.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 <memory>
11 #include <set>
12 #include <vector>
13 
14 #include "../squadcombat.h"
15 #include "basetypes.h"
16 #include "task.h"
17 
18 namespace cherrypi {
19 
20 class Agent;
21 struct EnemyState;
22 
23 /**
24  * SquadTask controls a "squad" of units (units which share the same Delete
25  * or Flee UPC)
26  *
27  * After doing some group-level coordination, SquadTask delegates individual
28  * unit controllers ("Agents") to emit sharp UPCs for translation into game
29  * commands (presumably via the UpcToCommand module)
30  *
31  * How UPCs are interpreted, loosely:
32  * - Flee = 1.0: Run away if able or otherwise evade the enemy
33  * (perhaps by Burrowing)
34  * - Delete = 1.0: Engage the UPC-specified targets recklessly
35  * - Delete < 1: Engage the UPC-specified targets or nearby enemies
36  */
37 class SquadTask : public Task {
38  public:
39  /// Enemies this Squad should target
40  std::vector<Unit*> targets;
41 
42  /// Location this Squad should defend or attack
43  int targetX = -1;
44  int targetY = -1;
45 
46  /// Whether to consider targets or targetX/targetY
48 
49  /// Does this Squad have any air units?
50  bool hasAirUnits = false;
51 
52  /// Does this Squad have any ground units?
53  bool hasGroundUnits = false;
54 
55  /// Does this Squad have any cloaked units that can kill things?
56  bool hasCloakedFighters = false;
57 
58  /// What is the probability -- presumably coming from combat simulation via
59  /// Tactics -- that we will win this fight?
60  double delProb;
61 
62  /// What is the probability that we should flee?
63  double fleeProb;
64 
65  /// What UPC -- presumably from Tactics -- is directing this squad?
66  std::shared_ptr<UPCTuple> sourceUpc;
67 
68  /// Centroid of the Squad units
70 
71  /// Known locations of Psionic Storms (so we can dodge them)
72  std::vector<Position> storms_;
73 
74  /// Stateful information about enemy units
75  std::unordered_map<Unit const*, EnemyState>* enemyStates_;
76 
77  /// Stateful information about our units
78  std::unordered_map<Unit const*, Agent>* agents_;
79 
80  /// Models to solicit for UPCs
81  std::unordered_map<std::string, std::shared_ptr<MicroModel>>* models;
82 
83  /// Enemies this Squad should target
84  std::vector<Unit*> targets_;
85 
86  /// Threatening enemies this Squad should be aware of
87  std::vector<Unit*> threats_;
88 
89  /// All units relevant to this squad.
90  std::unordered_set<Unit*> relevantUnits_;
91 
92  const std::unordered_set<Unit*>& squadUnits() const {
93  return units();
94  }
95 
96  const std::unordered_set<Unit*>& relevantUnits() const {
97  return relevantUnits_;
98  }
99 
101  int upcId,
102  std::shared_ptr<UPCTuple> upc,
103  std::unordered_set<Unit*> units,
104  std::vector<Unit*> targets,
105  std::unordered_map<Unit const*, EnemyState>* enemyStates,
106  std::unordered_map<Unit const*, Agent>* agents,
107  std::unordered_map<std::string, std::shared_ptr<MicroModel>>* models)
108  : Task(upcId, units),
109  targets(std::move(targets)),
110  targetingLocation(false),
111  delProb(upc->commandProb(Command::Delete)),
112  fleeProb(upc->commandProb(Command::Flee)),
113  sourceUpc(upc),
114  enemyStates_(enemyStates),
115  agents_(agents),
116  models(models) {}
117 
119  int upcId,
120  std::shared_ptr<UPCTuple> upc,
121  std::unordered_set<Unit*> units,
122  int x,
123  int y,
124  std::unordered_map<Unit const*, EnemyState>* enemyStates,
125  std::unordered_map<Unit const*, Agent>* agents,
126  std::unordered_map<std::string, std::shared_ptr<MicroModel>>* models)
127  : Task(upcId, units),
128  targetX(x),
129  targetY(y),
130  targetingLocation(true),
131  delProb(upc->commandProb(Command::Delete)),
132  fleeProb(upc->commandProb(Command::Flee)),
133  sourceUpc(upc),
134  enemyStates_(enemyStates),
135  agents_(agents),
136  models(models) {}
137 
138  void update(State* state) override;
139 
140  std::vector<Unit*> getGroupTargets(State* state) const;
141  std::vector<Unit*> getGroupThreats(State* state) const;
142 
143  void pickTargets(State* state);
144  void formation(State* state);
145 
146  std::vector<std::shared_ptr<UPCTuple>> makeUPCs(State* state);
147 
148  bool isIrrelevantTarget(Unit const* u) const;
149  bool isImportantTarget(Unit const* u) const;
150  bool isRelevantDetector(Unit const* u) const;
151  bool isThreat(Unit const* u) const;
152 
153  virtual const char* getName() const override {
154  return "Squad";
155  };
156 };
157 
158 } // namespace cherrypi
std::shared_ptr< UPCTuple > sourceUpc
What UPC – presumably from Tactics – is directing this squad?
Definition: squadtask.h:66
Game state.
Definition: state.h:42
bool hasCloakedFighters
Does this Squad have any cloaked units that can kill things?
Definition: squadtask.h:56
std::unordered_set< Unit * > relevantUnits_
All units relevant to this squad.
Definition: squadtask.h:90
std::vector< Unit * > targets_
Enemies this Squad should target.
Definition: squadtask.h:84
void update(State *state) override
Definition: squadtask.cpp:24
Command
Abstract "meta" commands for UPCTuples.
Definition: basetypes.h:314
const std::unordered_set< Unit * > & relevantUnits() const
Definition: squadtask.h:96
std::unordered_map< Unit const *, EnemyState > * enemyStates_
Stateful information about enemy units.
Definition: squadtask.h:75
std::vector< Unit * > targets
Enemies this Squad should target.
Definition: squadtask.h:40
std::unordered_set< Unit * > const & units() const
A set of units occupied performing this task.
Definition: task.h:94
std::unordered_map< Unit const *, Agent > * agents_
Stateful information about our units.
Definition: squadtask.h:78
bool targetingLocation
Whether to consider targets or targetX/targetY.
Definition: squadtask.h:47
int targetY
Definition: squadtask.h:44
std::vector< Unit * > threats_
Threatening enemies this Squad should be aware of.
Definition: squadtask.h:87
bool isImportantTarget(Unit const *u) const
Should we prioritze this target?
Definition: squadtask.cpp:768
std::vector< std::shared_ptr< UPCTuple > > makeUPCs(State *state)
Get micro decisions for all units.
Definition: squadtask.cpp:690
Position center_
Centroid of the Squad units.
Definition: squadtask.h:69
bool hasAirUnits
Does this Squad have any air units?
Definition: squadtask.h:50
STL namespace.
The primary way for modules to publish their activity.
Definition: task.h:50
double delProb
What is the probability – presumably coming from combat simulation via Tactics – that we will win t...
Definition: squadtask.h:60
std::vector< Unit * > getGroupTargets(State *state) const
Definition: squadtask.cpp:96
std::vector< Unit * > getGroupThreats(State *state) const
Definition: squadtask.cpp:102
Represents a unit in the game.
Definition: unitsinfo.h:35
bool isThreat(Unit const *u) const
Can this unit hurt us?
Definition: squadtask.cpp:746
double fleeProb
What is the probability that we should flee?
Definition: squadtask.h:63
SquadTask(int upcId, std::shared_ptr< UPCTuple > upc, std::unordered_set< Unit * > units, int x, int y, std::unordered_map< Unit const *, EnemyState > *enemyStates, std::unordered_map< Unit const *, Agent > *agents, std::unordered_map< std::string, std::shared_ptr< MicroModel >> *models)
Definition: squadtask.h:118
void formation(State *state)
Calculate a combat formation position for all Agents.
Definition: squadtask.cpp:541
Definition: basetypes.h:324
SquadTask(int upcId, std::shared_ptr< UPCTuple > upc, std::unordered_set< Unit * > units, std::vector< Unit * > targets, std::unordered_map< Unit const *, EnemyState > *enemyStates, std::unordered_map< Unit const *, Agent > *agents, std::unordered_map< std::string, std::shared_ptr< MicroModel >> *models)
Definition: squadtask.h:100
Definition: basetypes.h:319
bool hasGroundUnits
Does this Squad have any ground units?
Definition: squadtask.h:53
int targetX
Location this Squad should defend or attack.
Definition: squadtask.h:43
std::unordered_map< std::string, std::shared_ptr< MicroModel > > * models
Models to solicit for UPCs.
Definition: squadtask.h:81
void pickTargets(State *state)
Select which units are valid targets for this Squad.
Definition: squadtask.cpp:113
Main namespace for bot-related code.
Definition: areainfo.cpp:17
virtual const char * getName() const override
A name for this task, for debugging purposes.
Definition: squadtask.h:153
std::vector< Position > storms_
Known locations of Psionic Storms (so we can dodge them)
Definition: squadtask.h:72
bool isRelevantDetector(Unit const *u) const
Is this unit helping detect allied cloaked fighters?
Definition: squadtask.cpp:738
const std::unordered_set< Unit * > & squadUnits() const
Definition: squadtask.h:92
bool isIrrelevantTarget(Unit const *u) const
Can we ignore this target?
Definition: squadtask.cpp:732