-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.cc
1065 lines (866 loc) · 31.4 KB
/
bot.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 <sc2api/sc2_api.h>
#include <iostream>
#include <queue>
#include <algorithm>
#include <math.h>
#include <random>
#include "bot_examples.h"
#include "utils.h"
using namespace sc2;
// These Two Structs Were Needed From "bot_examples.cc"
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 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_;
};
class Bot : public MultiplayerBot {
private: // Bot Global Variables
// Note: ~1200 steps == 1 in-game minute
size_t step_count = 0;
int target_worker_count;
double marine_to_maruader_ratio = 2.7; // Used By Barracks To Determine What To Produce
// Queue of Enemy Sightings - Flushed Periodically
std::queue<Point2D> enemy_unit_locations;
// Enemy unit quantity threshold for siege mode
int siege_threshold = 5;
// Enemy Base Location
Point2D enemy_base_loc;
bool found_enemy_base = false;
// Constants Inherited
// staging_location_ : Point2D location used for rallying created troops
public: // Public Functions Of Bot - On Event Handles Provided By Interface
virtual void OnGameStart() final {
// Call Setup Function of Multiplayer Bot - Sets up Many Helpful constants
MultiplayerBot::OnGameStart();
}
virtual void OnStep() final {
step_count++;
const ObservationInterface* observation = Observation();
// Try To Avoid Doing Too Much Per Step Here
// Using Prime Numbers Between 0-1200 (1 In-game minute) to offload some work..
if (step_count % 3 == 0)
{
BuildOrder();
ManageWorkers();
}
if (step_count % 19 == 0) {
ManageCombatAbilities();
}
if (step_count % 103 == 0)
{
ManageRallyPoints();
ManageDefense();
}
if (step_count % 367 == 0)
{
ManageIdleArmyUnits();
}
if (step_count % 891 == 0)
{
ManageUpgrades();
}
if (step_count % 1200 == 0)
{
ManageScouts();
ManageAttack();
}
if (step_count % 2400 == 0)
{
FlushKnownEnemyLocations();
}
}
bool isCloseToBase(const Unit* unit)
{
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Self, IsTownHall());
// Check to see if the unit is near any of our bases.
for (const Unit* base : bases)
{
if (Distance2D(unit->pos, base->pos) < 25) // Temporary value until something more accurate can be found.
{
return true;
}
}
return false;
}
virtual void OnUnitEnterVision(const sc2::Unit *unit)
{
// On sighting an enemy, record its position in the locations list.
if (unit->alliance == Unit::Enemy && !isCloseToBase(unit))
{
enemy_unit_locations.push(unit->pos);
}
}
virtual void OnBuildingConstructionComplete(const sc2::Unit* unit) {}
virtual void OnUnitCreated(const sc2::Unit *unit)
{
// On Construction Of Combat Units, Rally Them To Staging Location.
switch (unit->unit_type.ToType())
{
case UNIT_TYPEID::TERRAN_MARINE:
GoToPoint(unit, staging_location_);
break;
case UNIT_TYPEID::TERRAN_MARAUDER:
GoToPoint(unit, staging_location_);
break;
case UNIT_TYPEID::TERRAN_SIEGETANK:
GoToPoint(unit, staging_location_);
break;
case UNIT_TYPEID::TERRAN_VIKINGFIGHTER:
GoToPoint(unit, staging_location_);
break;
default:
break;
}
}
Point2D getClosestStartLoc(const Point2D point) {
Point2D closest;
bool first = false;
for (Point2D loc : game_info_.enemy_start_locations)
{
if (first) {
closest = loc;
}
else {
closest = Distance2D(closest, point) < Distance2D(loc, point) ? closest : loc;
}
}
return closest;
}
virtual void OnUnitDestroyed(const sc2::Unit *unit)
{
// Unit could have been killed by something outside its LOS, consider this a hostile location.
if (!isCloseToBase(unit))
{
if (!found_enemy_base)
{
enemy_base_loc = getClosestStartLoc(unit->pos);
found_enemy_base = true;
}
else
{
enemy_unit_locations.push(unit->pos);
}
}
}
virtual void OnUnitIdle(const Unit* unit) {
// On Worker Idle, Assign Workers to Mineral Patch
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::TERRAN_SCV: {
OnWorkerIdle(unit);
break;
}
case UNIT_TYPEID::TERRAN_MULE: {
OnWorkerIdle(unit);
break;
}
// On Unit Production Idle, Delegate To Factory Handler
case UNIT_TYPEID::TERRAN_BARRACKS: {
HandleBarracks(unit);
break;
}
case UNIT_TYPEID::TERRAN_FACTORY: {
HandleFactory(unit);
break;
}
case UNIT_TYPEID::TERRAN_STARPORT: {
HandleStarport(unit);
break;
}
// Note: Upgrade Building is handle in a manager function.
default: {
break;
}
}
}
private: // Private Functions of Bot
/*
ManageCombatAbilities
- Calls all unit combat abilities together for cleanliness
*/
void ManageCombatAbilities()
{
ManageSiegeOn();
ManageSiegeOff();
ManageVikingAssaultOn();
ManageVikingAssaultOff();
// Other abilities can be added here later (ie Stimpacks, Viking morph)
}
/*
ManageVikingAssaultOn
- Checks if there are nearby enemy units
- Morphs to assault mode if none are flying
*/
void ManageVikingAssaultOn() {
const ObservationInterface* observation = Observation();
Units vikings = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_VIKINGFIGHTER));
Units enemyUnits = observation->GetUnits(Unit::Enemy);
for (const Unit* viking : vikings)
{
bool nearby = false;
bool nearbyFlying = false;
// Count enemy units within range
for (const Unit* enemy : enemyUnits)
{
if (Distance2D(viking->pos, enemy->pos) < 11) // Viking vision range is 11
{
nearby = true;
if (enemy->is_flying) { nearbyFlying = true; } // Flying enemies nearby, stay in AA mode
}
}
if (nearby && !nearbyFlying) {
Actions()->UnitCommand(viking, ABILITY_ID::MORPH_VIKINGASSAULTMODE);
}
}
}
/*
ManageVikingAssaultOff
- Checks if there are nearby enemy units
- If there are none, or there are nearby flying enemies, return to fighter mode
*/
void ManageVikingAssaultOff() {
const ObservationInterface* observation = Observation();
Units vikings = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_VIKINGASSAULT));
Units enemyUnits = observation->GetUnits(Unit::Enemy);
for (const Unit* viking : vikings)
{
bool nearby = false;
bool nearbyFlying = false;
// Count enemy units within range
for (const Unit* enemy : enemyUnits)
{
if (Distance2D(viking->pos, enemy->pos) < 11) // Viking vision range is 11
{
nearby = true;
if (enemy->is_flying) { nearbyFlying = true; } // Flying enemies nearby, stay in AA mode
}
}
if (!nearby || nearbyFlying) {
Actions()->UnitCommand(viking, ABILITY_ID::MORPH_VIKINGFIGHTERMODE);
}
}
}
/*
ManageSiegeOn
- Checks each non-sieged tank for a threshold number of enemy units within range
- Activates siege mode if the check passes
*/
void ManageSiegeOn()
{
const ObservationInterface* observation = Observation();
Units tanks = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SIEGETANK));
Units enemyUnits = observation->GetUnits(Unit::Enemy);
for (const Unit* tank : tanks)
{
int total = 0;
// Count enemy units within range
for (const Unit* enemy : enemyUnits)
{
if (Distance2D(tank->pos, enemy->pos) < 13) // Tank range when sieged is 13
{
total += 1;
}
}
// Siege if there are enough enemy units within range
if (total >= siege_threshold)
{
Actions()->UnitCommand(tank, ABILITY_ID::MORPH_SIEGEMODE);
}
}
}
/*
ManageSiegeOff
- Checks each sieged tank for units within range
- Deactivates siege mode if there are none are found
*/
void ManageSiegeOff()
{
const ObservationInterface* observation = Observation();
Units tanks = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SIEGETANKSIEGED));
Units enemyUnits = observation->GetUnits(Unit::Enemy);
for (const Unit* tank : tanks) {
//If no enemy units are within range of the sieged tank, unsiege it
if (std::none_of(begin(enemyUnits), end(enemyUnits),
[&](const Unit* a) {
return Distance2D(a->pos, tank->pos) < 13; // Tank range when sieged is 13
})) {
Actions()->UnitCommand(tank, ABILITY_ID::MORPH_UNSIEGE);
}
}
}
/*
Manage Attack
- Tries to attack move to a location where an enemy was sighted.
- Tries only to attack past the six minute mark in steps and when there are a reasonable amount of troops.
*/
void ManageAttack()
{
const ObservationInterface* observation = Observation();
// Setup - Get Army Units, new unit types must be added here for them to be included.
Units marines = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARINE));
Units maruaders = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARAUDER));
Units tanks = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SIEGETANK));
Units vikings = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_VIKINGFIGHTER));
bool past_six_minutes = step_count > 1200 * 6;
bool significant_army = marines.size() + maruaders.size() > 25;
// Check Attack Preconditions
if (past_six_minutes && significant_army)
{
// Select Attack Location
Point2D attack_location;
// Prioritze enemy structures;
Units enemy_units = observation->GetUnits(Unit::Enemy);
bool found_structure = false;
for (auto unit : enemy_units) {
if (unit->display_type == Unit::DisplayType::Snapshot) // Structures show up in FOW as snapshots
{
attack_location = unit->pos;
found_structure = true;
}
}
if (enemy_unit_locations.size() > 0 || found_structure || found_enemy_base)
{
if (!found_structure) {
// If we havent seen any units recently but we know where their base is, attack that.
if (enemy_unit_locations.size() == 0) {
attack_location = enemy_base_loc;
}
// If there is only one unit location in the queue, attack there
else if (enemy_unit_locations.size() == 1)
{
attack_location = enemy_unit_locations.front();
}
// If there are multiple unit location in the queue, pick one at random
else
{
size_t skip = GetRandomInteger(0, enemy_unit_locations.size() - 1);
for (size_t i = 0; i < skip; i++)
{
enemy_unit_locations.pop();
}
attack_location = enemy_unit_locations.front();
}
}
// Order All Idle Units To Attack - new unit types must be added here for them to be included.
for (const Unit* unit : marines)
{
if (unit->orders.empty() || Distance2D(unit->pos, staging_location_) < 15)
{
GoToPoint(unit, attack_location);
}
}
for (const Unit* unit : maruaders)
{
if (unit->orders.empty() || Distance2D(unit->pos, staging_location_) < 15)
{
GoToPoint(unit, attack_location);
}
}
for (const Unit* unit : tanks)
{
if (unit->orders.empty() || Distance2D(unit->pos, staging_location_) < 15)
{
GoToPoint(unit, attack_location);
}
}
for (const Unit* unit : vikings) // TODO force vikings to keep pace with ground units.
{
if (unit->orders.empty() || Distance2D(unit->pos, staging_location_) < 15)
{
GoToPoint(unit, attack_location);
}
}
}
}
}
/*
Manage Workers
Handles Economy Related Functions
- Balances Workers To Minerals / Gas Refineries Via Call To Multiplayer Bot Function
- Tries To Call Down MULE when able to.
- Tries To Build To Expected (Ideal) Number Of Workers Plus 2
*/
void ManageWorkers()
{
// Balance Workers Against Structures, ie CPs and Refineries
MultiplayerBot::ManageWorkers(UNIT_TYPEID::TERRAN_SCV, ABILITY_ID::HARVEST_GATHER, UNIT_TYPEID::TERRAN_REFINERY);
// Try To Use The Mule Call Down Whenever Possible On Orbital Command Centers
const ObservationInterface* observation = Observation();
Units orbitals = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ORBITALCOMMAND));
for (auto &unit : orbitals)
{
if (unit->energy > 50)
{
float rx = GetRandomScalar();
float ry = GetRandomScalar();
Actions()->UnitCommand(unit, ABILITY_ID::EFFECT_CALLDOWNMULE, Point2D(unit->pos.x + rx * 2, unit->pos.y + ry * 2));
}
}
// Try to Build Up To The Optimal Number of Workers Plus 2 For Construction
target_worker_count = GetExpectedWorkers(UNIT_TYPEID::TERRAN_REFINERY) + 2;
if (CountUnitType(UNIT_TYPEID::TERRAN_SCV) < target_worker_count) {
TryBuildUnit(ABILITY_ID::TRAIN_SCV, UNIT_TYPEID::TERRAN_COMMANDCENTER);
TryBuildUnit(ABILITY_ID::TRAIN_SCV, UNIT_TYPEID::TERRAN_ORBITALCOMMAND);
}
}
/*
Manage Upgrades
Tries To Build Upgrades For Marines And Maruaders
- Only Builds Upgrades When Five Minutes Have Past And There Exists A Reasonable Marine Maruader Force
*/
void ManageUpgrades()
{
const ObservationInterface* observation = Observation();
// Setup - Get Upgrades Buildings and Unit Counts
Units engineering_bays = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ENGINEERINGBAY));
Units armories = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ARMORY));
Units barracks_tech = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_BARRACKSTECHLAB));
Units factorys_tech = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_FACTORYTECHLAB));
Units marines = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARINE));
Units maruaders = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARAUDER));
bool past_five_minutes = step_count > 1200 * 5;
size_t marine_maruader_count = marines.size() + maruaders.size();
bool significant_bio_force = marine_maruader_count > 25;
// Try To Build Upgrades If Army Is Sufficent And Game Has Progressed Far Enough
if (past_five_minutes)
{
if (engineering_bays.size() > 0 && significant_bio_force)
{
const Unit* engineering_bay = engineering_bays.front();
Actions()->UnitCommand(engineering_bay, ABILITY_ID::RESEARCH_TERRANINFANTRYWEAPONS);
Actions()->UnitCommand(engineering_bay, ABILITY_ID::RESEARCH_TERRANINFANTRYARMOR);
}
if (barracks_tech.size() > 0 && significant_bio_force)
{
const Unit* barracks_tech_lab = barracks_tech.front();
Actions()->UnitCommand(barracks_tech, ABILITY_ID::RESEARCH_COMBATSHIELD);
Actions()->UnitCommand(barracks_tech, ABILITY_ID::RESEARCH_CONCUSSIVESHELLS);
}
}
}
/*
Build Order
Tries To Build Buildings Following Heuristics
- Tries To Build Supply Depots When Near Supply Cap
- Tries To Build Other Structures Via Heuristics
- Tries To Expand Via Inherited Function
*/
void BuildOrder() {
const ObservationInterface* observation = Observation();
// Setup - Get Building Counts
Units bases = observation->GetUnits(Unit::Self, IsTownHall());
Units barracks = observation->GetUnits(Unit::Self, IsUnits(barrack_types));
Units factorys = observation->GetUnits(Unit::Self, IsUnits(factory_types));
Units starports = observation->GetUnits(Unit::Self, IsUnits(starport_types));
Units barracks_tech = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_BARRACKSTECHLAB));
Units factorys_tech = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_FACTORYTECHLAB));
Units starports_tech = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_STARPORTTECHLAB));
Units supply_depots = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SUPPLYDEPOT));
Units refinerys = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_REFINERY));
Units engineering_bays = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ENGINEERINGBAY));
Units armories = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ARMORY));
Units orbitals = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_ORBITALCOMMAND));
// Setup - Get Building Count Targets
size_t barracks_count_target = std::min<size_t>(2 * bases.size(), 8);
size_t factory_count_target = 1;
size_t engineering_bay_count_target = 1;
size_t armory_count_target = 1;
size_t starport_count_target = 1;
// Build
// Try Convert Command Center
if (!barracks.empty())
{
for (const auto& base : bases) {
if (base->unit_type == UNIT_TYPEID::TERRAN_COMMANDCENTER && observation->GetMinerals() > 150) {
Actions()->UnitCommand(base, ABILITY_ID::MORPH_ORBITALCOMMAND);
}
}
}
// Try Build Depot - Calculate our own total supply cap here to account for buildings in progress..
size_t FoodCapInProgress = supply_depots.size() * 8 + bases.size() * 15;
if (observation->GetFoodUsed() >= FoodCapInProgress - 3 && observation->GetFoodCap() != 200)
{
TryBuildStructure(ABILITY_ID::BUILD_SUPPLYDEPOT);
if (observation->GetMinerals() > 250 && observation->GetFoodUsed() == observation->GetFoodCap())
{
TryBuildStructure(ABILITY_ID::BUILD_SUPPLYDEPOT);
}
}
// Try Build Refinery - Do not over build refineries, keep pace with orbital command centers
if (barracks.size() >= 2 && orbitals.size() >= 1 && refinerys.size() < orbitals.size())
{
for (auto u : bases)
{
TryBuildGas(ABILITY_ID::BUILD_REFINERY, UNIT_TYPEID::TERRAN_SCV, Point2D(u->pos.x, u->pos.y));
}
}
// Try Build Barracks - Try To Build Barracks Depending On Number Of Bases
if (barracks.size() < barracks_count_target && observation->GetMinerals() > 170)
{
TryBuildStructure(ABILITY_ID::BUILD_BARRACKS);
}
// Try Build Factory - Try To Build Factories Only After Having Sufficent Barracks
if (factorys.size() < factory_count_target && barracks.size() > 3)
{
TryBuildStructure(ABILITY_ID::BUILD_FACTORY);
}
// Try Build Starport - Try To Build Starports Only After Having Sufficent Factories
if (starports.size() < starport_count_target && factorys.size() > 0)
{
TryBuildStructure(ABILITY_ID::BUILD_STARPORT);
}
// Try Build Engineering Bay
if (engineering_bays.size() < engineering_bay_count_target && barracks.size() > 3)
{
TryBuildStructure(ABILITY_ID::BUILD_ENGINEERINGBAY);
}
// Try Build Armory
if (armories.size() < armory_count_target && bases.size() > 2 && factorys.size() > 0 && engineering_bays.size() > 0)
{
TryBuildStructure(ABILITY_ID::BUILD_ARMORY);
}
// Try Expand
if (barracks.size() >= 2 && bases.size() <= 3 && observation->GetMinerals() > 400 * bases.size())
{
TryExpand(ABILITY_ID::BUILD_COMMANDCENTER, UNIT_TYPEID::TERRAN_SCV);
}
// Try Build Barracks Addons
// Moved To Barracks On Idle
// Try Build Factory Addons
// Moved To Factory On Idle
}
/*
Manage Rally Points
Manages The Staging Point Location Used To Rally Newly Created Troops
- Tries to place the rally point to the closest expansion to the middle of the map
- Shifts units slightly such that they are in front of the base, closer to the map center.
*/
void ManageRallyPoints()
{
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Self, IsTownHall());
if (bases.size() == 1)
{
return;
}
if (bases.size() > 1)
{
const Unit* base_closest_to_mid = bases.front();
for (const Unit *u : bases)
{
if (Distance2D(u->pos, getMapCenter(observation)) < (Distance2D(base_closest_to_mid->pos, getMapCenter(observation))))
{
base_closest_to_mid = u;
}
}
Point2D unit_vector_to_map_center = pointTowards(base_closest_to_mid->pos, getMapCenter(Observation()));
staging_location_.x = base_closest_to_mid->pos.x + unit_vector_to_map_center.x * 5.0f;
staging_location_.y = base_closest_to_mid->pos.y + unit_vector_to_map_center.y * 4.0f;
}
}
/*
Manage Scouts
Sends Scouts To Scout The Map
- Tries to scout potentical enemy spawns if the game is relatively early.
- Tries to scout random pathable locations if the game has gone on for sometime.
- If a game has gone on for some time, we likely have constant contact with the enemy already
or their original base may have been destroyed. Hence we use randomness here to help uncover more area.
*/
void ManageScouts()
{
const ObservationInterface* observation = Observation();
bool in_first_15_minutes = step_count < 1200 * 15;
if (in_first_15_minutes)
{
for (Point2D point : game_info_.enemy_start_locations)
{
const Unit* unit;
GetRandomUnit(unit, observation, UNIT_TYPEID::TERRAN_MARINE);
if (unit && unit->orders.empty())
{
Actions()->UnitCommand(unit, ABILITY_ID::SMART, point);
}
}
}
else
{
for (int i = 0; i < 3; i++)
{
const Unit* unit;
GetRandomUnit(unit, observation, UNIT_TYPEID::TERRAN_MARINE);
if (unit && unit->orders.empty())
{
ScoutWithUnit(unit, observation);
}
}
}
}
/*
Mange Idle Army Units
Rallies Army Units To The Staging Location If They Have No Other Orders
*/
void ManageIdleArmyUnits()
{
const ObservationInterface* observation = Observation();
Units marines = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARINE));
Units maruaders = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARAUDER));
Units tanks = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SIEGETANK));
Units vikings = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_VIKINGFIGHTER));
for (const Unit* unit : marines)
{
if (unit->orders.empty())
{
GoToPoint(unit, staging_location_);
}
}
for (const Unit* unit : maruaders)
{
if (unit->orders.empty())
{
GoToPoint(unit, staging_location_);
}
}
for (const Unit* unit : tanks)
{
if (unit->orders.empty())
{
GoToPoint(unit, staging_location_);
}
}
for (const Unit* unit : vikings)
{
if (unit->orders.empty())
{
GoToPoint(unit, staging_location_);
}
}
}
/*
Manage Defense
Uses Idle Units To Defend Bases And Unit Staging Lcoation
- Checks Each Enemy Unit Location, if close to base or staging attacks using Idle Units.
*/
void ManageDefense()
{
const ObservationInterface* observation = Observation();
Units bases = observation->GetUnits(Unit::Self, IsTownHall());
Units marines = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARINE));
Units maruaders = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_MARAUDER));
Units tanks = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_SIEGETANK));
Units vikings = observation->GetUnits(Unit::Self, IsUnit(UNIT_TYPEID::TERRAN_VIKINGFIGHTER));
Units enemy_units = observation->GetUnits(Unit::Enemy);
for (const Unit* enemy_unit : enemy_units)
{
bool close_to_staging_point = Distance2D(enemy_unit->pos, staging_location_) < 20.0f;
bool close_to_base = close_to_base = isCloseToBase(enemy_unit);
if (close_to_staging_point || close_to_base)
{
for (const Unit* army_unit : marines)
{
if (army_unit->orders.empty())
{
GoToPoint(army_unit, enemy_unit->pos);
}
}
for (const Unit* army_unit : maruaders)
{
if (army_unit->orders.empty())
{
GoToPoint(army_unit, enemy_unit->pos);
}
}
for (const Unit* army_unit : tanks)
{
if (army_unit->orders.empty())
{
GoToPoint(army_unit, enemy_unit->pos);
}
}
for (const Unit* army_unit : vikings)
{
if (army_unit->orders.empty())
{
GoToPoint(army_unit, enemy_unit->pos);
}
}
}
}
}
// Per Factory Functions
void HandleBarracks(const Unit* unit)
{
size_t tech_lab_count = CountUnitType(UNIT_TYPEID::TERRAN_BARRACKSTECHLAB) + 1;
size_t reactor_count = CountUnitType(UNIT_TYPEID::TERRAN_BARRACKSREACTOR) + 1;
size_t marine_count = CountUnitType(UNIT_TYPEID::TERRAN_MARINE) + 1;
size_t maruader_count = CountUnitType(UNIT_TYPEID::TERRAN_MARAUDER) + 1;
if ((tech_lab_count / reactor_count) < 2)
{
TryBuildAddOn(ABILITY_ID::BUILD_TECHLAB_BARRACKS, unit->tag);
}
else
{
TryBuildAddOn(ABILITY_ID::BUILD_REACTOR_BARRACKS, unit->tag);
}
if (marine_count / maruader_count > marine_to_maruader_ratio)
{
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_MARAUDER);
}
if (unit->orders.empty())
{
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_MARINE);
}
}
void HandleFactory(const Unit* unit)
{
TryBuildAddOn(ABILITY_ID::BUILD_TECHLAB_FACTORY, unit->tag);
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_SIEGETANK);
}
void HandleStarport(const Unit* unit)
{
Actions()->UnitCommand(unit, ABILITY_ID::TRAIN_VIKINGFIGHTER);
}
// Per Unit Functions
void GoToPoint(const Unit* unit, Point2D point)
{
Actions()->UnitCommand(unit, ABILITY_ID::ATTACK_ATTACK, point);
}
void OnWorkerIdle(const Unit* unit)
{
const Unit* mineral_target = FindNearestMineralPatch(unit->pos);
if (!mineral_target)
return;
Actions()->UnitCommand(unit, ABILITY_ID::SMART, mineral_target);
}
// Helper Functions
const Unit* 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;
}
}
}
return target;
}
size_t CountUnitType(UNIT_TYPEID unit_type) {
return Observation()->GetUnits(Unit::Alliance::Self, IsUnit(unit_type)).size();
}
bool TryBuildStructure(ABILITY_ID ability_type_for_structure, UNIT_TYPEID unit_type = UNIT_TYPEID::TERRAN_SCV) {
const ObservationInterface* observation = Observation();
// If a unit already is building a supply structure of this type, do nothing.
// Also get an scv to build the structure.
const Unit* unit_to_build = nullptr;
Units units = observation->GetUnits(Unit::Alliance::Self);
for (const auto& unit : units) {
for (const auto& order : unit->orders) {
if (order.ability_id == ability_type_for_structure) {
continue;
}
}
if (unit->unit_type == unit_type) {
unit_to_build = unit;
}
}
float rx = GetRandomScalar();
float ry = GetRandomScalar();
if (unit_to_build)
{
Actions()->UnitCommand(unit_to_build,
ability_type_for_structure,
Point2D(unit_to_build->pos.x + rx * 10.0f, unit_to_build->pos.y + ry * 10.0f));
}
return true;
}
bool TryBuildAddOn(AbilityID ability_type_for_structure, Tag base_structure) {
float rx = GetRandomScalar();
float ry = GetRandomScalar();
const Unit* unit = Observation()->GetUnit(base_structure);
if (unit->build_progress != 1) {
return false;
}
Point2D build_location = Point2D(unit->pos.x + rx * 15, unit->pos.y + ry * 15);
Units units = Observation()->GetUnits(Unit::Self, IsStructure(Observation()));
if (Query()->Placement(ability_type_for_structure, unit->pos, unit)) {
Actions()->UnitCommand(unit, ability_type_for_structure);