-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.java
143 lines (126 loc) · 3.81 KB
/
Worker.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
package hax;
import battlecode.common.*;
import java.util.HashSet;
public class Worker extends RobotBase {
private MapLocation archon;
private MapLocation target;
private HashSet<MapLocation> bad_blocks = new HashSet<MapLocation>();
public Worker(RobotController rc) {
super(rc);
state = State.GET_BACK;
}
protected void init() throws GameActionException {
for (MapLocation l : rc.senseAlliedArchons()) {
if (l.isAdjacentTo(rc.getLocation()) && rc.senseFluxDepositAtLocation(l) != null) {
archon = l;
break;
}
}
}
protected void go() throws GameActionException {
rc.setIndicatorString(0, state.toString());
while (rc.isMovementActive()) {
rc.yield();
}
//help in DEFENCE
receiveTargets();
if(targets_a.size() + targets_g.size() > 2)
{
rc.transform(RobotType.CANNON);
RobotBase robot = new Cannon(rc);
try {
robot.play();
} catch (GameActionException e) {}
}
switch (state) {
case SEARCH:
search();
break;
case GET_BACK:
get_back();
break;
case GET_BLOCK:
get_block();
break;
case UNLOAD:
unload();
break;
}
}
private void search() throws GameActionException {
if (rc.getEventualEnergonLevel() < RobotType.WORKER.maxEnergon() / 2) {
state = State.GET_BACK;
return;
}
MapLocation[] blocks = rc.senseNearbyBlocks();
for (MapLocation l : blocks) {
if (l.distanceSquaredTo(archon) > 4) {
if (target == null || (! bad_blocks.contains(l) && archon.distanceSquaredTo(l) < archon.distanceSquaredTo(target)))
target = l;
}
}
if (target != null) {
state = State.GET_BLOCK;
return;
}
wander(1, 10);
}
private void get_block() throws GameActionException {
switch (moveToAdjacent(target)) {
case BLOCKED:
bad_blocks.add(target);
state = State.SEARCH;
return;
case DONE:
break;
case MOVING:
return;
}
if (rc.canLoadBlockFromLocation(target)) {
rc.loadBlockFromLocation(target);
state = State.GET_BACK;
target = null;
bad_blocks.clear();
} else {
bad_blocks.add(target);
state = State.SEARCH;
}
}
private void get_back() throws GameActionException {
switch (moveToAdjacent(archon)) {
case BLOCKED:
target = rc.getLocation();
state = State.UNLOAD;
break;
case DONE:
target = archon;
state = State.UNLOAD;
break;
case MOVING:
return;
}
}
private void unload() throws GameActionException {
if (rc.getNumBlocks() > 0) {
switch (moveToAdjacent(target)) {
case BLOCKED:
rc.suicide();
return;
case DONE:
break;
case MOVING:
return;
}
if (! rc.canUnloadBlockToLocation(target)) {
target = rc.getLocation();
return;
}
while (rc.getCurrentAction() != ActionType.IDLE) {
rc.yield();
}
rc.unloadBlockToLocation(target);
}
target = null;
state = State.SEARCH;
}
}