-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.java
More file actions
executable file
·338 lines (275 loc) · 13.7 KB
/
Worker.java
File metadata and controls
executable file
·338 lines (275 loc) · 13.7 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import bc.*;
import java.util.*;
public class Worker extends Robot {
// TODO: create a method that will analyze the map and determine the number of workers needed
private static final int NUMBER_OF_WORKERS_NEEDED = 4;
private MapLocation spawnLocation;
public Worker(int id) {
super(id);
spawnLocation = Player.gc.unit(id).location().mapLocation();
}
@Override
public void run() {
// System.out.println("Worker: " + this.getId() + " location: " + Player.locationToString(this.getLocation()));
if (this.getEmergencyTask() != null) {
executeEmergencyTask();
}
if (this.hasTasks()) {
checkTaskStatus();
executeCurrentTask();
if (this.hasTasks() && this.getCurrentTask().getCommand() == Command.STALL) {
return;
}
} else if (!this.hasTasks()) {
executeIdleActions();
}
// The worker will always try to mine karbonite when it can
mineKarbonite();
}
/**
* Method that will check the current status of the the worker's task. Removes task if it is already finished
*/
private void checkTaskStatus() {
if (this.getCurrentTask().getTaskId() != -1) {
GlobalTask currentGlobalTask = Earth.earthTaskMap.get(this.getCurrentTask().getTaskId());
if (currentGlobalTask.checkGlobalTaskStatus(this.getCurrentTask().getCommand())) {
// System.out.println("Worker: " + this.getId() + " popped task " + this.getCurrentTask().getCommand());
this.pollCurrentTask();
// If the task was already completed, check if the next one was completed as well
if (this.hasTasks()) {
checkTaskStatus();
}
}
}
}
/**
* Helper method that will control how the robot operates when it has an emergency task that is not STALL
*/
private void executeEmergencyTask() {
if (executeTask(this.getEmergencyTask())) {
// System.out.println("Worker: " + this.getId() + " Finished emergency task!");
this.setEmergencyTask(null);
}
}
/**
* Helper method that will run the workers current tasks. If it finished one, it checks if it can start the next
*/
private void executeCurrentTask() {
if (this.hasTasks() || this.getEmergencyTask() != null) {
// System.out.println("Worker " + this.getId() + " on task " + this.getCurrentTask().getCommand() +
// " in global task: " + this.getCurrentTask().getTaskId() + " at " + Player.locationToString(this.getCurrentTask().getCommandLocation()));
}
if (this.hasTasks() || this.getEmergencyTask() != null) {
if (executeTask(this.getCurrentTask())) {
// System.out.println("Worker: " + this.getId() + " has finished task: " + this.getCurrentTask().getCommand());
this.pollCurrentTask();
// If the worker has completed the current task, check if it can also complete the next one
if (this.hasTasks()) {
executeCurrentTask();
}
}
}
}
/**
* Executes the task from the task queue
* @param robotTask The task the robot has to complete
* @return If the task was completed or not
*/
private boolean executeTask(RobotTask robotTask) {
Command robotCommand = robotTask.getCommand();
MapLocation commandLocation = robotTask.getCommandLocation();
switch (robotCommand) {
case MOVE:
return this.pathManager(commandLocation);
case WANDER:
return this.pathManager(commandLocation);
case CLONE:
return cloneWorker(commandLocation);
case BUILD:
return buildStructure(commandLocation);
case BLUEPRINT_FACTORY:
return blueprintStructure(commandLocation, UnitType.Factory);
case BLUEPRINT_ROCKET:
return blueprintStructure(commandLocation, UnitType.Rocket);
case STALL:
this.requestUnitToLoad(commandLocation);
return false;
default:
// System.out.println("Critical error occurred in Worker: " + this.getId());
return true;
}
}
/**
* Helper method that will control what the robot does when it has no current tasks
*/
private void executeIdleActions() {
MapLocation newMoveLocation = getNearestKarboniteLocation();
if (newMoveLocation != null) {
this.addTaskToQueue(new RobotTask(-1, Command.WANDER, newMoveLocation));
// System.out.println("Worker: " + this.getId() + " Setting task to wander and mine");
} else {
wanderWithinRadius(100);
// System.out.println("Worker: " + this.getId() + " Wandering!");
}
}
/**
* Given a MapLocation, see if you can clone a worker and put it at that spot
* @param commandLocation The MapLocation of the new worker
* @return If the worker was cloned or not
*/
private boolean cloneWorker(MapLocation commandLocation) {
MapLocation robotCurrentLocation = Player.gc.unit(this.getId()).location().mapLocation();
for (int i = 0; i < 8; i++) {
Direction direction = Direction.swigToEnum(i);
MapLocation newLocation = robotCurrentLocation.add(direction);
if (commandLocation.isAdjacentTo(newLocation)) {
Direction directionToClone = robotCurrentLocation.directionTo(newLocation);
if (Player.gc.canReplicate(this.getId(), directionToClone)) {
Player.gc.replicate(this.getId(), directionToClone);
int clonedWorkerId = Player.senseUnitAtLocation(newLocation).id();
UnitInstance newWorker = new Worker(clonedWorkerId);
Earth.earthStagingWorkerMap.put(clonedWorkerId, newWorker);
// System.out.println("Worker: " + this.getId() + " Cloned worker!");
// System.out.println("New worker has ID of: " + clonedWorkerId);
return true;
}
}
}
return false;
}
/**
* Given the map location of a blueprint you want to build, check if you can build it and add
* a new blueprint instance to the blueprint map
* @param commandLocation The MapLocation of the blueprint you want to build
* @param structureType Either a factory or rocket blueprint
* @return If the blueprint was built or not
*/
private boolean blueprintStructure(MapLocation commandLocation, UnitType structureType) {
Direction directionToBlueprint = this.getLocation().directionTo(commandLocation);
if (Player.gc.canBlueprint(this.getId(), structureType, directionToBlueprint) &&
this.getLocation().isAdjacentTo(commandLocation)) {
Player.gc.blueprint(this.getId(), structureType, directionToBlueprint);
int structureId = Player.gc.senseUnitAtLocation(commandLocation).id();
if (structureType == UnitType.Factory) {
UnitInstance newStructure = new Factory(structureId, false);
Earth.earthFactoryMap.put(structureId, newStructure);
} else {
Rocket newStructure = new Rocket(structureId, false);
Earth.earthRocketMap.put(structureId, newStructure);
}
// Set the global task variable hasBlueprinted to true
Earth.earthTaskMap.get(this.getCurrentTask().getTaskId()).structureHasBeenBlueprinted();
// System.out.println("Worker: " + this.getId() + " Blueprinted structure at " + Player.locationToString(commandLocation));
return true;
}
return false;
}
/**
* Given a MapLocation of a blueprint, build it until it reaches full health and becomes a rocket/factory
* @param commandLocation The location of the unfinished structure
* @return If the structure finished building
*/
private boolean buildStructure(MapLocation commandLocation) {
int structureId;
if (Player.gc.hasUnitAtLocation(commandLocation)) {
structureId = Player.senseUnitAtLocation(commandLocation).id();
} else {
// If for some reason the factory at the given location disappeared, return true to pop the task
return true;
}
if (Player.gc.canBuild(this.getId(), structureId)) {
Player.gc.build(this.getId(), structureId);
// System.out.println("Worker: " + this.getId() + " is building structure " + structureId);
// Check if it can clone here because we know it has no path when it is building and while building
// Is when you need another worker the most
if (Player.gc.karbonite() > 60 && Earth.earthWorkerMap.size() < NUMBER_OF_WORKERS_NEEDED) {
executeTask(new RobotTask(-1, Command.CLONE, commandLocation));
}
if (Player.gc.unit(structureId).structureIsBuilt() > 0) {
UnitType unitType = Player.gc.unit(structureId).unitType();
if (unitType == UnitType.Factory) {
UnitInstance builtFactory = new Factory(structureId, true);
Earth.earthFactoryMap.put(structureId, builtFactory);
} else {
Rocket builtRocket = new Rocket(structureId, true);
Earth.earthFactoryMap.put(structureId, builtRocket);
}
// Set the global task variable hasBlueprinted to true
Earth.earthTaskMap.get(this.getCurrentTask().getTaskId()).structureHasBeenBuilt();
// System.out.println("Worker: " + this.getId() + " Built structure");
return true;
}
}
return false;
}
/**
* Method that will check if a worker can mine karbonite. If it has not performed an action this turn and
* there is a karbonite pocket in adjacent squares, it will mine it
*/
private void mineKarbonite() {
for (int i = 0; i < 8 + 1; i++) {
Direction direction = Direction.swigToEnum(i);
MapLocation newLocation = Player.gc.unit(this.getId()).location().mapLocation().add(direction);
if (Player.gc.canHarvest(this.getId(), direction) && Player.karboniteAt(newLocation) > 0) {
Player.gc.harvest(this.getId(), direction);
// System.out.println("Worker: " + this.getId() + " mined karbonite");
break;
}
}
}
/**
* Method that will get the nearest location of a karbonite deposit. POTENTIAL MEM LEAK IF RUNS TOO MUCH
* @return The MapLocation of the nearest karbonite deposit. Null if there is no karbonite on the map
*/
// TODO: Change this so that is senses all locations within a radius of x. If is finds any within the radius
// TODO: Of the unit, THEN start the search algorithm.
private MapLocation getNearestKarboniteLocation() {
MapLocation destinationLocation = null;
MapLocation myLocation = this.getLocation();
Queue<MapLocation> frontier = new LinkedList<>();
frontier.add(myLocation);
HashMap<String, MapLocation> checkedLocations = new HashMap<>();
checkedLocations.put(Player.locationToString(myLocation), myLocation);
int counter = 0;
while (!frontier.isEmpty() && counter < 100) {
// Get next direction to check around. Will put in the checked location a pair with the key as the
// Next location with the value as the current location.
MapLocation currentLocation = frontier.poll();
if (Player.gc.canSenseLocation(currentLocation) && Player.gc.karboniteAt(currentLocation) > 0) {
destinationLocation = currentLocation;
//checkedLocations.put(Player.locationToString(destinationLocation), currentLocation);
frontier.clear();
break;
}
// shuffle makes stay more in a general area rather than constantly gravitate northward then
ArrayList<Direction> moveDirections = Player.getMoveDirections();
Collections.shuffle(moveDirections);
// Check if locations around frontier location have already been added to came from and if they are empty
for (Direction nextDirection: moveDirections) {
MapLocation nextLocation = currentLocation.add(nextDirection);
if (Player.gc.canSenseLocation(nextLocation) && Player.isLocationEmpty(nextLocation) &&
!checkedLocations.containsKey(Player.locationToString(nextLocation))) {
frontier.add(nextLocation);
checkedLocations.put(Player.locationToString(nextLocation), currentLocation);
}
}
counter++;
}
return destinationLocation;
}
/**
* Method that will set a robots task to wander within a certain radius
* @param radius The radius to wander in
*/
private void wanderWithinRadius(int radius) {
VecMapLocation mapLocations = Player.gc.allLocationsWithin(spawnLocation, radius);
MapLocation wanderLocation = null;
while (wanderLocation == null) {
int randomLocation = (int)(Math.random() * mapLocations.size());
if (Player.isLocationEmpty(mapLocations.get(randomLocation))) {
wanderLocation = mapLocations.get(randomLocation);
}
}
this.addTaskToQueue(new RobotTask(-1, Command.WANDER, wanderLocation));
}
}