-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150-percent-anywhere.ts
107 lines (92 loc) · 3.03 KB
/
150-percent-anywhere.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
asm`set version "2.5"`;
asm`set state "init"`;
let item1: ItemSymbol = Items.phaseFabric;
let item2: ItemSymbol = Items.silicon;
let takeCount = 30;
let idlePerItem = 0.1;
const source = () => getCore();
const dest = () => getLink(0);
const currentItem = () => sensor(item2, dest()) > sensor(item1, dest()) ? item1 : item2;
let lastUnitCheck = 0;
let unitCheckFail = false;
main();
function main() {
while (1) {
// always release unit for a while
if (Vars.unit) {
reachBuilding(source());
unitControl.unbind(); // not actually "unbind" from Vars.unit
}
setState("idle: release unit");
wait(0.1 + (sensor(currentItem(), dest()) as number) * idlePerItem);
while (sensor(currentItem(), dest()) > dest().itemCapacity - takeCount) {
setState("idle: dest almost full");
wait(1);
}
bindAvailableUnit();
// won't bind a new unit, unless it's dead or controlled by other processor
// discard useless items
if (Vars.unit.totalItems > 0 && Vars.unit.firstItem != currentItem()) {
unitControl.itemDrop(Blocks.air, 999);
wait(0.2);
}
// take
if (Vars.unit.totalItems < takeCount) {
setState("take from source");
unitControl.itemTake(reachBuilding(source()), currentItem(), takeCount - Vars.unit.totalItems);
}
// drop
while (Vars.unit.totalItems) {
setState("drop into dest");
checkAlive();
unitControl.itemDrop(reachBuilding(dest()), Vars.unit.totalItems);
if (sensor(currentItem(), dest()) == dest().itemCapacity) {
break;
}
}
}
}
function bindAvailableUnit() {
setState("bind unit");
while (1) {
while (!Vars.unit || Vars.unit.dead || Vars.unit.controlled) {
unitBind(Units.poly);
}
unitControl.idle();
setState("bind unit locking");
wait(0.3);
if (Vars.unit.controller === Vars.this) break;
setState("bind unit retry");
}
unitControl.flag(150);
}
function reachBuilding(building: AnyBuilding) {
const { x, y } = building;
while (!unitControl.within({ x, y, radius: 6 })) {
checkAlive();
unitControl.approach({ x, y, radius: 4 });
}
return building;
}
function getCore() {
const [, , , building] = unitLocate.building({ group: 'core', enemy: false });
return building;
}
function checkAlive() {
let unitExists = Vars.unit;
let unitDead = Vars.unit.dead;
let unitControlled = Vars.unit.controlled;
let unitController = Vars.unit.controller;
let unitControlByThis = unitController === Vars.this;
unitCheckFail = unitExists == undefined ||
unitDead ||
(unitControlled && unitControlled != ControlKind.ctrlProcessor) ||
(unitControlled && !unitControlByThis);
lastUnitCheck = Vars.time;
if (unitCheckFail) {
endScript();
}
}
function setState(str: string) {
asm`set state ${str}`
}