-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRobotPlayer
More file actions
1541 lines (1420 loc) · 48 KB
/
RobotPlayer
File metadata and controls
1541 lines (1420 loc) · 48 KB
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 examplefuncsplayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import battlecode.common.*;
public strictfp class RobotPlayer {
static RobotController rc;
static int GARDENER_DISTRESS_CHANNEL = 2;
static int GARDENER_PRODUCTION_CHANNEL = 1;
static int GARDENER_NUM_CHANNEL = 10;
static int ARCHON_DISTRESS_CHANNEL = 3;
static int ARCHON_NUM_CHANNEL = 4;
static int ARCHON_PRODUCTION_CHANNEL = 5;
static int ARCHON_TOTAL_CHANNEL = 6;
static int ARCHON_FUCK_CHANNEL = 7;
static int GARDENER_SCHEDULE_CHANNEL = 8;
static int GARDENER_AVAILABILITY_CHANNEL = 9;
static int BIO_NUM_CHANNEL = 11;
static int TARGET_X_COORD = 20;
static int TARGET_Y_COORD = 21;
static Team ourTeam;
static Team opponentTeam;
/**
* run() is the method that is called when a robot is instantiated in the Battlecode world.
* If this method returns, the robot dies!
**/
@SuppressWarnings("unused")
public static void run(RobotController rc) throws GameActionException {
// This is the RobotController object. You use it to perform actions from this robot,
// and to get information on its current status.
RobotPlayer.rc = rc;
ourTeam = rc.getTeam();
opponentTeam = rc.getTeam().opponent();
// Here, we've separated the controls into a different method for each RobotType.
// You can add the missing ones or rewrite this into your own control structure.
switch (rc.getType()) {
case ARCHON:
runArchon();
break;
case GARDENER:
runGardener();
break;
case SOLDIER:
runSoldier();
break;
case LUMBERJACK:
runLumberjack();
break;
case SCOUT:
runScout();
break;
case TANK:
runTank();
break;
}
}
static void runArchon() throws GameActionException {
MapLocation[] initialArchonLoc = rc.getInitialArchonLocations(rc.getTeam());
int numberOfArchons = initialArchonLoc.length;
rc.broadcast(ARCHON_TOTAL_CHANNEL, numberOfArchons);
int archonNum = 0;
boolean pinged = false;
int numDecs = 0;
for(int i = 0; i < numberOfArchons; i++) {
if(rc.getLocation().equals(initialArchonLoc[i])) {
archonNum = i;
}
}
rc.broadcast(GARDENER_PRODUCTION_CHANNEL, (-1 * numberOfArchons)-1);
// The code you want your robot to perform every round should be in this loop
while (true) {
// Try/catch blocks stop unhandled exceptions, which cause your robot to explode
try {
donateBullets();
if (rc.isBuildReady()){
/*if (rc.getTeamBullets()/500 > rc.readBroadcast(GARDENER_PRODUCTION_CHANNEL)+3){
rc.broadcast(GARDENER_DISTRESS_CHANNEL, rc.readBroadcast(GARDENER_DISTRESS_CHANNEL)+1);
}*/
Direction checkDir = Direction.getNorth();
boolean checker = false;
for (int i = 10; i<360; i+=10){
if (rc.canHireGardener(checkDir)){
checker = true;
break;
}
checkDir.rotateLeftDegrees(i);
}
if(!checker){
rc.broadcast(ARCHON_FUCK_CHANNEL, 1);
}
}
if (rc.readBroadcast(ARCHON_FUCK_CHANNEL) == 1){
archonNum = cycleArchonNums(archonNum);
if (archonNum == 2){
rc.broadcast(ARCHON_FUCK_CHANNEL, 0);
}
}
// every 300 rounds produce a gardener unless surrounded
if (archonNum == 0 && rc.getRoundNum()<1100 && rc.getRoundNum()%300 == 100){
rc.broadcast(GARDENER_DISTRESS_CHANNEL, rc.readBroadcast(GARDENER_DISTRESS_CHANNEL) + 1);
}
if (archonNum == 0 && rc.getRoundNum() % 50 < 3) {
System.out.println(rc.getRoundNum());
if (rc.readBroadcast(GARDENER_NUM_CHANNEL) < rc.getTeamBullets()/75&& !checkSurrounded()){
rc.broadcast(GARDENER_DISTRESS_CHANNEL, rc.readBroadcast(GARDENER_DISTRESS_CHANNEL) + 1);
}
}
if (rc.getHealth()<10 && !pinged){
rc.broadcast(ARCHON_DISTRESS_CHANNEL, rc.readBroadcast(ARCHON_DISTRESS_CHANNEL)+1);
rc.broadcast(ARCHON_NUM_CHANNEL, archonNum);
rc.broadcast(ARCHON_TOTAL_CHANNEL, rc.readBroadcast(ARCHON_TOTAL_CHANNEL)-1);
pinged = true;
}
if (rc.readBroadcast(ARCHON_TOTAL_CHANNEL)-rc.readBroadcast(ARCHON_DISTRESS_CHANNEL)<rc.readBroadcast(ARCHON_TOTAL_CHANNEL)){
if (archonNum>rc.readBroadcast(ARCHON_NUM_CHANNEL) && numDecs<rc.readBroadcast(ARCHON_DISTRESS_CHANNEL)){
archonNum--;
numDecs++;
}
}
// Generate a random direction
Direction dir = randomDirection();
int distressSignals = rc.readBroadcast(GARDENER_DISTRESS_CHANNEL);
int deadGardeners = rc.readBroadcast(GARDENER_PRODUCTION_CHANNEL);
if (rc.canHireGardener(dir) && checkGardeners(deadGardeners, distressSignals)) {
// figure out which archon will build gardener
if(Math.abs(deadGardeners) % (rc.readBroadcast(ARCHON_TOTAL_CHANNEL)) == archonNum) {
rc.hireGardener(dir);
rc.broadcast(GARDENER_NUM_CHANNEL, rc.readBroadcast(GARDENER_NUM_CHANNEL)+1);
}
}
Direction randDir = randomDirection();
if (rc.canMove(randDir)){
tryMove(randDir);
}
// Clock.yield() makes the robot wait until the next turn, then it will perform this loop again
Clock.yield();
} catch (Exception e) {
System.out.println("Archon Exception");
e.printStackTrace();
}
}
}
static boolean checkSurrounded() {
boolean surrounded = false;
TreeInfo[] trees = rc.senseNearbyTrees(1);
RobotInfo[] robots = rc.senseNearbyRobots(1);
if(trees.length + robots.length > 4) {
surrounded = true;
}
return surrounded;
}
private static int cycleArchonNums(int archonNum) throws GameActionException{
switch (archonNum){
case 0:
return 2;
case 1:
return 0;
case 2:
return 1;
}
return -1;
}
static boolean checkGardeners(int deadGardeners, int distressSignals) throws GameActionException{
if (distressSignals > deadGardeners){
return true;
}
return false;
}
static void donateBullets() throws GameActionException {
float cost = (float) (7.5 + rc.getRoundNum() * 12.5 / 3000);
float bullets = rc.getTeamBullets();
float surplus = bullets - 500 + rc.getRoundNum() / cost;
if(surplus > 0) {
rc.donate(surplus - (surplus % cost));
}
// if can win this turn, then win this turn
if(rc.getTeamVictoryPoints() + (bullets / cost) >= 1000){
rc.donate(bullets);
}
if(rc.getRoundLimit()-rc.getRoundNum() < 3){
rc.donate(bullets);
}
}
static void runGardener() throws GameActionException {
boolean toProduce = false;
boolean pinged = false;
int buildCount = 0;
rc.broadcast(GARDENER_PRODUCTION_CHANNEL, rc.readBroadcast(GARDENER_PRODUCTION_CHANNEL)+1);
//int id = Math.abs(rc.readBroadcast(GARDENER_PRODUCTION_CHANNEL))%2;
int id = 1;
int spawnRound = rc.getRoundNum();
int pauseProductionRound = rc.getRoundLimit();
int productionCount = 0;
RobotInfo[] initialRobots = rc.senseNearbyRobots(2, ourTeam);
int numArchs = rc.getInitialArchonLocations(ourTeam).length;
Direction oppDir = randomDirection();
for(RobotInfo robot : initialRobots) {
if(robot.type.equals(RobotType.ARCHON)) {
oppDir = rc.getLocation().directionTo(robot.location).opposite();
}
}
int bioMax = 50;
if (numArchs == 1){
bioMax = 50;
}
if (numArchs == 2){
bioMax = 80;
}
if (numArchs == 3){
bioMax = 100;
}
boolean hasBuiltLumb = false;
while (true){
// Try/catch blocks stop unhandled exceptions, which cause your robot to explode
try {
if (rc.getRoundNum() > 200) {
hasBuiltLumb = true;
}
donateBullets();
// Generate a random direction
Direction dir = randomDirection();
buildCount++;
if(rc.getRoundNum() < spawnRound + 8 + (rc.readBroadcast(GARDENER_NUM_CHANNEL) * 2)) {
tryMove(oppDir);
} else if(!hasBuiltLumb){
Direction LumbDir = findOpenDirection(RobotType.LUMBERJACK);
if (LumbDir != null && rc.canBuildRobot(RobotType.LUMBERJACK, LumbDir)){
rc.buildRobot(RobotType.LUMBERJACK, LumbDir);
hasBuiltLumb = true;
}
}
if (!rc.hasMoved() && hasBuiltLumb) {
GardenersMove(id);
}
TreeInfo[] neutralTrees = rc.senseNearbyTrees(rc.getType().sensorRadius, Team.NEUTRAL);
if (neutralTrees.length > 0) {
shakeTrees(neutralTrees);
}
if (rc.getHealth()<10 && !pinged){
rc.broadcast(GARDENER_DISTRESS_CHANNEL, rc.readBroadcast(GARDENER_DISTRESS_CHANNEL)+1);
pinged = true;
rc.broadcast(GARDENER_NUM_CHANNEL, rc.readBroadcast(GARDENER_NUM_CHANNEL)-1);
}
if (rc.readBroadcast(BIO_NUM_CHANNEL)>bioMax) {
toProduce = false;
} else {
toProduce = true;
}
RobotType potentialRobot = null;
int scheduleNum = rc.readBroadcast(GARDENER_SCHEDULE_CHANNEL) % 6;
if (rc.getRoundNum()>200){
if (scheduleNum == 0){
potentialRobot = RobotType.SCOUT;
}
if (scheduleNum == 1){
potentialRobot = RobotType.LUMBERJACK;
}
if (scheduleNum == 2){
potentialRobot = RobotType.LUMBERJACK;
}
if (scheduleNum == 3){
potentialRobot = RobotType.SOLDIER;
}
if (scheduleNum == 4){
potentialRobot = RobotType.SOLDIER;
}
if (scheduleNum == 5){
potentialRobot = RobotType.SOLDIER;
}
}else if(scheduleNum == 0){
potentialRobot = RobotType.SCOUT;
}else if(scheduleNum>0){
potentialRobot = RobotType.LUMBERJACK;
}
if(rc.isBuildReady() && toProduce && id == 1) {
dir = findOpenDirection(potentialRobot);
if (dir != (null) && rc.canBuildRobot(potentialRobot, dir)) {
rc.buildRobot(potentialRobot, dir);
if (potentialRobot!=RobotType.SCOUT){
rc.broadcast(BIO_NUM_CHANNEL, rc.readBroadcast(BIO_NUM_CHANNEL)+1);
}
rc.broadcast(GARDENER_SCHEDULE_CHANNEL, rc.readBroadcast(GARDENER_SCHEDULE_CHANNEL)+1);
}
}
// Clock.yield() makes the robot wait until the next turn, then it will perform this loop again
Clock.yield();
} catch (Exception e) {
System.out.println("Gardener Exception");
e.printStackTrace();
}
}
}
static Direction findOpenDirection(RobotType potentialRobot) {
boolean open = false;
int degrees = 0;
Direction openDir = new Direction(degrees);
while(!open && degrees < 360) {
openDir = openDir.rotateRightDegrees(5);
degrees += 5;
if(rc.canBuildRobot(potentialRobot, openDir)) {
open = true;
}
}
if (open) {
return openDir;
} else {
return null;
}
}
static void GardenersMove(int id) throws GameActionException{
try{
Direction treeDir = rc.getLocation().directionTo(rc.getInitialArchonLocations(rc.getTeam())[0]);
boolean north = true;
boolean south = true;
boolean east = true;
boolean west = true;
if (!rc.canPlantTree(Direction.getNorth())){
north = false;
}
if (!rc.canPlantTree(Direction.getEast())){
east = false;
}
if (!rc.canPlantTree(Direction.getSouth())){
south = false;
}
if (!rc.canPlantTree(Direction.getWest())){
west = false;
}
if (!north || !east || !west || !south){
if (!north){
treeDir = Direction.getNorth();
}
if (!east){
treeDir = Direction.getEast();
}
if (!west){
treeDir = Direction.getWest();
}
if (!south){
treeDir = Direction.getSouth();
}
for (int i = 0; i<72; i++){
treeDir = treeDir.rotateLeftDegrees(5);
if (rc.canPlantTree(treeDir)){
break;
}
}
}
//tree gardener id 0
/*if(id == 0){
TreeInfo[] nearbyTrees = rc.senseNearbyTrees(2, rc.getTeam());
if(nearbyTrees.length < 6){
if(rc.canPlantTree(treeDir)){
rc.plantTree(treeDir);
}else{
Direction dir = rc.getLocation().directionTo(nearbyTrees[0].location);
for(int i = 60; i < 360; i+= 60){
if(rc.canPlantTree(dir.rotateLeftDegrees(i))){
rc.plantTree(dir.rotateLeftDegrees(i));
}
}
}
}
if(nearbyTrees.length > 0){
float[] remainingHealth = new float[nearbyTrees.length];
for(int j = 0; j < nearbyTrees.length; j++){
remainingHealth[j] = nearbyTrees[j].getHealth();
}
int lowestSpot = 0;
for(int k = 1; k < remainingHealth.length; k++){
if(remainingHealth[k] < remainingHealth[lowestSpot]){
lowestSpot = k;
}
}
if (rc.canMove(rc.getLocation().directionTo(nearbyTrees[lowestSpot].location))){
rc.move(rc.getLocation().directionTo(nearbyTrees[lowestSpot].location));
}
if (nearbyTrees.length > lowestSpot){
if(rc.canWater(nearbyTrees[lowestSpot].getLocation())){
rc.water(nearbyTrees[lowestSpot].getLocation());
}
}
}
}else{*/
TreeInfo[] nearbyTrees = rc.senseNearbyTrees(2, rc.getTeam());
boolean skipped = false;
if(nearbyTrees.length < 5){
if(nearbyTrees.length==0){
if(rc.canPlantTree(treeDir)){
rc.plantTree(treeDir);
}
}else if (nearbyTrees.length == 5){
skipped = false;
}else{
Direction dir = rc.getLocation().directionTo(nearbyTrees[0].location);
for(int i = 60; i < 360; i+= 60){
if(rc.canPlantTree(dir.rotateLeftDegrees(i))){
if(rc.canPlantTree(dir.rotateLeftDegrees(i+60))){
rc.plantTree(dir.rotateLeftDegrees(i));
}else{
skipped = true;
}
if (skipped && rc.senseNearbyTrees(2, rc.getTeam()).length == 4 && rc.canPlantTree(dir.rotateLeftDegrees(i))){
rc.plantTree(dir.rotateLeftDegrees(i));
}
}
}
}
}
if(nearbyTrees.length > 0){
float[] remainingHealth = new float[nearbyTrees.length];
for(int j = 0; j < nearbyTrees.length; j++){
remainingHealth[j] = nearbyTrees[j].getHealth();
}
int lowestSpot = 0;
for(int k = 1; k < remainingHealth.length; k++){
if(remainingHealth[k] < remainingHealth[lowestSpot]){
lowestSpot = k;
}
}
if (rc.canMove(rc.getLocation().directionTo(nearbyTrees[lowestSpot].location))){
rc.move(rc.getLocation().directionTo(nearbyTrees[lowestSpot].location));
}
if (nearbyTrees.length > lowestSpot){
if(rc.canWater(nearbyTrees[lowestSpot].getLocation())){
rc.water(nearbyTrees[lowestSpot].getLocation());
}
}
}
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
static void runSoldier() throws GameActionException {
MapLocation targetLoc2 = rc.getInitialArchonLocations(opponentTeam)[0];
Direction targetDir2 = rc.getLocation().directionTo(targetLoc2);
int spawnRound = rc.getRoundNum();
boolean pinged = false;
boolean movingLeft = false;
// The code you want your robot to perform every round should be in this loop
while (true) {
// Try/catch blocks stop unhandled exceptions, which cause your robot to explode
try {
MapLocation myLoc = rc.getLocation();
RobotType myType = rc.getType();
// get target location and direction
int xCoord = rc.readBroadcast(TARGET_X_COORD);
int yCoord = rc.readBroadcast(TARGET_Y_COORD);
MapLocation targetLoc = new MapLocation(xCoord, yCoord);
if (!pinged && rc.getHealth()<10){
rc.broadcast(BIO_NUM_CHANNEL, rc.readBroadcast(BIO_NUM_CHANNEL)-1);
pinged = true;
}
RobotInfo[] initialRobots = rc.senseNearbyRobots(2, ourTeam);
Direction oppDir = randomDirection();
for(RobotInfo robot : initialRobots) {
if(robot.type.equals(RobotType.GARDENER)) {
oppDir = rc.getLocation().directionTo(robot.location).opposite();
}
}
if(rc.getRoundNum() < spawnRound + 5) {
tryMove(oppDir);
}
//sense enemy robots and trees
Direction targetDir = null;
RobotInfo[] enemies = rc.senseNearbyRobots(myType.sensorRadius, opponentTeam);
TreeInfo[] trees = rc.senseNearbyTrees(myType.sensorRadius, opponentTeam);
// broadcast enemy archons and gardeners
if (rc.readBroadcast(TARGET_X_COORD) == 0) {
reportEnemyArchonsAndGardeners(enemies);
}
// if target eliminated, broadcast : else, set targetDir
if (xCoord != 0) {
boolean eliminated = checkTarget(targetLoc, targetDir, enemies);
if (eliminated) {
rc.broadcast(TARGET_X_COORD, 0);
rc.broadcast(TARGET_Y_COORD, 0);
} else {
targetDir = myLoc.directionTo(targetLoc);
}
}
MapLocation nearbyTargetLoc = chooseTarget(enemies, trees);
Direction nearbyTargetDir = null;
// if no target, choose a nearby enemy
if (nearbyTargetLoc != null) {
nearbyTargetDir = myLoc.directionTo(nearbyTargetLoc);
}
// move towards enemy
if (nearbyTargetLoc != null) {
RobotInfo b = null;
if (rc.canSenseLocation(nearbyTargetLoc)) {
b = rc.senseRobotAtLocation(nearbyTargetLoc);
}
if(!rc.hasMoved() && b != null){
MapLocation shifty = stayShifty(b);
if(shifty != null){
moveToLoc(shifty, movingLeft);
}
}
}
if (nearbyTargetDir != null) {
if(!rc.hasMoved()) {
moveToLoc(nearbyTargetLoc, movingLeft);
}
} else if (targetDir != null) {
if(!rc.hasMoved()) {
moveToLoc(targetLoc, movingLeft);
}
}
// randomly move
if (!rc.hasMoved()) {
Direction randomDir = randomDirection();
if(rc.canMove(randomDir)){
tryMove(randomDir);
}
}
// if enemies, attack them
if(nearbyTargetLoc != null){
attack(nearbyTargetLoc);
}
// Clock.yield() makes the robot wait until the next turn, then it will perform this loop again
Clock.yield();
} catch (Exception e) {
System.out.println("Soldier Exception");
e.printStackTrace();
}
}
}
static boolean checkTarget(MapLocation targetLoc, Direction targetDir, RobotInfo[] enemies) throws GameActionException {
boolean eliminated = false;
if(rc.getLocation().isWithinDistance(targetLoc, 1)) {
int pos = 0;
boolean found = false;
while (!found && pos < enemies.length) {
RobotInfo robot = enemies[pos];
RobotType type = robot.type;
if (type.equals(RobotType.ARCHON) || type.equals(RobotType.GARDENER)) {
found = true;
rc.broadcast(TARGET_X_COORD, (int) robot.location.x);
rc.broadcast(TARGET_Y_COORD, (int) robot.location.y);
}
pos++;
}
// set eliminated to if archon is not found
eliminated = !found;
}
return eliminated;
}
static void shakeTrees(TreeInfo[] neutralTrees) throws GameActionException {
MapLocation myLoc = rc.getLocation();
if (neutralTrees.length > 0){
ArrayList<TreeInfo> bulleted = new ArrayList<TreeInfo>();
for (int i = 0; i<neutralTrees.length; i++){
if (neutralTrees[i].containedBullets > 0){
bulleted.add(neutralTrees[i]);
}
}
if (bulleted.size() > 0){
float dist = 10000;
int closest = 0;
for (int i = 0; i<bulleted.size(); i++){
float d = bulleted.get(i).getLocation().distanceTo(myLoc);
if (d<dist){
dist = d;
closest = i;
}
}
Direction moveTo = myLoc.directionTo(bulleted.get(closest).getLocation());
if(moveTo != null && rc.canMove(moveTo) && !rc.hasMoved() && rc.getType() != RobotType.GARDENER){
rc.move(moveTo);
}
}
for (int i = 0; i<neutralTrees.length; i++){
MapLocation loc = neutralTrees[i].location;
if(rc.canShake(loc)){
rc.shake(loc);
}
}
}
}
static MapLocation chooseTarget(RobotInfo[] enemies, TreeInfo[] trees) throws GameActionException {
MapLocation target = null;
// choose robot target
if(enemies.length > 0) {
target = chooseRobotTarget(enemies);
}
// if no robot target, choose tree target
if(target == null && trees.length > 0) {
target = chooseTreeTarget(trees);
}
// return target location
return target;
}
static void attack(MapLocation target) throws GameActionException {
if(target != null) {
Direction targetDir = rc.getLocation().directionTo(target);
int shotType = chooseShotType(target);
if(shotType == 1 && rc.canFireSingleShot()) {
rc.fireSingleShot(targetDir);
} else if (shotType == 3 && rc.canFireTriadShot()) {
rc.fireTriadShot(targetDir);
} else if(rc.canFirePentadShot()) {
rc.firePentadShot(targetDir);
}
}
}
static MapLocation chooseTreeTarget(TreeInfo[] trees) throws GameActionException {
float[] treePoints = new float[trees.length];
MapLocation myLoc = rc.getLocation();
float minPoint = 1000;
MapLocation treeLoc = trees[0].location;
for(int i = 0; i < trees.length; i++) {
TreeInfo tree = trees[i];
float distance = myLoc.distanceTo(tree.location);
treePoints[i] = distance * distance / 10;
treePoints[i] += tree.health / tree.maxHealth * 10;
if (checkFriendlyFire(myLoc.directionTo(tree.location), distance)) {
treePoints[i] += 1000;
}
if(treePoints[i] < minPoint) {
minPoint = treePoints[i];
treeLoc = tree.location;
}
}
return treeLoc;
}
static float findObstacleBetweenTreeTarget(float distance, TreeInfo target) throws GameActionException {
float points = 0;
float minDistance = rc.getType().bodyRadius + 1;
float maxDistance = distance - target.getRadius();
MapLocation myLoc = rc.getLocation();
Direction dir = myLoc.directionTo(target.location);
boolean isObstacle = false;
// loop through locations between yourself and target at intervals of 1
while(!isObstacle && minDistance < maxDistance) {
// get location between you and target
MapLocation loc = myLoc.add(dir, minDistance);
if(!rc.canSenseLocation(loc)) {
break;
}
// find if robot or tree at location
if(rc.isLocationOccupiedByRobot(loc)) {
//obstacle found
isObstacle = true;
RobotInfo robot = rc.senseRobotAtLocation(loc);
// if our team, don't attack; if opponent, then replace target with obstacle
if(robot.team.equals(ourTeam)) {
points += 1000;
}
} else if (rc.isLocationOccupiedByTree(loc)) {
//obstacle found
isObstacle = true;
TreeInfo tree = rc.senseTreeAtLocation(loc);
// if ourTeam, don't attack; if opponent, replace with 18; if neutral, don't attack
Team treeTeam = tree.team;
if(treeTeam.equals(ourTeam)) {
points += 1000;
} else if (treeTeam.equals(Team.NEUTRAL)) {
points += 1000;
}
}
minDistance++;
}
return points;
}
static boolean checkFriendlyFire(Direction d, float distance){
RobotInfo[] friendlyBots = rc.senseNearbyRobots(distance, rc.getTeam());
TreeInfo[] neutralTrees = rc.senseNearbyTrees(distance, Team.NEUTRAL);
int i = 0;
MapLocation myLoc = rc.getLocation();
boolean friendlyFire = false;
while (!friendlyFire && i < friendlyBots.length) {
if(willPotentiallyCollideWithMapLoc(d, friendlyBots[i].getLocation(), myLoc)){
friendlyFire = true;
}
i++;
}
if (!friendlyFire) {
i = 0;
while (!friendlyFire && i < neutralTrees.length) {
if(willPotentiallyCollideWithMapLoc(d, neutralTrees[i].getLocation(), myLoc)){
friendlyFire = true;
}
i++;
}
}
return friendlyFire;
}
static float findObstacleBetweenTarget(float distance, RobotInfo target, Map<RobotType, Integer> RobotTypePoints) throws GameActionException {
float points = 0;
float minDistance = rc.getType().bodyRadius + 1;
float maxDistance = distance - target.getRadius();
MapLocation myLoc = rc.getLocation();
Direction dir = myLoc.directionTo(target.location);
boolean isObstacle = false;
// loop through locations between yourself and target at intervals of 1
while(!isObstacle && minDistance < maxDistance) {
// get location between you and target
MapLocation loc = myLoc.add(dir, minDistance);
if(!rc.canSenseLocation(loc) || minDistance < maxDistance) {
break;
}
// find if robot or tree at location
if(rc.isLocationOccupiedByRobot(loc)) {
//obstacle found
isObstacle = true;
RobotInfo robot = rc.senseRobotAtLocation(loc);
// if our team, don't attack; if opponent, then replace target with obstacle
if(robot.team.equals(ourTeam)) {
points += 1000;
} else {
points += RobotTypePoints.get(robot.type) * 3 - RobotTypePoints.get(target.type);
}
} else if (rc.isLocationOccupiedByTree(loc)) {
//obstacle found
isObstacle = true;
TreeInfo tree = rc.senseTreeAtLocation(loc);
// if ourTeam, don't attack; if opponent, replace with 18; if neutral, don't attack
if(tree.team.equals(ourTeam)) {
points += 1000;
} else if (tree.team.equals(opponentTeam)) {
points += 18 - RobotTypePoints.get(target.type);
} else {
points += 1000;
}
}
minDistance++;
}
return points;
}
static MapLocation chooseRobotTarget(RobotInfo[] enemies) throws GameActionException {
Map<RobotType, Integer> RobotTypePoints = new HashMap<RobotType, Integer>();
RobotTypePoints.put(RobotType.TANK, 0);
RobotTypePoints.put(RobotType.LUMBERJACK, 1);
RobotTypePoints.put(RobotType.SOLDIER, 2);
RobotTypePoints.put(RobotType.GARDENER, 3);
RobotTypePoints.put(RobotType.SCOUT, 4);
RobotTypePoints.put(RobotType.ARCHON, 5);
float[] robotPoints = new float[enemies.length];
MapLocation myLoc = rc.getLocation();
for(int i = 0; i < enemies.length; i++) {
MapLocation enemyLoc = enemies[i].getLocation();
RobotType enemyType = enemies[i].getType();
//Adds 4 things together:
//1: gets the distance to the other robot, squares it
float distance = myLoc.distanceTo(enemyLoc);
robotPoints[i] = distance * distance /10;
//2: gets the percent health (as a decimal) that the other robot is at and multiplies that decimal by 10
robotPoints[i] += (float)(enemies[i].getHealth()/enemyType.maxHealth)*10;
//3: gets the map value for the type of robot
robotPoints[i] += RobotTypePoints.get(enemyType)*3;
//4: finds if there is something in between you and the target
if(checkFriendlyFire(myLoc.directionTo(enemyLoc), distance)){
robotPoints[i] += 1000;
}
}
int counter = 0;
for(int i = 1; i < robotPoints.length; i++){
if(robotPoints[counter] < robotPoints[i]){
counter = i;
}
}
if(robotPoints[counter] > 999) {
return null;
} else {
return enemies[counter].location;
}
}
static int chooseShotType(MapLocation target) {
int shots = 1;
if (rc.canSenseLocation(target)){
int robotsNearTarget = rc.senseNearbyRobots(target, 5, opponentTeam).length;
int treesNearTarget = rc.senseNearbyTrees(target, 5, opponentTeam).length;
if(robotsNearTarget + treesNearTarget >= 2) {
float distance = rc.getLocation().distanceTo(target);
Direction dir = rc.getLocation().directionTo(target);
if(robotsNearTarget + treesNearTarget >= 3) {
boolean friendlyFire = false;
Direction offThirty = dir.rotateLeftDegrees(30);
Direction offFifteen = dir.rotateLeftDegrees(15);
Direction onFifteen = dir.rotateRightDegrees(15);
Direction onThirty = dir.rotateRightDegrees(30);
if (checkFriendlyFire(offThirty, distance)) {
friendlyFire = true;
}
if (!friendlyFire && checkFriendlyFire(offFifteen, distance)) {
friendlyFire = true;
}
if (!friendlyFire && checkFriendlyFire(offThirty, distance)) {
friendlyFire = true;
}
if (!friendlyFire && checkFriendlyFire(onFifteen, distance)) {
friendlyFire = true;
}
if (!friendlyFire && checkFriendlyFire(onThirty, distance)) {
friendlyFire = true;
}
if (!friendlyFire) {
shots = 5;
}
}
if (shots == 1) {
boolean friendlyFire = false;
Direction offTwenty = dir.rotateLeftDegrees(20);
Direction onTwenty = dir.rotateRightDegrees(20);
if (checkFriendlyFire(offTwenty, distance)) {
friendlyFire = true;
}
if (!friendlyFire && checkFriendlyFire(onTwenty, distance)) {
friendlyFire = true;
}
if (!friendlyFire) {
shots = 3;
}
}
}
}
return shots;
}
static void runLumberjack() throws GameActionException {
int spawnRound = rc.getRoundNum();
boolean pinged = false;
boolean movingLeft = false;
// The code you want your robot to perform every round should be in this loop
while (true) {
// Try/catch blocks stop unhandled exceptions, which cause your robot to explode
try {
// get target location and direction
int xCoord = rc.readBroadcast(TARGET_X_COORD);
int yCoord = rc.readBroadcast(TARGET_Y_COORD);
MapLocation targetLoc = new MapLocation(xCoord, yCoord);
//sense enemy robots and trees
Direction targetDir = null;
RobotInfo[] enemies = rc.senseNearbyRobots(rc.getType().sensorRadius, opponentTeam);
TreeInfo[] enemyTrees = rc.senseNearbyTrees(rc.getType().sensorRadius, opponentTeam);
TreeInfo[] neutralTrees = rc.senseNearbyTrees(rc.getType().sensorRadius, Team.NEUTRAL);
if (!pinged &&rc.getHealth()<10){
rc.broadcast(BIO_NUM_CHANNEL, rc.readBroadcast(BIO_NUM_CHANNEL)-1);
pinged = true;
}
RobotInfo[] initialRobots = rc.senseNearbyRobots(2, ourTeam);
Direction oppDir = randomDirection();
for(RobotInfo robot : initialRobots) {
if(robot.type.equals(RobotType.GARDENER)) {
oppDir = rc.getLocation().directionTo(robot.location).opposite();
}
}
if(rc.getRoundNum() < spawnRound + 5) {
tryMove(oppDir);
}
// broadcast enemy archons and gardeners
if (rc.readBroadcast(TARGET_X_COORD) == 0) {
reportEnemyArchonsAndGardeners(enemies);
}
// if target eliminated, broadcast : else, set targetDir
if (xCoord != 0) {
boolean eliminated = checkTarget(targetLoc, targetDir, enemies);
if (eliminated) {
rc.broadcast(TARGET_X_COORD, 0);
rc.broadcast(TARGET_Y_COORD, 0);
} else {
targetDir = rc.getLocation().directionTo(targetLoc);
}
}
RobotInfo[] nearbyBots = rc.senseNearbyRobots(2);
if(nearbyBots.length > 0){
MapLocation myLoc = rc.getLocation();
int numOfEnemies = 0;
int numOfFriends = 0;
for(int i = 0; i < nearbyBots.length; i++){
if(nearbyBots[i].getTeam().equals(ourTeam)){
numOfFriends++;
}
else if(nearbyBots[i].getTeam().equals(opponentTeam)) {
numOfEnemies++;
}
}
//ratio of friends to enemies
if(numOfFriends < numOfEnemies){
rc.strike();
}
}
MapLocation nearbyTargetLoc = chooseTarget(enemies, enemyTrees);
Direction nearbyTargetDir = null;
// if no target, choose a nearby enemy
if (nearbyTargetLoc != null) {
nearbyTargetDir = rc.getLocation().directionTo(nearbyTargetLoc);
}
// move towards enemy
if (nearbyTargetDir != null) {
if(!rc.hasMoved()) {
moveToLoc(nearbyTargetLoc, movingLeft);
}
} else if (targetDir != null) {
if(!rc.hasMoved()) {
moveToLoc(targetLoc,movingLeft);
}
}
if (neutralTrees.length > 0) {
TreeInfo closestTree = neutralTrees[0];
float distance = closestTree.location.distanceTo(rc.getLocation());
for(int i = 1; i < neutralTrees.length; i++) {
float neutralDistance = neutralTrees[i].location.distanceTo(rc.getLocation());
if(neutralDistance < distance) {
distance = neutralDistance;
closestTree = neutralTrees[i];
}
}
if(rc.canShake(closestTree.location)){
rc.shake(closestTree.location);
}
if(rc.canChop(closestTree.location)) {
rc.chop(closestTree.location);
} else if(!rc.hasMoved() && rc.canMove(rc.getLocation().directionTo(closestTree.location))){
rc.move(rc.getLocation().directionTo(closestTree.location));
}
}
if(!rc.hasMoved()) {
tryMove(randomDirection());
}
// Clock.yield() makes the robot wait until the next turn, then it will perform this loop again
Clock.yield();
} catch (Exception e) {
System.out.println("Lumberjack Exception");
e.printStackTrace();
}
}
}
static void runTank() throws GameActionException {
//choose location here so it doesnt change every round
MapLocation myLocation = rc.getLocation();
MapLocation[] initialArchons = rc.getInitialArchonLocations(opponentTeam);
Direction attack = myLocation.directionTo(initialArchons[(int)Math.random()*initialArchons.length]);
int spawnRound = rc.getRoundNum();
boolean pinged = false;
boolean movingLeft = false;
// The code you want your robot to perform every round should be in this loop
while (true) {