-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.java
1352 lines (1228 loc) · 43 KB
/
Player.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
//import the API.
//See xxx for the javadocs.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import bc.*;
public class Player {
public static Direction[] directions = new Direction[8];
public static boolean finishedFactories = false;
public static boolean finishedRockets = false;
public static MapLocation swarmLoc;
public static Team myTeam;
public static Team opponentTeam;
public static PlanetMap earthMap;
public static Planet thisPlanet;
public static PlanetMap marsMap;
public static PlanetMap thisMap;
public static MapLocation landLoc = new MapLocation(Planet.Mars, 1, 1);
public static int buildTeamSize;
public static long totalKarboniteAmount;
public static MapLocation buildLoc = null;
public static MapLocation startLoc = null;
public static int karboniteCheckFrequency = 5;
public static HashMap<Integer, Integer> rocketLaunch = new HashMap<Integer, Integer>();
// size adaption variables
public static int troopSize;
public static int workforceSize;
public static int minTroopSwarmSize;
public static int minFactorySize;
// pathfinding static variables
public static Direction[][] spreadPathfindingMapEarthSwarm;
public static Direction[][] spreadPathfindingMapMarsSwarm;
public static Direction[][] spreadPathfindingMapEarthBuildLoc;
public static Direction[][] karboniteCollectionMap;
//can make robotDirections a class variable
public static HashMap<Integer, Integer> robotDirections = new HashMap<Integer, Integer>();
//directions already is a class variable, directions is the movement shit
//make maplocation dootadoot for saving locations, checking if things get stuck and such
public static HashMap<Integer, MapLocation> robotChecker = new HashMap<Integer, MapLocation>();
// production chances
public static int knightFactoryEarlyChance;
public static int rangerFactoryEarlyChance;
public static int healerFactoryEarlyChance;
// unit ArrayList variables
public static ArrayList<Unit> workers = new ArrayList<Unit>();
public static ArrayList<Unit> factories = new ArrayList<Unit>();
public static ArrayList<Unit> knights = new ArrayList<Unit>();
public static ArrayList<Unit> mages = new ArrayList<Unit>();
public static ArrayList<Unit> rangers = new ArrayList<Unit>();
public static ArrayList<Unit> rockets = new ArrayList<Unit>();
public static void main(String[] args) {
// Connect to the manager, starting the game
GameController gc = new GameController();
// get planet and planet map
thisPlanet = gc.planet();
earthMap = gc.startingMap(Planet.Earth);
marsMap = gc.startingMap(Planet.Mars);
thisMap = gc.startingMap(thisPlanet);
// don't build if on mars
if (thisPlanet.equals(Planet.Mars)) {
buildTeamSize = 0;
} else {
buildTeamSize = 4;
}
// Direction is a normal java enum.
directions[0] = Direction.North;
directions[1] = Direction.Northeast;
directions[2] = Direction.East;
directions[3] = Direction.Southeast;
directions[4] = Direction.South;
directions[5] = Direction.Southwest;
directions[6] = Direction.West;
directions[7] = Direction.Northwest;
myTeam = gc.team();
if (myTeam.equals(Team.Blue)) {
opponentTeam = Team.Red;
} else {
opponentTeam = Team.Blue;
}
// karbonite finding stuff
//map dimensions
int h, w;
if(thisPlanet.equals(Planet.Earth)){
h = (int) earthMap.getHeight();
w = (int) earthMap.getWidth();
}
else{
h = (int) marsMap.getHeight();
w = (int) marsMap.getWidth();
}
EarthDeposit[][] karboniteAmts = new EarthDeposit[w][h];
//loads matrix of earth deposits that have an earth deposit
for(int i = 0; i < w; i++){
for(int j = 0; j < h; j++){
MapLocation loca = new MapLocation(thisPlanet, i, j);
long x;
if(thisPlanet.equals(Planet.Earth)){
x = earthMap.initialKarboniteAt(loca);
}
else{
x = marsMap.initialKarboniteAt(loca);
}
if(x != 0){
karboniteAmts[i][j] = new EarthDeposit(loca, x);
}
else{
karboniteAmts[i][j] = new EarthDeposit(loca, 0);
}
totalKarboniteAmount += x;
}
}
// research code
gc.queueResearch(UnitType.Worker);
// round 25
if (h * w <= 1000) {
if (h * w <= 500) {
minFactorySize = 2;
minTroopSwarmSize = 15;
knightFactoryEarlyChance = 5;
rangerFactoryEarlyChance = 10;
healerFactoryEarlyChance = 10;
} else {
minFactorySize = 3;
minTroopSwarmSize = 18;
knightFactoryEarlyChance = 2;
rangerFactoryEarlyChance = 9;
healerFactoryEarlyChance = 10;
}
gc.queueResearch(UnitType.Ranger);
// round 50
gc.queueResearch(UnitType.Knight);
// round 75
gc.queueResearch(UnitType.Ranger);
// round 175
gc.queueResearch(UnitType.Healer);
// round 200
gc.queueResearch(UnitType.Rocket);
// round 250
gc.queueResearch(UnitType.Knight);
// round 325
gc.queueResearch(UnitType.Healer);
// round 425
gc.queueResearch(UnitType.Healer);
// round 525
gc.queueResearch(UnitType.Rocket);
//round 625
gc.queueResearch(UnitType.Knight);
// round 725 JAVELIN UNLOCKED
} else {
minFactorySize = 3;
minTroopSwarmSize = 20;
knightFactoryEarlyChance = 1;
rangerFactoryEarlyChance = 8;
healerFactoryEarlyChance = 10;
gc.queueResearch(UnitType.Ranger);
// round 50
gc.queueResearch(UnitType.Ranger);
// round 150
gc.queueResearch(UnitType.Knight);
// round 175
gc.queueResearch(UnitType.Healer);
// round 200
gc.queueResearch(UnitType.Rocket);
// round 250
gc.queueResearch(UnitType.Healer);
// round 350
gc.queueResearch(UnitType.Healer);
// round 450
gc.queueResearch(UnitType.Knight);
// round 525
gc.queueResearch(UnitType.Rocket);
//round 625
gc.queueResearch(UnitType.Knight);
// round 725 JAVELIN UNLOCKED
}
OrbitPattern earthPatt = gc.orbitPattern();
while (true) {
int roundNum = (int) gc.round();
System.out.println("Current round: "+roundNum);
// VecUnit is a class that you can think of as similar to ArrayList<Unit>, but immutable.
VecUnit units = gc.myUnits();
// set initial base
if (roundNum < 10 && buildLoc == null && units.size() != 0) {
startLoc = units.get(0).location().mapLocation();
buildLoc = findOpenAdjacentSpot(gc, startLoc);
spreadPathfindingMapEarthBuildLoc = updatePathfindingMap(buildLoc, earthMap, 10000);
}
if (roundNum == 100) {
karboniteCheckFrequency = 10;
}
if (roundNum == 200) {
karboniteCheckFrequency = 25;
}
if (gc.karbonite() > 400 && minFactorySize < 3) {
minFactorySize = 3;
}
if (gc.karbonite() > 600 && minFactorySize < 4) {
minFactorySize = 4;
}
if (gc.karbonite() > 800 && minFactorySize < 5) {
minFactorySize = 5;
}
for (int i = 0; i < units.size(); i++) {
Unit unit = units.get(i);
if (!unit.location().isInSpace() && !unit.location().isInGarrison()) {
switch (unit.unitType()) {
case Factory:
factories.add(unit);
break;
case Healer:
runHealer(unit, rockets, gc, units);
break;
case Knight:
knights.add(unit);
break;
case Mage:
mages.add(unit);
break;
case Ranger:
rangers.add(unit);
break;
case Rocket:
rockets.add(unit);
if(rocketLaunch.get(unit.id()) == null && unit.structureIsBuilt() == 1){
ResearchInfo k = gc.researchInfo();
int priorityTurn = roundNum;
long priorityVal = 1001;
long tempVal = 0;
long maybeBarrier = -1;
long halfPeriod = earthPatt.getPeriod()/2;
if(k.hasNextInQueue()){
if(k.nextInQueue().equals(UnitType.Rocket) && k.getLevel(UnitType.Rocket) == 1){
if(k.roundsLeft() < halfPeriod){
maybeBarrier = k.roundsLeft();
}
}
}
for(int m = 0; m < halfPeriod; m++){
if(maybeBarrier != -1 && m >= maybeBarrier){
tempVal = -1 * unit.rocketTravelTimeDecrease();
}
else{
tempVal = 0;
}
long x = earthPatt.duration(m + roundNum);
tempVal += x;
tempVal += m;
tempVal += roundNum;
if(tempVal < priorityVal){
if(m + roundNum < 750){
priorityTurn = m + roundNum;
priorityVal = x + m + roundNum;
}
}
}
rocketLaunch.put(unit.id(), priorityTurn);
}
runRocket(gc, unit);
break;
case Worker:
MapLocation myLoc = unit.location().mapLocation();
int visionRange = (int) unit.visionRange();
VecUnit enemies = gc.senseNearbyUnitsByTeam(myLoc, visionRange, opponentTeam);
if (enemies.size() > 0) {
Unit enemy = findClosestEnemy(gc, unit, enemies);
if (swarmLoc == null) {
swarmLoc = enemy.location().mapLocation();
if (thisPlanet.equals(Planet.Earth)) {
spreadPathfindingMapEarthSwarm = updatePathfindingMap(swarmLoc, thisMap, 10000);
} else {
spreadPathfindingMapMarsSwarm = updatePathfindingMap(swarmLoc, thisMap, 10000);
}
}
if (gc.isMoveReady(unit.id())) {
runAwayWorker(gc, unit, enemies);
}
}
workers.add(unit);
break;
}
}
}
// keep rocket building going
if (buildLoc == null && workers.size() > 0 && !thisPlanet.equals(Planet.Mars)) {
buildLoc = findFarAwaySpot(gc, workers.get(0).location().mapLocation());
if (buildLoc != null && factories.size() > 1) {
spreadPathfindingMapEarthBuildLoc = updatePathfindingMap(buildLoc, earthMap, 10000);
}
}
// get troop size and workforce size
troopSize = knights.size() + rangers.size() + mages.size();
workforceSize = workers.size();
// worker code
// loop through workers
if (thisPlanet.equals(Planet.Earth)) {
for (int i = 0; i < workers.size(); i++) {
Unit worker = workers.get(i);
if (!worker.location().isInGarrison() && !worker.location().isInSpace()) {
MapLocation myLoc = worker.location().mapLocation();
boolean shouldCollect = true;
if (roundNum < 5) {
produceWorkers(gc, worker);
}
if (i % 3 > 0) {
UnitType buildType = null;
int size = 0;
boolean areBuilding = true;
if (factories.size() < minFactorySize || !finishedFactories) {
buildType = UnitType.Factory;
size = factories.size();
} else if (workers.size() < 5 && gc.isAttackReady(worker.id())
&& gc.karbonite() >= 60) {
areBuilding = false;
produceWorkers(gc, worker);
} else if (roundNum > 250 && (rockets.size() == 0 || !finishedRockets)) {
buildType = UnitType.Rocket;
size = rockets.size();
} else {
areBuilding = false;
// harvestKarbonite(gc, worker, karboniteAmts);
}
// if building, run build sequences
if (areBuilding) {
if (myLoc.isAdjacentTo(buildLoc)) {
shouldCollect = false;
}
runBuildSequence(gc, worker, buildLoc, buildType, size, h);
}
} else {
if (workers.size() < 4 && gc.isAttackReady(worker.id()) && gc.karbonite() >= 60) {
produceWorkers(gc, worker);
}
else if ((workers.size() < h / 7 || workers.size() < 6) && factories.size() > 1
&& gc.isAttackReady(worker.id())
&& gc.karbonite() >= 60) {
produceWorkers(gc, worker);
}
else if (rockets.size() > 0 && i % 4 == 2) {
MapLocation rocketLoc = rockets.get(0).location().mapLocation();
if (!myLoc.isAdjacentTo(rocketLoc)) {
moveToLoc(gc, worker, rocketLoc);
} else {
shouldCollect = false;
}
}
else {
// harvestKarbonite(gc, worker, karboniteAmts);
}
}
if (shouldCollect) {
harvestKarbonite(gc,worker,karboniteAmts);
}
}
}
} else { // on mars
for(int i = 0; i < workers.size(); i++){
Unit worker = workers.get(i);
if((gc.karbonite() > 200 || workers.size() < 4 || gc.round() >= 750)
&& gc.isAttackReady(worker.id())){
produceWorkers(gc, worker);
}
if (gc.isMoveReady(worker.id())) {
harvestKarbonite(gc, worker, karboniteAmts);
}
}
}
// knight code
for (int i = 0; i < knights.size(); i++) {
// boolean goingToMars = false;
Unit knight = knights.get(i);
if (!knight.location().isInGarrison() && !knight.location().isInSpace()) {
MapLocation myLoc = knight.location().mapLocation();
// if (!thisPlanet.equals(Planet.Mars) && rockets.size() > 0) {
// Unit rocket = rockets.get(0);
// MapLocation rocketLoc = rocket.location().mapLocation();
// if (myLoc.isAdjacentTo(rocketLoc)) {
// goingToMars = true;
// } else if ((myLoc.distanceSquaredTo(rocketLoc) < 64 || i < 2) && rocket.health() == rocket.maxHealth()) {
// moveToLoc(gc, knight, rocketLoc);
// }
// }
if (!unitToRocket(knight, myLoc, rockets, i, gc)) {
int visionRange = (int) knight.visionRange();
VecUnit enemies = gc.senseNearbyUnitsByTeam(myLoc, visionRange, opponentTeam);
MapLocation attackLoc = swarmLoc;
// if reached target, stop swarming
if (swarmLoc != null && myLoc.equals(swarmLoc)) {
swarmLoc = null;
}
if (enemies.size() > 0) {
// find closest enemy
Unit closestEnemy = findClosestEnemy(gc, knight, enemies);
long dist = myLoc.distanceSquaredTo(closestEnemy.location().mapLocation());
// attack closest enemy
attackLoc = closestEnemy.location().mapLocation();
if (gc.isAttackReady(knight.id())) {
if (dist == 1 && gc.canAttack(knight.id(), closestEnemy.id())) {
gc.attack(knight.id(), closestEnemy.id());
} else if (knight.researchLevel() == 3) {
if (gc.isJavelinReady(knight.id()) && gc.canJavelin(knight.id(), closestEnemy.id())) {
gc.javelin(knight.id(), closestEnemy.id());
}
}
}
if (swarmLoc == null && attackLoc != null) {
swarmLoc = attackLoc;
if (thisPlanet.equals(Planet.Earth)) {
spreadPathfindingMapEarthSwarm = updatePathfindingMap(swarmLoc, thisMap, 10000);
} else {
spreadPathfindingMapMarsSwarm = updatePathfindingMap(swarmLoc, thisMap, 10000);
}
}
}
if (attackLoc == null) {
// bounce if no enemies
bounceMove(knight, gc);
} else if (enemies.size() > 0){
// move towards enemy if nearby or go to swarmLoc if enough troops
moveToLoc(gc, knight, attackLoc);
} else if (swarmLoc != null && (troopSize > minTroopSwarmSize || thisPlanet.equals(Planet.Mars))) {
moveAlongBFSPath(gc, knight, spreadPathfindingMapEarthSwarm);
} else {
bounceMove(knight, gc);
}
}
}
}
runMage(mages, rockets, gc);
runRanger(rangers, rockets, gc);
// factory code
if (roundNum > 250 && gc.karbonite() < 150 && rockets.size() == 0) {
int divideFactor = 100;
if (minFactorySize == 4) {
divideFactor = 60;
} else if (minFactorySize == 5) {
divideFactor = 30;
}
if (troopSize < minTroopSwarmSize - 5 || rockets.size() > 0) {
runFactories(gc, factories, 1);
} else if (troopSize < 20) {
runFactories(gc, factories, roundNum / divideFactor);
} else {
runFactories(gc, factories, roundNum / divideFactor * (troopSize / 10));
}
} else {
runFactories(gc, factories, 1);
}
workers.clear();
factories.clear();
knights.clear();
mages.clear();
rangers.clear();
rockets.clear();
// Submit the actions we've done, and wait for our next turn.
gc.nextTurn();
}
}
private static void runAwayWorker(GameController gc, Unit unit, VecUnit enemies) {
boolean hasMoved = false;
int counter = 0;
MapLocation myLoc = unit.location().mapLocation();
while (!hasMoved && counter < enemies.size()) {
Unit enemy = enemies.get(counter);
MapLocation enemyLoc = enemy.location().mapLocation();
long dist = myLoc.distanceSquaredTo(enemyLoc);
if ((enemy.unitType().equals(UnitType.Ranger) ||
enemy.unitType().equals(UnitType.Knight) ||
enemy.unitType().equals(UnitType.Mage)) &&
dist <= enemy.attackRange()) {
MapLocation moveLoc = myLoc.subtract(myLoc.directionTo(enemyLoc));
if (thisMap.onMap(moveLoc) && gc.canMove(unit.id(), myLoc.directionTo(moveLoc))) {
gc.moveRobot(unit.id(), myLoc.directionTo(moveLoc));
hasMoved = true;
}
}
counter++;
}
}
private static void harvestKarbonite(GameController gc, Unit worker, EarthDeposit[][] karboniteAmts) {
// leon code
int m = 0;
int n = 0;
int h = (int) thisMap.getHeight();
int w = (int) thisMap.getWidth();
boolean gotSomething = false;
MapLocation playerLocation = worker.location().mapLocation();
for(int a = 0; a < w; a++){
for(int b = 0; b < h; b++){
if(gc.round() >= 5 && gc.round()%karboniteCheckFrequency == 0 && gc.canSenseLocation(karboniteAmts[a][b].getLoc())){
karboniteAmts[a][b].changeCount(gc.karboniteAt(karboniteAmts[a][b].getLoc()));
}
if(karboniteAmts[a][b].getCount() != 0){
if(karboniteAmts[m][n].getValue(playerLocation) > karboniteAmts[a][b].getValue(playerLocation)){
m = a;
n = b;
gotSomething = true;
}
}
}
}
if(!gotSomething && gc.isMoveReady(worker.id())){
bounceMove(worker, gc);
}
else{
boolean harvested = false;
MapLocation karbLoc = karboniteAmts[m][n].getLoc();
if(karbLoc.equals(playerLocation)){
if(gc.canHarvest(worker.id(), Direction.Center)){
harvested = true;
gc.harvest(worker.id(), Direction.Center);
karboniteAmts[m][n].changeCount(gc.karboniteAt(karbLoc));
}
}
if(karbLoc.isAdjacentTo(worker.location().mapLocation())){
if(gc.canHarvest(worker.id(), playerLocation.directionTo(karbLoc))){
harvested = true;
gc.harvest(worker.id(), worker.location().mapLocation().directionTo(karbLoc));
karboniteAmts[m][n].changeCount(gc.karboniteAt(karbLoc));
}
}
if(!harvested && karbLoc != null && gc.isMoveReady(worker.id())){
int dist = (int) playerLocation.distanceSquaredTo(karbLoc);
if (thisPlanet.equals(Planet.Earth) && dist > 10 && gc.round() < 200) {
karboniteCollectionMap = updatePathfindingMap(karbLoc, thisMap, dist);
moveAlongBFSPath(gc, worker, karboniteCollectionMap);
} else {
moveToLoc(gc, worker, karbLoc);
}
}
}
}
private static Unit findClosestEnemy(GameController gc, Unit unit, VecUnit enemies) {
Unit closestEnemy = enemies.get(0);
MapLocation myLoc = unit.location().mapLocation();
long shortestDist = myLoc.distanceSquaredTo(closestEnemy.location().mapLocation());
for (int j = 1; j < enemies.size(); j++) {
Unit enemy = enemies.get(j);
long dist = myLoc.distanceSquaredTo(enemy.location().mapLocation());
if (dist < shortestDist) {
shortestDist = dist;
closestEnemy = enemy;
}
}
return closestEnemy;
}
private static void runBuildSequence(GameController gc, Unit worker, MapLocation buildLoc, UnitType buildType, int builtNum, int h) {
MapLocation myLoc = worker.location().mapLocation();
int id = worker.id();
if (myLoc.isAdjacentTo(buildLoc)) {
Direction buildDir = myLoc.directionTo(buildLoc);
// if no blueprint, then put one there
// else if there is a blueprint, work on blueprint
if (gc.canBlueprint(id, buildType, buildDir)) {
gc.blueprint(id, buildType, buildDir);
if (buildType.equals(UnitType.Factory)) {
finishedFactories = false;
}
if (buildType.equals(UnitType.Rocket)) {
finishedRockets = false;
}
} else if (gc.hasUnitAtLocation(buildLoc)){
Unit blueprint = gc.senseUnitAtLocation(buildLoc);
// if can build blueprint, then do so
// if done, then move on to next factory
if (gc.canBuild(id, blueprint.id())) {
gc.build(id, blueprint.id());
}
if (blueprint.health() == blueprint.maxHealth()) {
MapLocation possibleLoc = null;
if (buildType.equals(UnitType.Factory)) {
possibleLoc = findOpenAdjacentSpot(gc, myLoc);
} else {
possibleLoc = findFarAwaySpot(gc, myLoc);
}
if (possibleLoc != null) {
buildLoc.setX(possibleLoc.getX());
buildLoc.setY(possibleLoc.getY());
spreadPathfindingMapEarthBuildLoc = updatePathfindingMap(buildLoc, earthMap, 10000);
}
// stop building factories
if (builtNum == minFactorySize && buildType.equals(UnitType.Factory)) {
finishedFactories = true;
}
// stop building rockets
if (builtNum == (1 + gc.round() / 300) && buildType.equals(UnitType.Rocket)) {
finishedRockets = true;
}
}
}
} else {
// move towards build location
// if still building factories, use moveToLoc
// if building rockets, use bfs path
if (!finishedFactories) {
moveAlongBFSPath(gc, worker, spreadPathfindingMapEarthBuildLoc);
} else {
moveAlongBFSPath(gc, worker, spreadPathfindingMapEarthBuildLoc);
}
}
}
private static MapLocation findFarAwaySpot(GameController gc, MapLocation myLoc) {
MapLocation possibleLoc = null;
boolean foundOpenSpace = false;
int distance = 2;
while (!foundOpenSpace && distance < 5) {
for (int i = 0; i < directions.length; i++) {
MapLocation newLoc = myLoc.addMultiple(directions[i], distance);
if (thisMap.onMap(newLoc) && gc.isOccupiable(newLoc) == 1) {
possibleLoc = newLoc;
foundOpenSpace = true;
}
}
distance++;
}
return possibleLoc;
}
private static MapLocation findOpenAdjacentSpot(GameController gc, MapLocation myLoc) {
MapLocation returnLoc = null;
boolean dirFound = false;
int counter = 0;
while (!dirFound && counter < 8) {
MapLocation possibleLoc = myLoc.add(directions[counter]);
if (thisMap.onMap(possibleLoc) && gc.isOccupiable(possibleLoc) == 1) {
dirFound = true;
returnLoc = possibleLoc;
}
counter++;
}
return returnLoc;
}
private static void moveToLoc(GameController gc, Unit unit, MapLocation targetLoc) {
// get current location and target direction
MapLocation myLoc = unit.location().mapLocation();
Direction targetDir = myLoc.directionTo(targetLoc);
// if unit can move to target location then it does
if (gc.isMoveReady(unit.id())) {
if (gc.canMove(unit.id(), targetDir) && shouldMoveTowards(myLoc, targetDir)) {
gc.moveRobot(unit.id(), targetDir);
} else {
// finds position in directions array
int position = 0;
for (int i = 0; i < directions.length; i++) {
if (directions[i].equals(targetDir)) {
position = i;
}
}
Direction moveDir = null;
// rotate to find viable direction
int counter = 1;
while (moveDir == null && counter < 4) {
int dirPos = position - counter;
if (dirPos < 0) {
dirPos += 8;
}
if (gc.canMove(unit.id(), directions[dirPos]) && shouldMoveTowards(myLoc, directions[dirPos])) {
moveDir = directions[dirPos];
} else {
dirPos = position + counter;
if (dirPos > 7) {
dirPos -= 8;
}
if (gc.canMove(unit.id(), directions[dirPos]) && shouldMoveTowards(myLoc, directions[dirPos])) {
moveDir = directions[dirPos];
}
}
counter++;
}
// if position found, move there
if (moveDir != null) {
gc.moveRobot(unit.id(), moveDir);
}
}
}
}
private static boolean shouldMoveTowards(MapLocation myLoc, Direction direction) {
boolean shouldMove = true;
if (buildLoc != null && myLoc.add(direction).equals(buildLoc)) {
shouldMove = false;
}
return shouldMove;
}
public static void moveAlongBFSPath(GameController gc, Unit unit, Direction[][] map){
MapLocation myLoc = unit.location().mapLocation();
Direction oppositeDir = null;
if (map != null) {
oppositeDir = getValueInPathfindingMap(myLoc.getX(), myLoc.getY(), map);
}
if (oppositeDir != null) {
moveToLoc(gc, unit, myLoc.subtract(oppositeDir));
} else {
bounceMove(unit, gc);
}
}
/**
@param target assumes the target location is on the same planet as the planet currently being run
@param mapToUpdate pathfinding map to update
@param planetMap map of planet to create pathfinding for
*/
public static Direction[][] updatePathfindingMap(MapLocation target, PlanetMap planetMap, int maxDist){
Direction[][] currentMap;
Planet currentPlanet = planetMap.getPlanet();
currentMap = new Direction[(int)planetMap.getHeight()][(int)planetMap.getWidth()];
LinkedList<MapLocation> bfsQueue = new LinkedList<MapLocation>();
MapLocation tempLocation;
MapLocation currentBFSLocation;
long dist = 0;
bfsQueue.add(target);
while (bfsQueue.size() > 0 && dist <= maxDist) {
//gets first item in bfsQueue
currentBFSLocation = bfsQueue.poll();
dist = currentBFSLocation.distanceSquaredTo(target);
for (int i = 0; i < 8; i++) {
tempLocation = currentBFSLocation.add(directions[i]);
//only runs calculations if the added part is actually on the map...
if ( (tempLocation.getY() >= 0 && tempLocation.getY() < currentMap.length)
&& (tempLocation.getX() >= 0 && tempLocation.getX() < currentMap[0].length)) {
//only runs calculation if that area of the pathfindingMap hasn't been filled in yet
if (getValueInPathfindingMap(tempLocation.getX(), tempLocation.getY(), currentMap) == null) {
if (planetMap.isPassableTerrainAt(new MapLocation(currentPlanet, tempLocation.getX(), tempLocation.getY())) != 0) {
bfsQueue.add(tempLocation);
setValueInPathfindingMap(tempLocation.getX(), tempLocation.getY(), directions[i], currentMap);
}
}
}
}
}
return currentMap;
}
public static Direction getValueInPathfindingMap(int x, int y, Direction[][] map){
return map[map.length-1-y][x];
}
public static void setValueInPathfindingMap(int x, int y, Direction myDirection, Direction[][] map){
map[map.length-1-y][x] = myDirection;
}
public static void bounceMove(Unit u, GameController gc){
if (!u.location().isInGarrison() && !u.location().isInSpace()) {
MapLocation myLoc = u.location().mapLocation();
long dist = -1;
if (startLoc != null) {
dist = myLoc.distanceSquaredTo(startLoc);
}
double safeDistance = Math.pow(troopSize / 2 + (thisMap.getHeight() * thisMap.getWidth() / 133), 2);
if (u.unitType().equals(UnitType.Healer)) {
safeDistance /= 2;
}
if (dist >= safeDistance && troopSize < minTroopSwarmSize) {
moveToLoc(gc, u, startLoc);
} else {
int id = u.id();
MapLocation selfLocation = u.location().mapLocation();
if(gc.round()%20 == 0){
if(robotChecker.get(id) == null){
robotChecker.put(id, selfLocation);
}
else{
if(robotChecker.get(id).distanceSquaredTo(selfLocation) <= 25){
robotDirections.put(id, getRandomDiagonalDirection());
}
}
}
Integer currentDir = robotDirections.get(id);
if(currentDir == null){
currentDir = getRandomDiagonalDirection();
robotDirections.put(id, currentDir);
}
if(gc.isMoveReady(id)){
if(gc.canMove(id, directions[currentDir]) && shouldMoveTowards(selfLocation, directions[currentDir])){
gc.moveRobot(id, directions[currentDir]);
}
else{
if(!moveUnit(gc, id, currentDir, 2, directions)){
if(!moveUnit(gc, id, currentDir, 1, directions)){
if(!moveUnit(gc, id, currentDir, 3, directions)){
Direction moveDir = directions[getNumUp(currentDir, 4)];
if(gc.canMove(id, moveDir) && shouldMoveTowards(selfLocation, moveDir)){
gc.moveRobot(id, moveDir);
robotDirections.put(id, getNumUp(currentDir, 4));
}
}
}
}
}
}
}
}
}
public static int getRandomDiagonalDirection(){
return ((((int)(Math.random() * 4))*2)+1);
}
public static int getNumUp(int i, int n){
if(i > 7-n){
i -= 8-n;
}
else{
i += n;
}
return i;
}
public static int getNumDown(int i, int n){
if(i < n){
i += 8-n;
}
else{
i -= n;
}
return i;
}
//directions is class variable in other thingyyyyyyyyyy
public static boolean moveUnit(GameController gc, int id, int currentDir, int n, Direction[] directions){
int leftNum = getNumDown(currentDir, n);
int rightNum = getNumUp(currentDir, n);
boolean left = gc.canMove(id, directions[leftNum]);
boolean right = gc.canMove(id, directions[rightNum]);
if(left && !right){
gc.moveRobot(id, directions[leftNum]);
determineNewDirection(n, id, leftNum, true);
return true;
}
if(!left && right){
gc.moveRobot(id, directions[rightNum]);
determineNewDirection(n, id, rightNum, false);
return true;
}
if(left && right){
if((int)(Math.random() * 2) % 2 == 0){
gc.moveRobot(id, directions[leftNum]);
determineNewDirection(n, id, leftNum, true);
}
else{
gc.moveRobot(id, directions[rightNum]);
determineNewDirection(n, id, rightNum, false);
}
return true;
}
return false;
}
public static void determineNewDirection(int n, int id, int num, boolean left){
int determinant = num;
if(num%2 == 0){
if(left){
determinant = getNumUp(num, 1);
}
else{
determinant = getNumDown(num, 1);
}
}
robotDirections.put(id, determinant);
}
private static void produceWorkers(GameController gc, Unit worker) {
MapLocation myLoc = worker.location().mapLocation();
MapLocation makeLoc = findOpenAdjacentSpot(gc, myLoc);
//replicates worker in open direction
if(makeLoc != null && gc.karbonite() >= 60 && gc.canReplicate(worker.id(), myLoc.directionTo(makeLoc))) {
gc.replicate(worker.id(), myLoc.directionTo(makeLoc));
}
}
private static void runRocket(GameController gc, Unit rocket) {
MapLocation myLoc = rocket.location().mapLocation();
VecUnitID garrison = rocket.structureGarrison();
if (rocket.rocketIsUsed() == 0) {
VecUnit adjacentUnits = gc.senseNearbyUnitsByTeam(myLoc, 1, myTeam);
if (adjacentUnits.size() > 0) {
for (int i = 0; i < adjacentUnits.size(); i++) {
Unit unit = adjacentUnits.get(i);
if (gc.canLoad(rocket.id(), unit.id()) && gc.isMoveReady(unit.id())) {
gc.load(rocket.id(), unit.id());
}
}
}
// decide when to launch rockets
findLandLoc(gc);
Integer x = rocketLaunch.get(rocket.id());
if (gc.canLaunchRocket(rocket.id(), landLoc) && garrison.size() == 8 && x != null && (gc.round() >= rocketLaunch.get(rocket.id()) || rocket.health() < rocket.maxHealth())) {
gc.launchRocket(rocket.id(), landLoc);
landLoc = new MapLocation(Planet.Mars, (int) (Math.random() * marsMap.getWidth()), (int) (Math.random() * marsMap.getHeight()));
} else if (gc.canLaunchRocket(rocket.id(), landLoc) && gc.round() > 745) {
gc.launchRocket(rocket.id(), landLoc);
}
} else {
for (int i = 0; i < garrison.size(); i++) {
int counter = 0;
boolean unloaded = false;
while (!unloaded && counter < 8) {
if (gc.canUnload(rocket.id(), directions[counter])) {
gc.unload(rocket.id(), directions[counter]);
unloaded = true;
}
counter++;
}
}
}
}
private static void findLandLoc(GameController gc) {
if (marsMap.onMap(landLoc) && marsMap.isPassableTerrainAt(landLoc) != 1) {
landLoc = findFarAwaySpotOnMars(gc, landLoc);
}
if (landLoc == null) {
landLoc = new MapLocation(Planet.Mars, (int) (Math.random() * marsMap.getWidth()), (int) (Math.random() * marsMap.getHeight()));
}
}
private static MapLocation findFarAwaySpotOnMars(GameController gc, MapLocation myLoc) {
MapLocation possibleLoc = null;
boolean foundOpenSpace = false;
int distance = 3;
while (!foundOpenSpace && distance < 6) {
for (int i = 0; i < directions.length; i++) {
MapLocation newLoc = myLoc.addMultiple(directions[i], distance);
if (marsMap.onMap(newLoc) && marsMap.isPassableTerrainAt(newLoc) == 1) {
possibleLoc = newLoc;
foundOpenSpace = true;
}
}
distance++;
}
return possibleLoc;
}
private static boolean unitToRocket(Unit unit, MapLocation myLoc, ArrayList<Unit> rockets, int count, GameController gc) {