-
Notifications
You must be signed in to change notification settings - Fork 0
/
tank-monitor-renderer.ts
441 lines (371 loc) · 12.2 KB
/
tank-monitor-renderer.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
var __appName = "Liquid and Item Container Monitor"
// === configurations ===
const mem = new Memory(getBuilding("cell1"))
const s1 = new Memory(getBuilding("cell2"))
const s2 = new Memory(getBuilding("cell3"))
const s3 = new Memory(getBuilding("cell4"))
const s4 = new Memory(getBuilding("cell5"))
const enum MemoryFields {
_,
TIMESTAMP,
DELTA,
CONTAINERS_COUNT,
TOTAL_LIQUIDS,
TOTAL_LIQUID_CAPACITY,
SUBSTANCE_TYPE,
STRUCT_SIZE = 12,
BASE = 8,
}
const FLAG_LIQUID = 1 << 29;
const FLAG_ITEM = 1 << 28;
const ID_MASK = 0xFFFF
const backendDeltaSmoothingSampleCount = 5;
// === utilities ===
function getFirst(type): AnyBuilding {
for (var i = 0; i < Vars.links; i++) {
const node = getLink(i);
if (node.type == type) {
return node;
}
}
return undefined;
}
function constrainAbsolute(value, maxAbsolute) {
if (value > maxAbsolute) {
return maxAbsolute;
}
if (value < -maxAbsolute) {
return -maxAbsolute;
}
return value;
}
function copyMem(srcPos, dstPos, dstInterval) {
mem[dstPos + 0 * dstInterval] = s1[srcPos]
mem[dstPos + 1 * dstInterval] = s2[srcPos]
mem[dstPos + 2 * dstInterval] = s3[srcPos]
mem[dstPos + 3 * dstInterval] = s4[srcPos]
print`${s1[srcPos]} ${s2[srcPos]} ${s3[srcPos]} ${s4[srcPos]} `
}
// === life-cycle management and bootloader
var initialLinks = Vars.links;
var disp = getFirst(Blocks.logicDisplay) ?? getFirst(Blocks.largeLogicDisplay);
if (disp === undefined) {
runBackend()
}
draw.clear(0, 180, 0)
drawFlush(disp)
const isLargeDisplay = disp.type == Blocks.largeLogicDisplay;
const D = isLargeDisplay ? 176 : 80; // display size
var msg = getFirst(Blocks.message)
// === globals ===
const barHeight = isLargeDisplay ? 12 : 5
const barSpacing = isLargeDisplay ? 10 : 5
const barHeightHalf = barHeight / 2;
var warningLevel = 0;
// === initialization
runInitialChecks()
while (Vars.links == initialLinks) {
mainLoop()
}
// === functions ===
function mainLoop() {
if (warningLevel != 0) {
draw.clear(158, 52, 80);
} else {
draw.clear(90, 90, 90)
}
copyMem(MemoryFields.BASE + MemoryFields.DELTA, MemoryFields.BASE + MemoryFields.DELTA, MemoryFields.STRUCT_SIZE)
copyMem(MemoryFields.BASE + MemoryFields.TOTAL_LIQUIDS, MemoryFields.BASE + MemoryFields.TOTAL_LIQUIDS, MemoryFields.STRUCT_SIZE)
copyMem(MemoryFields.BASE + MemoryFields.TOTAL_LIQUID_CAPACITY, MemoryFields.BASE + MemoryFields.TOTAL_LIQUID_CAPACITY, MemoryFields.STRUCT_SIZE)
copyMem(MemoryFields.BASE + MemoryFields.TIMESTAMP, MemoryFields.BASE + MemoryFields.TIMESTAMP, MemoryFields.STRUCT_SIZE)
copyMem(MemoryFields.BASE + MemoryFields.SUBSTANCE_TYPE, MemoryFields.BASE + MemoryFields.SUBSTANCE_TYPE, MemoryFields.STRUCT_SIZE)
printFlush()
warningLevel = 0;
drawStats(0)
drawStats(1)
drawStats(2)
drawStats(3)
drawWarning();
drawFlush(disp)
}
function drawStats(i) {
const y0 = (barHeight + barSpacing) * (4 - i)
const baseAddr = MemoryFields.BASE + i * MemoryFields.STRUCT_SIZE
if (Math.abs(mem[baseAddr + MemoryFields.TIMESTAMP] - Vars.time) > 3000) {
draw.color(200, 200, 200)
draw.stroke(3)
draw.line({
x: 5,
y: y0,
x2: 5 + barHeight,
y2: y0 + barHeight,
})
drawIcon(i, undefined)
return
}
const delta = mem[baseAddr + MemoryFields.DELTA]
const liquids = mem[baseAddr + MemoryFields.TOTAL_LIQUIDS]
const liquidCapacity = mem[baseAddr + MemoryFields.TOTAL_LIQUID_CAPACITY]
const substanceType = mem[baseAddr + MemoryFields.SUBSTANCE_TYPE]
const liquidRatio = liquids / liquidCapacity
const barWidth = (liquidRatio) * D
const barDeltaRatio = delta / liquidCapacity
const barDeltaUnitWidth = barDeltaRatio * D
const barDeltaWidth60s = constrainAbsolute(barDeltaUnitWidth * 60, D)
const barDeltaWidth300s = constrainAbsolute(barDeltaUnitWidth * 300, D)
const liquidDrainsIn = liquids / -Math.min(-1e-7, delta);
print`liquids drains in ${liquidDrainsIn} (${liquids} / ${delta}\n\n`
if (liquidDrainsIn < 60) {
warningLevel = Math.max(warningLevel, 2);
} else if (liquidDrainsIn < 300) {
warningLevel = Math.max(warningLevel, 1);
}
if (liquidRatio < 0.5) {
draw.color(200, 200, 30)
warningLevel = Math.max(warningLevel, 1);
} else {
draw.color(69, 169, 230)
}
draw.rect({
x: 0,
y: y0,
height: barHeight,
width: barWidth
})
draw.color(100, 200, 220)
draw.rect({
x: barWidth,
y: y0,
height: barHeightHalf,
width: barDeltaWidth300s,
})
draw.color(200, 230, 230)
draw.rect({
x: barWidth,
y: y0,
height: barHeightHalf,
width: barDeltaWidth60s,
})
var image = undefined;
if (substanceType) {
if ((substanceType & FLAG_ITEM)) {
image = lookup.item(substanceType & ID_MASK)
} else if ((substanceType & FLAG_LIQUID)) {
image = lookup.liquid(substanceType & ID_MASK)
}
}
// if (image) {
// draw.image({
// x: barHeightHalf + 3,
// y: y0 + barHeightHalf,
// image: image,
// size: barHeight + barSpacing / 2,
// rotation: 0
// })
// }
print`[blue]drawIcon(${i}, ${image})\n[white]`
drawIcon(i, image)
if (liquidRatio < 0.1) {
draw.color(240, 0, 0)
draw.stroke(3)
draw.line({
x: D - 5 - barHeight,
y: y0,
x2: D - 5,
y2: y0 + barHeight,
})
}
}
function runInitialChecks() {
if (!checkMemoryState()) {
drawMemoryError()
haltUntilConfiguredLinksChange()
}
}
function drawMemoryError() {
const size = 30;
draw.clear(90, 90, 90)
draw.color(255, 255, 255)
draw.image({
x: D / 2,
y: D / 2,
size: size,
rotation: 0,
image: Blocks.memoryCell
})
const posBegin = D / 2 - size / 2 - 5
const posEnd = D / 2 + size / 2 + 5
draw.stroke(5)
draw.color(200, 0, 0)
draw.line({
x: posBegin, y: posBegin, x2: posEnd, y2: posEnd
// x: 0, y: 0, x2: 90, y2: 90
})
drawFlush(disp)
}
function haltUntilConfiguredLinksChange() {
while (Vars.links == initialLinks);
endScript()
}
function drawIcon(pos, image) {
print`[red]image = ${image}[white]\n`
var px = 10 + pos * 15
var py = D - 17
var sz = 15
var halfSz = sz / 2
if (image) {
draw.color(255, 255, 255, 255)
draw.image({
x: px,
y: py,
image: image,
size: sz,
rotation: 0
})
} else {
draw.color(140, 140, 140)
draw.stroke(2)
draw.lineRect({
x: px - halfSz,
y: py - halfSz,
width: sz,
height: sz
})
}
}
function drawWarning() {
switch (warningLevel) {
case 0:
return;
case 1:
draw.color(255, 240, 0)
break;
case 2:
draw.color(240, 30, 40)
break;
}
draw.triangle({
x: D - 25, y: D - 25,
x2: D - 5, y2: D - 25,
x3: D - 15, y3: D - 25 + 20 * 0.866
})
}
function checkMemoryState() {
var target = undefined;
target = mem[0];
// printFlush()
// stopScript();
return target !== undefined;
}
// === backend ===
function invalidSwitch(sw) {
var target = undefined;
target = sw.enabled;
return target === undefined;
}
function runBackend() {
const mem = new Memory(getBuilding("cell1"), 64)
const beginSmoothingBufferSection = 32
var deltaSmoothingBufferHead = 0
var deltaSmoothingBufferLength = 0;
var oldestSampleValue = 0
var latestSampleValue = 0;
var oldestSampleTimestamp = 0;
var latestSampleTimestamp = 1e9;
var previousSubstanceId = 0;
var itemCategoryOfInterestSorter = getFirst(Blocks.sorter) ?? getFirst(Blocks.invertedSorter) ?? getFirst(Blocks.unloader);
while (Vars.links == initialLinks) {
main()
}
endScript()
function pushDeltaSmoothingValue(value) {
const headAddr = beginSmoothingBufferSection + deltaSmoothingBufferHead * 2
const headAddr_1 = headAddr + 1
if (deltaSmoothingBufferLength == backendDeltaSmoothingSampleCount) {
oldestSampleValue = mem[headAddr]
oldestSampleTimestamp = mem[headAddr_1]
deltaSmoothingBufferLength -= 1;
} else if (deltaSmoothingBufferLength == 0) {
oldestSampleValue = value;
oldestSampleTimestamp = Vars.time;
}
latestSampleValue = value;
latestSampleTimestamp = Vars.time;
mem[headAddr] = value
mem[headAddr_1] = Vars.time
deltaSmoothingBufferHead = (deltaSmoothingBufferHead + 1) % backendDeltaSmoothingSampleCount
deltaSmoothingBufferLength += 1
}
function getSmoothedDeltaValue() {
print`${latestSampleValue} ${deltaSmoothingBufferLength} ${oldestSampleTimestamp}\n`
return (latestSampleValue - oldestSampleValue) / (latestSampleTimestamp - oldestSampleTimestamp) * 1000
}
function getLiquidIdFromLiquidContainer(liquidContainer) {
var maxStorage = 0;
var res = 0;
for (var ti = 0; ti < Vars.liquidCount; ti++) {
var curStorage = liquidContainer[lookup.liquid(ti)];
if (curStorage > maxStorage) {
maxStorage = curStorage;
res = ti | FLAG_LIQUID;
}
}
return res;
}
function getItemIdFromItemType(itemType) {
for (var ti = 0; ti < Vars.itemCount; ti++) {
if (lookup.item(ti) == itemType) {
return ti | FLAG_ITEM;
}
}
return 0;
}
function main() {
var totalLiquids = 0
var totalCapacity = 0
var delta = 0
var substanceType = 0;
for (var i = 0; i < Vars.links; i++) {
const building = getLink(i)
if (building.type == Blocks.liquidContainer || building.type == Blocks.liquidTank) {
totalLiquids += building.totalLiquids
totalCapacity += building.liquidCapacity
if (!substanceType) {
substanceType = getLiquidIdFromLiquidContainer(building);
}
} else if (building.type == Blocks.container || building.type == Blocks.vault) {
if (itemCategoryOfInterestSorter?.config !== undefined) {
const itemConfig = itemCategoryOfInterestSorter.config
totalLiquids += building[itemConfig]
if (!substanceType) {
substanceType = getItemIdFromItemType(itemConfig);
}
} else {
totalLiquids += building.totalItems
if (!substanceType && building.totalItems != 0) {
substanceType = getItemIdFromItemType(building.firstItem);
}
}
totalCapacity += building.itemCapacity
}
}
// print`substanceType = raw: ${substanceType} (id: ${substanceType & ID_MASK}): ${lookup.liquid(substanceType & ID_MASK)} or ${lookup.item(substanceType & ID_MASK)}`
// printFlush()
if (substanceType) {
previousSubstanceId = substanceType
}
// print("liquids:", previousTotalLiquids, "->", totalLiquids, ", in ", Vars.time - previousDataUpdatedAt, "\n")
// print("delta=", totalLiquids - previousTotalLiquids)
// printFlush()
pushDeltaSmoothingValue(totalLiquids)
delta = getSmoothedDeltaValue()
print`(${Vars.time}) delta=${delta}\n`
printFlush()
const baseAddr = MemoryFields.BASE
mem[baseAddr + MemoryFields.DELTA] = delta;
mem[baseAddr + MemoryFields.TOTAL_LIQUIDS] = totalLiquids
mem[baseAddr + MemoryFields.TOTAL_LIQUID_CAPACITY] = totalCapacity
mem[baseAddr + MemoryFields.TIMESTAMP] = Vars.time
mem[baseAddr + MemoryFields.SUBSTANCE_TYPE] = previousSubstanceId
}
}