-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBuild_Order.cpp
337 lines (293 loc) · 9.17 KB
/
Build_Order.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "BasicSc2Bot.h"
// Replace Sleep(10) with the following code
using namespace sc2;
void BasicSc2Bot::ExecuteBuildOrder() {
// what do I do if some buildings are destroyed?
BuildBarracks();
BuildFactory();
BuildOrbitalCommand();
BuildAddon();
BuildStarport();
Swap(swap_a, swap_b, false);
BuildFusionCore();
if (current_gameloop % 46 == 0) {
// Building one more battlecruiser might be more helpful??
BuildEngineeringBay();
// don't need
BuildArmory();
}
}
// Build Barracks if we have a Supply Depot and enough resources
void BasicSc2Bot::BuildBarracks() {
const ObservationInterface* obs = Observation();
// Get Supply Depots
Units dps = obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return (unit.unit_type == UNIT_TYPEID::TERRAN_SUPPLYDEPOT ||
unit.unit_type == UNIT_TYPEID::TERRAN_SUPPLYDEPOTLOWERED) &&
ALLBuildingsFilter(unit);
});
// Can't built Barracks without Supply Depots
if (dps.empty()) {
return;
}
// Rebuild Barracks on the ramp if it is destroyed
if (ramp_mid_destroyed != nullptr &&
ramp_mid_destroyed->unit_type == UNIT_TYPEID::TERRAN_BARRACKS &&
CanBuild(150)) {
TryBuildStructure(ABILITY_ID::BUILD_BARRACKS, UNIT_TYPEID::TERRAN_SCV);
ramp_mid_destroyed = nullptr;
return;
}
// Build only 1 Barrack
Units barracks =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_BARRACKS &&
ALLBuildingsFilter(unit);
});
// Build Barracks to block ramps at both bases if we have enough resources
if (phase < 1) {
if (barracks.empty() && CanBuild(150)) {
TryBuildStructure(ABILITY_ID::BUILD_BARRACKS,
UNIT_TYPEID::TERRAN_SCV);
}
}
else if (phase == 3) {
if (barracks.size() < 2 && bases.size() > 1 && CanBuild(550) &&
current_gameloop % 46 == 0) {
TryBuildStructure(ABILITY_ID::BUILD_BARRACKS,
UNIT_TYPEID::TERRAN_SCV);
}
}
}
// Build Orbital Command if we have a Command Center and enough resources
void BasicSc2Bot::BuildOrbitalCommand() {
const ObservationInterface* obs = Observation();
// Can't build Orbital Command without Barracks or Factories
if (!num_barracks || !num_factories) {
return;
}
// Find a Command Center that can be upgraded
Units command_centers = obs->GetUnits(
Unit::Alliance::Self, IsUnit(UNIT_TYPEID::TERRAN_COMMANDCENTER));
if (command_centers.empty()) {
return;
}
for (const auto& cc : command_centers) {
// Check if this Command Center is already being upgraded
bool is_upgrading = false;
for (const auto& order : cc->orders) {
if (order.ability_id == ABILITY_ID::MORPH_ORBITALCOMMAND) {
is_upgrading = true;
break;
}
}
// If its not upgrading, upgrade it
if (!is_upgrading && CanBuild(150)) {
Actions()->UnitCommand(cc, ABILITY_ID::MORPH_ORBITALCOMMAND, true);
return;
}
}
}
// Build Factory if we have a Barracks and enough resources
void BasicSc2Bot::BuildFactory() {
const ObservationInterface* observation = Observation();
// Can't build Factory without Barracks
if (!num_barracks) {
return;
}
if (ramp_mid_destroyed != nullptr &&
ramp_mid_destroyed->unit_type == UNIT_TYPEID::TERRAN_FACTORY &&
CanBuild(150, 100)) {
TryBuildStructure(ABILITY_ID::BUILD_FACTORY, UNIT_TYPEID::TERRAN_SCV);
ramp_mid_destroyed = nullptr;
return;
}
// Build only 1 Factory
Units factories =
observation->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_FACTORY ||
unit.unit_type == UNIT_TYPEID::TERRAN_FACTORYFLYING;
});
if (factories.empty() && CanBuild(150, 100)) {
TryBuildStructure(ABILITY_ID::BUILD_FACTORY, UNIT_TYPEID::TERRAN_SCV);
}
}
// Build Starport if we have a Factory and enough resources
void BasicSc2Bot::BuildStarport() {
const ObservationInterface* obs = Observation();
// Can't build Starports without Factories
if (!num_factories) {
return;
}
// Build only 1 Starport
Units starports =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_STARPORT ||
unit.unit_type == UNIT_TYPEID::TERRAN_STARPORTFLYING;
});
if (starports.empty() && CanBuild(150, 100)) {
TryBuildStructure(ABILITY_ID::BUILD_STARPORT, UNIT_TYPEID::TERRAN_SCV);
}
}
// Build Tech lab if we have a Factory and enough resources
void BasicSc2Bot::BuildAddon() {
if (!swap_in_progress) {
const ObservationInterface* obs = Observation();
// Get Barracks
Units barracks =
obs->GetUnits(Unit::Alliance::Self, [](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_BARRACKS &&
!unit.is_flying && !unit.add_on_tag;
});
// Get Factories
Units factories =
obs->GetUnits(Unit::Alliance::Self, [](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_FACTORY &&
!unit.is_flying && !unit.add_on_tag;
});
// Get Starports
Units starports =
obs->GetUnits(Unit::Alliance::Self, [](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_STARPORT &&
!unit.is_flying && !unit.add_on_tag;
});
Units barracks_techlab =
obs->GetUnits(Unit::Alliance::Self, [](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_BARRACKSTECHLAB;
});
// use 3 bits to represent buildings without any addons
char addon_bits = 0;
addon_bits |= (!barracks.empty() ? 1 : 0);
addon_bits |= (!factories.empty() ? 2 : 0);
addon_bits |= (!starports.empty() ? 4 : 0);
// any is not empty then..
if (addon_bits != 0) {
// enough resources
if (CanBuild(50, 25)) {
const Unit* building = nullptr;
for (size_t i = 0; i < 3; ++i) {
if (addon_bits & 1) {
ABILITY_ID build_order = ABILITY_ID::BUILD_TECHLAB;
// Get the first building without an addon
switch (i) {
case 0:
building = barracks.front();
break;
case 1:
building = factories.front();
break;
case 2:
building = starports.front();
break;
}
// second barracks
if (!barracks.empty() && barracks_techlab.size()) {
if (CanBuild(450, 350)) {
build_order = ABILITY_ID::BUILD_REACTOR;
}
else {
break;
}
}
// Build addon
if (!EnemyNearby(building->pos, true)) {
Actions()->UnitCommand(building, build_order);
break;
}
}
addon_bits >>= 1;
}
}
}
}
return;
}
// Build Fusion Core if we have a Starport and enough resources
void BasicSc2Bot::BuildFusionCore() {
const ObservationInterface* obs = Observation();
// Can't build fusion core without Starports
if (!num_starports || num_fusioncores) {
return;
}
// Build only 1 Fusion core
Units fusioncore =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_FUSIONCORE &&
ALLBuildingsFilter(unit);
});
if (fusioncore.empty() && CanBuild(150, 125)) {
// TODO: reduce this call?
TryBuildStructure(ABILITY_ID::BUILD_FUSIONCORE,
UNIT_TYPEID::TERRAN_SCV);
}
}
// Build Armory if we have a Fusion core and enough resources
void BasicSc2Bot::BuildArmory() {
const ObservationInterface* obs = Observation();
// Can't build Armory core without the First Battlecruiser
if (!first_battlecruiser) {
return;
}
// Build only 1 Armory
Units armories =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_ARMORY &&
ALLBuildingsFilter(unit);
});
if (armories.empty() && CanBuild(400 + 150, 300 + 50)) {
TryBuildStructure(ABILITY_ID::BUILD_ARMORY, UNIT_TYPEID::TERRAN_SCV);
}
}
// Build Engineering bay if we have a Barrack and enough resources
void BasicSc2Bot::BuildEngineeringBay() {
const ObservationInterface* obs = Observation();
// Get Barracks
Units barracks =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_BARRACKS &&
ALLBuildingsFilter(unit);
});
// Get Startports
Units starports =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_STARPORT &&
ALLBuildingsFilter(unit);
});
// Can't build Engineering bay without Barracks
if (barracks.empty() || !first_battlecruiser) {
return;
}
// Build only 1 engineering bay (After Starports)
Units engineeringbays =
obs->GetUnits(Unit::Alliance::Self, [this](const Unit& unit) {
return unit.unit_type == UNIT_TYPEID::TERRAN_ENGINEERINGBAY &&
ALLBuildingsFilter(unit);
});
if (!engineeringbays.size() && !starports.empty() && bases.size() > 1 &&
CanBuild(250)) {
TryBuildStructure(ABILITY_ID::BUILD_ENGINEERINGBAY,
UNIT_TYPEID::TERRAN_SCV);
}
}
// Swaps a Factory with a Starport
void BasicSc2Bot::Swap(const Unit* a, const Unit* b, bool lift) {
// lift buildings
if (lift) {
swap_in_progress = true;
Actions()->UnitCommand(a, ABILITY_ID::LIFT);
Actions()->UnitCommand(b, ABILITY_ID::LIFT);
swap_a = a;
swap_b = b;
}
else {
if (swap_in_progress && a->is_flying && b->is_flying) {
const ObservationInterface* obs = Observation();
Point2D swap_position_a = a->pos;
Point2D swap_position_b = b->pos;
Actions()->UnitCommand(a, ABILITY_ID::LAND, swap_position_b);
Actions()->UnitCommand(b, ABILITY_ID::LAND, swap_position_a);
swap_in_progress = false;
}
}
return;
}