-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_examples.cc
3258 lines (2910 loc) · 132 KB
/
bot_examples.cc
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bot_examples.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#include <iterator>
#include "sc2api/sc2_api.h"
#include "sc2lib/sc2_lib.h"
namespace sc2 {
static int TargetSCVCount = 15;
struct IsAttackable {
bool operator()(const Unit& unit) {
switch (unit.unit_type.ToType()) {
case UNIT_TYPEID::ZERG_OVERLORD: return false;
case UNIT_TYPEID::ZERG_OVERSEER: return false;
case UNIT_TYPEID::PROTOSS_OBSERVER: return false;
default: return true;
}
}
};
struct IsFlying {
bool operator()(const Unit& unit) {
return unit.is_flying;
}
};
//Ignores Overlords, workers, and structures
struct IsArmy {
IsArmy(const ObservationInterface* obs) : observation_(obs) {}
bool operator()(const Unit& unit) {
auto attributes = observation_->GetUnitTypeData().at(unit.unit_type).attributes;
for (const auto& attribute : attributes) {
if (attribute == Attribute::Structure) {
return false;
}
}
switch (unit.unit_type.ToType()) {
case UNIT_TYPEID::ZERG_OVERLORD: return false;
case UNIT_TYPEID::PROTOSS_PROBE: return false;
case UNIT_TYPEID::ZERG_DRONE: return false;
case UNIT_TYPEID::TERRAN_SCV: return false;
case UNIT_TYPEID::ZERG_QUEEN: return false;
case UNIT_TYPEID::ZERG_LARVA: return false;
case UNIT_TYPEID::ZERG_EGG: return false;
case UNIT_TYPEID::TERRAN_MULE: return false;
case UNIT_TYPEID::TERRAN_NUKE: return false;
default: return true;
}
}
const ObservationInterface* observation_;
};
struct IsTownHall {
bool operator()(const Unit& unit) {
switch (unit.unit_type.ToType()) {
case UNIT_TYPEID::ZERG_HATCHERY: return true;
case UNIT_TYPEID::ZERG_LAIR: return true;
case UNIT_TYPEID::ZERG_HIVE : return true;
case UNIT_TYPEID::TERRAN_COMMANDCENTER: return true;
case UNIT_TYPEID::TERRAN_ORBITALCOMMAND: return true;
case UNIT_TYPEID::TERRAN_ORBITALCOMMANDFLYING: return true;
case UNIT_TYPEID::TERRAN_PLANETARYFORTRESS: return true;
case UNIT_TYPEID::PROTOSS_NEXUS: return true;
default: return false;
}
}
};
struct IsVespeneGeyser {
bool operator()(const Unit& unit) {
switch (unit.unit_type.ToType()) {
case UNIT_TYPEID::NEUTRAL_VESPENEGEYSER: return true;
case UNIT_TYPEID::NEUTRAL_SPACEPLATFORMGEYSER: return true;
case UNIT_TYPEID::NEUTRAL_PROTOSSVESPENEGEYSER: return true;
default: return false;
}
}
};
struct IsStructure {
IsStructure(const ObservationInterface* obs) : observation_(obs) {};
bool operator()(const Unit& unit) {
auto& attributes = observation_->GetUnitTypeData().at(unit.unit_type).attributes;
bool is_structure = false;
for (const auto& attribute : attributes) {
if (attribute == Attribute::Structure) {
is_structure = true;
}
}
return is_structure;
}
const ObservationInterface* observation_;
};
int CountUnitType(const ObservationInterface* observation, UnitTypeID unit_type) {
int count = 0;
Units my_units = observation->GetUnits(Unit::Alliance::Self);
for (const auto unit : my_units) {
if (unit->unit_type == unit_type)
++count;
}
return count;
}
bool FindEnemyStructure(const ObservationInterface* observation, const Unit*& enemy_unit) {
Units my_units = observation->GetUnits(Unit::Alliance::Enemy);
for (const auto unit : my_units) {
if (unit->unit_type == UNIT_TYPEID::TERRAN_COMMANDCENTER ||
unit->unit_type == UNIT_TYPEID::TERRAN_SUPPLYDEPOT ||
unit->unit_type == UNIT_TYPEID::TERRAN_BARRACKS) {
enemy_unit = unit;
return true;
}
}
return false;
}
bool GetRandomUnit(const Unit*& unit_out, const ObservationInterface* observation, UnitTypeID unit_type) {
Units my_units = observation->GetUnits(Unit::Alliance::Self);
std::random_shuffle(my_units.begin(), my_units.end()); // Doesn't work, or doesn't work well.
for (const auto unit : my_units) {
if (unit->unit_type == unit_type) {
unit_out = unit;
return true;
}
}
return false;
}
void MultiplayerBot::PrintStatus(std::string msg) {
int64_t bot_identifier = int64_t(this) & 0xFFFLL;
std::cout << std::to_string(bot_identifier) << ": " << msg << std::endl;
}
void MultiplayerBot::OnGameStart() {
game_info_ = Observation()->GetGameInfo();
PrintStatus("game started.");
expansions_ = search::CalculateExpansionLocations(Observation(), Query());
//Temporary, we can replace this with observation->GetStartLocation() once implemented
startLocation_ = Observation()->GetStartLocation();
staging_location_ = startLocation_;
};
size_t MultiplayerBot::CountUnitType(const ObservationInterface* observation, UnitTypeID unit_type) {
return observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type)).size();
}
size_t MultiplayerBot::CountUnitTypeBuilding(const ObservationInterface* observation, UNIT_TYPEID production_building, ABILITY_ID ability) {
int building_count = 0;
Units buildings = observation->GetUnits(Unit::Self, IsUnit(production_building));
for (const auto& building : buildings) {
if (building->orders.empty()) {
continue;
}
for (const auto order : building->orders) {
if (order.ability_id == ability) {
building_count++;
}
}
}
return building_count;
}
size_t MultiplayerBot::CountUnitTypeTotal(const ObservationInterface* observation, UNIT_TYPEID unit_type, UNIT_TYPEID production, ABILITY_ID ability) {
return CountUnitType(observation, unit_type) + CountUnitTypeBuilding(observation, production, ability);
}
size_t MultiplayerBot::CountUnitTypeTotal(const ObservationInterface* observation, std::vector<UNIT_TYPEID> unit_type, UNIT_TYPEID production, ABILITY_ID ability) {
size_t count = 0;
for (const auto& type : unit_type) {
count += CountUnitType(observation, type);
}
return count + CountUnitTypeBuilding(observation, production, ability);
}
bool MultiplayerBot::GetRandomUnit(const Unit*& unit_out, const ObservationInterface* observation, UnitTypeID unit_type) {
Units my_units = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
if (!my_units.empty()) {
unit_out = GetRandomEntry(my_units);
return true;
}
return false;
}
const Unit* MultiplayerBot::FindNearestMineralPatch(const Point2D& start) {
Units units = Observation()->GetUnits(Unit::Alliance::Neutral);
float distance = std::numeric_limits<float>::max();
const Unit* target = nullptr;
for (const auto& u : units) {
if (u->unit_type == UNIT_TYPEID::NEUTRAL_MINERALFIELD) {
float d = DistanceSquared2D(u->pos, start);
if (d < distance) {
distance = d;
target = u;
}
}
}
//If we never found one return false;
if (distance == std::numeric_limits<float>::max()) {
return target;
}
return target;
}
// Tries to find a random location that can be pathed to on the map.
// Returns 'true' if a new, random location has been found that is pathable by the unit.
bool MultiplayerBot::FindEnemyPosition(Point2D& target_pos) {
if (game_info_.enemy_start_locations.empty()) {
return false;
}
target_pos = game_info_.enemy_start_locations.front();
return true;
}
bool MultiplayerBot::TryFindRandomPathableLocation(const Unit* unit, Point2D& target_pos) {
// First, find a random point inside the playable area of the map.
float playable_w = game_info_.playable_max.x - game_info_.playable_min.x;
float playable_h = game_info_.playable_max.y - game_info_.playable_min.y;
// The case where game_info_ does not provide a valid answer
if (playable_w == 0 || playable_h == 0) {
playable_w = 236;
playable_h = 228;
}
target_pos.x = playable_w * GetRandomFraction() + game_info_.playable_min.x;
target_pos.y = playable_h * GetRandomFraction() + game_info_.playable_min.y;
// Now send a pathing query from the unit to that point. Can also query from point to point,
// but using a unit tag wherever possible will be more accurate.
// Note: This query must communicate with the game to get a result which affects performance.
// Ideally batch up the queries (using PathingDistanceBatched) and do many at once.
float distance = Query()->PathingDistance(unit, target_pos);
return distance > 0.1f;
}
void MultiplayerBot::AttackWithUnitType(UnitTypeID unit_type, const ObservationInterface* observation) {
Units units = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
for (const auto& unit : units) {
AttackWithUnit(unit, observation);
}
}
void MultiplayerBot::AttackWithUnit(const Unit* unit, const ObservationInterface* observation) {
//If unit isn't doing anything make it attack.
Units enemy_units = observation->GetUnits(Unit::Alliance::Enemy);
if (enemy_units.empty()) {
return;
}
if (unit->orders.empty()) {
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK, enemy_units.front()->pos);
return;
}
//If the unit is doing something besides attacking, make it attack.
if (unit->orders.front().ability_id != ABILITY_ID::ATTACK) {
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK, enemy_units.front()->pos);
}
}
void MultiplayerBot::ScoutWithUnits(UnitTypeID unit_type, const ObservationInterface* observation) {
Units units = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
for (const auto& unit : units) {
ScoutWithUnit(unit, observation);
}
}
void MultiplayerBot::ScoutWithUnit(const Unit* unit, const ObservationInterface* observation) {
Units enemy_units = observation->GetUnits(Unit::Alliance::Enemy, IsAttackable());
if (!unit->orders.empty()) {
return;
}
Point2D target_pos;
if (FindEnemyPosition(target_pos)) {
if (Distance2D(unit->pos, target_pos) < 20 && enemy_units.empty()) {
if (TryFindRandomPathableLocation(unit, target_pos)) {
Actions()->UnitCommand(unit, ABILITY_ID::SMART, target_pos);
return;
}
}
else if (!enemy_units.empty())
{
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK, enemy_units.front());
return;
}
Actions()->UnitCommand(unit, ABILITY_ID::SMART, target_pos);
}
else {
if (TryFindRandomPathableLocation(unit, target_pos)) {
Actions()->UnitCommand(unit, ABILITY_ID::SMART, target_pos);
}
}
}
//Try build structure given a location. This is used most of the time
bool MultiplayerBot::TryBuildStructure(AbilityID ability_type_for_structure, UnitTypeID unit_type, Point2D location, bool isExpansion = false) {
const ObservationInterface* observation = Observation();
Units workers = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
//if we have no workers Don't build
if (workers.empty()) {
return false;
}
// Check to see if there is already a worker heading out to build it
for (const auto& worker : workers) {
for (const auto& order : worker->orders) {
if (order.ability_id == ability_type_for_structure) {
return false;
}
}
}
// If no worker is already building one, get a random worker to build one
const Unit* unit = GetRandomEntry(workers);
// Check to see if unit can make it there
if (Query()->PathingDistance(unit, location) < 0.1f) {
return false;
}
if (!isExpansion) {
for (const auto& expansion : expansions_) {
if (Distance2D(location, Point2D(expansion.x, expansion.y)) < 7) {
return false;
}
}
}
// Check to see if unit can build there
if (Query()->Placement(ability_type_for_structure, location)) {
Actions()->UnitCommand(unit, ability_type_for_structure, location);
return true;
}
return false;
}
//Try to build a structure based on tag, Used mostly for Vespene, since the pathing check will fail even though the geyser is "Pathable"
bool MultiplayerBot::TryBuildStructure(AbilityID ability_type_for_structure, UnitTypeID unit_type, Tag location_tag) {
const ObservationInterface* observation = Observation();
Units workers = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
const Unit* target = observation->GetUnit(location_tag);
if (workers.empty()) {
return false;
}
// Check to see if there is already a worker heading out to build it
for (const auto& worker : workers) {
for (const auto& order : worker->orders) {
if (order.ability_id == ability_type_for_structure) {
return false;
}
}
}
// If no worker is already building one, get a random worker to build one
const Unit* unit = GetRandomEntry(workers);
// Check to see if unit can build there
if (Query()->Placement(ability_type_for_structure, target->pos)) {
Actions()->UnitCommand(unit, ability_type_for_structure, target);
return true;
}
return false;
}
//Expands to nearest location and updates the start location to be between the new location and old bases.
bool MultiplayerBot::TryExpand(AbilityID build_ability, UnitTypeID worker_type) {
const ObservationInterface* observation = Observation();
float minimum_distance = std::numeric_limits<float>::max();
Point3D closest_expansion;
for (const auto& expansion : expansions_) {
float current_distance = Distance2D(startLocation_, expansion);
if (current_distance < .01f) {
continue;
}
if (current_distance < minimum_distance) {
if (Query()->Placement(build_ability, expansion)) {
closest_expansion = expansion;
minimum_distance = current_distance;
}
}
}
//only update staging location up till 3 bases.
if (TryBuildStructure(build_ability, worker_type, closest_expansion, true) && observation->GetUnits(Unit::Self, IsTownHall()).size() < 4) {
staging_location_ = Point3D(((staging_location_.x + closest_expansion.x) / 2), ((staging_location_.y + closest_expansion.y) / 2),
((staging_location_.z + closest_expansion.z) / 2));
return true;
}
return false;
}
//Tries to build a geyser for a base
bool MultiplayerBot::TryBuildGas(AbilityID build_ability, UnitTypeID worker_type, Point2D base_location) {
const ObservationInterface* observation = Observation();
Units geysers = observation->GetUnits(Unit::Alliance::Neutral, IsVespeneGeyser());
//only search within this radius
float minimum_distance = 15.0f;
Tag closestGeyser = 0;
for (const auto& geyser : geysers) {
float current_distance = Distance2D(base_location, geyser->pos);
if (current_distance < minimum_distance) {
if (Query()->Placement(build_ability, geyser->pos)) {
minimum_distance = current_distance;
closestGeyser = geyser->tag;
}
}
}
// In the case where there are no more available geysers nearby
if (closestGeyser == 0) {
return false;
}
return TryBuildStructure(build_ability, worker_type, closestGeyser);
}
bool MultiplayerBot::TryBuildUnit(AbilityID ability_type_for_unit, UnitTypeID unit_type) {
const ObservationInterface* observation = Observation();
//If we are at supply cap, don't build anymore units, unless its an overlord.
if (observation->GetFoodUsed() >= observation->GetFoodCap() && ability_type_for_unit != ABILITY_ID::TRAIN_OVERLORD) {
return false;
}
const Unit* unit = nullptr;
if (!GetRandomUnit(unit, observation, unit_type)) {
return false;
}
if (!unit->orders.empty()) {
return false;
}
if (unit->build_progress != 1) {
return false;
}
Actions()->UnitCommand(unit, ability_type_for_unit);
return true;
}
// Mine the nearest mineral to Town hall.
// If we don't do this, probes may mine from other patches if they stray too far from the base after building.
void MultiplayerBot::MineIdleWorkers(const Unit* worker, AbilityID worker_gather_command, UnitTypeID vespene_building_type) {
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Alliance::Self, IsTownHall());
Units geysers = observation->GetUnits(Unit::Alliance::Self, IsUnit(vespene_building_type));
const Unit* valid_mineral_patch = nullptr;
if (bases.empty()) {
return;
}
for (const auto& geyser : geysers) {
if (geyser->assigned_harvesters < geyser->ideal_harvesters) {
Actions()->UnitCommand(worker, worker_gather_command, geyser);
return;
}
}
//Search for a base that is missing workers.
for (const auto& base : bases) {
//If we have already mined out here skip the base.
if (base->ideal_harvesters == 0 || base->build_progress != 1) {
continue;
}
if (base->assigned_harvesters < base->ideal_harvesters) {
valid_mineral_patch = FindNearestMineralPatch(base->pos);
Actions()->UnitCommand(worker, worker_gather_command, valid_mineral_patch);
return;
}
}
if (!worker->orders.empty()) {
return;
}
//If all workers are spots are filled just go to any base.
const Unit* random_base = GetRandomEntry(bases);
valid_mineral_patch = FindNearestMineralPatch(random_base->pos);
Actions()->UnitCommand(worker, worker_gather_command, valid_mineral_patch);
}
//An estimate of how many workers we should have based on what buildings we have
int MultiplayerBot::GetExpectedWorkers(UNIT_TYPEID vespene_building_type) {
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Alliance::Self, IsTownHall());
Units geysers = observation->GetUnits(Unit::Alliance::Self, IsUnit(vespene_building_type));
int expected_workers = 0;
for (const auto& base : bases) {
if (base->build_progress != 1) {
continue;
}
expected_workers += base->ideal_harvesters;
}
for (const auto& geyser : geysers) {
if (geyser->vespene_contents > 0) {
if (geyser->build_progress != 1) {
continue;
}
expected_workers += geyser->ideal_harvesters;
}
}
return expected_workers;
}
// To ensure that we do not over or under saturate any base.
void MultiplayerBot::ManageWorkers(UNIT_TYPEID worker_type, AbilityID worker_gather_command, UNIT_TYPEID vespene_building_type) {
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Alliance::Self, IsTownHall());
Units geysers = observation->GetUnits(Unit::Alliance::Self, IsUnit(vespene_building_type));
if (bases.empty()) {
return;
}
for (const auto& base : bases) {
//If we have already mined out or still building here skip the base.
if (base->ideal_harvesters == 0 || base->build_progress != 1) {
continue;
}
//if base is
if (base->assigned_harvesters > base->ideal_harvesters) {
Units workers = observation->GetUnits(Unit::Alliance::Self, IsUnit(worker_type));
for (const auto& worker : workers) {
if (!worker->orders.empty()) {
if (worker->orders.front().target_unit_tag == base->tag) {
//This should allow them to be picked up by mineidleworkers()
MineIdleWorkers(worker, worker_gather_command,vespene_building_type);
return;
}
}
}
}
}
Units workers = observation->GetUnits(Unit::Alliance::Self, IsUnit(worker_type));
for (const auto& geyser : geysers) {
if (geyser->ideal_harvesters == 0 || geyser->build_progress != 1) {
continue;
}
if (geyser->assigned_harvesters > geyser->ideal_harvesters) {
for (const auto& worker : workers) {
if (!worker->orders.empty()) {
if (worker->orders.front().target_unit_tag == geyser->tag) {
//This should allow them to be picked up by mineidleworkers()
MineIdleWorkers(worker, worker_gather_command, vespene_building_type);
return;
}
}
}
}
else if (geyser->assigned_harvesters < geyser->ideal_harvesters) {
for (const auto& worker : workers) {
if (!worker->orders.empty()) {
//This should move a worker that isn't mining gas to gas
const Unit* target = observation->GetUnit(worker->orders.front().target_unit_tag);
if (target == nullptr) {
continue;
}
if (target->unit_type != vespene_building_type) {
//This should allow them to be picked up by mineidleworkers()
MineIdleWorkers(worker, worker_gather_command, vespene_building_type);
return;
}
}
}
}
}
}
void MultiplayerBot::RetreatWithUnits(UnitTypeID unit_type, Point2D retreat_position) {
const ObservationInterface* observation = Observation();
Units units = observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type));
for (const auto& unit : units) {
RetreatWithUnit(unit, retreat_position);
}
}
void MultiplayerBot::RetreatWithUnit(const Unit* unit, Point2D retreat_position) {
float dist = Distance2D(unit->pos, retreat_position);
if (dist < 10) {
if (unit->orders.empty()) {
return;
}
Actions()->UnitCommand(unit, ABILITY_ID::STOP);
return;
}
if (unit->orders.empty() && dist > 14) {
Actions()->UnitCommand(unit, ABILITY_ID::MOVE, retreat_position);
}
else if (!unit->orders.empty() && dist > 14) {
if (unit->orders.front().ability_id != ABILITY_ID::MOVE) {
Actions()->UnitCommand(unit, ABILITY_ID::MOVE, retreat_position);
}
}
}
void MultiplayerBot::OnNuclearLaunchDetected() {
const ObservationInterface* observation = Observation();
nuke_detected = true;
nuke_detected_frame = observation->GetGameLoop();
}
//Manages attack and retreat patterns, as well as unit micro
void ProtossMultiplayerBot::ManageArmy() {
const ObservationInterface* observation = Observation();
Units enemy_units = observation->GetUnits(Unit::Alliance::Enemy);
Units army = observation->GetUnits(Unit::Alliance::Self, IsArmy(observation));
int wait_til_supply = 100;
//There are no enemies yet, and we don't have a big army
if (enemy_units.empty() && observation->GetFoodArmy() < wait_til_supply) {
for (const auto& unit : army) {
RetreatWithUnit(unit, staging_location_);
}
}
else if(!enemy_units.empty()){
for (const auto& unit : army) {
AttackWithUnit(unit, observation);
switch (unit->unit_type.ToType()) {
//Stalker blink micro, blinks back towards your base
case(UNIT_TYPEID::PROTOSS_OBSERVER): {
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK, enemy_units.front()->pos);
break;
}
case(UNIT_TYPEID::PROTOSS_STALKER): {
if (blink_reasearched_) {
/*const Unit* old_unit = observation->GetPreviousUnit(unit.tag);
const Unit* target_unit = observation->GetUnit(unit.engaged_target_tag);
if (old_unit == nullptr) {
break;
}
Point2D blink_location = startLocation_;
if (old_unit->shield > 0 && unit.shield < 1) {
if (!unit.orders.empty()) {
if (target_unit != nullptr) {
Vector2D diff = unit.pos - target_unit->pos;
Normalize2D(diff);
blink_location = unit.pos + diff * 7.0f;
}
else {
Vector2D diff = unit.pos - startLocation_;
Normalize2D(diff);
blink_location = unit.pos - diff * 7.0f;
}
Actions()->UnitCommand(unit.tag, ABILITY_ID::EFFECT_BLINK, blink_location);
}
}*/
}
break;
}
//Turns on guardian shell when close to an enemy
case (UNIT_TYPEID::PROTOSS_SENTRY): {
if (!unit->orders.empty()) {
if (unit->orders.front().ability_id == ABILITY_ID::ATTACK) {
float distance = std::numeric_limits<float>::max();
for (const auto& u : enemy_units) {
float d = Distance2D(u->pos, unit->pos);
if (d < distance) {
distance = d;
}
}
if (distance < 6 && unit->energy >= 75) {
Actions()->UnitCommand(unit, ABILITY_ID::EFFECT_GUARDIANSHIELD);
}
}
}
break;
}
//Turns on Damage boost when close to an enemy
case (UNIT_TYPEID::PROTOSS_VOIDRAY): {
if (!unit->orders.empty()) {
if (unit->orders.front().ability_id == ABILITY_ID::ATTACK) {
float distance = std::numeric_limits<float>::max();
for (const auto& u : enemy_units) {
float d = Distance2D(u->pos, unit->pos);
if (d < distance) {
distance = d;
}
}
if (distance < 8) {
Actions()->UnitCommand(unit, ABILITY_ID::EFFECT_VOIDRAYPRISMATICALIGNMENT);
}
}
}
break;
}
//Turns on oracle weapon when close to an enemy
case (UNIT_TYPEID::PROTOSS_ORACLE): {
if (!unit->orders.empty()) {
float distance = std::numeric_limits<float>::max();
for (const auto& u : enemy_units) {
float d = Distance2D(u->pos, unit->pos);
if (d < distance) {
distance = d;
}
}
if (distance < 6 && unit->energy >= 25) {
Actions()->UnitCommand(unit, ABILITY_ID::BEHAVIOR_PULSARBEAMON);
}
}
break;
}
//fires a disruptor nova when in range
case (UNIT_TYPEID::PROTOSS_DISRUPTOR): {
float distance = std::numeric_limits<float>::max();
Point2D closest_unit;
for (const auto& u : enemy_units) {
float d = Distance2D(u->pos, unit->pos);
if (d < distance) {
distance = d;
closest_unit = u->pos;
}
}
if (distance < 7) {
Actions()->UnitCommand(unit, ABILITY_ID::EFFECT_PURIFICATIONNOVA, closest_unit);
}
else {
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK, closest_unit);
}
break;
}
//controls disruptor novas.
case (UNIT_TYPEID::PROTOSS_DISRUPTORPHASED): {
float distance = std::numeric_limits<float>::max();
Point2D closest_unit;
for (const auto& u : enemy_units) {
if (u->is_flying) {
continue;
}
float d = DistanceSquared2D(u->pos, unit->pos);
if (d < distance) {
distance = d;
closest_unit = u->pos;
}
}
Actions()->UnitCommand(unit, ABILITY_ID::MOVE, closest_unit);
break;
}
default:
break;
}
}
}
else {
for (const auto& unit : army) {
ScoutWithUnit(unit, observation);
}
}
}
//Manages when to build and how many to build of units
bool ProtossMultiplayerBot::TryBuildArmy() {
const ObservationInterface* observation = Observation();
if (observation->GetFoodWorkers() <= target_worker_count_) {
return false;
}
//Until we have 2 bases, hold off on building too many units
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_NEXUS) < 2 && observation->GetFoodArmy() > 10) {
return false;
}
//If we have a decent army already, try hold until we expand again
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_NEXUS) < 3 && observation->GetFoodArmy() > 40) {
return false;
}
size_t mothership_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_MOTHERSHIPCORE);
mothership_count += CountUnitType(observation, UNIT_TYPEID::PROTOSS_MOTHERSHIP);
if (observation->GetFoodWorkers() > target_worker_count_ && mothership_count < 1) {
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_CYBERNETICSCORE) > 0) {
TryBuildUnit(ABILITY_ID::TRAIN_MOTHERSHIPCORE, UNIT_TYPEID::PROTOSS_NEXUS);
}
}
size_t colossus_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_COLOSSUS);
size_t carrier_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_CARRIER);
Units templar = observation->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_HIGHTEMPLAR));
if (templar.size() > 1) {
Units templar_merge;
for (int i = 0; i < 2; ++i) {
templar_merge.push_back(templar.at(i));
}
Actions()->UnitCommand(templar_merge, ABILITY_ID::MORPH_ARCHON);
}
if (air_build_) {
//If we have a fleet beacon, and haven't hit our carrier count, build more carriers
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_FLEETBEACON) > 0 && carrier_count < max_colossus_count_) {
//After the first carrier try and make a Mothership
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_MOTHERSHIP) < 1 && mothership_count > 0) {
if (TryBuildUnit(ABILITY_ID::MORPH_MOTHERSHIP, UNIT_TYPEID::PROTOSS_MOTHERSHIPCORE)) {
return true;
}
return false;
}
if (observation->GetMinerals() > 350 && observation->GetVespene() > 250) {
if (TryBuildUnit(ABILITY_ID::TRAIN_CARRIER, UNIT_TYPEID::PROTOSS_STARGATE)) {
return true;
}
}
else if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_TEMPEST) < 1) {
TryBuildUnit(ABILITY_ID::TRAIN_TEMPEST, UNIT_TYPEID::PROTOSS_STARGATE);
}
else if (carrier_count < 1){ //Try to build at least 1
return false;
}
}
else {
// If we can't build Carrier, try to build voidrays
if (observation->GetMinerals() > 250 && observation->GetVespene() > 150) {
TryBuildUnit(ABILITY_ID::TRAIN_VOIDRAY, UNIT_TYPEID::PROTOSS_STARGATE);
}
//Have at least 1 void ray before we build the other units
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_VOIDRAY) > 0) {
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_ORACLE) < 1) {
TryBuildUnit(ABILITY_ID::TRAIN_ORACLE, UNIT_TYPEID::PROTOSS_STARGATE);
}
else if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_PHOENIX) < 2) {
TryBuildUnit(ABILITY_ID::TRAIN_PHOENIX, UNIT_TYPEID::PROTOSS_STARGATE);
}
}
}
}
else {
//If we have a robotics bay, and haven't hit our colossus count, build more colossus
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_OBSERVER) < 1) {
TryBuildUnit(ABILITY_ID::TRAIN_OBSERVER, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY);
}
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_ROBOTICSBAY) > 0 && colossus_count < max_colossus_count_) {
if (observation->GetMinerals() > 300 && observation->GetVespene() > 200) {
if (TryBuildUnit(ABILITY_ID::TRAIN_COLOSSUS, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY)) {
return true;
}
}
else if (CountUnitTypeTotal(observation, UNIT_TYPEID::PROTOSS_DISRUPTOR, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY, ABILITY_ID::TRAIN_DISRUPTOR) < 2) {
TryBuildUnit(ABILITY_ID::TRAIN_DISRUPTOR, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY);
}
}
else {
// If we can't build Colossus, try to build immortals
if (observation->GetMinerals() > 250 && observation->GetVespene() > 100) {
if (TryBuildUnit(ABILITY_ID::TRAIN_IMMORTAL, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY)) {
return true;
}
}
}
}
if (warpgate_reasearched_ && CountUnitType(observation, UNIT_TYPEID::PROTOSS_WARPGATE) > 0) {
if (observation->GetMinerals() > 1000 && observation->GetVespene() < 200) {
return TryWarpInUnit(ABILITY_ID::TRAINWARP_ZEALOT);
}
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_STALKER) > max_stalker_count_){
return false;
}
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_ADEPT) > max_stalker_count_) {
return false;
}
if (!air_build_) {
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_SENTRY) < max_sentry_count_) {
return TryWarpInUnit(ABILITY_ID::TRAINWARP_SENTRY);
}
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_HIGHTEMPLAR) < 2 && CountUnitType(observation, UNIT_TYPEID::PROTOSS_ARCHON) < 2) {
return TryWarpInUnit(ABILITY_ID::TRAINWARP_HIGHTEMPLAR);
}
}
//build adepts until we have robotics facility, then switch to stalkers.
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY) > 0) {
return TryWarpInUnit(ABILITY_ID::TRAINWARP_STALKER);
}
else {
return TryWarpInUnit(ABILITY_ID::TRAINWARP_ADEPT);
}
}
else {
//Train Adepts if we have a core otherwise build Zealots
if (observation->GetMinerals() > 120 && observation->GetVespene() > 25) {
if (CountUnitType(observation, UNIT_TYPEID::PROTOSS_CYBERNETICSCORE) > 0) {
return TryBuildUnit(ABILITY_ID::TRAIN_ADEPT, UNIT_TYPEID::PROTOSS_GATEWAY);
}
}
else if (observation->GetMinerals() > 100) {
return TryBuildUnit(ABILITY_ID::TRAIN_ZEALOT, UNIT_TYPEID::PROTOSS_GATEWAY);
}
return false;
}
}
//Manages when to build your buildings
void ProtossMultiplayerBot::BuildOrder() {
const ObservationInterface* observation = Observation();
size_t gateway_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_GATEWAY) + CountUnitType(observation,UNIT_TYPEID::PROTOSS_WARPGATE);
size_t cybernetics_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_CYBERNETICSCORE);
size_t forge_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_FORGE);
size_t twilight_council_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_TWILIGHTCOUNCIL);
size_t templar_archive_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_TEMPLARARCHIVE);
size_t base_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_NEXUS);
size_t robotics_facility_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY);
size_t robotics_bay_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_ROBOTICSBAY);
size_t stargate_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_STARGATE);
size_t fleet_beacon_count = CountUnitType(observation, UNIT_TYPEID::PROTOSS_FLEETBEACON);
// 3 Gateway per expansion
if (gateway_count < std::min<size_t>(2 * base_count, 7)) {
//If we have 1 gateway, prioritize building CyberCore
if (cybernetics_count < 1 && gateway_count > 0) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_CYBERNETICSCORE, UNIT_TYPEID::PROTOSS_PROBE);
return;
}
else {
//If we have 1 gateway Prioritize getting another expansion out before building more gateways
if (base_count < 2 && gateway_count > 0) {
TryBuildExpansionNexus();
return;
}
if (observation->GetFoodWorkers() >= target_worker_count_ && observation->GetMinerals() > 150 + (100 * gateway_count)) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_GATEWAY, UNIT_TYPEID::PROTOSS_PROBE);
}
}
}
if (cybernetics_count > 0 && forge_count < 2) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_FORGE, UNIT_TYPEID::PROTOSS_PROBE);
}
//go stargate or robo depending on build
if (air_build_) {
if (gateway_count > 1 && cybernetics_count > 0) {
if (stargate_count < std::min<size_t>(base_count, 5)) {
if (observation->GetMinerals() > 150 && observation->GetVespene() > 150) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_STARGATE, UNIT_TYPEID::PROTOSS_PROBE);
}
}
else if (stargate_count > 0 && fleet_beacon_count < 1) {
if (observation->GetMinerals() > 300 && observation->GetVespene() > 200) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_FLEETBEACON, UNIT_TYPEID::PROTOSS_PROBE);
}
}
}
}
else {
if (gateway_count > 2 && cybernetics_count > 0) {
if (robotics_facility_count < std::min<size_t>(base_count, 4)) {
if (observation->GetMinerals() > 200 && observation->GetVespene() > 100) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_ROBOTICSFACILITY, UNIT_TYPEID::PROTOSS_PROBE);
}
}
else if (robotics_facility_count > 0 && robotics_bay_count < 1) {
if (observation->GetMinerals() > 200 && observation->GetVespene() > 200) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_ROBOTICSBAY, UNIT_TYPEID::PROTOSS_PROBE);
}
}
}
}
if (forge_count > 0 && twilight_council_count < 1 && base_count > 1) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_TWILIGHTCOUNCIL, UNIT_TYPEID::PROTOSS_PROBE);
}
if (twilight_council_count > 0 && templar_archive_count < 1 && base_count > 2) {
TryBuildStructureNearPylon(ABILITY_ID::BUILD_TEMPLARARCHIVE, UNIT_TYPEID::PROTOSS_PROBE);
}
}