forked from andrepl/bcode2013
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.java
More file actions
107 lines (80 loc) · 2.93 KB
/
Bot.java
File metadata and controls
107 lines (80 loc) · 2.93 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
package team028;
import battlecode.common.*;
public abstract class Bot implements IBot {
protected RobotController rc;
protected RobotType myRobotType;
protected int myRobotId;
protected Team myTeam;
protected Team opponentTeam;
protected Robot myRobot;
protected MapLocation myLocation;
protected MapLocation enemyHqLocation;
protected MapLocation myHqLocation;
protected int width;
protected int height;
protected RobotInfo myRobotInfo = null;
protected MapLocation[] alliedEncampents;
protected MapLocation[] allEncampments;
protected double myHealth;
protected static final Direction[] ALL_DIRECTIONS = new Direction[] {
Direction.NORTH,
Direction.SOUTH,
Direction.EAST,
Direction.WEST,
Direction.NORTH_WEST,
Direction.NORTH_EAST,
Direction.SOUTH_WEST,
Direction.SOUTH_EAST
};
public int readBroadcastSecure(int slot, int defaultValue) {
return Util.readBroadcastSecure(rc, slot, defaultValue);
}
public boolean broadcastSecure(int slot, int val) {
return Util.broadcastSecure(rc, slot, val);
}
protected boolean hasEnemyMine(MapLocation loc) {
Team team = rc.senseMine(loc);
return team == Team.NEUTRAL || team == opponentTeam;
}
public Bot(Robot robot, int robotId, Team team, RobotType robotType, RobotController rc) {
this.myRobot = robot;
this.myRobotId = robotId;
this.myTeam = team;
this.opponentTeam = team.opponent();
this.myRobotType = robotType;
this.rc = rc;
this.enemyHqLocation = rc.senseEnemyHQLocation();
this.myHqLocation = rc.senseHQLocation();
this.width = rc.getMapWidth();
this.height = rc.getMapHeight();
}
public MapLocation selectNearestEnemy(int range) throws GameActionException {
Robot[] enemies = rc.senseNearbyGameObjects(Robot.class, myLocation, range*range, opponentTeam);
MapLocation nearest = null;
int nearestDistSq = -1;
int distSq = -1;
MapLocation robotLoc;
for (Robot r: enemies) {
robotLoc = rc.senseLocationOf(r);
distSq = robotLoc.distanceSquaredTo(myLocation);
if (nearest == null || distSq < nearestDistSq) {
nearest = robotLoc;
nearestDistSq = distSq;
if (nearest.isAdjacentTo(myLocation)) {
return nearest;
}
}
}
return nearest;
}
public void go() throws GameActionException {
// keep this up to date each turn
myRobotInfo = rc.senseRobotInfo(myRobot);
myHealth = myRobotInfo.energon;
myLocation = myRobotInfo.location;
// clear per-bot debug output
rc.setIndicatorString(0, "");
rc.setIndicatorString(1, "");
rc.setIndicatorString(2, "");
}
}