-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoldier.java
95 lines (77 loc) · 2.31 KB
/
Soldier.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
package hax;
import java.util.HashSet;
import battlecode.common.*;
public class Soldier extends RobotBase {
public Soldier(RobotController rc) {
super(rc);
state = State.SEARCH;
}
protected void init() throws GameActionException {
}
protected void go() throws GameActionException {
rc.setIndicatorString(0, state.toString());
while (rc.isMovementActive()) {
rc.yield();
}
Position pos = Position.NONE;
sense();
if(e_nearby.isEmpty())
pos = receiveTargets();
switch(pos)
{
case NONE:
state = State.SEARCH;
break;
case AIR:
state = State.ATTACK_A;
break;
case GROUND:
state = State.ATTACK_G;
break;
case BOTH:
state = State.ATTACK_G; //ground priority
break;
}
if (Clock.getRoundNum() % 5 == 0) {
transfer();
}
switch (state) {
case SEARCH:
search();
break;
case ATTACK_G:
attack(Position.GROUND, targets_g);
break;
case ATTACK_A:
attack(Position.AIR, targets_g);
break;
}
}
private void search() throws GameActionException {
RobotInfo target = null;
int dist = Integer.MAX_VALUE;
for (Robot r : e_nearby) {
RobotInfo ri = r_info.get(r);
if (dist > ri.location.distanceSquaredTo(rc.getLocation())) {
dist = ri.location.distanceSquaredTo(rc.getLocation());
target = ri;
}
}
if (target == null) {
wander(2, 10);
} else if (rc.canAttackSquare(target.location)) {
if (rc.isAttackActive()) {
return;
}
if (target.type.isAirborne()) {
rc.attackAir(target.location);
} else {
rc.attackGround(target.location);
}
} else if (rc.getLocation().directionTo(target.location) != rc.getDirection()) {
rc.setDirection(rc.getLocation().directionTo(target.location));
} else if (rc.canMove(rc.getDirection())) {
rc.moveForward();
}
}
}