-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.lua
More file actions
492 lines (466 loc) · 12.5 KB
/
main.lua
File metadata and controls
492 lines (466 loc) · 12.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
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
os.sleep(2)
rednet.open("left")
speaker = peripheral.wrap("bottom")
m = peripheral.wrap("right")
m.setTextScale(5)
m.setTextColor(colors.white)
m.setBackgroundColor(colors.black)
m.clear()
button = {}
currentStatus = "up"
frameController = 456
reactorComputer = 458
alarm = false
reacteur = false
reactemp = 0
reacout = 0
reacinv = {}
warnedAboutOpenReac = false
warnedAboutUnexpectedWork = false
isDrawModeMissing = true
modifyDrawModeT = 0
isModifyDrawmodeTimerDone = true
isMoving = false
currentMode = 0
local f = fs.open("nominal","r")
actualNom = textutils.unserialize(f.readAll())
f.close()
s = "Wait"
messages = {"", "", "", "", "", "", "", "", "", ""}
mx,my = m.getSize()
m.setCursorPos(math.ceil(mx/2)-math.ceil(#s/2), math.ceil(my/2))
m.write(s)
rednet.send(frameController, "up")
local w = true
while w do
e,a,b = os.pullEvent("rednet_message")
if a == frameController and b == "doneMoving" then w = false end
end
redstone.setOutput("back", alarm)
function logMessage(msg)
local t = {}
t.level = "message"
t.msg = msg
table.insert(messages, t)
print(textutils.serialize(messages))
redraw()
end
function logWarning(msg)
speaker.playNote(0,18)
local t = {}
t.level = "warning"
t.msg = msg
table.insert(messages, t)
redraw()
end
function logAlert(msg)
enableAlarm()
local t = {}
t.level = "alert"
t.msg = msg
table.insert(messages, t)
redraw()
end
function clear()
m.setTextScale(1)
setColorScheme()
m.clear()
mx,my = m.getSize()
end
function disableButton(name)
if button[name] then button[name]["valid"] = false end
redraw()
end
function drawButtons()
for n,d in pairs(button) do
if d["valid"] == true then
if d["active"] then m.setBackgroundColor(buttonActiveB) m.setTextColor(buttonActiveT)
else m.setBackgroundColor(buttonInactiveB) m.setTextColor(buttonInactiveT) end
for i=d["ymin"], d["ymax"] do
local str = ""
for j=d["xmin"], d["xmax"] do
str = str .. " "
end
m.setCursorPos(d["xmin"], i)
m.write(str)
end
m.setCursorPos(math.ceil((d["xmin"]+d["xmax"])/2)-math.ceil(#n/2), math.ceil((d["ymin"]+d["ymax"])/2))
m.write(n)
end
end
end
function redraw()
clear()
for i=1, mx do
m.setCursorPos(i,1)
m.write("-")
m.setCursorPos(i,my)
m.write("-")
end
for i=2, my-1 do
m.setCursorPos(1, i)
m.write("|")
m.setCursorPos(mx, i)
m.write("|")
m.setCursorPos(math.ceil(mx/3), i)
m.write("|")
end
for i=math.ceil(mx/3)+1, mx-1 do
m.setCursorPos(i, my-7)
m.write("-")
end
m.setCursorPos(math.ceil(mx/3)+2, my-6)
m.write("Derniers messages")
local cnt = 6
for i=#messages, #messages-3, -1 do
cnt = cnt - 1
local cmsg = messages[i]
if cmsg.level == "message" then
setColorScheme()
elseif cmsg.level == "warning" then
m.setBackgroundColor(colors.orange)
m.setTextColor(colors.black)
elseif cmsg.level == "alert" then
if alarm then
m.setBackgroundColor(colors.black)
m.setTextColor(colors.red)
else
m.setBackgroundColor(colors.red)
m.setTextColor(colors.white)
end
end
local oldx,oldy = m.getCursorPos()
m.setCursorPos(math.ceil(mx/3)+1, my-cnt)
if cmsg.msg then m.write(cmsg.msg) end
setColorScheme()
m.setCursorPos(oldx,oldy)
end
if reacteur then
m.setTextColor(colors.green)
m.setCursorPos(mx-2,2)
m.write("On")
else
if alarm then m.setTextColor(colors.black) else m.setTextColor(colors.red) end
m.setCursorPos(mx-3,2)
m.write("Off")
end
local temp = reactemp.." degres"
m.setCursorPos(mx-#temp,3)
m.write(temp)
local out = reacout.." EU/t"
m.setCursorPos(mx-#out,4)
m.write(out)
local baseX = math.ceil(mx/3)+2
local baseY = 2
m.setTextColor(colors.gray)
for i=baseX, baseX+19 do
m.setCursorPos(i, baseY)
m.write("-")
m.setCursorPos(i, baseY+7)
m.write("-")
end
for i=baseY+1, baseY+6 do
m.setCursorPos(baseX, i)
m.write("|")
m.setCursorPos(baseX+19, i)
m.write("|")
end
print(reactemp)
local asd = ""
local cnt = 1
for cY = baseY+1, baseY+6 do
for cX = baseX+1, baseX+17,2 do
m.setCursorPos(cX, cY)
local cE = reacinv[cnt]
cnt = cnt+1
if currentMode == 0 then
if cE ~= nil then
if cE.Name == "item.reactorHeatSwitchDiamond" then
m.setBackgroundColor(colors.orange)
m.setTextColor(colors.white)
m.write("<>")
elseif cE.Name == "item.reactorVentDiamond" then
m.setBackgroundColor(colors.lightBlue)
m.setTextColor(colors.white)
m.write("VA")
elseif cE.Name == "item.360k_NaK_Coolantcell" then
m.setBackgroundColor(colors.green)
m.setTextColor(colors.white)
m.write("Na")
elseif cE.Name == "item.360k_Helium_Coolantcell" then
m.setBackgroundColor(colors.yellow)
m.setTextColor(colors.black)
m.write("He")
elseif cE.Name == "item.reactorCoolantSix" then
m.setBackgroundColor(colors.blue)
m.setTextColor(colors.white)
m.write("Co")
elseif cE.Name == "item.Quad_Plutoniumcell" then
m.setBackgroundColor(colors.lime)
m.setTextColor(colors.black)
m.write("Pl")
elseif cE.Name == "item.reactorUraniumSimple" then
m.setBackgroundColor(colors.lime)
m.setTextColor(colors.black)
m.write("Ur")
else
local lolcnt = cnt-1
if actualNom[lolcnt] ~= nil then
local lolnom = actualNom[lolcnt]
print(lolnom)
print(lolcnt)
if isDrawModeMissing then
m.setBackgroundColor(colors.black)
m.setTextColor(colors.white)
m.write("??")
else
if lolnom == "item.reactorHeatSwitchDiamond" then
m.setBackgroundColor(colors.orange)
m.setTextColor(colors.white)
m.write("<>")
elseif lolnom == "item.reactorVentDiamond" then
m.setBackgroundColor(colors.lightBlue)
m.setTextColor(colors.white)
m.write("VA")
elseif lolnom == "item.360k_NaK_Coolantcell" then
m.setBackgroundColor(colors.green)
m.setTextColor(colors.white)
m.write("Na")
elseif lolnom == "item.360k_Helium_Coolantcell" then
m.setBackgroundColor(colors.yellow)
m.setTextColor(colors.black)
m.write("He")
elseif lolnom == "item.reactorCoolantSix" then
m.setBackgroundColor(colors.blue)
m.setTextColor(colors.white)
m.write("Co")
elseif lolnom == "item.Quad_Plutoniumcell" then
m.setBackgroundColor(colors.lime)
m.setTextColor(colors.black)
m.write("Pl")
elseif lolnom == "item.reactorUraniumSimple" then
m.setBackgroundColor(colors.lime)
m.setTextColor(colors.black)
m.write("Ur")
end
end
end
end
end
elseif currentMode == 1 then
if cE ~= nil then
if cE["nbt"] ~= nil then
local h = cE.nbt.heat
if h ~= nil then
if h > reactemp+(5*reactemp) then
m.setBackgroundColor(colors.red)
m.setTextColor(colors.red)
elseif h > reactemp then
m.setBackgroundColor(colors.orange)
m.setTextColor(colors.orange)
elseif h < reactemp-(5*reactemp) then
m.setBackgroundColor(colors.blue)
m.setTextColor(colors.blue)
elseif h <= reactemp then
m.setBackgroundColor(colors.lightBlue)
m.setTextColor(colors.lightBlue)
end
asd = asd .. h .. " "
m.write("--")
end
end
end
elseif currentMode == 2 then
if cE ~= nil then
if cE.DamageValue ~= nil then
local n = cE.Name
local d = cE.DamageValue
if n == "item.360k_Helium_Coolantcell" or n == "item.360k_NaK_Coolantcell" then
if d < 80 then
m.setBackgroundColor(colors.lightBlue)
m.setTextColor(colors.lightBlue)
elseif d >= 80 and d < 90 then
m.setBackgroundColor(colors.green)
m.setTextColor(colors.green)
elseif d >= 90 and d < 97 then
m.setBackgroundColor(colors.orange)
m.setTextColor(colors.orange)
elseif d >= 97 then
m.setBackgroundColor(colors.red)
m.setTextColor(colors.red)
end
m.write("--")
end
end
end
end
end
end
print(asd)
drawButtons()
end
function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["valid"] = true
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
redraw()
end
function createButtons()
setTable("Reacteur", toggleReacteur, 3, 19, 2, 6)
setTable("Ouvrir", openPanel, 3, 19, 8, 12)
setTable("Sauver", saveReacteur, 23, 31, 10, 10)
setTable("Mode", modeButton, 34, 42, 10, 10)
end
function setColorScheme()
if alarm then
m.setBackgroundColor(colors.red)
m.setTextColor(colors.white)
buttonInactiveB = colors.black
buttonActiveB = colors.orange
buttonInactiveT = colors.white
buttonActiveT = colors.white
else
m.setBackgroundColor(colors.black)
m.setTextColor(colors.white)
buttonInactiveB = colors.red
buttonActiveB = colors.green
buttonInactiveT = colors.white
buttonActiveT = colors.white
end
end
function enableAlarm()
redstone.setOutput("back", true)
alarm = true
setTable("Reset alarme", disableAlarm, 3, 19, 14, 18)
redraw()
end
function disableAlarm()
disableButton("Reset alarme")
redstone.setOutput("back", false)
alarm = false
redraw()
end
function saveReacteur()
local nom = {}
for k,v in pairs(reacinv) do
table.insert(nom, v.Name)
end
local f = fs.open("nominal","w")
f.write(textutils.serialize(nom))
f.close()
local f = fs.open("nominal","r")
actualNom = textutils.unserialize(f.readAll())
f.close()
logMessage("Sauvegarde effectuee")
end
function toggleReacteur(button)
if not reacteur then
local nom = {}
for k,v in pairs(reacinv) do
table.insert(nom, v.Name)
end
local ok = true
local invalid = {}
local hecells = 0
for k,v in pairs(nom) do
if v == "item.360k_Helium_Coolantcell" then hecells = hecells+1 end
if v ~= actualNom[k] then
ok = false
table.insert(invalid, k)
end
end
if not ok then
logWarning("Configuration invalide")
end
print(hecells)
if hecells < 9 then
logWarning("Helium manquant")
return
end
end
if currentStatus == "down" then
logWarning("Reacteur desactive: ouvert")
return
end
reacteur = not reacteur
if button then button["active"] = reacteur end
redstone.setOutput("top", reacteur)
if reacteur == true then logMessage("Reacteur on")
else logMessage("Reacteur off") end
end
function openPanel()
if isMoving then return end
isMoving = true
disableButton("Ouvrir")
disableButton("Reacteur")
if reacteur == true then
toggleReacteur(nil)
logWarning("Reacteur desactive auto.")
end
rednet.send(frameController, "down")
end
function closePanel()
if isMoving then return end
isMoving = true
disableButton("Fermer")
rednet.send(frameController, "up")
end
function doneMoving()
isMoving = false
if currentStatus == "up" then
currentStatus = "down"
setTable("Fermer", closePanel, 3, 19, 8, 12)
else
currentStatus = "up"
setTable("Ouvrir", openPanel, 3, 19, 8, 12)
setTable("Reacteur", toggleReacteur, 3, 19, 2, 6)
end
end
function checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] and data["valid"] == true then
data["func"](data)
print(name)
return
end
end
end
end
function modeButton()
currentMode = currentMode + 1
if currentMode == 3 then currentMode = 0 end
redraw()
end
createButtons()
while true do
redraw()
local e,a,b,c = os.pullEvent()
isDrawModeMissing = not isDrawModeMissing
if e == "monitor_touch" then checkxy(b,c) sleep(0.1)
elseif e == "rednet_message" then
if a == reactorComputer then
local t = textutils.unserialize(b)
reactemp = t.heat
reacout = t.out
setColorScheme()
if t.on and not reacteur and not warnedAboutUnexpectedWork then logWarning("Fonctionnement imprevu") warnedAboutUnexpectedWork = true end
if not t.on and warnedAboutUnexpectedWork then logWarning("Retour a la normale") warnedAboutUnexpectedWork = false end
if t.on and warnedAboutUnexpectedWork then m.setCursorPos(mx-6,5) m.write("Erreur") end
if t.on and currentStatus ~= "up" and not warnedAboutOpenReac then warnedAboutOpenReac = true logAlert("Reacteur ouvert") closePanel() end
if warnedAboutOpenReac and currentStatus == "up" then logMessage("Panneau ferme") disableAlarm() warnedAboutOpenReac = false end
if reactemp > 3000 and reacteur then
logAlert("Reacteur en surchauffe")
toggleReacteur(button["Reacteur"])
end
reacinv = t.inv
elseif a == frameController and b == "doneMoving" then
doneMoving()
end
end
end