Skip to content

Commit f930196

Browse files
author
Filip Balejko
committed
communication (enemy locations) and simple cannon
1 parent 329ef47 commit f930196

File tree

5 files changed

+206
-1
lines changed

5 files changed

+206
-1
lines changed

Archon.java

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Archon extends RobotBase {
99
private MapLocation[] archons;
1010
private MapLocation flux_to_get;
1111
private int[] staticArchons = new int[6];
12+
private int spawned_soldiers = 0;
1213

1314
enum State {
1415
GET_NEAREST,
@@ -34,10 +35,16 @@ protected void go() throws GameActionException {
3435
}
3536
if (Clock.getRoundNum() % 2 == 0) {
3637
sense();
38+
sendTargets();
3739
transfer();
3840
if (e_nearby.size() > a_soldiers - 2 && rc.getEnergonLevel() > rc.getMaxEnergonLevel() / 2) {
3941
spawn(RobotType.SOLDIER);
42+
spawned_soldiers++;
4043
}
44+
if(spawned_soldiers % 3 == 0 && e_nearby.size() > a_soldiers - 2 && rc.getEnergonLevel() > rc.getMaxEnergonLevel() / 2)
45+
{
46+
spawn(RobotType.CANNON);
47+
}
4148
}
4249

4350
switch (state) {

Cannon.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package hax;
2+
3+
import java.util.HashSet;
4+
5+
import battlecode.common.*;
6+
7+
public class Cannon extends RobotBase {
8+
private State state = State.ATTACK_A;
9+
10+
public Cannon(RobotController rc) {
11+
super(rc);
12+
}
13+
14+
enum State {
15+
ATTACK_G,
16+
ATTACK_A
17+
}
18+
19+
20+
protected void init() throws GameActionException {
21+
}
22+
23+
protected void go() throws GameActionException {
24+
rc.setIndicatorString(0, state.toString());
25+
while (rc.isMovementActive()) {
26+
rc.yield();
27+
}
28+
29+
if (Clock.getRoundNum() % 3 == 0) {
30+
transfer();
31+
}
32+
33+
Position pos = receiveTargets();
34+
if (pos == Position.NONE) {
35+
wander(2, 10);
36+
return;
37+
}
38+
39+
switch(pos)
40+
{
41+
case AIR:
42+
state = State.ATTACK_A;
43+
break;
44+
case GROUND:
45+
state = State.ATTACK_G;
46+
break;
47+
case BOTH:
48+
state = State.ATTACK_A; //air priority
49+
break;
50+
}
51+
52+
53+
switch (state) {
54+
case ATTACK_G:
55+
attack(Position.GROUND, targets_g);
56+
break;
57+
case ATTACK_A:
58+
attack(Position.AIR, targets_a);
59+
break;
60+
}
61+
}
62+
63+
}

RobotBase.java

+100
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public abstract class RobotBase {
1717

1818
protected final HashSet<Robot> enemies = new HashSet<Robot>();
1919
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>();
2022
protected int e_archons = 0;
2123
protected int e_workers = 0;
2224
protected int e_soldiers = 0;
@@ -28,6 +30,13 @@ protected enum MoveState {
2830
DONE,
2931
BLOCKED
3032
}
33+
34+
enum Position {
35+
AIR, //0
36+
GROUND, //1
37+
NONE,
38+
BOTH
39+
}
3140

3241
public RobotBase(RobotController rc) {
3342
this.rc = rc;
@@ -63,11 +72,17 @@ protected final void sense() throws GameActionException {
6372
e_archons = 0;
6473
e_workers = 0;
6574
e_soldiers = 0;
75+
targets_a.clear();
76+
targets_g.clear();
6677

6778
doSense(rc.senseNearbyGroundRobots());
6879
doSense(rc.senseNearbyAirRobots());
6980
}
7081

82+
protected final boolean is_air(RobotType rt){
83+
return rt == RobotType.ARCHON || rt == RobotType.SCOUT;
84+
}
85+
7186
protected final void doSense(Robot[] robots) throws GameActionException {
7287
for (Robot r : robots) {
7388
if (rc.canSenseObject(r)) {
@@ -76,6 +91,11 @@ protected final void doSense(Robot[] robots) throws GameActionException {
7691
if (ri.team != rc.getTeam()) {
7792
enemies.add(r);
7893
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+
}
7999
switch (ri.type) {
80100
case ARCHON:
81101
++e_archons;
@@ -207,4 +227,84 @@ protected MoveState moveToAdjacent(MapLocation l) throws GameActionException {
207227
return MoveState.BLOCKED;
208228
}
209229
}
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+
210310
}

RobotPlayer.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public RobotPlayer(RobotController rc) {
1717
case SOLDIER:
1818
robot = new Soldier(rc);
1919
break;
20+
case CANNON:
21+
robot = new Cannon(rc);
22+
break;
2023
default:
2124
System.out.print("No robot for " + rc.getRobotType().toString());
2225
}

Soldier.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package hax;
22

3+
import hax.Cannon.State;
4+
import hax.RobotBase.Position;
5+
6+
import java.util.HashSet;
7+
38
import battlecode.common.*;
49

510
public class Soldier extends RobotBase {
@@ -10,7 +15,8 @@ public Soldier(RobotController rc) {
1015
}
1116

1217
enum State {
13-
ATTACK,
18+
ATTACK_G,
19+
ATTACK_A,
1420
SEARCH
1521
}
1622

@@ -24,6 +30,24 @@ protected void go() throws GameActionException {
2430
}
2531

2632
sense();
33+
34+
Position pos = receiveTargets();
35+
36+
switch(pos)
37+
{
38+
case NONE:
39+
state = State.SEARCH;
40+
break;
41+
case AIR:
42+
state = State.ATTACK_A;
43+
break;
44+
case GROUND:
45+
state = State.ATTACK_G;
46+
break;
47+
case BOTH:
48+
state = State.ATTACK_G; //ground priority
49+
break;
50+
}
2751

2852
if (Clock.getRoundNum() % 5 == 0) {
2953
transfer();
@@ -33,6 +57,12 @@ protected void go() throws GameActionException {
3357
case SEARCH:
3458
search();
3559
break;
60+
case ATTACK_G:
61+
attack(Position.GROUND, targets_g);
62+
break;
63+
case ATTACK_A:
64+
attack(Position.AIR, targets_g);
65+
break;
3666
}
3767
}
3868

@@ -64,4 +94,6 @@ private void search() throws GameActionException {
6494
rc.moveForward();
6595
}
6696
}
97+
98+
6799
}

0 commit comments

Comments
 (0)