-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobotBase.java
334 lines (300 loc) · 9.88 KB
/
RobotBase.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
package hax;
import battlecode.common.*;
import java.util.*;
public abstract class RobotBase {
protected final RobotController rc;
protected final MapLocation startLocation;
protected State state;
protected Random rand;
protected final HashSet<Robot> allies = new HashSet<Robot>();
protected final HashSet<Robot> a_nearby = new HashSet<Robot>();
protected int a_archons = 0;
protected int a_workers = 0;
protected int a_soldiers = 0;
protected int a_cannons = 0;
protected final HashSet<Robot> enemies = new HashSet<Robot>();
protected final HashSet<Robot> e_nearby = new HashSet<Robot>();
protected final HashSet<MapLocation> targets_g = new HashSet<MapLocation>();
protected final HashSet<MapLocation> targets_a = new HashSet<MapLocation>();
protected int e_archons = 0;
protected int e_workers = 0;
protected int e_soldiers = 0;
protected int e_cannons = 0;
protected final HashMap<Robot, RobotInfo> r_info = new HashMap<Robot, RobotInfo>();
protected enum MoveState {
MOVING,
DONE,
BLOCKED
}
enum Position {
AIR, //0
GROUND, //1
NONE,
BOTH
}
protected enum State {
GET_NEAREST,
GET_FLUX,
SEARCH,
FOUND,
GATHER,
DONE,
DEFENCE,
ATTACK_G,
ATTACK_A,
GET_BLOCK,
GET_BACK,
UNLOAD
}
public RobotBase(RobotController rc) {
this.rc = rc;
this.startLocation = rc.getLocation();
rand = new Random(rc.getLocation().hashCode());
//System.out.println(rc.getRobotType().toString() + " @ " + rc.getLocation());
}
public void play() throws GameActionException {
init();
while (true) {
try {
go();
} catch(Exception e) {
System.out.println("caught exception:");
e.printStackTrace();
}
rc.yield();
}
}
protected abstract void init() throws GameActionException;
protected abstract void go() throws GameActionException;
protected final void sense() throws GameActionException {
a_nearby.clear();
a_archons = 0;
a_workers = 0;
a_soldiers = 0;
e_nearby.clear();
e_archons = 0;
e_workers = 0;
e_soldiers = 0;
targets_a.clear();
targets_g.clear();
doSense(rc.senseNearbyGroundRobots());
doSense(rc.senseNearbyAirRobots());
}
protected final boolean is_air(RobotType rt){
return rt == RobotType.ARCHON || rt == RobotType.SCOUT;
}
protected final void doSense(Robot[] robots) throws GameActionException {
for (Robot r : robots) {
if (rc.canSenseObject(r)) {
RobotInfo ri = rc.senseRobotInfo(r);
r_info.put(r, ri);
if (ri.team != rc.getTeam()) {
enemies.add(r);
e_nearby.add(r);
if(is_air(ri.type)) {
targets_a.add(ri.location);
} else {
targets_g.add(ri.location);
}
switch (ri.type) {
case ARCHON:
++e_archons;
break;
case CANNON:
++e_cannons;
break;
case CHANNELER:
break;
case SCOUT:
break;
case SOLDIER:
++e_soldiers;
break;
case WORKER:
break;
}
} else {
allies.add(r);
a_nearby.add(r);
switch (ri.type) {
case ARCHON:
++a_archons;
break;
case CANNON:
++a_cannons;
break;
case CHANNELER:
break;
case SCOUT:
break;
case SOLDIER:
++a_soldiers;
break;
case WORKER:
++a_workers;
break;
}
}
}
}
}
protected final void transfer() throws GameActionException {
if (rc.getEnergonLevel() < rc.getMaxEnergonLevel() / 3) {
return;
}
double min = Double.MAX_VALUE;
Robot minr = null;
RobotInfo minri = null;
for (Robot r : a_nearby) {
RobotInfo ri = r_info.get(r);
if(state == State.DEFENCE && ri.type == RobotType.WORKER)//if defence => dont charge workers
continue;
if (ri.location.isAdjacentTo(rc.getLocation()) || ri.location.equals(rc.getLocation())) {
if (min > ri.energonLevel + ri.energonReserve) {
min = ri.energonLevel + ri.energonReserve;
minr = r;
minri = ri;
}
}
}
if (minr != null) {
double e = GameConstants.ENERGON_RESERVE_SIZE - minri.energonReserve;
e = Math.min(e, rc.getEnergonLevel() / 2);
if (e > GameConstants.ENERGON_RESERVE_SIZE / 2
&& rc.senseGroundRobotAtLocation(minri.location) != null)
{
rc.transferEnergon(e, minri.location, minr.getRobotLevel());
}
}
}
protected void wander(int chance, int all) throws GameActionException {
Direction d = rc.getDirection();
int r = rand.nextInt(all);
if (rc.canMove(d)) {
if (r < chance) {
do {
d = d.rotateLeft();
} while (! rc.canMove(d));
} else if (r > all - chance - 1) {
do {
d = d.rotateRight();
} while (! rc.canMove(d));
}
} else if (r < all/2) {
do {
d = d.rotateLeft();
} while (! rc.canMove(d));
} else {
do {
d = d.rotateRight();
} while (! rc.canMove(d));
}
if (! rc.getDirection().equals(d)) {
rc.setDirection(d);
} else {
rc.moveForward();
}
}
protected MoveState moveToAdjacent(MapLocation l) throws GameActionException {
if (rc.getLocation().isAdjacentTo(l)) {
return MoveState.DONE;
}
if (! rc.getLocation().equals(l)) {
if (! rc.getDirection().equals(rc.getLocation().directionTo(l))) {
rc.setDirection(rc.getLocation().directionTo(l));
return MoveState.MOVING;
}
} else {
Direction d = rc.getDirection();
while (! rc.canMove(d)) {
d = d.rotateRight();
if (rc.getDirection() == d) {
return MoveState.BLOCKED;
}
}
if (! rc.getDirection().equals(d)) {
rc.setDirection(d);
return MoveState.MOVING;
}
}
if (rc.canMove(rc.getDirection())) {
rc.moveForward();
return MoveState.MOVING;
} else {
return MoveState.BLOCKED;
}
}
protected void attack(Position pos, HashSet<MapLocation> _targets) throws GameActionException {
int dist = Integer.MAX_VALUE;
MapLocation target = null;
//find close target
for (MapLocation l : _targets) {
if (dist > l.distanceSquaredTo(rc.getLocation())) {
dist = l.distanceSquaredTo(rc.getLocation());
target = l;
}
}
if (target == null) {
wander(2, 10);
} else if (rc.canAttackSquare(target)) {
if (rc.isAttackActive()) {
return;
}
if (pos == Position.AIR) {
rc.attackAir(target);
} else {
rc.attackGround(target);
}
} else if (rc.getLocation().directionTo(target) != rc.getDirection()) {
rc.setDirection(rc.getLocation().directionTo(target));
} else if (rc.canMove(rc.getDirection())) {
rc.moveForward();
}
}
//communication
private void doSendTargets(Position pos) throws GameActionException {
Message m = new Message();
m.locations = new MapLocation[0];
m.ints = new int[2];
m.ints[0] = rc.getTeam().hashCode();
if(pos == Position.AIR) {
m.ints[1] = 0;
m.locations = targets_a.toArray(m.locations);
} else {
m.ints[1] = 1;
m.locations = targets_g.toArray(m.locations);
}
rc.broadcast(m);
rc.yield();
}
protected void sendTargets() throws GameActionException {
if(!targets_a.isEmpty())
doSendTargets(Position.AIR);
if(!targets_g.isEmpty())
doSendTargets(Position.GROUND);
}
protected Position receiveTargets() throws GameActionException {
Message[] messages = rc.getAllMessages();
boolean air = false, ground = false;
if (messages.length == 0)
return Position.NONE;
targets_a.clear();
targets_g.clear();
for (Message m : messages) {
if(m.ints.length == 2 && m.ints[0] == rc.getTeam().hashCode()) {
for (MapLocation l : m.locations) {
if(m.ints[1] == 0) {
air = true;
targets_a.add(l);
} else {
ground = true;
targets_g.add(l);
}
}
}
}
if(ground && !air) return Position.GROUND;
else if(!ground && air) return Position.AIR;
else return Position.BOTH;
}
}