@@ -17,6 +17,8 @@ public abstract class RobotBase {
17
17
18
18
protected final HashSet <Robot > enemies = new HashSet <Robot >();
19
19
protected final HashSet <Robot > e_nearby = new HashSet <Robot >();
20
+ protected final HashSet <MapLocation > targets_g = new HashSet <MapLocation >();
21
+ protected final HashSet <MapLocation > targets_a = new HashSet <MapLocation >();
20
22
protected int e_archons = 0 ;
21
23
protected int e_workers = 0 ;
22
24
protected int e_soldiers = 0 ;
@@ -28,6 +30,13 @@ protected enum MoveState {
28
30
DONE ,
29
31
BLOCKED
30
32
}
33
+
34
+ enum Position {
35
+ AIR , //0
36
+ GROUND , //1
37
+ NONE ,
38
+ BOTH
39
+ }
31
40
32
41
public RobotBase (RobotController rc ) {
33
42
this .rc = rc ;
@@ -63,11 +72,17 @@ protected final void sense() throws GameActionException {
63
72
e_archons = 0 ;
64
73
e_workers = 0 ;
65
74
e_soldiers = 0 ;
75
+ targets_a .clear ();
76
+ targets_g .clear ();
66
77
67
78
doSense (rc .senseNearbyGroundRobots ());
68
79
doSense (rc .senseNearbyAirRobots ());
69
80
}
70
81
82
+ protected final boolean is_air (RobotType rt ){
83
+ return rt == RobotType .ARCHON || rt == RobotType .SCOUT ;
84
+ }
85
+
71
86
protected final void doSense (Robot [] robots ) throws GameActionException {
72
87
for (Robot r : robots ) {
73
88
if (rc .canSenseObject (r )) {
@@ -76,6 +91,11 @@ protected final void doSense(Robot[] robots) throws GameActionException {
76
91
if (ri .team != rc .getTeam ()) {
77
92
enemies .add (r );
78
93
e_nearby .add (r );
94
+ if (is_air (ri .type )) {
95
+ targets_a .add (ri .location );
96
+ } else {
97
+ targets_g .add (ri .location );
98
+ }
79
99
switch (ri .type ) {
80
100
case ARCHON :
81
101
++e_archons ;
@@ -207,4 +227,84 @@ protected MoveState moveToAdjacent(MapLocation l) throws GameActionException {
207
227
return MoveState .BLOCKED ;
208
228
}
209
229
}
230
+
231
+ protected void attack (Position pos , HashSet <MapLocation > _targets ) throws GameActionException {
232
+ int dist = Integer .MAX_VALUE ;
233
+ MapLocation target = null ;
234
+ //find close target
235
+ for (MapLocation l : _targets ) {
236
+ if (dist > l .distanceSquaredTo (rc .getLocation ())) {
237
+ dist = l .distanceSquaredTo (rc .getLocation ());
238
+ target = l ;
239
+ }
240
+ }
241
+
242
+ if (target == null ) {
243
+ wander (2 , 10 );
244
+ } else if (rc .canAttackSquare (target )) {
245
+ if (rc .isAttackActive ()) {
246
+ return ;
247
+ }
248
+ if (pos == Position .AIR ) {
249
+ rc .attackAir (target );
250
+ } else {
251
+ rc .attackGround (target );
252
+ }
253
+ } else if (rc .getLocation ().directionTo (target ) != rc .getDirection ()) {
254
+ rc .setDirection (rc .getLocation ().directionTo (target ));
255
+ } else if (rc .canMove (rc .getDirection ())) {
256
+ rc .moveForward ();
257
+ }
258
+ }
259
+
260
+ //communication
261
+ private void doSendTargets (Position pos ) throws GameActionException {
262
+ Message m = new Message ();
263
+ m .locations = new MapLocation [0 ];
264
+ m .ints = new int [2 ];
265
+ m .ints [0 ] = rc .getTeam ().hashCode ();
266
+ if (pos == Position .AIR ) {
267
+ m .ints [1 ] = 0 ;
268
+ m .locations = targets_a .toArray (m .locations );
269
+ } else {
270
+ m .ints [1 ] = 1 ;
271
+ m .locations = targets_g .toArray (m .locations );
272
+ }
273
+ rc .broadcast (m );
274
+ rc .yield ();
275
+ }
276
+
277
+ protected void sendTargets () throws GameActionException {
278
+ if (!targets_a .isEmpty ())
279
+ doSendTargets (Position .AIR );
280
+ if (!targets_g .isEmpty ())
281
+ doSendTargets (Position .GROUND );
282
+
283
+ }
284
+
285
+ protected Position receiveTargets () throws GameActionException {
286
+ Message [] messages = rc .getAllMessages ();
287
+ boolean air = false , ground = false ;
288
+ if (messages .length == 0 )
289
+ return Position .NONE ;
290
+ targets_a .clear ();
291
+ targets_g .clear ();
292
+ for (Message m : messages ) {
293
+ if (m .ints .length == 2 && m .ints [0 ] == rc .getTeam ().hashCode ()) {
294
+ for (MapLocation l : m .locations ) {
295
+ if (m .ints [1 ] == 0 ) {
296
+ air = true ;
297
+ targets_a .add (l );
298
+ } else {
299
+ ground = true ;
300
+ targets_g .add (l );
301
+ }
302
+ }
303
+ }
304
+ }
305
+ if (ground && !air ) return Position .GROUND ;
306
+ else if (!ground && air ) return Position .AIR ;
307
+ else return Position .BOTH ;
308
+ }
309
+
210
310
}
0 commit comments