-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseRobot.java
554 lines (495 loc) · 14.5 KB
/
BaseRobot.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
package team022;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import battlecode.common.*;
import team022.Guard.GuardState;
public abstract class BaseRobot {
public RobotController rc;
public Random rand; //Random number generator
public Direction[] directionValues;
public int archonCount = 1;
public MapLocation archonSpawn;
public ComSystem com;
public double threat;
public ArrayList<MapLocation> oldLocs;
public ArrayList<MapLocation> targets = new ArrayList<>();
public ArrayList<MapLocation> destroyedTargets = new ArrayList<>();
public MetaStrategy lifePlan;
public enum MetaStrategy{
TURRTLE, MONGOLS, GETEM;
};
public BaseRobot(RobotController rcin){
rc = rcin;
rand = new Random(rc.getID());
directionValues = new Direction[]{Direction.NORTH, Direction.NORTH_EAST, Direction.EAST, Direction.SOUTH_EAST,
Direction.SOUTH, Direction.SOUTH_WEST, Direction.WEST, Direction.NORTH_WEST};
com = new ComSystem(rc);
oldLocs = new ArrayList<MapLocation>();
setMetaStrategy();
}
//Abstract method for major functionality
public abstract void run();
//-------------------Base Methods for all Units------------------
//shoot enemy with lowest HP within range
public void shootWeakest() throws GameActionException{
RobotInfo[] enemiesInRange = rc.senseNearbyRobots(rc.getType().attackRadiusSquared, rc.getTeam().opponent());
double LowestHealth = 0;
RobotInfo weakestLink = null;
for(RobotInfo ri:enemiesInRange){
if(ri.health>LowestHealth){
weakestLink = ri;
LowestHealth = ri.health;
}
}
if(weakestLink != null){
if(rc.isWeaponReady()&&rc.canAttackLocation(weakestLink.location)){
rc.attackLocation(weakestLink.location);
}
}
}
//Move as close as possible to the provided direction
public boolean moveAsCloseToDirection(Direction toMove, boolean allowBackwards) throws GameActionException{
if(rc.isCoreReady()){
Direction[] toTry;
if(allowBackwards){
toTry = new Direction[]{toMove,
toMove.rotateLeft(),
toMove.rotateRight(),
toMove.rotateLeft().rotateLeft(),
toMove.rotateRight().rotateRight(),
toMove.rotateLeft().rotateLeft().rotateLeft(),
toMove.rotateRight().rotateRight().rotateRight(),
toMove.rotateLeft().rotateLeft().rotateLeft().rotateLeft()
};
}else{
toTry = new Direction[]{toMove,
toMove.rotateLeft(),
toMove.rotateRight(),
toMove.rotateLeft().rotateLeft(),
toMove.rotateRight().rotateRight()
};
}
for(Direction dir:toTry){
if(rc.canMove(dir)&&rc.isCoreReady()){
rc.move(dir);
return true;
}
}
}
return false;
}
//Does the same thing as move as close to, but is better
public void slugPathing(MapLocation toPathTo){
Direction toMove = rc.getLocation().directionTo(toPathTo);
if(rc.isCoreReady()){
Direction[] toTry;
toTry = new Direction[]{toMove,
toMove.rotateLeft(),
toMove.rotateRight(),
toMove.rotateLeft().rotateLeft(),
toMove.rotateRight().rotateRight(),
toMove.rotateLeft().rotateLeft().rotateLeft(),
toMove.rotateRight().rotateRight().rotateRight(),
toMove.rotateLeft().rotateLeft().rotateLeft().rotateLeft()
};
for(Direction dir:toTry){
boolean okay = true;
for(MapLocation loc : oldLocs)
if(loc.equals(rc.getLocation().add(dir)))
okay = false;
if(!okay) continue;
if(rc.canMove(dir)&&rc.isCoreReady()){
try {
rc.move(dir);
oldLocs.add(rc.getLocation());
oldLocs.remove(0);
break;
} catch (GameActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return;
}
}
}
}
//Returns a random direction
public Direction getRandomDirection(){
return directionValues[rand.nextInt(8)];
}
/**
* preliminary basic combat micro
* @throws GameActionException
*/
public void kiteingMicro() throws GameActionException{
if(rc.isCoreReady()){
DashAway(0.0);
}
if(rc.isWeaponReady()){
destroy();
}
}
/**
* If there is more than numRobots in radSquared tiles, move away from them
* Note: only uses robots from our team
* @throws GameActionException
*/
public void spreadOut(int numRobots, int radSquared) throws GameActionException{
RobotInfo[] ourBots = rc.senseNearbyRobots(radSquared, rc.getTeam());
rc.setIndicatorString(0, ourBots.length + " Nearby");
if(ourBots.length > numRobots){
int avgx = 0;
int avgy = 0;
for(RobotInfo bot : ourBots){
avgx += bot.location.x;
avgy += bot.location.y;
}
avgx /= ourBots.length;
avgy /= ourBots.length;
moveAsCloseToDirection(new MapLocation(avgx, avgy).directionTo(rc.getLocation()), false);
}
}
/**
* Code for moving away from enemy robots
*/
public boolean DashAway(double bravado) throws GameActionException{
MapLocation[] adjacent = getAllAdjacent(rc.getLocation());
RobotInfo[] enemies = rc.senseNearbyRobots(1000, rc.getTeam().opponent());
RobotInfo[] zombles = rc.senseNearbyRobots(1000, Team.ZOMBIE);
ArrayList<RobotInfo> danger = new ArrayList<RobotInfo>(Arrays.asList(enemies));
danger.addAll(Arrays.asList(zombles));
//Have danger score for all adjacent squares, plus one for our square
double[] dangerscore = new double[adjacent.length];
for(RobotInfo badGuy : danger){
for(int i = 0; i < adjacent.length; i++){
if(badGuy.type.canAttack() && badGuy.type.attackRadiusSquared >= adjacent[i].distanceSquaredTo(badGuy.location)
&& badGuy.weaponDelay <= 1.0){
dangerscore[i] += badGuy.attackPower;
}
}
}
threat = dangerscore[0];
if(dangerscore[0] > bravado){
double best = dangerscore[0];
MapLocation toMoveTo = null;
for(int i = 1; i < dangerscore.length; i++){
if(best > dangerscore[i] && rc.canMove(rc.getLocation().directionTo(adjacent[i]))){
best = dangerscore[i];
toMoveTo = adjacent[i];
}
}
if(toMoveTo != null){
if(rc.isCoreReady()){
rc.move(rc.getLocation().directionTo(toMoveTo));
return true;
}
}else{
//We aren't in open terrain! So, we're going to try and run away from the average
double vecx = 0;
double vecy = 0;
for(int i = 1; i < dangerscore.length; i++){
vecx += dangerscore[i] * dirToCos(directionValues[i-1]);
vecy += dangerscore[i] * dirToSin(directionValues[i-1]);
}
if(vecx != 0 || vecy != 0){
double angle = Math.atan2(vecy, vecx);
Direction desired = angleToDir(angle).opposite();
return moveAsCloseToDirection(desired, false);
}
}
}
return false;
}
/**
* Given an angle, return the closest possible direction
*/
Direction angleToDir(double angle){
angle = Math.toDegrees(angle);
double[] angles = {90, 45, 0, 315, 270, 225, 180, 135};
double best = 10000;
int index = 0;
for(int i = 0; i < 8; i++){
if(Math.abs(angles[i] - angle) < best){
best = Math.abs(angles[i] - angle);
index = i;
}
}
return directionValues[index];
}
/**
* Converts given direction into cos of angle
*/
double dirToCos(Direction dir){
switch(dir){
case EAST:
return 1;
case NORTH_EAST:
return 1/Math.sqrt(2);
case NORTH:
return 0;
case NORTH_WEST:
return -1/Math.sqrt(2);
case WEST:
return -1;
case SOUTH_WEST:
return -1/Math.sqrt(2);
case SOUTH:
return 0;
case SOUTH_EAST:
return 1/Math.sqrt(2);
default:
return 0;
}
}
/**
* Converts given direction into sin of angle
*/
double dirToSin(Direction dir){
switch(dir){
case EAST:
return 0;
case NORTH_EAST:
return 1/Math.sqrt(2);
case NORTH:
return 1;
case NORTH_WEST:
return 1/Math.sqrt(2);
case WEST:
return 0;
case SOUTH_WEST:
return -1/Math.sqrt(2);
case SOUTH:
return -1;
case SOUTH_EAST:
return -1/Math.sqrt(2);
default:
return 0;
}
}
/**
* Gets all adjacent map squares. NOTE: the zero index is the square the robot is on
*/
public MapLocation[] getAllAdjacent(MapLocation center){
MapLocation[] adjacent = new MapLocation[9];
adjacent[0] = center;
for(int i = 0; i<directionValues.length; i++){
adjacent[i+1] = center.add(directionValues[i]);
}
return adjacent;
}
/**
* Cleans off the most rubble heavy square in range
* @throws GameActionException
*/
public void springCleaning() throws GameActionException{
if(rc.isCoreReady()){
MapLocation[] squares = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(), 2);
double mostRubble = 0;
MapLocation toClean = null;
for(MapLocation square : squares){
if(rc.senseRubble(square) > mostRubble){
mostRubble = rc.senseRubble(square);
toClean = square;
}
}
if(toClean == null) return;
Direction dir = rc.getLocation().directionTo(toClean);
if(dir != Direction.OMNI){
rc.clearRubble(rc.getLocation().directionTo(toClean));
}
}
}
/**
* Better shooting code
* @throws GameActionException
*/
public void destroy() throws GameActionException {
RobotInfo toDestroy = commonEnemy(senseAllNearbyFoes(true));
if(toDestroy != null){
rc.setIndicatorString(1, "Should be killing" + toDestroy.ID);
if(rc.isWeaponReady()&&rc.canAttackLocation(toDestroy.location)){
rc.attackLocation(toDestroy.location);
}
}
}
public RobotInfo[] senseAllNearbyFoes(boolean attack){
RobotInfo[] enemies;
RobotInfo[] zombles;
if(attack){
enemies = rc.senseNearbyRobots(rc.getType().attackRadiusSquared, rc.getTeam().opponent());
zombles = rc.senseNearbyRobots(rc.getType().attackRadiusSquared, Team.ZOMBIE);
}else{
enemies = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, rc.getTeam().opponent());
zombles = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, Team.ZOMBIE);
}
RobotInfo[] all = new RobotInfo[enemies.length + zombles.length];
int i = 0;
for(RobotInfo badGuy : enemies){
all[i] = badGuy;
i++;
}
for(RobotInfo badGuy : zombles){
all[i] = badGuy;
i++;
}
return all;
}
public RobotInfo commonEnemy(RobotInfo[] enemies){
RobotInfo toDestroy = null;
int lowestHealth =100000;
int lowestID = 1000000;
for(RobotInfo enemy : enemies){
boolean beatsBest = (enemy.health < lowestHealth)? true: enemy.ID < lowestID;
if(enemy.type == RobotType.BIGZOMBIE) return enemy;
if(beatsBest){
lowestHealth = (int) enemy.health;
lowestID = enemy.ID;
toDestroy = enemy;
}
}
return toDestroy;
}
public void bullRush() throws GameActionException{
RobotInfo rodeoClown = commonEnemy(senseAllNearbyFoes(false));
if(rodeoClown != null){
if(rc.getLocation().distanceSquaredTo(rodeoClown.location) > rc.getType().attackRadiusSquared){
slugPathing(rodeoClown.location);
}
}
}
public void crowedArchon() throws GameActionException{
RobotInfo[] bots = rc.senseNearbyRobots(rc.getType().sensorRadiusSquared, rc.getTeam());
for(RobotInfo bot : bots){
if(bot.type == RobotType.ARCHON){
if(rc.getLocation().distanceSquaredTo(bot.location) > 16){
moveAsCloseToDirection(rc.getLocation().directionTo(bot.location), false);
}
}
}
}
public void defense(){
try {
bullRush();
} catch (GameActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
destroy();
} catch (GameActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
crowedArchon();
} catch (GameActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void attack(MapLocation loc){
seek(loc);
try {
destroy();
} catch (GameActionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public MapLocation closestTarget(){
int shortestDistance = 10000000;
MapLocation bestTarget = null;
for(MapLocation target : targets){
if(shortestDistance > target.distanceSquaredTo(rc.getLocation())){
bestTarget = target;
shortestDistance = target.distanceSquaredTo(rc.getLocation());
}
}
return bestTarget;
}
public void recieveTargets(){
for(Signal message : rc.emptySignalQueue()){
if(ComSystem.getFlag( message ) == 0x00){
if(notInTargets(ComSystem.intToMap(message.getMessage()[1])))
targets.add(ComSystem.intToMap(message.getMessage()[1]));
}
}
}
public boolean notInTargets(MapLocation toCheck){
boolean notIn = true;
for(MapLocation inTargets : targets){
if(toCheck.equals(inTargets)){
notIn = false;
break;
}
}
for(MapLocation destroyed : destroyedTargets){
if(toCheck.equals(destroyed)){
notIn = false;
}
}
return notIn;
}
public void seek(MapLocation loc){
if(rc.getLocation().distanceSquaredTo(loc) > rc.getType().attackRadiusSquared)
slugPathing(loc);
}
public void setMetaStrategy(){
int size = getApproxSize();
if(doTheZombiesEvenLift()){
lifePlan = MetaStrategy.MONGOLS;
return;
}
if(size >= 2500){
lifePlan = MetaStrategy.TURRTLE;
}else{
lifePlan = MetaStrategy.MONGOLS;
}
}
public int getApproxSize(){
MapLocation[] archons1 = rc.getInitialArchonLocations(rc.getTeam());
MapLocation[] archons2 = rc.getInitialArchonLocations(rc.getTeam().opponent());
MapLocation[] archons= new MapLocation[archons1.length + archons2.length];
System.arraycopy(archons1, 0, archons, 0, archons1.length);
System.arraycopy(archons2, 0, archons, archons1.length, archons2.length);
MapLocation upperLeft = archons[0];
MapLocation lowerRight = archons[0];
for(MapLocation archon : archons){
if(archon.x < upperLeft.x)
upperLeft = new MapLocation(archon.x, upperLeft.y);
if(archon.y < upperLeft.y)
upperLeft = new MapLocation(upperLeft.x, archon.y);
if(archon.x > lowerRight.x)
lowerRight = new MapLocation(archon.x, lowerRight.y);
if(archon.y > lowerRight.y)
lowerRight = new MapLocation(lowerRight.x, archon.y);
}
if(upperLeft != null && lowerRight != null)
return upperLeft.distanceSquaredTo(lowerRight);
return 0;
}
public boolean doTheZombiesEvenLift(){
ZombieSpawnSchedule theSchedule = rc.getZombieSpawnSchedule();
HashMap<RobotType, Integer> howBad = new HashMap<>();
RobotType[] zombles = {RobotType.BIGZOMBIE,
RobotType.FASTZOMBIE,
RobotType.RANGEDZOMBIE,
RobotType.STANDARDZOMBIE};
for(RobotType zType : zombles){
howBad.put(zType, (int)(zType.maxHealth*zType.attackPower/zType.attackDelay));
}
int howBadIsIt = 0;
for(int round : theSchedule.getRounds()){
for(ZombieCount z: theSchedule.getScheduleForRound(round)){
howBadIsIt += howBad.get(z.getType())* z.getCount();
}
}
if(howBadIsIt > 120000){
return true;
}
return false;
}
}