-
Notifications
You must be signed in to change notification settings - Fork 4
/
Cryptbot.cpp
1784 lines (1636 loc) · 54 KB
/
Cryptbot.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
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 <iostream>
#include "Cryptbot.h"
#include "sc2api/sc2_api.h"
#include "Strategys.h"
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 IsBuilding {
bool operator()(const sc2::Unit & unit)
{
switch (unit.unit_type.ToType())
{
case sc2::UNIT_TYPEID::TERRAN_ARMORY: return true;
case sc2::UNIT_TYPEID::PROTOSS_ASSIMILATOR: return true;
case sc2::UNIT_TYPEID::ZERG_BANELINGNEST: return true;
case sc2::UNIT_TYPEID::TERRAN_BARRACKS: return true;
case sc2::UNIT_TYPEID::TERRAN_BARRACKSREACTOR: return true;
case sc2::UNIT_TYPEID::TERRAN_BARRACKSTECHLAB: return true;
case sc2::UNIT_TYPEID::TERRAN_BUNKER: return true;
case sc2::UNIT_TYPEID::TERRAN_COMMANDCENTER: return true;
case sc2::UNIT_TYPEID::PROTOSS_CYBERNETICSCORE: return true;
case sc2::UNIT_TYPEID::PROTOSS_DARKSHRINE: return true;
case sc2::UNIT_TYPEID::TERRAN_ENGINEERINGBAY: return true;
case sc2::UNIT_TYPEID::ZERG_EVOLUTIONCHAMBER: return true;
case sc2::UNIT_TYPEID::ZERG_EXTRACTOR: return true;
case sc2::UNIT_TYPEID::TERRAN_FACTORY: return true;
case sc2::UNIT_TYPEID::TERRAN_FACTORYREACTOR: return true;
case sc2::UNIT_TYPEID::TERRAN_FACTORYTECHLAB: return true;
case sc2::UNIT_TYPEID::PROTOSS_FLEETBEACON: return true;
case sc2::UNIT_TYPEID::PROTOSS_FORGE: return true;
case sc2::UNIT_TYPEID::TERRAN_FUSIONCORE: return true;
case sc2::UNIT_TYPEID::PROTOSS_GATEWAY: return true;
case sc2::UNIT_TYPEID::PROTOSS_WARPGATE: return true;
case sc2::UNIT_TYPEID::TERRAN_GHOSTACADEMY: return true;
case sc2::UNIT_TYPEID::ZERG_HATCHERY: return true;
case sc2::UNIT_TYPEID::ZERG_HYDRALISKDEN: return true;
case sc2::UNIT_TYPEID::ZERG_INFESTATIONPIT: return true;
case sc2::UNIT_TYPEID::TERRAN_MISSILETURRET: return true;
case sc2::UNIT_TYPEID::PROTOSS_NEXUS: return true;
case sc2::UNIT_TYPEID::ZERG_NYDUSCANAL: return true;
case sc2::UNIT_TYPEID::ZERG_NYDUSNETWORK: return true;
case sc2::UNIT_TYPEID::PROTOSS_PHOTONCANNON: return true;
case sc2::UNIT_TYPEID::PROTOSS_PYLON: return true;
case sc2::UNIT_TYPEID::TERRAN_REFINERY: return true;
case sc2::UNIT_TYPEID::ZERG_ROACHWARREN: return true;
case sc2::UNIT_TYPEID::PROTOSS_ROBOTICSBAY: return true;
case sc2::UNIT_TYPEID::PROTOSS_ROBOTICSFACILITY: return true;
case sc2::UNIT_TYPEID::TERRAN_SENSORTOWER: return true;
case sc2::UNIT_TYPEID::ZERG_SPAWNINGPOOL: return true;
case sc2::UNIT_TYPEID::ZERG_SPINECRAWLER: return true;
case sc2::UNIT_TYPEID::ZERG_SPIRE: return true;
case sc2::UNIT_TYPEID::ZERG_SPORECRAWLER: return true;
case sc2::UNIT_TYPEID::PROTOSS_STARGATE: return true;
case sc2::UNIT_TYPEID::TERRAN_STARPORT: return true;
case sc2::UNIT_TYPEID::TERRAN_STARPORTREACTOR: return true;
case sc2::UNIT_TYPEID::TERRAN_STARPORTTECHLAB: return true;
case sc2::UNIT_TYPEID::TERRAN_SUPPLYDEPOT: return true;
case sc2::UNIT_TYPEID::PROTOSS_TEMPLARARCHIVE: return true;
case sc2::UNIT_TYPEID::PROTOSS_TWILIGHTCOUNCIL: return true;
case sc2::UNIT_TYPEID::ZERG_ULTRALISKCAVERN: return true;
case sc2::UNIT_TYPEID::ZERG_HIVE: return true;
case sc2::UNIT_TYPEID::ZERG_LAIR: return true;
case sc2::UNIT_TYPEID::ZERG_GREATERSPIRE: return true;
case sc2::UNIT_TYPEID::TERRAN_ORBITALCOMMAND: return true;
case sc2::UNIT_TYPEID::TERRAN_PLANETARYFORTRESS: return true;
case sc2::UNIT_TYPEID::TERRAN_SUPPLYDEPOTLOWERED: return true;
case sc2::UNIT_TYPEID::TERRAN_FACTORYFLYING: return true;
case sc2::UNIT_TYPEID::TERRAN_BARRACKSFLYING: return true;
case sc2::UNIT_TYPEID::TERRAN_COMMANDCENTERFLYING: return true;
case sc2::UNIT_TYPEID::TERRAN_ORBITALCOMMANDFLYING: return true;
case sc2::UNIT_TYPEID::TERRAN_STARPORTFLYING: return true;
case sc2::UNIT_TYPEID::TERRAN_TECHLAB: return true;
default: return false;
}
}
};
struct IsArmy {
bool operator()(const sc2::Unit & unit)
{
switch (unit.unit_type.ToType())
{
case UNIT_TYPEID::TERRAN_BANSHEE: return true;
case UNIT_TYPEID::TERRAN_BATTLECRUISER: return true;
case UNIT_TYPEID::TERRAN_CYCLONE: return true;
case UNIT_TYPEID::TERRAN_GHOST: return true;
case UNIT_TYPEID::TERRAN_HELLION: return true;
case UNIT_TYPEID::TERRAN_HELLIONTANK: return true;
case UNIT_TYPEID::TERRAN_LIBERATOR: return true;
case UNIT_TYPEID::TERRAN_LIBERATORAG: return true;
case UNIT_TYPEID::TERRAN_MARAUDER: return true;
case UNIT_TYPEID::TERRAN_MARINE: return true;
case UNIT_TYPEID::TERRAN_MEDIVAC: return true;
case UNIT_TYPEID::TERRAN_MULE: return true;
case UNIT_TYPEID::TERRAN_RAVEN: return true;
case UNIT_TYPEID::TERRAN_REAPER: return true;
case UNIT_TYPEID::TERRAN_SCV: return true;
case UNIT_TYPEID::TERRAN_SIEGETANK: return true;
case UNIT_TYPEID::TERRAN_SIEGETANKSIEGED: return true;
case UNIT_TYPEID::TERRAN_THOR: return true;
case UNIT_TYPEID::TERRAN_THORAP: return true;
case UNIT_TYPEID::TERRAN_VIKINGASSAULT: return true;
case UNIT_TYPEID::TERRAN_VIKINGFIGHTER: return true;
case UNIT_TYPEID::ZERG_BANELING: return true;
case UNIT_TYPEID::ZERG_BROODLING: return true;
case UNIT_TYPEID::ZERG_BROODLORD: return true;
case UNIT_TYPEID::ZERG_BROODLORDCOCOON: return true;
case UNIT_TYPEID::ZERG_CHANGELING: return true;
case UNIT_TYPEID::ZERG_CHANGELINGMARINE: return true;
case UNIT_TYPEID::ZERG_CHANGELINGMARINESHIELD: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZEALOT: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZERGLING: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZERGLINGWINGS: return true;
case UNIT_TYPEID::ZERG_CORRUPTOR: return true;
case UNIT_TYPEID::ZERG_DRONE: return true;
case UNIT_TYPEID::ZERG_HYDRALISK: return true;
case UNIT_TYPEID::ZERG_INFESTEDTERRANSEGG: return true;
case UNIT_TYPEID::ZERG_INFESTOR: return true;
case UNIT_TYPEID::ZERG_INFESTORTERRAN: return true;
case UNIT_TYPEID::ZERG_LOCUSTMP: return true;
case UNIT_TYPEID::ZERG_LOCUSTMPFLYING: return true;
case UNIT_TYPEID::ZERG_LURKERMP: return true;
case UNIT_TYPEID::ZERG_MUTALISK: return true;
case UNIT_TYPEID::ZERG_OVERLORD: return true;
case UNIT_TYPEID::ZERG_OVERLORDTRANSPORT: return true;
case UNIT_TYPEID::ZERG_OVERSEER: return true;
case UNIT_TYPEID::ZERG_QUEEN: return true;
case UNIT_TYPEID::ZERG_RAVAGER: return true;
case UNIT_TYPEID::ZERG_ROACH: return true;
case UNIT_TYPEID::ZERG_SPINECRAWLERUPROOTED: return true;
case UNIT_TYPEID::ZERG_SPORECRAWLERUPROOTED: return true;
case UNIT_TYPEID::ZERG_SWARMHOSTBURROWEDMP: return true;
case UNIT_TYPEID::ZERG_SWARMHOSTMP: return true;
case UNIT_TYPEID::ZERG_TRANSPORTOVERLORDCOCOON: return true;
case UNIT_TYPEID::ZERG_ULTRALISK: return true;
case UNIT_TYPEID::ZERG_VIPER: return true;
case UNIT_TYPEID::ZERG_ZERGLING: return true;
case UNIT_TYPEID::PROTOSS_ADEPT: return true;
case UNIT_TYPEID::PROTOSS_ARCHON: return true;
case UNIT_TYPEID::PROTOSS_CARRIER: return true;
case UNIT_TYPEID::PROTOSS_COLOSSUS: return true;
case UNIT_TYPEID::PROTOSS_DARKTEMPLAR: return true;
case UNIT_TYPEID::PROTOSS_DISRUPTOR: return true;
case UNIT_TYPEID::PROTOSS_HIGHTEMPLAR: return true;
case UNIT_TYPEID::PROTOSS_IMMORTAL: return true;
case UNIT_TYPEID::PROTOSS_INTERCEPTOR: return true;
case UNIT_TYPEID::PROTOSS_MOTHERSHIP: return true;
case UNIT_TYPEID::PROTOSS_MOTHERSHIPCORE: return true;
case UNIT_TYPEID::PROTOSS_OBSERVER: return true;
case UNIT_TYPEID::PROTOSS_ORACLE: return true;
case UNIT_TYPEID::PROTOSS_PHOENIX: return true;
case UNIT_TYPEID::PROTOSS_PROBE: return true;
case UNIT_TYPEID::PROTOSS_SENTRY: return true;
case UNIT_TYPEID::PROTOSS_STALKER: return true;
case UNIT_TYPEID::PROTOSS_TEMPEST: return true;
case UNIT_TYPEID::PROTOSS_VOIDRAY: return true;
case UNIT_TYPEID::PROTOSS_WARPPRISM: return true;
case UNIT_TYPEID::PROTOSS_ZEALOT: return true;
default: return false;
}
}
};
struct IsGroundArmy {
bool operator()(const sc2::Unit & unit)
{
switch (unit.unit_type.ToType())
{
case UNIT_TYPEID::TERRAN_BANSHEE: return true;
case UNIT_TYPEID::TERRAN_BATTLECRUISER: return true;
case UNIT_TYPEID::TERRAN_CYCLONE: return true;
case UNIT_TYPEID::TERRAN_GHOST: return true;
case UNIT_TYPEID::TERRAN_HELLION: return true;
case UNIT_TYPEID::TERRAN_HELLIONTANK: return true;
case UNIT_TYPEID::TERRAN_MARAUDER: return true;
case UNIT_TYPEID::TERRAN_MARINE: return true;
case UNIT_TYPEID::TERRAN_MULE: return true;
case UNIT_TYPEID::TERRAN_REAPER: return true;
case UNIT_TYPEID::TERRAN_SCV: return true;
case UNIT_TYPEID::TERRAN_SIEGETANK: return true;
case UNIT_TYPEID::TERRAN_SIEGETANKSIEGED: return true;
case UNIT_TYPEID::TERRAN_THOR: return true;
case UNIT_TYPEID::TERRAN_THORAP: return true;
case UNIT_TYPEID::TERRAN_VIKINGASSAULT: return true;
case UNIT_TYPEID::ZERG_BANELING: return true;
case UNIT_TYPEID::ZERG_BROODLING: return true;
case UNIT_TYPEID::ZERG_BROODLORDCOCOON: return true;
case UNIT_TYPEID::ZERG_CHANGELING: return true;
case UNIT_TYPEID::ZERG_CHANGELINGMARINE: return true;
case UNIT_TYPEID::ZERG_CHANGELINGMARINESHIELD: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZEALOT: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZERGLING: return true;
case UNIT_TYPEID::ZERG_CHANGELINGZERGLINGWINGS: return true;
case UNIT_TYPEID::ZERG_CORRUPTOR: return true;
case UNIT_TYPEID::ZERG_DRONE: return true;
case UNIT_TYPEID::ZERG_HYDRALISK: return true;
case UNIT_TYPEID::ZERG_INFESTEDTERRANSEGG: return true;
case UNIT_TYPEID::ZERG_INFESTOR: return true;
case UNIT_TYPEID::ZERG_INFESTORTERRAN: return true;
case UNIT_TYPEID::ZERG_LURKERMP: return true;
case UNIT_TYPEID::ZERG_OVERLORD: return true;
case UNIT_TYPEID::ZERG_OVERLORDTRANSPORT: return true;
case UNIT_TYPEID::ZERG_OVERSEER: return true;
case UNIT_TYPEID::ZERG_QUEEN: return true;
case UNIT_TYPEID::ZERG_RAVAGER: return true;
case UNIT_TYPEID::ZERG_ROACH: return true;
case UNIT_TYPEID::ZERG_SPINECRAWLERUPROOTED: return true;
case UNIT_TYPEID::ZERG_SPORECRAWLERUPROOTED: return true;
case UNIT_TYPEID::ZERG_SWARMHOSTBURROWEDMP: return true;
case UNIT_TYPEID::ZERG_SWARMHOSTMP: return true;
case UNIT_TYPEID::ZERG_TRANSPORTOVERLORDCOCOON: return true;
case UNIT_TYPEID::ZERG_ULTRALISK: return true;
case UNIT_TYPEID::ZERG_ZERGLING: return true;
case UNIT_TYPEID::PROTOSS_ADEPT: return true;
case UNIT_TYPEID::PROTOSS_ARCHON: return true;
case UNIT_TYPEID::PROTOSS_COLOSSUS: return true;
case UNIT_TYPEID::PROTOSS_DARKTEMPLAR: return true;
case UNIT_TYPEID::PROTOSS_DISRUPTOR: return true;
case UNIT_TYPEID::PROTOSS_HIGHTEMPLAR: return true;
case UNIT_TYPEID::PROTOSS_IMMORTAL: return true;
case UNIT_TYPEID::PROTOSS_PROBE: return true;
case UNIT_TYPEID::PROTOSS_SENTRY: return true;
case UNIT_TYPEID::PROTOSS_STALKER: return true;
case UNIT_TYPEID::PROTOSS_ZEALOT: return true;
default: return false;
}
}
};
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 IsWorker {
bool operator()(const Unit& unit) {
switch (unit.unit_type.ToType()) {
case UNIT_TYPEID::PROTOSS_PROBE: return true;
case UNIT_TYPEID::ZERG_DRONE: return true;
case UNIT_TYPEID::TERRAN_SCV: return true;
default: return false;
}
}
};
size_t CalculateQueries(float radius, float step_size, const Point2D& center, sc2::ABILITY_ID Structure, QueryType QType, std::vector<QueryInterface::PlacementQuery>& queries) {
Point2D current_grid, previous_grid(std::numeric_limits<float>::max(), std::numeric_limits<float>::max());
size_t valid_queries = 0;
// Find a buildable location on the circumference of the sphere
float loc = 0.0f;
while (loc < 360.0f) {
Point2D point = Point2D(
(radius * std::cos((loc * PI) / 180.0f)) + center.x,
(radius * std::sin((loc * PI) / 180.0f)) + center.y);
switch (QType)
{
case MaxXMinY:
if (point.x > center.x || point.y < center.y)
{
loc += step_size;
continue;
}
break;
case MaxXMaxY:
if (point.x > center.x || point.y > center.y)
{
loc += step_size;
continue;
}
break;
case MinXMinY:
if (point.x > center.x || point.y > center.y)
{
loc += step_size;
continue;
}
break;
case MinXMaxY:
if (point.x < center.x || point.y > center.y)
{
loc += step_size;
continue;
}
break;
}
QueryInterface::PlacementQuery query(Structure, point);
current_grid = Point2D(floor(point.x), floor(point.y));
if (previous_grid != current_grid) {
queries.push_back(query);
++valid_queries;
}
previous_grid = current_grid;
loc += step_size;
}
return valid_queries;
}
Point2D CryptBot::GetRandomBuildableLocationFor(sc2::ABILITY_ID Structure, sc2::Point2D Location, QueryType QType, sc2::search::ExpansionParameters parameters)
{
// Get the required queries for this cluster.
std::vector<QueryInterface::PlacementQuery> queries;
size_t query_count = 0;
for (auto r : parameters.radiuses_) {
query_count += CalculateQueries(r, parameters.circle_step_size_, Location, Structure, QType, queries);
}
float distance = std::numeric_limits<float>::max();
std::vector<bool> results = Query()->Placement(queries);
std::vector<QueryInterface::PlacementQuery> validqueries;
for (size_t j = 0; j < results.size(); ++j)
{
if (!results[j]) {
continue;
}
validqueries.push_back(queries[j]);
}
Point2D place;
srand(time(0));
if (validqueries.size() < 1)
{
std::cout << "No valid placement locations \n";
}
const QueryInterface::PlacementQuery& random_location = GetRandomEntry(validqueries);
place = random_location.target_pos;
return place;
}
Point2D CryptBot::GetNearestBuildableLocationFor(sc2::ABILITY_ID Structure, sc2::Point2D Location, QueryType QType, sc2::search::ExpansionParameters parameters)
{
// Get the required queries for this cluster.
std::vector<QueryInterface::PlacementQuery> queries;
size_t query_count = 0;
for (auto r : parameters.radiuses_) {
query_count += CalculateQueries(r, parameters.circle_step_size_, Location, Structure, QType, queries);
}
float distance = std::numeric_limits<float>::max();
Point2D closest;
std::vector<bool> results = Query()->Placement(queries);
for (size_t j = 0; j < results.size(); ++j)
{
if (!results[j]) {
continue;
}
Point2D& p = queries[j].target_pos;
float d = Distance2D(p, Location);
if (d < distance)
{
distance = d;
closest = p;
}
}
return closest;
}
CryptBot::CryptBot()
: Scouting(false)
, ScoutingUnitTag(0)
, RushUnitTag(0)
, Expanding(false)
, FirstPylon(true)
, RushPylon(true)
, CurrentDefenseCannons(0)
, MaxDefenseCannons(3)
, CurrentOffenseno(0)
, HasTrainedCarrierLaunch(false)
, RushPylonDestroyed(false)
, UseAltStrategy(false)
{
PylonSearchParams.radiuses_ = { 1.0f, 1.5f, 2.0f, 2.5f, 3.0f, 3.5f, 4.0f, 4.5f, 5.0f, 5.5f, 6.0f };
PylonSearchParams.circle_step_size_ = 0.5f;
PylonSearchParams.cluster_distance_ = 15.0f;
}
void CryptBot::SetupRushLocation(const ObservationInterface *observation)
{
if (StartPosition == nullptr || (StartPosition->x == 0 && StartPosition->y == 0 && StartPosition->z == 0))
{
return;
}
Point2D Point1;
Point2D Point2;
if (game_info_->map_name.compare("Ascension to Aiur LE") == 0)
{
Point1 = Point2D(33.397f, 119.001f);//, 10.0061f);
Point2 = Point2D(142.58f, 32.9744f);//, 10.0032f);
}
else if (game_info_->map_name.compare("Proxima Station LE") == 0)
{
Point1 = Point2D(55.5349f, 45.8899f);//, 12.0083f);
Point2 = Point2D(144.946f, 119.996f);//, 12.0068f);
}
else if (game_info_->map_name.compare("Mech Depot LE") == 0)
{
Point1 = Point2D(144.891f, 126.548f);//, 9.99219f);
Point2 = Point2D(38.4912f, 36.8269f);//, 10.0034f);
}
else if (game_info_->map_name.compare("Odyssey LE") == 0)
{
Point1 = Point2D(140.301f, 46.1042f);//, 10.0193f);
Point2 = Point2D(29.7517f, 123.95f);//, 9.99219f);
}
else if (game_info_->map_name.compare("Abyssal Reef LE") == 0)
{
Point1 = Point2D(51.3499f, 119.31f);//, 9.98828f);
Point2 = Point2D(148.648f, 24.686f);//, 9.98828f);
}
else if (game_info_->map_name.compare("Interloper LE") == 0)
{
Point1 = Point2D(113.626f, 43.3684f);
Point2 = Point2D(40.6443f, 130.659f);
}
else if (game_info_->map_name.compare("Blackpink LE") == 0)
{
Point1 = Point2D(144.061f, 122.754f);
Point2 = Point2D(24.4409f, 37.0681f);
}
else if (game_info_->map_name.compare("Acolyte LE") == 0)
{
Point1 = Point2D(123.395f, 160.331f);
Point2 = Point2D(48.7954f, 23.0759f);
}
else
{
return;
}
if (Distance2D(*StartPosition, Point1) > Distance2D(*StartPosition, Point2))
{
RushLocation = Point1;
}
else
{
RushLocation = Point2;
}
/*
MapLocations.clear();
MapLocations.push_back(PylonLocations("ProximaStationLE.SC2Map", Point3D(144.352, 122.625, 11.9883), Point3D(55.6426, 45.3745, 11.9883)));
MapLocations.push_back(PylonLocations("AscensiontoAiurLE.SC2Map", Point3D(27.9182, 111.522, 10.0061), Point3D(148.082, 40.4775, 10.0032)));
MapLocations.push_back(PylonLocations("InterloperLE.SC2Map", Point3D(32.7385, 126.32, 12.0083), Point3D(119.261, 41.6794, 12.0068)));
MapLocations.push_back(PylonLocations("MechDepotLE.SC2Map", Point3D(145.541, 115.666, 9.99219), Point3D(37.6521, 48.0205, 10.0034)));
MapLocations.push_back(PylonLocations("OdysseyLE.SC2Map", Point3D(140.301, 46.1042, 10.0193), Point3D(29.7517, 123.95, 9.99219)));
MapLocations.push_back(PylonLocations("AbyssalReefLE.SC2Map", Point3D(61.4775, 112.321, 9.98828), Point3D(139.389, 30.9661, 9.98828)));
*/
}
int32_t CryptBot::GetCurrentMaxSupply()
{
int32_t MaxSupply = 0;
const Units NewUnits = Observation()->GetUnits();
for (auto &u : NewUnits)
{
if (u->unit_type == UNIT_TYPEID::PROTOSS_NEXUS && u->build_progress == 1.0f)
{
MaxSupply += 15;
}
else if (u->unit_type == UNIT_TYPEID::PROTOSS_PYLON && u->build_progress == 1.0f)
{
MaxSupply += 8;
}
}
return MaxSupply;
}
void CryptBot::OnGameStart()
{
const Units NewUnits = Observation()->GetUnits();
for (auto &u : NewUnits)
{
if (u->unit_type == UNIT_TYPEID::PROTOSS_NEXUS)
{
StartPosition = new sc2::Point3D(u->pos);
Actions()->UnitCommand(u, ABILITY_ID::TRAIN_PROBE);
break;
}
}
expansions_ = search::CalculateExpansionLocations(Observation(), Query());
game_info_ = new sc2::GameInfo(Observation()->GetGameInfo());
bool OwnRaceFound = false;
for (const auto &info : game_info_->player_info)
{
if (info.race_actual == sc2::Race::Protoss && OwnRaceFound == false)
{
OwnRaceFound = true;
continue;
}
OpponentRace = info.race_actual;
}
SetupRushLocation(Observation());
}
void CryptBot::OnUnitDestroyed(const Unit *unit) {
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::PROTOSS_PROBE: {
if (unit->tag == ScoutingUnitTag)
{
Scouting = false;
ScoutingUnitTag = GetAvailableWorker();
}
break;
}
case UNIT_TYPEID::PROTOSS_PYLON:
if (unit->alliance == sc2::Unit::Alliance::Self && Distance2D(unit->pos, *StartPosition) > Distance2D(unit->pos, RushLocation))
{
RushPylonDestroyed = true;
UseAltStrategy = true;
}
break;
case UNIT_TYPEID::PROTOSS_PHOTONCANNON:
{
if (unit->alliance == sc2::Unit::Alliance::Self && Distance2D(unit->pos, *StartPosition) < Distance2D(unit->pos, RushLocation))
{
CurrentDefenseCannons -= 1;
MaxDefenseCannons += 2;
}
break;
}
case UNIT_TYPEID::PROTOSS_VOIDRAY: {
/*
for (auto& ThisBG : BattleGroups)
{
int thisMember = 0;
for (const uint64_t ThisUnit : ThisBG.Members)
{
if (ThisUnit == unit.tag)
{
ThisBG.Members.erase(ThisBG.Members.begin() + thisMember);
}
thisMember++;
}
}
*/
}
}
}
bool CryptBot::CheckShouldBuildProbe(const sc2::Unit Nexus)
{
const ObservationInterface* observation = Observation();
if (CurrentMaxSupply >= observation->GetFoodUsed())
{
return false;
}
uint64_t mineral_target;
if (Nexus.assigned_harvesters < Nexus.ideal_harvesters)
{
Actions()->UnitCommand(&Nexus, ABILITY_ID::TRAIN_PROBE);
return true;
}
else if (FindNearestGeaser(Nexus.pos, mineral_target, MAX_GEASER_DISTANCE))
{
Actions()->UnitCommand(&Nexus, ABILITY_ID::TRAIN_PROBE);
return true;
}
return false;
}
void CryptBot::OnUnitEnterVision(const Unit *unit)
{
if (unit->unit_type == sc2::UNIT_TYPEID::TERRAN_SCV
|| unit->unit_type == sc2::UNIT_TYPEID::PROTOSS_PROBE
|| unit->unit_type == sc2::UNIT_TYPEID::ZERG_DRONE)
{
const Units NewUnits = Observation()->GetUnits(sc2::Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_PROBE));
for (const Unit *Nextunit : NewUnits)
{
if (Nextunit->tag != ScoutingUnitTag && sc2::Distance3D(Nextunit->pos, unit->pos) < 100)
{
Actions()->UnitCommand(Nextunit, ABILITY_ID::ATTACK, unit);
return;
}
}
}
else if (unit->unit_type == sc2::UNIT_TYPEID::PROTOSS_NEXUS
|| unit->unit_type == sc2::UNIT_TYPEID::ZERG_HATCHERY
|| unit->unit_type == sc2::UNIT_TYPEID::ZERG_LAIR
|| unit->unit_type == sc2::UNIT_TYPEID::ZERG_HIVE
|| unit->unit_type == sc2::UNIT_TYPEID::TERRAN_COMMANDCENTER
|| unit->unit_type == sc2::UNIT_TYPEID::TERRAN_ORBITALCOMMAND
|| unit->unit_type == sc2::UNIT_TYPEID::TERRAN_PLANETARYFORTRESS
|| unit->unit_type == sc2::UNIT_TYPEID::TERRAN_COMMANDCENTERFLYING)
{
// Begin cannon rush
}
}
void CryptBot::OnBuildingConstructionComplete(const Unit* unit)
{
if (unit->unit_type == sc2::UNIT_TYPEID::PROTOSS_PYLON)
{
if (Distance2D(RushLocation, unit->pos) < 20)
{
std::cout << "Rush Pylon Built";
}
}
}
void CryptBot::OnUnitCreated(const Unit *unit)
{
if (unit->alliance != sc2::Unit::Alliance::Self)
{
return;
}
const ObservationInterface* observation = Observation();
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::PROTOSS_NEXUS: {
Expanding = false;
ExpandingFrame = 0;
break;
}
case UNIT_TYPEID::PROTOSS_PYLON:
{
if (Distance2D(unit->pos, *StartPosition) > Distance2D(unit->pos, RushLocation))
{
CurrentOffenseno = 0;
}
}
break;
case UNIT_TYPEID::PROTOSS_PHOTONCANNON:
{
if (Distance2D(unit->pos, *StartPosition) > Distance2D(unit->pos, RushLocation))
{
CurrentOffenseno++;
}
else
{
CurrentDefenseCannons++;
}
}
break;
case UNIT_TYPEID::PROTOSS_ADEPT:
case UNIT_TYPEID::PROTOSS_ARCHON:
case UNIT_TYPEID::PROTOSS_CARRIER:
case UNIT_TYPEID::PROTOSS_COLOSSUS:
case UNIT_TYPEID::PROTOSS_DARKTEMPLAR:
case UNIT_TYPEID::PROTOSS_DISRUPTOR:
case UNIT_TYPEID::PROTOSS_HIGHTEMPLAR:
case UNIT_TYPEID::PROTOSS_IMMORTAL:
case UNIT_TYPEID::PROTOSS_INTERCEPTOR:
case UNIT_TYPEID::PROTOSS_MOTHERSHIP:
case UNIT_TYPEID::PROTOSS_MOTHERSHIPCORE:
case UNIT_TYPEID::PROTOSS_OBSERVER:
case UNIT_TYPEID::PROTOSS_ORACLE:
case UNIT_TYPEID::PROTOSS_PHOENIX:
case UNIT_TYPEID::PROTOSS_SENTRY:
case UNIT_TYPEID::PROTOSS_STALKER:
case UNIT_TYPEID::PROTOSS_TEMPEST:
case UNIT_TYPEID::PROTOSS_VOIDRAY:
case UNIT_TYPEID::PROTOSS_WARPPRISM:
case UNIT_TYPEID::PROTOSS_ZEALOT:
{
for (auto& ThisBG : BattleGroups)
{
if (ThisBG.UnitType == unit->unit_type.ToType() && ThisBG.Members.size() < BATTLEGROUP_SIZE && !ThisBG.Attacking)
{
ThisBG.Members.push_back(unit->tag);
if (ThisBG.Members.size() >= BATTLEGROUP_SIZE)
{
ThisBG.Attacking = true;
}
break;
}
else if (unit->unit_type.ToType() == UNIT_TYPEID::PROTOSS_MOTHERSHIP)
{
ThisBG.Members.push_back(unit->tag);
break;
}
}
BattleGroup BG;
BG.Attacking = false;
BG.engaged_tag = 0;
BG.UnitType = unit->unit_type.ToType();
BG.Members.push_back(unit->tag);
BattleGroups.push_back(BG);
break;
Units enemy_units = observation->GetUnits(Unit::Alliance::Enemy, IsAttackable());
if (enemy_units.empty())
{
ScoutWithUnit(*unit, Observation());
}
}
}
if (observation->GetFoodUsed() > (GetCurrentMaxSupply() - 5) && observation->GetGameLoop() > 15)
{
bool ShouldBuildPylon = true;
const Units NewUnits = Observation()->GetUnits(sc2::Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_PYLON));
for (const auto &u : NewUnits)
{
if (u->build_progress < 1.0f)
{
// Pylon currently being built, no need to build another one
ShouldBuildPylon = false;
break;
}
}
if (ShouldBuildPylon)
{
TryBuildStructure(ABILITY_ID::BUILD_PYLON);
}
}
}
void CryptBot::ManageBattleGroups(const ObservationInterface* observation)
{
Units EnemyBuildings = observation->GetUnits(Unit::Alliance::Enemy, IsBuilding());
Units EnemyArmy = observation->GetUnits(Unit::Alliance::Enemy, IsArmy());
Units EnemyGroundArmy = observation->GetUnits(Unit::Alliance::Enemy, IsGroundArmy());
const Point2D nullpos = Point2D(0, 0);
for (auto &ThisBG : BattleGroups)
{
if (ThisBG.Members.empty())
{
continue;
}
if (!ThisBG.Attacking)
{
// Still building;
continue;
}
if (ThisBG.engaged_tag != 0)
{
const Unit *EngagedUnit = observation->GetUnit(ThisBG.engaged_tag);
if (EngagedUnit != nullptr)
{
// Finish killing this unit
continue;
}
}
const Unit *Primary = observation->GetUnit(ThisBG.Members.back());
if (Primary != nullptr)
{
int64_t AttackableUnit = 0;
if (Primary->unit_type.ToType() == UNIT_TYPEID::PROTOSS_DARKTEMPLAR)
{
AttackableUnit = FindNearestUnit(Primary->pos, EnemyArmy, ATTACK_TYPE::FLYING);
}
else
{
AttackableUnit = FindNearestUnit(Primary->pos, EnemyGroundArmy, ATTACK_TYPE::GROUND);
}
if (AttackableUnit == 0)
{
AttackableUnit = FindNearestUnit(Primary->pos, EnemyBuildings);
}
for (int64_t VoidRayTag : ThisBG.Members)
{
const Unit *VRUnit = observation->GetUnit(VoidRayTag);
if (VRUnit != nullptr)
{
if (AttackableUnit != 0)
{
/*
if (VRUnit->weapon_cooldown <= 0.0f)
{
Actions()->UnitCommand(VRUnit, ABILITY_ID::EFFECT_VOIDRAYPRISMATICALIGNMENT);
}
*/
Actions()->UnitCommand(VRUnit, ABILITY_ID::ATTACK, observation->GetUnit(AttackableUnit));
}
else
{
// Enter search pattern
if (Distance2D(VRUnit->pos, ThisBG.ScoutingTarget) < 5)
{
ThisBG.ScoutingTarget.x = 0.0f;
ThisBG.ScoutingTarget.y = 0.0f;
}
if ((ThisBG.ScoutingTarget.x == 0 && ThisBG.ScoutingTarget.y == 0) )
{
Point2D target_pos;
if (TryFindRandomPathableLocation(VRUnit->tag, target_pos)) {
Actions()->UnitCommand(VRUnit, ABILITY_ID::SMART, target_pos);
ThisBG.ScoutingTarget = target_pos;
return;
}
}
}
}
}
}
}
}
int64_t CryptBot::FindNearestUnit(const Point2D& start, Units UnitSet, ATTACK_TYPE AttackType) {
float distance = std::numeric_limits<float>::max();
int64_t ReturnedUnit = 0;
bool FoundUnit = false, FoundUnitType = false;
if (AttackType == ATTACK_TYPE::BOTH)
{
FoundUnitType = true;
}
for (const auto& u : UnitSet) {
float d = DistanceSquared2D(u->pos, start);
if (d < distance || FoundUnitType == false) {
FoundUnit = true;
if (AttackType == ATTACK_TYPE::BOTH)
{
distance = d;
ReturnedUnit = u->tag;
}
else if (FoundUnitType && d < distance)
{
ATTACK_TYPE UnitCanAttack = Strategy::CanAttack(u->unit_type.ToType());
if (UnitCanAttack == ATTACK_TYPE::BOTH || UnitCanAttack == AttackType)
{
distance = d;
ReturnedUnit = u->tag;
}
}
else if (FoundUnitType == false && d < distance)
{
distance = d;
ReturnedUnit = u->tag;
ATTACK_TYPE UnitCanAttack = Strategy::CanAttack(u->unit_type.ToType());
if (UnitCanAttack == ATTACK_TYPE::BOTH || UnitCanAttack == AttackType)
{
FoundUnitType = true;
}
}
}
}
if (!FoundUnit)
{
return 0;
}
return ReturnedUnit;
}
void CryptBot::OnUnitIdle(const Unit *unit) {
if (unit->tag == RushUnitTag && RushPylon == true)
{
ScoutingUnitTag = RushUnitTag;
TryBuildStructure(ABILITY_ID::BUILD_PYLON, UNIT_TYPEID::PROTOSS_PROBE, GetNearestBuildableLocationFor(ABILITY_ID::BUILD_PYLON, RushLocation, QueryType::None, PylonSearchParams), true);
RushPylon = false;
}
switch (unit->unit_type.ToType()) {
case UNIT_TYPEID::PROTOSS_PROBE: {
if (unit->tag != ScoutingUnitTag)
{
uint64_t valid_mineral_patch;
FindNearestMineralPatch(unit->pos, valid_mineral_patch);
Actions()->UnitCommand(unit, ABILITY_ID::HARVEST_GATHER, Observation()->GetUnit(valid_mineral_patch));
return;
}
}
}
}
bool CryptBot::BuildAvailableGeaser(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 closestGeyster = 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;
closestGeyster = geyser->tag;
}
}
}
// In the case where there are no more available geysers nearby
if (closestGeyster == 0) {
return false;
}
return TryBuildStructure(build_ability, worker_type, closestGeyster);
}
bool CryptBot::FindNearestGeaser(const Point2D &start, uint64_t& target, float MaxDistance)
{
const ObservationInterface* observation = Observation();
float distance = MaxDistance;
Units geysers = observation->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_ASSIMILATOR));
for (const auto& u : geysers) {
if(u->assigned_harvesters < u->ideal_harvesters)
{
float d = DistanceSquared2D(u->pos, start);
if (d < distance ) {
distance = d;
target = u->tag;
}
}
}
if (distance == MaxDistance) {
return false;
}
return true;
}
size_t CryptBot::CountUnitType(const ObservationInterface* observation, UnitTypeID unit_type) {
return observation->GetUnits(Unit::Alliance::Self, IsUnit(unit_type)).size();
}
void CryptBot::EconStrat(const ObservationInterface *observation)
{
Units bases = observation->GetUnits(Unit::Alliance::Self, IsTownHall());
Units NearbyGeasers = observation->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_ASSIMILATOR));
Units Probes = observation->GetUnits(Unit::Alliance::Self, IsUnit(UNIT_TYPEID::PROTOSS_PROBE));
bool ShouldExpandBase = true;
int workerMin = 0;
if (bases.size() > 1)
{
workerMin = 3;
}
for (const auto& base : bases)
{
if (!base->orders.empty())
{
ShouldExpandBase = false;
// Already making a probe
continue;
}
int32_t CurrentGeysers = 0;
for (const auto& geyser : NearbyGeasers)
{
if (DistanceSquared2D(base->pos, geyser->pos) < MAX_GEASER_DISTANCE)
{
CurrentGeysers ++;
if (geyser->assigned_harvesters < geyser->ideal_harvesters)
{
for (const auto& probe : Probes)
{
if (probe->tag != ScoutingUnitTag)
{
if (probe->orders.empty())
{
Actions()->UnitCommand(probe, ABILITY_ID::HARVEST_GATHER, geyser);
break;
}
UnitOrder CurrentOrder = probe->orders.back();
if (CurrentOrder.ability_id == ABILITY_ID::HARVEST_GATHER)
{
if (CurrentOrder.target_unit_tag == 0)
{
Actions()->UnitCommand(probe, ABILITY_ID::HARVEST_GATHER, geyser);
break;
}
const Unit *TargetUnit = observation->GetUnit(CurrentOrder.target_unit_tag);
if (TargetUnit == nullptr || TargetUnit->unit_type == UNIT_TYPEID::NEUTRAL_MINERALFIELD)