TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
commands.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 <algorithm>
11 #include <torchcraft/client.h>
12 #include <utility>
13 
14 #include "cherrypi.h"
15 
16 namespace cherrypi {
17 namespace utils {
18 
19 // Check whether a unit's current orders include the given command
20 inline bool isExecutingCommand(
21  tc::Unit const& unit,
22  tc::BW::UnitCommandType command) {
23  auto orders = tc::BW::commandToOrders(command);
24  auto res = std::find_first_of(
25  unit.orders.begin(),
26  unit.orders.end(),
27  orders.begin(),
28  orders.end(),
29  [](tc::Order const& o1, tc::BW::Order o2) { return o1.type == o2; });
30  return res != unit.orders.end();
31 }
32 
33 inline bool isExecutingCommand(
34  Unit const* unit,
35  tc::BW::UnitCommandType command) {
36  return isExecutingCommand(unit->unit, std::move(command));
37 }
38 
39 inline UnitId commandUnitId(tc::Client::Command const& cmd) {
40  if ((cmd.code == tc::BW::Command::CommandUnit ||
41  cmd.code == tc::BW::Command::CommandUnitProtected) &&
42  cmd.args.size() > 0) {
43  return cmd.args[0];
44  }
45  return -1;
46 }
47 
48 inline tc::BW::UnitCommandType commandUnitType(tc::Client::Command const& cmd) {
49  if ((cmd.code == tc::BW::Command::CommandUnit ||
50  cmd.code == tc::BW::Command::CommandUnitProtected) &&
51  cmd.args.size() > 1) {
52  auto uct = tc::BW::UnitCommandType::_from_integral_nothrow(cmd.args[1]);
53  if (uct) {
54  return *uct;
55  }
56  }
57  return tc::BW::UnitCommandType::MAX;
58 }
59 
60 inline tc::BW::UnitType buildCommandUnitType(tc::Client::Command const& cmd) {
61  if ((cmd.code == tc::BW::Command::CommandUnit ||
62  cmd.code == tc::BW::Command::CommandUnitProtected) &&
63  cmd.args.size() > 5 && cmd.args[1] == tc::BW::UnitCommandType::Build) {
64  auto ut = tc::BW::UnitType::_from_integral_nothrow(cmd.args[5]);
65  if (ut) {
66  return *ut;
67  }
68  }
69  return tc::BW::UnitType::MAX;
70 }
71 
72 inline tc::BW::UnitType trainCommandUnitType(tc::Client::Command const& cmd) {
73  if ((cmd.code == tc::BW::Command::CommandUnit ||
74  cmd.code == tc::BW::Command::CommandUnitProtected) &&
75  cmd.args.size() > 2 && cmd.args[1] == tc::BW::UnitCommandType::Train) {
76  if (cmd.args[2] < 0) {
77  // Unit type specified in 'extra' field
78  if (cmd.args.size() > 5) {
79  auto ut = tc::BW::UnitType::_from_integral_nothrow(cmd.args[5]);
80  if (ut) {
81  return *ut;
82  }
83  } else {
84  return tc::BW::UnitType::MAX;
85  }
86  } else {
87  auto ut = tc::BW::UnitType::_from_integral_nothrow(cmd.args[2]);
88  if (ut) {
89  return *ut;
90  }
91  }
92  }
93  return tc::BW::UnitType::MAX;
94 }
95 
96 inline Position buildCommandPosition(tc::Client::Command const& cmd) {
97  if ((cmd.code == tc::BW::Command::CommandUnit ||
98  cmd.code == tc::BW::Command::CommandUnitProtected) &&
99  cmd.args.size() > 4 && cmd.args[1] == tc::BW::UnitCommandType::Build) {
100  return Position(cmd.args[3], cmd.args[4]);
101  }
102  return Position(-1, -1);
103 }
104 
105 inline bool tcOrderIsAttack(int orderId) {
106  auto order = tc::BW::Order::_from_integral_nothrow(orderId);
107  if (!order) {
108  return false;
109  }
110  switch (*order) {
111  // case tc::BW::Order::Die:
112  // case tc::BW::Order::Stop:
113  case tc::BW::Order::Guard:
114  case tc::BW::Order::PlayerGuard:
115  case tc::BW::Order::TurretGuard:
116  case tc::BW::Order::BunkerGuard:
117  // case tc::BW::Order::Move:
118  // case tc::BW::Order::ReaverStop:
119  case tc::BW::Order::Attack1:
120  case tc::BW::Order::Attack2:
121  case tc::BW::Order::AttackUnit:
122  case tc::BW::Order::AttackFixedRange:
123  case tc::BW::Order::AttackTile:
124  // case tc::BW::Order::Hover:
125  case tc::BW::Order::AttackMove:
126  // case tc::BW::Order::InfestedCommandCenter:
127  // case tc::BW::Order::UnusedNothing:
128  // case tc::BW::Order::UnusedPowerup:
129  case tc::BW::Order::TowerGuard:
130  case tc::BW::Order::TowerAttack:
131  case tc::BW::Order::VultureMine:
132  case tc::BW::Order::StayInRange:
133  case tc::BW::Order::TurretAttack:
134  // case tc::BW::Order::Nothing:
135  // case tc::BW::Order::Unused_24:
136  // case tc::BW::Order::DroneStartBuild:
137  // case tc::BW::Order::DroneBuild:
138  case tc::BW::Order::CastInfestation:
139  case tc::BW::Order::MoveToInfest:
140  case tc::BW::Order::InfestingCommandCenter:
141  // case tc::BW::Order::PlaceBuilding:
142  // case tc::BW::Order::PlaceProtossBuilding:
143  // case tc::BW::Order::CreateProtossBuilding:
144  // case tc::BW::Order::ConstructingBuilding:
145  // case tc::BW::Order::Repair:
146  // case tc::BW::Order::MoveToRepair:
147  // case tc::BW::Order::PlaceAddon:
148  // case tc::BW::Order::BuildAddon:
149  // case tc::BW::Order::Train:
150  // case tc::BW::Order::RallyPointUnit:
151  // case tc::BW::Order::RallyPointTile:
152  // case tc::BW::Order::ZergBirth:
153  // case tc::BW::Order::ZergUnitMorph:
154  // case tc::BW::Order::ZergBuildingMorph:
155  // case tc::BW::Order::IncompleteBuilding:
156  // case tc::BW::Order::IncompleteMorphing:
157  // case tc::BW::Order::BuildNydusExit:
158  // case tc::BW::Order::EnterNydusCanal:
159  // case tc::BW::Order::IncompleteWarping:
160  // case tc::BW::Order::Follow:
161  // case tc::BW::Order::Carrier:
162  // case tc::BW::Order::ReaverCarrierMove:
163  // case tc::BW::Order::CarrierStop:
164  case tc::BW::Order::CarrierAttack:
165  case tc::BW::Order::CarrierMoveToAttack:
166  // case tc::BW::Order::CarrierIgnore2:
167  case tc::BW::Order::CarrierFight:
168  case tc::BW::Order::CarrierHoldPosition:
169  // case tc::BW::Order::Reaver:
170  case tc::BW::Order::ReaverAttack:
171  case tc::BW::Order::ReaverMoveToAttack:
172  case tc::BW::Order::ReaverFight:
173  case tc::BW::Order::ReaverHoldPosition:
174  // case tc::BW::Order::TrainFighter:
175  case tc::BW::Order::InterceptorAttack:
176  case tc::BW::Order::ScarabAttack:
177  // case tc::BW::Order::RechargeShieldsUnit:
178  // case tc::BW::Order::RechargeShieldsBattery:
179  // case tc::BW::Order::ShieldBattery:
180  // case tc::BW::Order::InterceptorReturn:
181  // case tc::BW::Order::DroneLand:
182  // case tc::BW::Order::BuildingLand:
183  // case tc::BW::Order::BuildingLiftOff:
184  // case tc::BW::Order::DroneLiftOff:
185  // case tc::BW::Order::LiftingOff:
186  // case tc::BW::Order::ResearchTech:
187  // case tc::BW::Order::Upgrade:
188  // case tc::BW::Order::Larva:
189  // case tc::BW::Order::SpawningLarva:
190  // case tc::BW::Order::Harvest1:
191  // case tc::BW::Order::Harvest2:
192  // case tc::BW::Order::MoveToGas:
193  // case tc::BW::Order::WaitForGas:
194  // case tc::BW::Order::HarvestGas:
195  // case tc::BW::Order::ReturnGas:
196  // case tc::BW::Order::MoveToMinerals:
197  // case tc::BW::Order::WaitForMinerals:
198  // case tc::BW::Order::MiningMinerals:
199  // case tc::BW::Order::Harvest3:
200  // case tc::BW::Order::Harvest4:
201  // case tc::BW::Order::ReturnMinerals:
202  // case tc::BW::Order::Interrupted:
203  // case tc::BW::Order::EnterTransport:
204  // case tc::BW::Order::PickupIdle:
205  // case tc::BW::Order::PickupTransport:
206  // case tc::BW::Order::PickupBunker:
207  // case tc::BW::Order::Pickup4:
208  // case tc::BW::Order::PowerupIdle:
209  // case tc::BW::Order::Sieging:
210  // case tc::BW::Order::Unsieging:
211  // case tc::BW::Order::WatchTarget:
212  // case tc::BW::Order::InitCreepGrowth:
213  // case tc::BW::Order::SpreadCreep:
214  // case tc::BW::Order::StoppingCreepGrowth:
215  // case tc::BW::Order::GuardianAspect:
216  // case tc::BW::Order::ArchonWarp:
217  // case tc::BW::Order::CompletingArchonSummon:
218  case tc::BW::Order::HoldPosition:
219  // case tc::BW::Order::QueenHoldPosition:
220  // case tc::BW::Order::Cloak:
221  // case tc::BW::Order::Decloak:
222  // case tc::BW::Order::Unload:
223  // case tc::BW::Order::MoveUnload:
224  case tc::BW::Order::FireYamatoGun:
225  case tc::BW::Order::MoveToFireYamatoGun:
226  case tc::BW::Order::CastLockdown:
227  // case tc::BW::Order::Burrowing:
228  // case tc::BW::Order::Burrowed:
229  // case tc::BW::Order::Unburrowing:
230  // case tc::BW::Order::CastDarkSwarm:
231  case tc::BW::Order::CastParasite:
232  case tc::BW::Order::CastSpawnBroodlings:
233  case tc::BW::Order::CastEMPShockwave:
234  // case tc::BW::Order::NukeWait:
235  // case tc::BW::Order::NukeTrain:
236  // case tc::BW::Order::NukeLaunch:
237  // case tc::BW::Order::NukePaint:
238  case tc::BW::Order::NukeUnit:
239  case tc::BW::Order::CastNuclearStrike:
240  // case tc::BW::Order::NukeTrack:
241  // case tc::BW::Order::InitializeArbiter:
242  // case tc::BW::Order::CloakNearbyUnits:
243  // case tc::BW::Order::PlaceMine:
244  // case tc::BW::Order::RightClickAction:
245  case tc::BW::Order::SuicideUnit:
246  // case tc::BW::Order::SuicideLocation:
247  case tc::BW::Order::SuicideHoldPosition:
248  // case tc::BW::Order::CastRecall:
249  // case tc::BW::Order::Teleport:
250  // case tc::BW::Order::CastScannerSweep:
251  // case tc::BW::Order::Scanner:
252  // case tc::BW::Order::CastDefensiveMatrix:
253  // case tc::BW::Order::CastPsionicStorm:
254  case tc::BW::Order::CastIrradiate:
255  // case tc::BW::Order::CastPlague:
256  // case tc::BW::Order::CastConsume:
257  // case tc::BW::Order::CastEnsnare:
258  // case tc::BW::Order::CastStasisField:
259  // case tc::BW::Order::CastHallucination:
260  // case tc::BW::Order::Hallucination2:
261  // case tc::BW::Order::ResetCollision:
262  // case tc::BW::Order::ResetHarvestCollision:
263  case tc::BW::Order::Patrol:
264  // case tc::BW::Order::CTFCOPInit:
265  // case tc::BW::Order::CTFCOPStarted:
266  // case tc::BW::Order::CTFCOP2:
267  // case tc::BW::Order::ComputerAI:
268  case tc::BW::Order::AtkMoveEP:
269  case tc::BW::Order::HarassMove:
270  case tc::BW::Order::AIPatrol:
271  // case tc::BW::Order::GuardPost:
272  // case tc::BW::Order::RescuePassive:
273  // case tc::BW::Order::Neutral:
274  // case tc::BW::Order::ComputerReturn:
275  // case tc::BW::Order::InitializePsiProvider:
276  // case tc::BW::Order::SelfDestructing:
277  // case tc::BW::Order::Critter:
278  // case tc::BW::Order::HiddenGun:
279  // case tc::BW::Order::OpenDoor:
280  // case tc::BW::Order::CloseDoor:
281  // case tc::BW::Order::HideTrap:
282  // case tc::BW::Order::RevealTrap:
283  // case tc::BW::Order::EnableDoodad:
284  // case tc::BW::Order::DisableDoodad:
285  // case tc::BW::Order::WarpIn:
286  // case tc::BW::Order::Medic:
287  // case tc::BW::Order::MedicHeal:
288  // case tc::BW::Order::HealMove:
289  // case tc::BW::Order::MedicHoldPosition:
290  // case tc::BW::Order::MedicHealToIdle:
291  // case tc::BW::Order::CastRestoration:
292  // case tc::BW::Order::CastDisruptionWeb:
293  case tc::BW::Order::CastMindControl:
294  // case tc::BW::Order::DarkArchonMeld:
295  case tc::BW::Order::CastFeedback:
296  case tc::BW::Order::CastOpticalFlare:
297  // case tc::BW::Order::CastMaelstrom:
298  // case tc::BW::Order::JunkYardDog:
299  // case tc::BW::Order::Fatal:
300  // case tc::BW::Order::None:
301  return true;
302  default:
303  return false;
304  }
305 }
306 
307 } // namespace utils
308 } // namespace cherrypi
bool tcOrderIsAttack(int orderId)
Definition: commands.h:105
Definition: frame.h:54
int32_t type
Definition: frame.h:56
int32_t UnitId
Definition: unitsinfo.h:26
Definition: frame.h:81
Position buildCommandPosition(tc::Client::Command const &cmd)
Definition: commands.h:96
Represents a unit in the game.
Definition: unitsinfo.h:35
auto order(Unit *worker)
Definition: gatherermicro.cpp:84
tc::BW::UnitType buildCommandUnitType(tc::Client::Command const &cmd)
Definition: commands.h:60
bool isExecutingCommand(tc::Unit const &unit, tc::BW::UnitCommandType command)
Definition: commands.h:20
tc::Unit unit
A copy of the torchcraft unit data.
Definition: unitsinfo.h:81
tc::BW::UnitType trainCommandUnitType(tc::Client::Command const &cmd)
Definition: commands.h:72
std::vector< Order > orders
Definition: frame.h:94
UnitId commandUnitId(tc::Client::Command const &cmd)
Definition: commands.h:39
Main namespace for bot-related code.
Definition: areainfo.cpp:17
tc::BW::UnitCommandType commandUnitType(tc::Client::Command const &cmd)
Definition: commands.h:48
Vec2T< int > Position
Definition: basetypes.h:178