-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmekanism-hud.lua
605 lines (527 loc) · 18.5 KB
/
mekanism-hud.lua
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
component = require("component")
event = require("event")
serialization = require("serialization")
sides = require("sides")
buttons = {}
maxCaseHeat = 0
maxPlasmaHeat = 0
maxEnergyStored = 0
maxEnergy = 0
laMaxEnergyStored = 0
imMaxStorage = 0
statusbox = {}
statusbox.widgets = {}
injectionRateWidget = {}
function initComponents()
for address,type in pairs(component.list("reactor_logic_adapter")) do
if reactor == false then
print("found reactor "..address)
reactor = component.proxy(address)
else
print("found another reactor ("..address.."). first one will be used.")
end
end
for address,type in pairs(component.list("glasses")) do
if glassesTerminal == false then
print("found glasses Terminal "..address)
glassesTerminal = component.proxy(address)
else
print("found another glasses Terminal ("..address.."). first one will be used.")
end
end
for address,type in pairs(component.list("_gas_tank")) do checkTank(address); os.sleep(0); end
for address,type in pairs(component.list("laser_amplifier")) do
if laserAmplifier == false then
print("found laser amplifier "..address)
laserAmplifier = component.proxy(address)
else
print("found another laser amplifier ("..address.."). first one will be used.")
end
end
for address,type in pairs(component.list("redstone")) do
if redstone == false then
print("found redstone interface "..address)
redstone = component.proxy(address)
else
print("found another redstone interface ("..address.."). first one will be used.")
end
end
for address,type in pairs(component.list("induction_matrix")) do
if inductionMatrix == false then
print("found induction matrix "..address)
inductionMatrix = component.proxy(address)
else
print("found another induction matrix ("..address.."). first one will be used.")
end
end
end
function val2perc(val, maxVal)
if(maxVal == 0) then return 0; end
return (math.floor(0.5 + ((val/maxVal) * 100)*10)/10)
end
function readTankFull(address)
local tank = component.proxy(address)
return { gas = tank.getGas().label, amount = tank.getStoredGas(), maxAmount = tank.getMaxGas(), tankType = tank.type }
end
function readTankStorage(tank)
return { amount = tank.getStoredGas(), maxAmount = tank.getMaxGas() }
end
function checkTank(address)
local tankData = readTankFull(address)
if tankData.gas == nil then tankData.gas = "unknown"; end
print("# found ".. tankData.tankType .. " with " .. tankData.amount .. "/" .. tankData.maxAmount .. " of " .. tankData.gas)
if string.match(tankData.gas, "deuterium") then
print(" ... adding tank with address " .. address)
if tankDeuterium == false then
tankDeuterium = component.proxy(address)
else
print("found more than one Deuterium Gas Tank, using first one found")
print("set a address in the script to use a user defined tank")
end
elseif string.match(tankData.gas, "tritium") then
print(" ... adding tank")
if tankTritium == false then
tankTritium = component.proxy(address)
else
print("found more than one Tritium Gas Tank, using first one found")
print("set a address in the script to use a user defined tank")
end
end
end
function addInfoHUD(x, y, w, h, label)
local boxPadding = 3
local element = {}
b = glassesTerminal.addBox2D()
b.addTranslation(x + offsetX, y + offsetY, 0)
b.setSize(w, h)
b.addColor(0, 0, 0, 0.5)
b.addColor(0.05, 0.05, 0.05, 0.5)
table.insert(element, b)
s = glassesTerminal.addBox2D()
s.addTranslation(x + offsetX + boxPadding, y + offsetY + boxPadding, 0)
s.setSize(w-(2*boxPadding), h-(2*boxPadding))
s.addColor(0, 0.5, 0, 0.5)
s.addColor(0.05, 1, 0.05, 0.5)
table.insert(element, s)
t = glassesTerminal.addText2D()
t.addTranslation(x + offsetX + 7, y + offsetY + 7, 0)
t.setText(label)
t.addColor(1, 1, 1, 0.7)
table.insert(element, t)
return element
end
function addReactorHUD(x, y, w, h, label)
local boxPadding = 3
local element = {}
b = glassesTerminal.addBox2D()
b.addTranslation(x + offsetX, y + offsetY, 0)
b.setSize(w, h)
b.addColor(0, 0, 0, 0.5)
b.addColor(0.05, 0.05, 0.05, 0.5)
table.insert(element, b)
t = glassesTerminal.addText2D()
t.addTranslation(x + offsetX + 7, y + offsetY + 7, 0)
t.setText(label)
t.addColor(1, 1, 1, 0.7)
table.insert(element, t)
return element
end
function MJ2RF(mj)
if type(mj) == "string" then
return mj
end
return math.ceil(mj * 0.4)
end
function formatNumber(rf)
rf = (math.floor(0.5 + (rf*100)) / 100)
if rf >= 1000000000000 then
rf = (math.floor(0.5 + (rf*10/1000000000000)/10)) .. "T"
elseif rf >= 1000000000 then
rf = (math.floor(0.5 + (rf*10/1000000000)/10)) .. "G"
elseif rf >= 1000000 then
rf = (math.floor(0.5 + (rf*10/1000000)/10)) .. "M"
elseif rf >= 1000 then
rf = math.floor(0.5 + (rf/1000)).."k"
end
return rf
end
function perc2color(p)
return { red = (1 * (1 - (p/100))), green = (1 * (p/100)), blue = 0 }
end
function bool2color(bool)
if bool == true then
return { red = 0.2, green = 1, blue = 0.2 }
else
return { red = 1, green = 0.2, blue = 0.2 }
end
end
function addInfo(label, value, x, y)
info = {}
info.label = label
info.statusBar = false
info.r = 1
info.g = 1
info.b = 1
if type(value) == "boolean" then
color = bool2color(value)
info.r = color.red
info.g = color.green
info.b = color.blue
if value == true then
value = "true"
else
value = "false"
end
elseif type(value) == "number" then
value = ""..value
end
info.widgets = {}
os.sleep(0)
info.widgets.key = glassesTerminal.addText2D()
info.widgets.key.setText(label)
info.widgets.key.addTranslation(x + 120 + 5, y + 10, 0)
info.widgets.key.addColor(1, 1, 1, 0.7)
os.sleep(0)
info.widgets.value = glassesTerminal.addText2D()
info.widgets.value.setText(""..value.."")
info.widgets.value.addTranslation(x + 100 + 120 + 5, y + 10, 0)
info.widgets.value.addColor(info.r, info.g, info.b, 0.7)
return info
end
function setColor(el, p)
color = perc2color(p)
el.updateModifier(2, color.red, color.green, 0, 0.7)
el.updateModifier(3, color.red, color.green, 0, 0.8)
end
function updateInjectionRate()
injectionRate = reactor.getInjectionRate()
for i=1,#buttons do
if injectionRate == buttons[i].value then
buttons[i].widgets.box.updateModifier(2, 0, 0.7, 0, 0.5)
buttons[i].widgets.box.updateModifier(3, 0.05, 0.7, 0.05, 0.5)
else
buttons[i].widgets.box.updateModifier(2, 0, 0, 0, 0.5)
buttons[i].widgets.box.updateModifier(3, 0.05, 0.05, 0.05, 0.5)
end
os.sleep(0)
end
end
function addInjectionButton(value, x, y)
return addButton(value, x, y, 25, 25, function(value)
if value == 0 then
print("canceling, injection rate of 0 would disable the reactor")
return
end
reactor.setInjectionRate(value+0.0)
updateInjectionRate()
print("set injection rate to " .. value)
end)
end
function addStatusBar(label, value, x, y)
b = glassesTerminal.addBox2D()
b.addTranslation(x + 123, y - 3, 0)
b.addColor(1, 1, 1, 0.3)
b.addColor(1, 1, 1, 0.2)
return updateStatusBar(b, value)
end
function updateStatusBar(el, value)
w = ((value+5)/100) * 85 -- +5 to always display something
local color = perc2color(value)
el.setSize(w, 10)
el.updateModifier(2, color.red, color.green, color.blue, 0.3)
el.updateModifier(3, color.red, color.green, color.blue, 0.2)
return el
end
function addButton(value, x, y, w, h, cb)
el = {}
el.value = value
el.x = x
el.y = y
el.width = w
el.height = h
el.widgets = {}
el.widgets.box = glassesTerminal.addBox2D()
el.widgets.box.addTranslation(x, y, 0)
el.widgets.box.setSize(el.width, el.height)
el.widgets.box.addColor(0, 0, 0, 0.5)
el.widgets.box.addColor(0.05, 0.05, 0.05, 0.5)
el.widgets.text = glassesTerminal.addText2D()
el.widgets.text.addTranslation(x+5, y+5, 0)
el.widgets.text.setText(""..value.."")
el.cb = cb
table.insert(buttons, el)
return #buttons
end
function rsIgnite(side)
if reactor.isIgnited() then
print("reactor is already ignited!")
return
end
io.write("ignating...")
redstone.setOutput(side, 15)
os.sleep(5 * 0.05) -- wait 5 ticks
redstone.setOutput(side, 0)
os.sleep(0.05)
if reactor.isIgnited() then
print(" succed!")
return true
else
print(" FAILED!")
return false; end
end
function ignite(foo)
if redstone == false then
print("no redstone interface found!")
return false
end
if laserAmplifier ~= false and laserAmplifier.getEnergy() < laIgnitionEnergy then
print("not enough energy stored in the laser amplifier!")
return false
end
if redstoneSide == false then
print("no default side set, trying to figure out, good luck...")
for side=0,(#sides-1) do
print("trying "..sides[side])
if rsIgnite(side) then
print("saving ".. sides[side] .." as default")
redstoneSide = side
return true
end
os.sleep(0)
end
else
return rsIgnite(redstoneSide)
end
return false
end
function getIndex(label)
for i=1,#statusbox.widgets do
if statusbox.widgets[i].label == label then
return i
end
os.sleep(0)
end
return false
end
function updateStatusBoxKeyValue(label, value, perc)
index = getIndex(label)
if index == false then
print("unknown label: "..label)
return
end
statusbox.widgets[index].value = value
statusbox.widgets[index].widgets.value.setText(""..value.."")
if statusbox.widgets[index].statusBar ~= false then
updateStatusBar(statusbox.widgets[index].statusBar, perc)
end
end
function setVisibility(label, bool)
index = getIndex(label)
if index == false then
print("unknown label: "..label)
return
end
statusbox.widgets[index].widgets.value.setVisible(bool)
statusbox.widgets[index].widgets.key.setVisible(bool)
if statusbox.widgets[index].statusBar then
statusbox.widgets[index].statusBar.setVisible(bool)
end
end
function initInjectionRateMenue(x, y)
injectionRateWidget.box = glassesTerminal.addBox2D()
injectionRateWidget.box.addTranslation(x, y, 0)
injectionRateWidget.box.setSize(200, 90)
injectionRateWidget.box.addColor(0, 0, 0, 0.5)
injectionRateWidget.box.addColor(0.05, 0.05, 0.05, 0.6)
--injectionRateWidget.box.setCondition(injectionRateWidget.box.addScale(0, 0, 0), "OVERLAY_INACTIVE", true)
injectionRateWidget.text = glassesTerminal.addText2D()
injectionRateWidget.text.addTranslation(x + 5, y + 5, 0)
injectionRateWidget.text.setText("injection rate (ignition temp: "..formatNumber(reactor.getIgnitionTemp()).."°K)")
--injectionRateWidget.text.setCondition(injectionRateWidget.text.addScale(0, 0, 0), "OVERLAY_INACTIVE", true)
for i=0,9 do
if i < 5 then
addInjectionButton(2*i, x + 5 + (i*30), y + 25)
else
addInjectionButton(2*i, x + 5 + ((i-5)*30), y + 55)
end
os.sleep(0)
end
updateInjectionRate()
end
function setIgniteButton(visible)
for i=1,#buttons do
if buttons[i].value == "ignite" then
buttons[i].widgets.box.setVisible(visible)
buttons[i].widgets.text.setVisible(visible)
end
end
end
function initStatusBox()
statusbox.widgets = {}
statusbox.box = glassesTerminal.addBox2D()
statusbox.box.addTranslation(offsetX + 120, offsetY, 0)
statusbox.box.addColor(0, 0, 0, 0.5)
statusbox.box.addColor(0.05, 0.05, 0.05, 0.6)
table.insert(statusbox.widgets, addInfo("reactor status", "", offsetX , offsetY + (12*#statusbox.widgets)));
info = addInfo("energy", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("energy", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("energy stored", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("energy stored", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("case heat", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("case heat", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("plasma heat", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("plasma heat", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
table.insert(statusbox.widgets, addInfo("water", 0, offsetX , offsetY + (12*#statusbox.widgets)))
table.insert(statusbox.widgets, addInfo("steam", 0, offsetX , offsetY + (12*#statusbox.widgets)))
table.insert(statusbox.widgets, addInfo("can extract", false, offsetX , offsetY + (12*#statusbox.widgets)))
table.insert(statusbox.widgets, addInfo("can receive", false, offsetX , offsetY + (12*#statusbox.widgets)))
info = addInfo("deuterium", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("deuterium", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("tritium", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("tritium", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("dt-Fuel", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("dt-Fuel", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("laser amplifier", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("laser amplifier", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
info = addInfo("induction matrix", 0, offsetX , offsetY + (12*#statusbox.widgets))
info.statusBar = addStatusBar("induction matrix", 0, offsetX, offsetY + (12*(1+#statusbox.widgets)))
table.insert(statusbox.widgets, info)
table.insert(statusbox.widgets, addInfo("matrix input", 0, offsetX , offsetY + (12*#statusbox.widgets)))
table.insert(statusbox.widgets, addInfo("matrix output", 0, offsetX , offsetY + (12*#statusbox.widgets)))
statusbox.box.setSize(230, 15+(#statusbox.widgets*12))
updateLazyValues()
updateStatusBox()
end
function updateLazyValues()
maxCaseHeat = reactor.getMaxCaseHeat()
maxPlasmaHeat = reactor.getMaxPlasmaHeat()
maxEnergy = reactor.getMaxEnergy()
if maxEnergy > 0 then
setVisibility("energy", true)
else
setVisibility("energy", false)
end
if laserAmplifier then
laMaxEnergyStored = laserAmplifier.getMaxEnergy()
setVisibility("laser amplifier", true)
else
setVisibility("laser amplifier", false)
end
if inductionMatrix then
imMaxStorage = inductionMatrix.getMaxEnergy()
setVisibility("induction matrix", true)
setVisibility("matrix input", true)
setVisibility("matrix output", true)
else
setVisibility("induction matrix", false)
setVisibility("matrix input", false)
setVisibility("matrix output", false)
end
end
function updateStatusBox()
isIgnited = reactor.isIgnited()
if isIgnited then
setIgniteButton(false)
updateStatusBoxKeyValue("reactor status", "active")
else
if reactor.canIgnite() then
setIgniteButton(false)
updateStatusBoxKeyValue("reactor status", "inactive (can ignite)")
else
setIgniteButton(true)
updateStatusBoxKeyValue("reactor status", "inactive (can't ignite)")
end
end
if laserAmplifier then
laEnergyStored = laserAmplifier.getEnergy()
value = val2perc(laEnergyStored, laMaxEnergyStored).."% ("..formatNumber(MJ2RF(laEnergyStored)).." / "..formatNumber(MJ2RF(laMaxEnergyStored)).."RF)"
updateStatusBoxKeyValue("laser amplifier", value, val2perc(laEnergyStored, laMaxEnergyStored))
end
dtFuel = reactor.getFuel()
deuterium = reactor.getDeuterium()
tritium = reactor.getTritium()
if dtFuel > 0 then
updateStatusBoxKeyValue("dt-Fuel", dtFuel.."mB", val2perc(dtFuel, 1000))
setVisibility("dt-Fuel", true)
setVisibility("deuterium", false)
setVisibility("tritium", false)
elseif deuterium > 0 or tritium > 0 then
updateStatusBoxKeyValue("deuterium", deuterium.."mB", val2perc(deuterium, 1000))
updateStatusBoxKeyValue("tritium", tritium.."mB", val2perc(tritium, 1000))
setVisibility("dt-Fuel", false)
setVisibility("deuterium", true)
setVisibility("tritium", true)
end
caseHeat = reactor.getCaseHeat()
value = val2perc(caseHeat, maxCaseHeat).."% ("..formatNumber(caseHeat).." / "..formatNumber(maxCaseHeat).."°K)"
updateStatusBoxKeyValue("case heat", value, val2perc(caseHeat, maxCaseHeat))
plasmaHeat = reactor.getPlasmaHeat()
value = val2perc(plasmaHeat, maxPlasmaHeat).."% ("..formatNumber(plasmaHeat).." / "..formatNumber(maxPlasmaHeat).."°K)"
updateStatusBoxKeyValue("plasma heat", value, val2perc(plasmaHeat, maxPlasmaHeat))
water = reactor.getWater()
steam = reactor.getSteam()
if water > 0 or steam > 0 then
updateStatusBoxKeyValue("water", water.."mB", val2perc(water, 1000))
updateStatusBoxKeyValue("steam", steam.."mB", val2perc(steam, 1000))
updateStatusBoxKeyValue("can extract", reactor.canExtract())
updateStatusBoxKeyValue("can receive", reactor.canReceive())
setVisibility("water", true)
setVisibility("steam", true)
setVisibility("can extract", true)
setVisibility("can receive", true)
else
setVisibility("water", false)
setVisibility("steam", false)
setVisibility("can extract", false)
setVisibility("can receive", false)
end
if maxEnergy > 0 then
energy = reactor.getEnergy()
value = val2perc(energy, maxEnergy).."% ("..formatNumber(MJ2RF(energy)).." / "..formatNumber(MJ2RF(maxEnergy)).."RF)"
updateStatusBoxKeyValue("energy", value, val2perc(energy, maxEnergy))
end
if maxEnergyStored > 0 then
energyStored = reactor.getEnergyStored()
value = val2perc(energyStored, maxEnergyStored).."% ("..formatNumber(MJ2RF(energyStored)).." / "..formatNumber(MJ2RF(maxEnergyStored)).."RF)"
updateStatusBoxKeyValue("energy stored", value, val2perc(energyStored, maxEnergyStored))
end
if inductionMatrix then
imStorage = inductionMatrix.getEnergy()
value = val2perc(imStorage, imMaxStorage).."% ("..formatNumber(MJ2RF(imStorage)).." / "..formatNumber(MJ2RF(imMaxStorage)).."RF)"
updateStatusBoxKeyValue("induction matrix", value, val2perc(imStorage, imMaxStorage))
imInput = inductionMatrix.getInput()
value = formatNumber(MJ2RF(imInput)).."RF/tick"
updateStatusBoxKeyValue("matrix input", value)
imOutput = inductionMatrix.getOutput()
value = formatNumber(MJ2RF(imOutput)).."RF/tick"
updateStatusBoxKeyValue("matrix output", value)
end
os.sleep(0)
end
function updateTankHUD(label, tankComponent, el)
if tankComponent == false then
return
end
local tank = readTankStorage(tankComponent)
local w = (94 * (tank.amount / tank.maxAmount))
perc = val2perc(tank.amount, tank.maxAmount)
setColor(el[2], perc)
el[2].setSize(w, 14)
el[3].setText(label.." ".. perc .. "%")
os.sleep(0)
end
function touchEvent(EVENT, ID, USER, X, Y, BUTTON)
for i=1,#buttons do
if X >= buttons[i].x and X <= (buttons[i].x+buttons[i].width) then
if Y >= buttons[i].y and Y <= (buttons[i].y+buttons[i].height) then
buttons[i].cb(buttons[i].value)
end; end; end
end