-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerSquad.java
More file actions
389 lines (369 loc) · 11.5 KB
/
WorkerSquad.java
File metadata and controls
389 lines (369 loc) · 11.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import bc.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
/*
controlled by WorkerManager
basically just carry out assigned objective (build thing or idly mine)
sets objective to NONE when done (to be reassigned by manager)
*/
public class WorkerSquad extends Squad {
UnitType toBuild;
Strategy strat;
private boolean blueprinted;
HashMap<Integer, MapLocation> targetKarbLocs;
boolean debug = false;
public WorkerSquad(InfoManager im, Strategy s) {
super(im);
strat = s;
blueprinted = false;
targetKarbLocs = new HashMap<Integer,MapLocation>();
}
public void update() {
if(requestedUnits.isEmpty())
requestedUnits.add(UnitType.Worker);
urgency = strat.calcWorkerUrgency(units.size(),objective,toBuild);
}
public void moveTowardsBuildLoc(int id, Unit worker, Nav nav) {
if(!worker.location().mapLocation().isAdjacentTo(targetLoc) && gc.isMoveReady(id)) {
//Move towards the target location
Direction movedir = nav.dirToMoveSafely(worker.location().mapLocation(),targetLoc);
if (movedir != Direction.Center) {
infoMan.moveAndUpdate(id, movedir, UnitType.Worker);
worker = gc.unit(id);
}
else if(Utils.equalsMapLocation(worker.location().mapLocation(),targetLoc)) {//We are on top of the targetLoc, move away
if(debug)
Utils.log("Trying to move away from build location");
for(Direction dirToMove : Utils.orderedDirections) {
if (gc.canMove(id, dirToMove)) {
infoMan.moveAndUpdate(id, dirToMove, UnitType.Worker);
worker = gc.unit(id);
break;
}
}
}
//Last resort we're stuck and need to build
if(worker.location().mapLocation() == targetLoc) {
gc.disintegrateUnit(id);
}
}
}
public void tryToBuild (int id, Unit worker) {
int x = targetLoc.getX();
int y = targetLoc.getY();
Tile t = infoMan.tiles[x][y];
if(((t.unitID == -1 || t.myType != toBuild) && blueprinted) || (!blueprinted && infoMan.getTargetUnitsExcludeWorker(t.myLoc, MagicNumbers.FACTORY_SCARED_RADIUS).size() > 0)) {
//oh shit someone killed our building better get away or it's not safe
if(!blueprinted){
switch(toBuild){
case Factory: infoMan.factoriesToBeBuilt--; break;
default: infoMan.rocketsToBeBuilt--;
}
}
objective = Objective.NONE;
return;
}
if(t.unitID != -1 && t.myType == toBuild && gc.unit(t.unitID).structureIsBuilt() != 0) {
objective = Objective.NONE;
return;
}
//We're here! Lets make a blueprint/work on building it up.
if(worker.location().mapLocation().isAdjacentTo(targetLoc)) {
if(t.unitID != -1 && t.myType == toBuild) {
if (gc.canBuild(id, t.unitID)) {
gc.build(id, t.unitID);
}
}
}
if(t.unitID != -1 && t.myType == toBuild && gc.unit(t.unitID).structureIsBuilt() != 0) {
objective = Objective.NONE;
return;
}
if(t.unitID == -1) {
Direction dirToBuild = worker.location().mapLocation().directionTo(targetLoc);
if (gc.karbonite() > bc.bcUnitTypeBlueprintCost(toBuild)
&& gc.canBlueprint(id, toBuild, dirToBuild)) {
gc.blueprint(worker.id(), toBuild, dirToBuild);
Unit blueprint = gc.senseUnitAtLocation(targetLoc);
t.unitID = blueprint.id();
t.myType = toBuild;
blueprinted = true;
switch(toBuild){
case Factory: infoMan.factoriesToBeBuilt--;
break;
case Rocket: infoMan.rocketsToBeBuilt--;
break;
}
}
}
}
public int safeX(int x) {
if(x < 0)
return 0;
if(x > infoMan.width-1)
return infoMan.width-1;
return x;
}
public int safeY(int y) {
if(y < 0)
return 0;
if(y > infoMan.height-1)
return infoMan.height-1;
return y;
}
public void moveTowardsKarbonite(int id, Nav nav) {
if(!gc.isMoveReady(id))
return;
MapLocation myLoc = gc.unit(id).location().mapLocation();
MapLocation karbLoc = myLoc;
if(targetKarbLocs.containsKey(id)){
MapLocation targetKarbLoc = targetKarbLocs.get(id);
if(targetKarbLoc != null && infoMan.tiles[targetKarbLoc.getX()][targetKarbLoc.getY()].karbonite == 0){
//Utils.log("trying to find new stuff!");
karbLoc = findKarbLoc(myLoc);
if(karbLoc == null)
karbLoc = infoMan.getClosestKarbonite(myLoc);
}
else
karbLoc = targetKarbLoc;
}
else{
karbLoc = findKarbLoc(myLoc);
if(karbLoc == null)
karbLoc = infoMan.getClosestKarbonite(myLoc);
}
if(karbLoc == null){
//Utils.log("running away :(");
runAway(id,myLoc);
return;
}
targetKarbLocs.put(id, karbLoc);
Direction d = nav.dirToMoveSafely(myLoc, karbLoc);
if(debug && infoMan.myPlanet == Planet.Mars)
Utils.log("just a worker trying to move to " + karbLoc);
infoMan.moveAndUpdate(id, d, UnitType.Worker);
}
public void runAway(int id, MapLocation loc){
double bestScore = -10000000;
int bestInd = 8;
int x = loc.getX();
int y = loc.getY();
int nx,ny;
for(int i=0; i < 8; i++){
nx = x + Utils.dx[i];
ny = y + Utils.dy[i];
if(!infoMan.isOnMap(nx,ny))
continue;
Tile t = infoMan.tiles[nx][ny];
if(!t.isWalkable || !infoMan.isLocationClear(nx, ny))
continue;
t.updateEnemies(gc);
double score = -100.0 * t.possibleDamage + t.distFromNearestHostile;
if(score>bestScore){
bestScore = score;
bestInd = i;
}
}
infoMan.moveAndUpdate(id, Utils.indexToDirection(bestInd), UnitType.Worker);
}
public boolean tryToMine(int id) {
if(gc.canHarvest(id,Direction.Center)) {
gc.harvest(id, Direction.Center);
return true;
}
if(gc.canHarvest(id,Direction.North)) {
gc.harvest(id, Direction.North);
return true;
}
if(gc.canHarvest(id,Direction.South)) {
gc.harvest(id, Direction.South);
return true;
}
if(gc.canHarvest(id,Direction.East)) {
gc.harvest(id, Direction.East);
return true;
}
if(gc.canHarvest(id,Direction.West)) {
gc.harvest(id, Direction.West);
return true;
}
if(gc.canHarvest(id,Direction.Northwest)) {
gc.harvest(id, Direction.Northwest);
return true;
}
if(gc.canHarvest(id,Direction.Northeast)) {
gc.harvest(id, Direction.Northeast);
return true;
}
if(gc.canHarvest(id,Direction.Southwest)) {
gc.harvest(id, Direction.Southwest);
return true;
}
if(gc.canHarvest(id,Direction.Southeast)) {
gc.harvest(id, Direction.Southeast);
return true;
}
return false;
}
public void replicateWorker(int id, Nav nav) {
if(targetLoc != null) {
Direction tempDirection = gc.unit(id).location().mapLocation().directionTo(targetLoc);
if(tempDirection == Direction.Center) {
tempDirection = Direction.North;
}
for(Direction dirToReplicate : Utils.directionsToward(tempDirection)) {
//Utils.log("My direction to replicate is " + dirToReplicate + "," + targetLoc);
if (gc.canReplicate(id, dirToReplicate)) {
gc.replicate(id, dirToReplicate);
MapLocation rloc = gc.unit(id).location().mapLocation().add(dirToReplicate);
Unit repped = gc.senseUnitAtLocation(rloc);
if(!tryToMine(repped.id())) {
moveTowardsKarbonite(repped.id(),nav);
tryToMine(id);
}else {
moveTowardsKarbonite(id,nav);
}
infoMan.tiles[rloc.getX()][rloc.getY()].unitID = repped.id();
infoMan.tiles[rloc.getX()][rloc.getY()].myType = UnitType.Worker;
infoMan.workerCount++;
infoMan.workersToRep.remove(id);
break;
}
}
}
else {
for(Direction dirToReplicate : Utils.orderedDirections) {
if (gc.canReplicate(id, dirToReplicate)) {
gc.replicate(id, dirToReplicate);
MapLocation rloc = gc.unit(id).location().mapLocation().add(dirToReplicate);
Unit repped = gc.senseUnitAtLocation(rloc);
if(!tryToMine(repped.id())) {
moveTowardsKarbonite(repped.id(),nav);
tryToMine(id);
}else {
moveTowardsKarbonite(id,nav);
}
infoMan.tiles[rloc.getX()][rloc.getY()].unitID = repped.id();
infoMan.tiles[rloc.getX()][rloc.getY()].myType = UnitType.Worker;
infoMan.workerCount++;
infoMan.workersToRep.remove(id);
break;
}
}
}
}
public void move(Nav nav, Strategy strat) {
Utils.log("ws reporting: size = " + units.size() + " toBuild = " + toBuild + " objective = " + objective + " urgency = " + urgency);
if(targetLoc != null)
Utils.log("targetLoc = " + targetLoc);
for(int id: units) {
Unit worker = gc.unit(id);
if(worker.location().isInSpace() || worker.location().isInGarrison())
continue;
if(worker.abilityHeat() < 10 && infoMan.workersToRep.contains(id)){
replicateWorker(id, nav);
}
switch (objective) {
case BUILD:
if(blueprinted || strat.shouldGoToBuildLoc()){
if(targetLoc != null) {
moveTowardsBuildLoc(id, worker, nav);
if(worker.location().mapLocation().isAdjacentTo(targetLoc)) {
tryToBuild(id, worker);
}
}
else {
//we are told to build without a location??? angrily build in a random direction directions.
VecUnit vu = gc.senseNearbyUnits(worker.location().mapLocation(), 2);
for (int i = 0; i < vu.size(); i++) {
if(vu.get(i).unitType() == toBuild) {
if (gc.canBuild(id, vu.get(i).id())) {
gc.build(id, vu.get(i).id());
}
if(vu.get(i).structureIsBuilt()!=0) {
objective = Objective.NONE;
}
}
}
for(Direction dirToBuild : Utils.orderedDirections) {
if(blueprinted)
break;
if (gc.karbonite() > bc.bcUnitTypeBlueprintCost(toBuild)
&& gc.canBlueprint(id, toBuild, dirToBuild)) {
gc.blueprint(worker.id(), toBuild, dirToBuild);
MapLocation bloc = worker.location().mapLocation().add(dirToBuild);
Unit blueprint = gc.senseUnitAtLocation(bloc);
infoMan.tiles[bloc.getX()][bloc.getY()].unitID = blueprint.id();
infoMan.tiles[bloc.getX()][bloc.getY()].myType = toBuild;
blueprinted = true;
switch(toBuild){
case Factory: infoMan.factoriesToBeBuilt--;
break;
case Rocket: infoMan.rocketsToBeBuilt--;
break;
}
}
}
}
}
else if(!tryToMine(id))
moveTowardsKarbonite(id,nav);
tryToMine(id);
break;
case MINE:
if(!tryToMine(id)) {
moveTowardsKarbonite(id,nav);
tryToMine(id);
}else {
moveTowardsKarbonite(id,nav);
}
break;
default:
break;
}
}
infoMan.logTimeCheckpoint("workers moved");
}
private MapLocation findKarbLoc(MapLocation loc) {
MapLocation sampleLoc = loc;
MapLocation start = sampleLoc;
int x = start.getX();
int y = start.getY();
if(infoMan.tiles[x][y].karbonite > 0)
return start;
int increment = 1;
boolean dir = true;
for(int n = 0; n < 5; n++){
if(dir){
for(int a = 0; a < increment; a++){
y--;
if(infoMan.isReachable(x, y, sampleLoc) && infoMan.tiles[x][y].karbonite > 0 && infoMan.tiles[x][y].unitID == -1)
return new MapLocation(infoMan.myPlanet,x,y);
}
for(int a = 0; a < increment; a++){
x++;
if(infoMan.isReachable(x, y, sampleLoc) && infoMan.tiles[x][y].karbonite > 0 && infoMan.tiles[x][y].unitID == -1)
return new MapLocation(infoMan.myPlanet,x,y);
}
increment++;
dir = false;
}
else{
for(int a = 0; a < increment; a++){
y++;
if(infoMan.isReachable(x, y, sampleLoc) && infoMan.tiles[x][y].karbonite > 0 && infoMan.tiles[x][y].unitID == -1)
return new MapLocation(infoMan.myPlanet,x,y);
}
for(int a = 0; a < increment; a++){
x--;
if(infoMan.isReachable(x, y, sampleLoc) && infoMan.tiles[x][y].karbonite > 0 && infoMan.tiles[x][y].unitID == -1)
return new MapLocation(infoMan.myPlanet,x,y);
}
increment++;
dir = true;
}
}
return null;
}
}