-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRobotPlayer.java
2028 lines (1933 loc) · 64.8 KB
/
RobotPlayer.java
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
package team015; //So the program knows what to call our bot
import battlecode.common.*; //imports Battlecode UI
import java.util.Random; //Use this instead of Math.random(); seeded by the robot's id so more likely to be random than Math.random
import java.util.Arrays;
import java.util.Collection;
import java.util.ArrayList;
import java.lang.Math;
public class RobotPlayer{
/**
* Useful members
*
* $rc: the RobotController for this robot. Static so all methods can use it
* $randall: our source of all randomness
* $ourTeam: our Team, to save bytecodes
* $opponentTeam: Opponent's (NOT ZOMBIES) team
*
* $zombieDenLocations: locations of the zombie dens
* $enemyArchonIDs: the IDs of enemy archons
*/
static RobotController rc;
static Random randall;
static Team ourTeam;
static Team opponentTeam;
static int[] zombieRounds;
static MapLocation[] zombieDenLocations;
static ArrayList<Integer> enemyArchonIDs = new ArrayList<Integer>();
// Collection of <Archon ID, Archon Location, Round that measurement was taken>
static ArrayList<Triple<Integer,MapLocation,Integer>> mostRecentEnemyArchonLocations = new ArrayList<Triple<Integer,MapLocation,Integer>>();
static Direction[] DIRECTIONS = new Direction[]{Direction.NORTH,Direction.NORTH_EAST,Direction.EAST,Direction.SOUTH_EAST,Direction.SOUTH,Direction.SOUTH_WEST,Direction.WEST,Direction.NORTH_WEST};
/**
* run
*
* @param r: the RobotController passed in by the battlecode software. stored in $rc
* @selftype: stores our type so we don't have to call getType() each time, saving bytecodes
* @s: the innerclass instance of our robot
*
*/
public static void run(RobotController r){
rc = r;
randall = new Random(rc.getID());
RobotType selftype = rc.getType();
ourTeam = rc.getTeam();
opponentTeam = ourTeam.opponent();
if(selftype == RobotType.ARCHON){
Archon s = new RobotPlayer().new Archon();
s.run();
}else if(selftype == RobotType.SCOUT){
Scout s = new RobotPlayer().new Scout();
s.run();
}
else if(selftype == RobotType.TURRET){
Turret s = new RobotPlayer().new Turret();
s.run();
}
else if(selftype == RobotType.TTM){
TTM s = new RobotPlayer().new TTM();
s.run();
}
else if(selftype == RobotType.GUARD || selftype == RobotType.SOLDIER || selftype == RobotType.VIPER){
Swarmer s = new RobotPlayer().new Swarmer();
s.run();
}
}
/**
*
* Class Viper
*
* The class outlining our viper bots
*
*/
private class Viper{
public MapLocation enemyArchonLocation;
public boolean goOffense;
public Viper() {
enemyArchonLocation = rc.getInitialArchonLocations(opponentTeam)[0];
goOffense = true;
}
public void run() {
while(true) {
try {
// Use Guard AI (move out) until there are enough soldiers ammassed around, then go towards enemy archon and attack
Signal[] signals = rc.emptySignalQueue();
if (signals.length > 0) {
for (Signal s : signals) {
// receive a message containing enemy archon ID
if (s.getTeam() == ourTeam) {
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
if(f.type == 2){
int xPos = f.ints.first;
int yPos = f.ints.second;
enemyArchonLocation = new MapLocation(xPos, yPos);
goOffense = true;
}
}
}
}
if(rc.isCoreReady() && goOffense){
RESOURCE_FUNCTIONS.BUG(enemyArchonLocation);
}
if(rc.isWeaponReady()){
MapLocation target = RESOURCE_FUNCTIONS.viperAttackTarget();
if(target != null && rc.canAttackLocation(target)){
rc.attackLocation(target);
}
else{
Direction rubbleDirection = RESOURCE_FUNCTIONS.findRubbleDirection();
if(rubbleDirection != null){
rc.clearRubble(rubbleDirection);
}
}
}
Clock.yield();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
*
* Class Turret
*
* The class outlining our turret bots
*
*/
private class Turret{
//MapLocation enemyLocation;
public MapLocation enemyArchonLocation;
public Turret(){
}
public void run(){
while(true){
try{Signal[] signals = rc.emptySignalQueue();
if (signals.length > 0) {
for (Signal s : signals) {
// receive a message containing enemy archon ID
if (s.getTeam() == ourTeam) {
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
if(f.type == 2){
int xPos = f.ints.first;
int yPos = f.ints.second;
enemyArchonLocation = new MapLocation(xPos, yPos);
}
}
}
}
if(enemyArchonLocation.distanceSquaredTo(rc.getLocation())>40){
rc.pack();
}
else if(rc.isWeaponReady()){
RESOURCE_FUNCTIONS.attackWeakestEnemy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private class TTM{
//MapLocation enemyLocation;
public MapLocation enemyArchonLocation;
//public boolean goOffense;
public TTM(){
}
public void run(){
while(true){
try{
Signal[] signals = rc.emptySignalQueue();
if (signals.length > 0) {
for (Signal s : signals) {
// receive a message containing enemy archon ID
if (s.getTeam() == ourTeam) {
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
if(f.type == 2){
int xPos = f.ints.first;
int yPos = f.ints.second;
enemyArchonLocation = new MapLocation(xPos, yPos);
}
}
}
}
if(enemyArchonLocation.distanceSquaredTo(rc.getLocation())<40){
rc.unpack();
}
if(rc.isCoreReady()){
RESOURCE_FUNCTIONS.BUG(enemyArchonLocation);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Class Soldier
*
* The class outlining our soldier bots
*
*/
private class Soldier {
public MapLocation enemyArchonLocation;
public boolean goOffense;
public int moveCount;
public Soldier() {
enemyArchonLocation = rc.getInitialArchonLocations(opponentTeam)[0];
moveCount = 0;
goOffense = true;
}
public void run() {
while(true) {
try {
// Use Guard AI (move out) until there are enough soldiers ammassed around, then go towards enemy archon and attack
Signal[] signals = rc.emptySignalQueue();
if (signals.length > 0) {
for (Signal s : signals) {
// receive a message containing enemy archon ID
if (s.getTeam() == ourTeam) {
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
if(f.type == 2){
int xPos = f.ints.first;
int yPos = f.ints.second;
enemyArchonLocation = new MapLocation(xPos, yPos);
goOffense = true;
}
/*if (moveCount < 1 && rc.senseRobot(s.getID()).type == RobotType.ARCHON) {
MapLocation archonLocation = f.senderLocation;
Direction archonDirection = rc.getLocation().directionTo(archonLocation);
Direction oppositeDirection = archonDirection.opposite();
if (rc.isCoreReady() && rc.canMove(oppositeDirection)) {
moveCount += 1;
rc.move(oppositeDirection);
}
}*/
}
// intercept a message containing enemy archon location
/*if (s.getTeam() == opponentTeam && enemyArchonIDs.contains(s.getID())) {
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
}*/
}
}
if(rc.isCoreReady() && goOffense){
RESOURCE_FUNCTIONS.BUG(enemyArchonLocation);
}
if(rc.isWeaponReady()){
/* RobotInfo[] robots = rc.senseNearbyRobots();
for(RobotInfo robot: robots) {
if((robot.team == Team.ZOMBIE || robot.team == opponentTeam) && rc.canAttackLocation(robot.location)) {
rc.attackLocation(robot.location);
break;
}
}*/
RESOURCE_FUNCTIONS.attackWeakestEnemy();
if(rc.isWeaponReady()){
Direction rubbleDirection = RESOURCE_FUNCTIONS.findRubbleDirection();
if(rubbleDirection != null){
rc.clearRubble(rubbleDirection);
}
}
}
// If there are enough scouts around, move out towards enemy Archon
/* (mostRecentEnemyArchonLocations.size() > 0 && RESOURCE_FUNCTIONS.numberOfRobotsInRadiusAndThoseRobots(RobotType.SOLDIER, RobotType.SOLDIER.sensorRadiusSquared, rc.getTeam()).first > 5) {
RESOURCE_FUNCTIONS.BUG(RESOURCE_FUNCTIONS.mostRecentEnemyArchonLocation());
}*/
Clock.yield();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Class Guard
*
* The class outlining our Guard bots
*
*/
private class Guard {
public MapLocation archonLocation;
public int moveCount;
public Guard() {
moveCount = 0;
}
public void run() {
while(true){
try{
archonLocation = RESOURCE_FUNCTIONS.scanFriendlyArchonLocation();
Signal[] signals = rc.emptySignalQueue();
if(signals.length > 0){ //if == 0, no signals, so not ready
for(Signal s: signals){
if(moveCount < 1 && s.getTeam() == ourTeam){
FancyMessage f = FancyMessage.getFromRecievedSignal(s);
if(f.type == 0){
MapLocation archonCreatorLocation = f.senderLocation;
Direction archonDirection = rc.getLocation().directionTo(archonCreatorLocation);
Direction oppositeDirection = archonDirection.opposite();
if(rc.isCoreReady()){
if(rc.canMove(oppositeDirection)){
rc.move(oppositeDirection);
moveCount += 1;
}
}
}
}
}
}
if(rc.isCoreReady()){
RobotInfo[] robots = rc.senseNearbyRobots();
boolean targetFound = false;
if(robots != null && archonLocation != null){
for(RobotInfo robot:robots){
if(robot.location.distanceSquaredTo(archonLocation) < 25){
targetFound = true;
break;
}
}
}
if(targetFound == false && archonLocation != null){
RESOURCE_FUNCTIONS.BUG(archonLocation);
}
}
if(rc.isWeaponReady()){
RobotInfo[] robots = rc.senseNearbyRobots();
/*for(RobotInfo robot: robots) {
if((robot.team == Team.ZOMBIE) && rc.canAttackLocation(robot.location)) {
rc.attackLocation(robot.location);
break;
}
}
for(RobotInfo robot: robots) {
if((robot.team == opponentTeam) && rc.canAttackLocation(robot.location)) {
rc.attackLocation(robot.location);
break;
}
}*/
RESOURCE_FUNCTIONS.attackWeakestEnemy();
//If didn't attack anyone that is adjacent
if(rc.isWeaponReady() && robots != null && archonLocation != null){
MapLocation target = null;
for(RobotInfo robot: robots) {
if((robot.team == Team.ZOMBIE) && robot.location.distanceSquaredTo(archonLocation) < 25) {
target = robot.location;
break;
}
}
for(RobotInfo robot: robots) {
if((robot.team == opponentTeam) && robot.location.distanceSquaredTo(archonLocation) < 25) {
target = robot.location;
break;
}
}
if(target != null){
RESOURCE_FUNCTIONS.BUG(target);
}
}
if(rc.isWeaponReady()){
Direction rubbleDirection = RESOURCE_FUNCTIONS.nearestRubble();
if(rubbleDirection != null){
rc.clearRubble(rubbleDirection);
}
}
}
Clock.yield();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public class Swarmer{
public static final double SWARM_RATIO = 4.0 / 3; //ratio of all spaces to robots
public MapLocation coreLocation; //location of archon
public MapLocation target; //location of hostile target [zombiedens, enemyarchons]
public int moveType; //type of movement: 0==movement around friendly archon, 1==movement towards "stepping stone target", 2==movement towards hostile target
public int targetID;
public RobotType targetType = null;
public boolean locked = false;
public Swarmer(){
coreLocation = null;
target = null;
moveType = 0;
}
public void run(){
while(true) {
try {
Signal[] signals = rc.emptySignalQueue();
for(int i = 0; i < signals.length; i++){
if(signals[i].getTeam() == ourTeam){
FancyMessage s = FancyMessage.getFromRecievedSignal(signals[i]);
if(s.isMessage){
if(s.type == 0){ //Type 0: give our bots location of archon
coreLocation = s.senderLocation;
}else if(s.type == 1){ //Type 1: give our bots a "stepping stone" location
if(moveType == 0 && !locked){ //ignored if we already have a target, or if in locked mode
moveType = 1;
target = new MapLocation(s.ints.first,s.ints.second);
}
}else if(s.type == 2){ //Type 2: force robots back into regular swarming
locked = true;
moveType = 0;
}else if(s.type == 3){ //Type 3: if in forced swarming, brings back to regular behavior
locked = false;
}else if(s.type == 5){
target = new MapLocation(s.ints.first, s.ints.second);
moveType = 1;
}
}
}
}
if(moveType != 2 && rc.isWeaponReady()){
RESOURCE_FUNCTIONS.attackWeakestEnemy();
//if has not attacked yet
if(rc.isWeaponReady()) {
Direction rubbleDirection = RESOURCE_FUNCTIONS.clearRubbleForPath(target);
if(rubbleDirection == null) {
rubbleDirection = RESOURCE_FUNCTIONS.findRubbleDirection();
}
if(rubbleDirection != null && rc.isCoreReady()){
rc.clearRubble(rubbleDirection);
}
}
}
if(rc.isCoreReady()){
if(moveType == 0 && coreLocation != null){
int startDir = randall.nextInt(8);
int[] tryOrder = new int[]{0,1,-1,2,-2,3,-3,4};
double swarmRadius = RESOURCE_FUNCTIONS.getGoodSwarmRadius();
MapLocation current = rc.getLocation();
boolean hasMoved = false;
int minInd = -1;
int minDistance = -1;
for(int i = 0; i < tryOrder.length && !hasMoved; i++){
MapLocation moveTo = current.add(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i]));
int distance = moveTo.distanceSquaredTo(coreLocation);
if(distance > 2 && distance < (int)(swarmRadius + 1) && rc.canMove(current.directionTo(moveTo))){
rc.move(current.directionTo(moveTo));
hasMoved = true;
}
if(rc.canMove(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i]))){
int newdist = current.add(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i])).distanceSquaredTo(coreLocation);
if(minInd == -1 || newdist < minDistance){
minInd = i;
minDistance = newdist;
}
}
}
if(!hasMoved && minInd != -1){
rc.move(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[minInd]));
}
if(!locked){
RobotInfo[] enemiesInSight = rc.senseHostileRobots(rc.getLocation(),rc.getType().sensorRadiusSquared);
for(int i = 0; i < enemiesInSight.length; i++){
if(enemiesInSight[i].type == RobotType.ARCHON || enemiesInSight[i].type == RobotType.ZOMBIEDEN){
target = enemiesInSight[i].location;
targetID = enemiesInSight[i].ID;
moveType = 2;
rc.broadcastSignal((int)(rc.getLocation().distanceSquaredTo(coreLocation) * 1.1));
}
}
}
}
if(moveType == 1){
int startDir = randall.nextInt(8);
int[] tryOrder = new int[]{0,1,-1,2,-2,3,-3,4};
int minInd = -1;
int minDistance = -1;
MapLocation current = rc.getLocation();
for(int i = 0; i < tryOrder.length; i++){
if(rc.canMove(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i]))){
int newdist = current.add(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i])).distanceSquaredTo(target);
if(minInd == -1 || newdist < minDistance){
minInd = i;
minDistance = newdist;
}
}
}
if(minInd != -1){
rc.move(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[minInd]));
}
RobotInfo[] enemiesInSight = rc.senseHostileRobots(rc.getLocation(),rc.getType().sensorRadiusSquared);
for(int i = 0; i < enemiesInSight.length; i++){
if(enemiesInSight[i].type == RobotType.ARCHON || enemiesInSight[i].type == RobotType.ZOMBIEDEN){
target = enemiesInSight[i].location;
targetID = enemiesInSight[i].ID;
moveType = 2;
rc.broadcastSignal((int)(rc.getLocation().distanceSquaredTo(coreLocation) * 1.1));
}
}
}
if(moveType == 2){
if(rc.canSenseRobot(targetID)){
RobotInfo rr = rc.senseRobot(targetID);
target = rr.location;
targetType = rr.type;
if(rc.isWeaponReady() && rc.canAttackLocation(target)){
rc.attackLocation(target);
}
}else{
target = null;
targetID = -1;
moveType = 0;
}
int startDir = randall.nextInt(8);
int[] tryOrder = new int[]{0,1,-1,2,-2,3,-3,4};
int minInd = -1;
int minDistance = -1;
MapLocation current = rc.getLocation();
for(int i = 0; i < tryOrder.length && target != null; i++){
if(rc.canMove(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i]))){
int newdist = current.add(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[i])).distanceSquaredTo(target);
if((minInd == -1 || newdist < minDistance) && (targetType != RobotType.ZOMBIEDEN || newdist > 2)){
minInd = i;
minDistance = newdist;
}
}
}
if(minInd != -1 && rc.isCoreReady()){
rc.move(RESOURCE_FUNCTIONS.intToDir(startDir + tryOrder[minInd]));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Clock.yield();
}
}
}
/**
* class Archon
*
* The class outlining our Archon bots
*
*/
private class Archon{
public MapLocation target;
public boolean goToTarget;
public boolean alpha;
/**
* Constructor
*
*/
public Archon(){
zombieRounds = rc.getZombieSpawnSchedule().getRounds();
target = null;
goToTarget = false;
if(rc.getLocation().equals(rc.getInitialArchonLocations(ourTeam)[0])){
alpha = true;
}else{
target = rc.getInitialArchonLocations(ourTeam)[0];
goToTarget = true;
}
}
/**
* run
*
* @while loop: prevents this method from ending. method ending == robot dies D:
* @try-catch: catches any errors, prints stack trace, but keeps running.
*
*/
public void run(){
while(true){
try{
Signal[] signals = rc.emptySignalQueue();
for(int i = 0; i < signals.length; i++){
if(signals[i].getTeam() == ourTeam){
FancyMessage x = FancyMessage.getFromRecievedSignal(signals[i]);
if(!x.isMessage){
FancyMessage.sendMessage(1,x.senderLocation.x,x.senderLocation.y,(int)(RESOURCE_FUNCTIONS.getGoodSwarmRadius() * 1.1));
break;
}
else if(alpha && x.type == 4 && !goToTarget && rc.getRobotCount() > 30){
int xPos = x.ints.first;
int yPos = x.ints.second;
target = new MapLocation(xPos, yPos);
goToTarget = true;
}
else if(!alpha && x.type == 5) {
target = new MapLocation(x.ints.first, x.ints.second);
goToTarget = true;
}
}
}
//If it can, always tries to build Scouts.
if(rc.isCoreReady()){
if(alpha && rc.getRoundNum() % 10 == 0){
if(goToTarget) {
FancyMessage.sendMessage(5, target.x, target.y, 1000);
}
else {
FancyMessage.sendMessage(0,0,0,(int)(1000));
}
}
if(!alpha){
if(goToTarget && rc.getRoundNum() % 2 == 0 && rc.getLocation().distanceSquaredTo(target) > 2){
RESOURCE_FUNCTIONS.BUG(target);
}
}
if(goToTarget && alpha) {
RESOURCE_FUNCTIONS.BUG(target);
}
MapLocation neutral = RESOURCE_FUNCTIONS.findAdjacentNeutralRobot();
if(neutral != null){
rc.activate(neutral);
}
RobotType type = RESOURCE_FUNCTIONS.chooseRobotType();
if(rc.isCoreReady()){
if(RESOURCE_FUNCTIONS.tryBuild(type)){ //See function in RESOURCE_FUNCTIONS to know what it does
//After building scout, waits a turn, then signals it the location, so it has a good idea of where base is
//Also signals the scout which type to become
//FancyMessage.sendMessage(4, 0, 0, 6400);
//Clock.yield();
/*Triple<Integer,Integer,Integer> scoutType = getScoutInitType();
//Check if near zombie round
if (mostRecentEnemyArchonLocations.size() != 0 && RESOURCE_FUNCTIONS.isCloseToZombieSpawnRound()) {
scoutType = getScoutHerdingType();
}
FancyMessage.sendMessage(0,scoutType.first | scoutType.second,scoutType.third,3);*/
}
}
}
Clock.yield();
}catch(Exception e){
e.printStackTrace();
}
}
}
public Triple<Integer,Integer,Integer> getScoutInitType(){
return new Triple<Integer,Integer,Integer>(0,0,0);
}
public Triple<Integer, Integer, Integer> getScoutHerdingType(){
MapLocation enemyArchonLocation = RESOURCE_FUNCTIONS.mostRecentEnemyArchonLocation();
System.out.println("enemyArchonLocation =" + enemyArchonLocation);
int xPosition = (enemyArchonLocation.x + 16000) << 2;
int yPosition = (enemyArchonLocation.y + 16000);
return new Triple<Integer, Integer, Integer>(1,xPosition,yPosition);
}
public void movement(){
try{
if(rc.senseHostileRobots(rc.getLocation(), RobotType.ARCHON.sensorRadiusSquared) != null){
RESOURCE_FUNCTIONS.escapeEnemy();
}
if(rc.senseNearbyRobots(RobotType.ARCHON.sensorRadiusSquared, Team.NEUTRAL) != null){
RobotInfo neutral = RESOURCE_FUNCTIONS.seekNeutralRobots();
if(neutral != null){
RESOURCE_FUNCTIONS.BUG(neutral.location);
}
}
if(rc.sensePartLocations(RobotType.ARCHON.sensorRadiusSquared) != null){
MapLocation part = RESOURCE_FUNCTIONS.seekNearestPart();
if(part != null){
RESOURCE_FUNCTIONS.BUG(part);
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
/**
* class Scout
*
* The class outlining our Scout bots
*
* @base: where the Archon that spawned it is located. This is how we know where to herd zombies away from
* @disciples: the number of zombies that are within sight range and following us.
* @last: the last tile the robot was on
*
*/
private class Scout{
MapLocation base = null;
int disciples;
MapLocation last = null;
MapLocation mostRecentArchonLocation = null;
/**
* Constructor
*
* initializes disciples value.
*
*/
public Scout(){
disciples = 0;
base = rc.getLocation();
}
/**
* run
*
* @while,try: same purpose as Archon.run
* @signals: all signals in queue. should be updated to make sure its just from our archons, but basically averages all this stuff out so incase several archons tell it where they are it will avoid all of them
* @approxxCoordinates: averages all (assumed) archon coordinates
*
*/
public void run(){
while(true){
/*if(base == null){
Signal[] signals = rc.emptySignalQueue();
if(signals.length > 0){
for(int i = 0; i < signals.length; i++){
if(signals[i].getTeam() == ourTeam){
FancyMessage x = FancyMessage.getFromRecievedSignal(signals[i]);
if(x.isMessage){
if(x.type == 0){
base = x.senderLocation;
if((x.ints.first & 3) == 0){
runAsArchonSearcher();
}else if((x.ints.first & 3) == 1){
mostRecentArchonLocation = new MapLocation((x.ints.first >>> 2) - 16000,x.ints.second - 16000);
System.out.println("Running as zombie herder to archon at (" + mostRecentArchonLocation.x + "," + mostRecentArchonLocation.y + ")");
runAsZombieHerder();
}else if((x.ints.first & 3) == 2){
runAsTurretSights(x.ints.second);
}
}
}
}
}
}
}*/
runAsArchonSearcher();
}
}
public void runAsArchonSearcher(){
boolean foundArchon = false;
while(!foundArchon){
try{
MapLocation enemyArchonLocation = RESOURCE_FUNCTIONS.scanArchonLocation();
if(enemyArchonLocation == null) {
enemyArchonLocation = RESOURCE_FUNCTIONS.scanZombieDen();
}
if(enemyArchonLocation != null){
int xPos = enemyArchonLocation.x;
int yPos = enemyArchonLocation.y;
FancyMessage.sendMessage(4, xPos, yPos, 6400);
foundArchon = true;
}else if(rc.isCoreReady()){
RESOURCE_FUNCTIONS.moveAsFarAwayAsPossibleFrom(base);
}
}catch(Exception e){
e.printStackTrace();
}
Clock.yield();
}
runAsZombieBomber();
}
public void runAsZombieHerder(){
while(true){
try{
Signal[] signals = rc.emptySignalQueue();
for(Signal s : signals){
if(s.getTeam() == ourTeam){
FancyMessage x = FancyMessage.getFromRecievedSignal(s);
if(x.type == 2){
mostRecentArchonLocation = new MapLocation(x.ints.first - 16000,x.ints.second - 16000);
}
}
}
if(rc.isCoreReady()){
if(stillHerding()){
if(mostRecentArchonLocation != null){
wiggle(mostRecentArchonLocation);
}else{
RESOURCE_FUNCTIONS.moveAsFarAwayAsPossibleFrom(base);
}
}else{
if(last != null){
if(rc.canMove(rc.getLocation().directionTo(last))){
rc.move(rc.getLocation().directionTo(last));
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
Clock.yield();
}
}
public void runAsTurretSights(int turretID){
while(true){
try{
Clock.yield();
}catch(Exception e){
e.printStackTrace();
}
}
}
public void runAsZombieBomber(){
while(true){
try{
if(mostRecentArchonLocation != null){
if(rc.getLocation().distanceSquaredTo(mostRecentArchonLocation) < 25 && rc.getInfectedTurns() > 0){
rc.disintegrate();
}else if(rc.isCoreReady()){
wiggle(mostRecentArchonLocation);
}
}else{
runAsArchonSearcher();
}
Clock.yield();
}catch(Exception e){
e.printStackTrace();
}
}
}
/**
* boolean stillHerding
*
* makes sure the bot doesn't lose its herd
*
* @zombos: array of all zombies within sight
* @return true if there's at least 3 zombies, or at least as many as there used to be, false otherwise
*
*/
public boolean stillHerding(){
RobotInfo[] zombos = rc.senseNearbyRobots(RobotType.SCOUT.sensorRadiusSquared,Team.ZOMBIE);
int count = 0;
for(int i = 0; i < zombos.length; i++){
if(zombos[i].type != RobotType.ZOMBIEDEN){
count++;
}
}
rc.setIndicatorString(1,"disciples:" + disciples + "::count:" + count + "::zombos.length" + zombos.length);
if(count >= 3 || count >= disciples){
disciples = count;
return true;
}
return false;
}
public boolean wiggle(MapLocation target) throws GameActionException{
int[] tryOrder = new int[]{0,1,-1,2,-2,3,-3,4};
int toTarget = RESOURCE_FUNCTIONS.dirToInt(rc.getLocation().directionTo(target));
for(int i = 0; i < tryOrder.length; i++){
if(rc.canMove(RESOURCE_FUNCTIONS.intToDir(toTarget + tryOrder[i]))){
last = rc.getLocation();
rc.move(RESOURCE_FUNCTIONS.intToDir(toTarget + tryOrder[i]));
return true;
}
}
return false;
}
}
/**
* $class RESOURCE_FUNCTIONS
*
* Not a class to instantiate, but rather one with some useful functions to use in various robots
*
*/
public static class RESOURCE_FUNCTIONS{
public static double getGoodSwarmRadius(){
int numRobots = rc.getRobotCount();
double radius = Math.sqrt((Swarmer.SWARM_RATIO * numRobots) / (2 * Math.PI));
if(radius >= 5){
return radius;
}
return 5;
}
/**
* Direction intToDir
*
* Simplifies choosing a random direction
*
* @param i: the integer to convert
* @return: the Direction corresponding to that integer
*
*/
public static Direction intToDir(int i){
return DIRECTIONS[(i + 8) % 8];
}
/**
* MapLocation scanArchonLocation
*
* @robots: list of all visible enemy robots
* @pos: tracks position of Archon in @robots
* @return MapLocation of last Archon in list if it exists, null if no Archon is seen.
*
*/
public static MapLocation scanArchonLocation() {
RobotInfo[] robots;
robots = rc.senseNearbyRobots(RobotType.SCOUT.sensorRadiusSquared, opponentTeam);
for(int i = 0; i < robots.length; i++) {
if(robots[i].type == RobotType.ARCHON) {
return robots[i].location;
}
}
return null;
}
/**
*
* MapLocation scanFriendlyArchonLocation
* @return location of friendly archon
*
*/
public static MapLocation scanFriendlyArchonLocation() {
RobotInfo[] robots;
robots = rc.senseNearbyRobots(RobotType.SCOUT.sensorRadiusSquared, ourTeam);
for(int i = 0; i < robots.length; i++) {
if(robots[i].type == RobotType.ARCHON) {
return robots[i].location;
}
}
return null;
}
public static MapLocation scanZombieDen() {
RobotInfo[] zombies = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.ZOMBIE);
if(zombies != null) {
for(RobotInfo zombie: zombies) {
if(zombie.type.equals(RobotType.ZOMBIEDEN)) {
return zombie.location;
}
}
}
return null;
}
/**
* int dirToInt
*
* Reverses intToDir
*
* @param d: Direction to convert
* @return: the integer corresponding to the Direction
*
*/
public static int dirToInt(Direction d){
for(int i = 0; i < DIRECTIONS.length; i++){
if(d.equals(DIRECTIONS[i])){
return i;
}
}
return 0;
}
/**
* boolean tryBuild
*
* failable build method. Attempts to construct a robot in a space adjacent to the caller
*
* @param rt: the RobotType of the bot to be built
* @param startDirection: optional parameter, inferred to be a random direction if not given. First direction to be checked
* @return true if robot is constructed, false otherwise
*
*/
public static boolean tryBuild(RobotType rt) throws GameActionException{
return tryBuild(rt,intToDir(randall.nextInt(8)));
}