-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalTask.java
More file actions
executable file
·161 lines (134 loc) · 5.02 KB
/
GlobalTask.java
File metadata and controls
executable file
·161 lines (134 loc) · 5.02 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
import bc.MapLocation;
import bc.UnitType;
import java.util.HashSet;
public class GlobalTask {
public static int taskIndex = 0;
private static final int MAX_WORKER_COUNT = 4;
private int taskId;
private boolean isBuilt;
private boolean hasBlueprinted;
private HashSet<Integer> unitsOnTask;
private Command command;
private MapLocation taskLocation;
public GlobalTask(Command command, MapLocation taskLocation) {
taskIndex++;
this.taskId = taskIndex;
this.isBuilt = false;
this.hasBlueprinted = false;
this.command = command;
this.unitsOnTask = new HashSet<>();
this.taskLocation = taskLocation;
}
public int getTaskId() {
return taskId;
}
public void structureHasBeenBlueprinted() {
hasBlueprinted = true;
}
public boolean hasBlueprinted() {
return hasBlueprinted;
}
public void structureHasBeenBuilt() {
isBuilt = true;
}
public boolean hasBuilt() {
return isBuilt;
}
public Command getCommand() {
return command;
}
public HashSet<Integer> getUnitsOnTask() {
return unitsOnTask;
}
public MapLocation getTaskLocation() {
return taskLocation;
}
public void setTaskLocation(MapLocation taskLocation) {
this.taskLocation = taskLocation;
}
/**
* Adds a worker to the task list and send the worker the set of commands it needs to complete the global task
* @param workerId The id of the worker being added
*/
public void addWorkerToList(int workerId) {
unitsOnTask.add(workerId);
UnitInstance worker = Earth.earthWorkerMap.get(workerId);
// If the worker currently is wandering, then poll the task
if (worker.hasTasks() && worker.getCurrentTask().getCommand() == Command.WANDER) {
System.out.println("Worker: " + worker.getId() + " removed its wander task");
worker.pollCurrentTask();
}
RobotTask moveTask = new RobotTask(taskId, Command.MOVE, taskLocation);
worker.addTaskToQueue(moveTask);
RobotTask blueprintTask;
if (getCommand() == Command.CONSTRUCT_FACTORY) {
blueprintTask = new RobotTask(taskId, Command.BLUEPRINT_FACTORY, taskLocation);
} else {
blueprintTask = new RobotTask(taskId, Command.BLUEPRINT_ROCKET, taskLocation);
}
worker.addTaskToQueue(blueprintTask);
RobotTask buildTask = new RobotTask(taskId, Command.BUILD, taskLocation);
worker.addTaskToQueue(buildTask);
}
/**
* Method that will be used only when a rocket wants to be loaded. Adds the unit to the list and updates
* their tasks in their respective maps.
* @param unitId The id of the unit
*/
public void addUnitToList(int unitId) {
unitsOnTask.add(unitId);
UnitInstance unit;
if (Player.gc.unit(unitId).unitType() == UnitType.Worker) {
unit = Earth.earthWorkerMap.get(unitId);
} else {
unit = Earth.earthAttackerMap.get(unitId);
}
// If the unit is currently wandering, remove the task
if (unit.hasTasks() && unit.getCurrentTask().getCommand() == Command.WANDER) {
System.out.println("Unit: " + unit.getId() + " removed its wander task");
unit.pollCurrentTask();
}
RobotTask moveTask = new RobotTask(taskId, Command.MOVE, taskLocation);
unit.addTaskToQueue(moveTask);
RobotTask stallTask = new RobotTask(taskId, Command.STALL, taskLocation);
unit.addTaskToQueue(stallTask);
}
/**
* Removes a worker from the list and at the same time tries to find another one to complete the tasks
* @param workerId The id of the worker being removed
*/
public void removeWorkerFromList(int workerId) {
if (unitsOnTask.contains(workerId)) {
System.out.println("Removing worker: " + workerId);
unitsOnTask.remove(workerId);
} else {
System.out.println("Worker: " + workerId + " was not part of the task?");
}
// Assign the task to another worker. If none are found, it stops trying to find another unit
for (int unitId: Earth.earthWorkerMap.keySet()) {
UnitInstance unit = Earth.earthWorkerMap.get(unitId);
if (!unit.hasTasks() || (unit.hasTasks() && unit.getCurrentTask().getTaskId() == -1)) {
addWorkerToList(unitId);
break;
}
}
}
/**
* Method that will check the global task status.
* @return If the task has been completed or not
*/
public boolean checkGlobalTaskStatus(Command command) {
switch (command) {
case BLUEPRINT_FACTORY:
return hasBlueprinted;
case BLUEPRINT_ROCKET:
return hasBlueprinted;
case BUILD:
return isBuilt;
case STALL:
return false;
default:
return false;
}
}
}