-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit-dense-resource-supplier.ts
415 lines (351 loc) · 11.1 KB
/
unit-dense-resource-supplier.ts
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// stable
var dropCount = 999
var unitFlag = 140000 + Math.rand(999)
const config = {
waypoints: new MutableArray([
158, 322, 267, 322,
272, 351, 272, 380
// 272, 124, 343, 124,
// 299, 127, 272, 127
]),
itemTypes: new MutableArray([
Items.phaseFabric,
]),
acceptedBlocks: new MutableArray([
Blocks.forceProjector,
Blocks.mendProjector,
Blocks.overdriveProjector,
// Blocks.thoriumReactor,
]),
bindableUnitTypes: new MutableArray([
Units.mega,
Units.poly,
Units.mono,
]),
allowTakingFromContainer: 1,
}
var unitItemCapacity = undefined;
var unitRange = undefined;
main()
function main() {
print(' -- ')
printFlush()
bindFreeUnitFromConfig()
wait(1)
init()
while (true) {
for (var itemIndex = 0; itemIndex < config.itemTypes.size; itemIndex++) {
print`Trying to fill ${config.itemTypes[itemIndex]}`
printFlush()
doFill(config.itemTypes[itemIndex])
}
}
}
function doFill(itemType) {
var waypointCnts = Math.idiv(config.waypoints.size, 4)
for (var waypointIndex = 0; waypointIndex < waypointCnts; waypointIndex++) {
var bi = waypointIndex * 4;
var bx = config.waypoints[bi++];
var by = config.waypoints[bi++];
var ex = config.waypoints[bi++];
var ey = config.waypoints[bi++];
print`Ready to scan [green]${bx}->${ex}, ${by}->${ey}`
printFlush()
wait(0.25)
var lastBuilding = undefined;
var dx = ex - bx
var dy = ey - by
var stepCnt = Math.max(Math.abs(dx), Math.abs(dy))
var vx = dx / stepCnt
var vy = dy / stepCnt
var pxFloat = bx;
var pyFloat = by;
for (var step = 0; step <= stepCnt; step++, pxFloat += vx, pyFloat += vy) {
var px = Math.floor(pxFloat + 0.5)
var py = Math.floor(pyFloat + 0.5)
print`[blue]Scanning ${px}, ${py}\n${pxFloat}, ${pyFloat}`
printFlush()
// wait(0.5)
// continue
if (Vars.unit.totalItems == 0 || Vars.unit.firstItem != itemType) {
print`Fetch ${itemType} from home`
printFlush()
if (!fetchItemFromAutomaticSource(itemType)) return;
}
var typ = undefined, building = undefined;
while (true) {
[typ, building,] = unitControl.getBlock(px, py)
if (typ !== undefined) break;
doUnitApproachAndWait(px, py)
print`[accent]Cannot get block info on ${px}, ${py}`
printFlush()
}
if (building == lastBuilding) continue;
if (isAcceptedBlockType(typ)) {
approachIfNecessary(px, py, 4)
print`[orange]Dropping at ${px}, ${py}\n${pxFloat}, ${pyFloat}`
printFlush()
if (!ensureDrop(building, dropCount)) {
print`[red]Drop timeout exceeded.`
printFlush()
wait(0.5)
}
}
checkUnitAvailability()
selfHealIfNecessary()
lastBuilding = building
}
}
}
function ensureDrop(building, count) {
var unitItemType = Vars.unit.firstItem;
var dropUntil = Math.max(Vars.unit.totalItems - (building.itemCapacity - building[unitItemType]), Vars.unit.totalItems - count)
dropUntil = Math.max(dropUntil, 0)
var timeout = Vars.time + 5000
while (Vars.time < timeout && Vars.unit.totalItems > dropUntil) {
unitControl.itemDrop(building, Vars.unit.totalItems - dropUntil)
wait(0.3)
}
return Vars.unit.totalItems <= dropUntil
}
function isAcceptedBlockType(typ) {
for (var i = 0; i < config.acceptedBlocks.size; i++) {
if (typ == unchecked(config.acceptedBlocks[i])) return true;
}
return false;
}
function checkUnitAvailability() {
if (!Vars.unit) {
print`[red]Invalid unit ${Vars.unit}`
printFlush()
wait(1.5)
}
unitControl.flag(unitFlag)
var ctlr = Vars.unit.controller
if (Vars.unit.dead) {
endScript()
} else if (ctlr != Vars.this) {
print`[red]Possession taken by ${ctlr} @${ctlr.x}, ${ctlr.y}\n`
printFlush()
wait(1.5)
// stopScript()
endScript()
}
}
function selfHealIfNecessary() {
if (Vars.unit.health > 0.5 * Vars.unit.maxHealth) return;
var [found, x, y,] = unitLocate.building(
{
group: "repair",
enemy: false
}
)
if (!found) return;
print`[accent]Seeking repairment @${x}, ${y}`
printFlush()
doUnitApproachAndWait(x, y);
print`[green][accent]Repairing @${x}, ${y}`
printFlush()
var timeout = Vars.time + 10000;
while (Vars.unit.health < 0.9 * Vars.unit.maxHealth && Vars.time < timeout) {
checkUnitAvailability()
}
}
function waitUntilStop() {
var px = -1;
var py = -1;
var lastTime = Vars.time;
while (true) {
var nx = Vars.unit.x;
var ny = Vars.unit.y;
var time = Vars.time;
if ((Math.abs(nx - px) + Math.abs(ny - py)) / lastTime < 1 / 1000) {
return;
}
px = nx;
py = ny;
lastTime = time;
}
}
function init() {
unitItemCapacity = Vars.unit.itemCapacity;
unitRange = Vars.unit.range;
}
function recoverPosessionOfUnit(unitType) {
print`Trying to recover possession of ${unitType}`
printFlush()
var first = undefined;
var ttl = 64;
while (ttl--) {
unitBind(unitType)
if (Vars.unit === undefined) break;
if (first === undefined) {
first = Vars.unit;
} else if (first == Vars.unit) {
break;
}
if (Vars.unit.controller == Vars.this) {
unitControl.flag(unitFlag)
print`Recovered possession of ${Vars.unit}\n`
printFlush()
return true;
}
}
return undefined;
}
function bindFreeUnit(unitType, tryBind = false) {
var ttl = 64;
var first = Vars.unit;
while (true) {
unitBind(unitType)
if (Vars.unit === undefined) break;
if (first === undefined) {
first = Vars.unit;
} else if (first == Vars.unit) {
break;
}
print`Trying to bind to a new ${unitType} \nprevious ttl=${ttl}`
printFlush()
if (Vars.unit.controlled && Vars.unit.controller != Vars.this) {
// continue
} else {
unitControl.flag(unitFlag)
print`Checking exclusive access to ${Vars.unit}`
printFlush()
wait(1 + Math.rand(0.5))
if (Vars.unit.controller == Vars.this) {
print`Bound to ${Vars.unit}\n`
printFlush()
return true;
}
}
if (tryBind && (ttl-- <= 0 || Vars.unit == first)) {
break;
}
}
print`Unable to bind to ${unitType}\nprevious ttl=${ttl}`
printFlush()
return undefined;
}
function bindFreeUnitFromConfig() {
for (var i = 0; i < config.bindableUnitTypes.size; i++) {
if (recoverPosessionOfUnit(unchecked(config.bindableUnitTypes[i]))) {
return;
}
}
for (var i = 0; i < config.bindableUnitTypes.size; i++) {
if (bindFreeUnit(unchecked(config.bindableUnitTypes[i]))) {
return;
}
}
reportBindFailure()
}
function reportBindFailure() {
print`Failed to bind to a unit`
printFlush()
endScript()
}
// function fastUnitWithin({x, y, radius}) {
// var dx = x - Vars.unit.x
// var dy = y - Vars.unit.y;
// return Math.sqrt(dx * dx + dy * dy) < radius
// }
function approachIfNecessary(x, y, radius) {
if (unitControl.within({ x: x, y: y, radius: radius })) return;
doUnitApproachAndWait(x, y)
}
function doUnitApproachAndWait(x, y) {
print`En route to (${x}, ${y})\n`; printFlush()
do {
// unitControl.approach({ x: x, y: y, radius: 3 })
unitControl.move(x, y)
checkUnitAvailability()
wait(0.1)
// wait(3)
} while (!unitControl.within({ x: x, y: y, radius: 3 }));
print`En route to (${x}, ${y}) - waiting`; printFlush()
// waitUntilStop()
// wait(3)
}
function distSquareFromBuilding(building: AnyBuilding) {
var dx = building.x - Vars.unit.x
var dy = building.y - Vars.unit.y
return dx * dx + dy * dy
}
function nearerBuilding(building1: AnyBuilding, building2: AnyBuilding) {
if (building1 === undefined) {
return building2
}
if (building2 === undefined) {
return building1
}
var d1 = distSquareFromBuilding(building1)
var d2 = distSquareFromBuilding(building2)
return d1 < d2 ? building1 : building2
}
function findNearestAcceptableStorage(itemType) {
if (!config.allowTakingFromContainer) return undefined;
var [found, , , storage] = unitLocate.building({
group: "storage",
enemy: false
})
if (!found) return undefined;
if (storage[itemType] >= unitItemCapacity) {
return storage;
}
return undefined;
}
function getCoreOrPanic() {
var [, , , core] = unitLocate.building({
group: "core",
enemy: false
})
if (core === undefined) {
print`[red]No core found. result=${core}`
printFlush()
wait(1)
endScript()
}
return core;
}
function fetchItemFromAutomaticSource(itemType) {
// fetchItemFromSpecifiedSource(findNearestAcceptableBuilding(itemType), itemType)
var source = getCoreOrPanic()
var sourceX;
var sourceY;
var nearestDistSq = distSquareFromBuilding(source)
do {
var candidate = findNearestAcceptableStorage(itemType)
var candidateDistSq = distSquareFromBuilding(candidate)
if (candidate && candidateDistSq < nearestDistSq) {
nearestDistSq = candidateDistSq
source = candidate
}
sourceX = source.x
sourceY = source.y
print`On way of [accent]${itemType} (x${unitItemCapacity}) [white]from [#9999ff]${source} [white]@(${sourceX}, ${sourceY})\n`;
// print`(current @${Vars.unit.x}, ${Vars.unit.y})\n`
printFlush()
// wait(0.5)
unitControl.move(sourceX, sourceY)
checkUnitAvailability()
} while (!unitControl.within({ x: sourceX, y: sourceY, radius: 3 }));
print`Discarding ${Vars.unit.totalItems}\n`;
printFlush()
do {
ensureDrop(source, Vars.unit.totalItems)
// wait(2)
unitControl.itemDrop(Blocks.air, Vars.unit.totalItems)
checkUnitAvailability()
// wait(2)
} while (Vars.unit.totalItems)
var timeout = Vars.time + 5000
while (Vars.time < timeout && Vars.unit.totalItems == 0) {
const itemsToTake = Math.min(unitItemCapacity, source[itemType])
print`Taking [accent]${itemType} (x${itemsToTake}) [white]from [#9999ff]${source} [white]@(${sourceX}, ${sourceY})`; printFlush()
if (itemsToTake == 0) break;
unitControl.itemTake(source, itemType, itemsToTake)
// wait(2)
}
return Vars.unit.totalItems != 0
}