-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchon.java
195 lines (163 loc) · 5.02 KB
/
Archon.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
package hax;
import battlecode.common.*;
import java.util.Random;
public class Archon extends RobotBase {
private MapLocation[] archons;
private MapLocation flux_to_get;
private int[] staticArchons = new int[6];
public Archon(RobotController rc) {
super(rc);
state = State.SEARCH;
}
public void init() {
archons = rc.senseAlliedArchons();
}
protected void go() throws GameActionException {
rc.setIndicatorString(0, state.toString());
while (rc.isMovementActive()) {
rc.yield();
}
if (Clock.getRoundNum() % 2 == 0) {
sense();
sendTargets();
transfer();
if(state == State.DEFENCE
&& a_soldiers > 2 * a_cannons && rc.getEnergonLevel() > rc.getMaxEnergonLevel() / 2)
{
spawn(RobotType.CANNON);
}
if (state == State.DEFENCE && rc.getEnergonLevel() > rc.getMaxEnergonLevel() / 2) {
spawn(RobotType.SOLDIER);
}
}
if(!e_nearby.isEmpty()) {
state = State.DEFENCE;
if(rc.canBurnFlux() && rc.getEnergonLevel() < 2 * rc.getMaxEnergonLevel() / 3){
rc.burnFlux();
}
return;
} else if (state == State.DEFENCE){
if(rc.senseDirectionToOwnedFluxDeposit() == Direction.OMNI)
state = State.FOUND;
else
state = State.SEARCH;
}
switch (state) {
case SEARCH:
search();
break;
case GET_FLUX:
get_flux();
break;
case GET_NEAREST:
nearest();
break;
case FOUND:
found();
break;
case GATHER:
gather();
break;
case DONE:
return;
}
}
private void updateArchons() {
MapLocation[] new_archons = rc.senseAlliedArchons();
for (int i = 0 ; i < new_archons.length ; ++i) {
if (archons[i].equals(new_archons[i])) {
++staticArchons[i];
} else {
staticArchons[i] = 0;
}
}
archons = new_archons;
}
private void get_flux() throws GameActionException {
if (rc.getLocation().equals(flux_to_get)) {
state = State.FOUND;
return;
}
Direction uf = rc.getLocation().directionTo(flux_to_get);
if (rc.senseAirRobotAtLocation(flux_to_get) != null) {
state = State.SEARCH;
return;
}
if (! rc.getDirection().equals(uf)) {
rc.setDirection(uf);
} else if (rc.canMove(uf)) {
rc.moveForward();
}
}
private void search() throws GameActionException {
updateArchons();
Direction uf = rc.senseDirectionToUnownedFluxDeposit();
MapLocation ml = rc.getLocation();
boolean go = true;
for (MapLocation l : archons) {
if (ml.directionTo(l).equals(uf)) {
go = false;
break;
}
}
if (go) {
state = State.GET_NEAREST;
return;
}
wander(1, 10);
}
void found() throws GameActionException {
spawn(RobotType.WORKER);
state = State.GATHER;
}
private void spawn(RobotType rt) {
boolean done = false;
if (rt.isAirborne()) {
} else {
while (! done) {
try {
done = spawnGround(rt);
} catch (Exception e) {
}
}
}
rc.yield();
}
private boolean spawnGround(RobotType rt) throws GameActionException {
Direction d = rc.getDirection();
MapLocation l = rc.getLocation();
while (rc.senseTerrainTile(l.add(d)).getType() != TerrainTile.TerrainType.LAND || rc.senseGroundRobotAtLocation(l.add(d)) != null) {
d = d.rotateRight();
}
if (! d.equals(rc.getDirection())) {
rc.setDirection(d);
rc.yield();
}
while (rt.spawnCost() > rc.getEnergonLevel()) {
rc.yield();
}
rc.spawn(rt);
return true;
}
private void gather() {
if (Clock.getRoundNum() % 10 == 0) {
if (a_workers < 3 && rc.getEnergonLevel() > rc.getMaxEnergonLevel() / 2) {
spawn(RobotType.WORKER);
}
}
}
void nearest() throws GameActionException {
Direction uf = rc.senseDirectionToUnownedFluxDeposit();
if (uf.equals(Direction.NONE)) {
state = State.DONE;
} else if (uf.equals(Direction.OMNI)) {
state = State.FOUND;
} else if (! rc.getDirection().equals(uf)) {
rc.setDirection(uf);
} else if (rc.canMove(uf)) {
rc.moveForward();
} else {
state = State.SEARCH;
}
}
}