Advertisement
geremy44

reac ftp1

Mar 6th, 2021
1,131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.97 KB | None | 0 0
  1. os.sleep(2)
  2.  
  3. rednet.open("left")
  4. speaker = peripheral.wrap("bottom")
  5. m = peripheral.wrap("right")
  6. m.setTextScale(5)
  7. m.setTextColor(colors.white)
  8. m.setBackgroundColor(colors.black)
  9. m.clear()
  10.  
  11. button = {}
  12. currentStatus = "up"
  13. frameController = 456
  14. reactorComputer = 458
  15. alarm = false
  16. reacteur = false
  17. reactemp = 0
  18. reacout = 0
  19. reacinv = {}
  20. warnedAboutOpenReac = false
  21. warnedAboutUnexpectedWork = false
  22. isDrawModeMissing = true
  23. modifyDrawModeT = 0
  24. isModifyDrawmodeTimerDone = true
  25. isMoving = false
  26. currentMode = 0
  27.  
  28. local f = fs.open("nominal","r")
  29. actualNom = textutils.unserialize(f.readAll())
  30. f.close()
  31.  
  32. s = "Wait"
  33. messages = {"", "", "", "", "", "", "", "", "", ""}
  34. mx,my = m.getSize()
  35. m.setCursorPos(math.ceil(mx/2)-math.ceil(#s/2), math.ceil(my/2))
  36. m.write(s)
  37. rednet.send(frameController, "up")
  38. local w = true
  39. while w do
  40.     e,a,b = os.pullEvent("rednet_message")
  41.     if a == frameController and b == "doneMoving" then w = false end
  42. end
  43.  
  44. redstone.setOutput("back", alarm)
  45.  
  46. function logMessage(msg)
  47.     local t = {}
  48.     t.level = "message"
  49.     t.msg = msg
  50.     table.insert(messages, t)
  51.     print(textutils.serialize(messages))
  52.     redraw()
  53. end
  54.  
  55. function logWarning(msg)
  56.     speaker.playNote(0,18)
  57.     local t = {}
  58.     t.level = "warning"
  59.     t.msg = msg
  60.     table.insert(messages, t)
  61.     redraw()
  62. end
  63.  
  64. function logAlert(msg)
  65.     enableAlarm()
  66.     local t = {}
  67.     t.level = "alert"
  68.     t.msg = msg
  69.     table.insert(messages, t)
  70.     redraw()
  71. end
  72.  
  73. function clear()
  74.     m.setTextScale(1)
  75.     setColorScheme()
  76.     m.clear()
  77.     mx,my = m.getSize()
  78. end
  79.  
  80. function disableButton(name)
  81.     if button[name] then button[name]["valid"] = false end
  82.     redraw()
  83. end
  84. function drawButtons()
  85.     for n,d in pairs(button) do
  86.         if d["valid"] == true then
  87.             if d["active"] then m.setBackgroundColor(buttonActiveB) m.setTextColor(buttonActiveT)
  88.             else m.setBackgroundColor(buttonInactiveB) m.setTextColor(buttonInactiveT) end
  89.             for i=d["ymin"], d["ymax"] do
  90.                 local str = ""
  91.                 for j=d["xmin"], d["xmax"] do
  92.                     str = str .. " "
  93.                 end
  94.                 m.setCursorPos(d["xmin"], i)
  95.                 m.write(str)
  96.             end
  97.             m.setCursorPos(math.ceil((d["xmin"]+d["xmax"])/2)-math.ceil(#n/2), math.ceil((d["ymin"]+d["ymax"])/2))
  98.             m.write(n)
  99.         end
  100.     end
  101. end
  102.  
  103. function redraw()
  104.     clear()
  105.     for i=1, mx do
  106.         m.setCursorPos(i,1)
  107.         m.write("-")
  108.         m.setCursorPos(i,my)
  109.         m.write("-")
  110.     end
  111.     for i=2, my-1 do
  112.         m.setCursorPos(1, i)
  113.         m.write("|")
  114.         m.setCursorPos(mx, i)
  115.         m.write("|")
  116.         m.setCursorPos(math.ceil(mx/3), i)
  117.         m.write("|")
  118.     end
  119.     for i=math.ceil(mx/3)+1, mx-1 do
  120.         m.setCursorPos(i, my-7)
  121.         m.write("-")
  122.     end
  123.     m.setCursorPos(math.ceil(mx/3)+2, my-6)
  124.     m.write("Derniers messages")
  125.     local cnt = 6
  126.     for i=#messages, #messages-3, -1 do
  127.         cnt = cnt - 1
  128.         local cmsg = messages[i]
  129.         if cmsg.level == "message" then
  130.             setColorScheme()
  131.         elseif cmsg.level == "warning" then
  132.             m.setBackgroundColor(colors.orange)
  133.             m.setTextColor(colors.black)
  134.         elseif cmsg.level == "alert" then
  135.             if alarm then
  136.                 m.setBackgroundColor(colors.black)
  137.                 m.setTextColor(colors.red)
  138.             else
  139.                 m.setBackgroundColor(colors.red)
  140.                 m.setTextColor(colors.white)
  141.             end
  142.         end
  143.         local oldx,oldy = m.getCursorPos()
  144.         m.setCursorPos(math.ceil(mx/3)+1, my-cnt)
  145.         if cmsg.msg then m.write(cmsg.msg) end
  146.         setColorScheme()
  147.         m.setCursorPos(oldx,oldy)
  148.     end
  149.     if reacteur then
  150.         m.setTextColor(colors.green)
  151.         m.setCursorPos(mx-2,2)
  152.         m.write("On")
  153.     else
  154.         if alarm then m.setTextColor(colors.black) else m.setTextColor(colors.red) end
  155.         m.setCursorPos(mx-3,2)
  156.         m.write("Off")
  157.     end
  158.     local temp = reactemp.." degres"
  159.     m.setCursorPos(mx-#temp,3)
  160.     m.write(temp)  
  161.     local out = reacout.." EU/t"
  162.     m.setCursorPos(mx-#out,4)
  163.     m.write(out)   
  164.     local baseX = math.ceil(mx/3)+2
  165.     local baseY = 2
  166.     m.setTextColor(colors.gray)
  167.     for i=baseX, baseX+19 do
  168.         m.setCursorPos(i, baseY)
  169.         m.write("-")
  170.         m.setCursorPos(i, baseY+7)
  171.         m.write("-")
  172.     end
  173.     for i=baseY+1, baseY+6 do
  174.         m.setCursorPos(baseX, i)
  175.         m.write("|")
  176.         m.setCursorPos(baseX+19, i)
  177.         m.write("|")
  178.     end
  179.     print(reactemp)
  180.     local asd = ""
  181.     local cnt = 1
  182.     for cY = baseY+1, baseY+6 do
  183.         for cX = baseX+1, baseX+17,2 do
  184.             m.setCursorPos(cX, cY)
  185.             local cE = reacinv[cnt]
  186.             cnt = cnt+1
  187.             if currentMode == 0 then
  188.                 if cE ~= nil then
  189.                     if cE.Name == "item.reactorHeatSwitchDiamond" then
  190.                         m.setBackgroundColor(colors.orange)
  191.                         m.setTextColor(colors.white)
  192.                         m.write("<>")
  193.                     elseif cE.Name == "item.reactorVentDiamond" then
  194.                         m.setBackgroundColor(colors.lightBlue)
  195.                         m.setTextColor(colors.white)
  196.                         m.write("VA")
  197.                     elseif cE.Name == "item.360k_NaK_Coolantcell" then
  198.                         m.setBackgroundColor(colors.green)
  199.                         m.setTextColor(colors.white)
  200.                         m.write("Na")
  201.                     elseif cE.Name == "item.360k_Helium_Coolantcell" then
  202.                         m.setBackgroundColor(colors.yellow)
  203.                         m.setTextColor(colors.black)
  204.                         m.write("He")
  205.                     elseif cE.Name == "item.reactorCoolantSix" then
  206.                         m.setBackgroundColor(colors.blue)
  207.                         m.setTextColor(colors.white)
  208.                         m.write("Co")
  209.                     elseif cE.Name == "item.Quad_Plutoniumcell" then
  210.                         m.setBackgroundColor(colors.lime)
  211.                         m.setTextColor(colors.black)
  212.                         m.write("Pl")
  213.                     elseif cE.Name == "item.reactorUraniumSimple" then
  214.                         m.setBackgroundColor(colors.lime)
  215.                         m.setTextColor(colors.black)
  216.                         m.write("Ur")
  217.                     else
  218.                         local lolcnt = cnt-1
  219.                         if actualNom[lolcnt] ~= nil then
  220.                             local lolnom = actualNom[lolcnt]
  221.                             print(lolnom)
  222.                             print(lolcnt)
  223.                             if isDrawModeMissing then
  224.                                 m.setBackgroundColor(colors.black)
  225.                                 m.setTextColor(colors.white)
  226.                                 m.write("??")
  227.                             else
  228.                                 if lolnom == "item.reactorHeatSwitchDiamond" then
  229.                                     m.setBackgroundColor(colors.orange)
  230.                                     m.setTextColor(colors.white)
  231.                                     m.write("<>")
  232.                                 elseif lolnom == "item.reactorVentDiamond" then
  233.                                     m.setBackgroundColor(colors.lightBlue)
  234.                                     m.setTextColor(colors.white)
  235.                                     m.write("VA")
  236.                                 elseif lolnom == "item.360k_NaK_Coolantcell" then
  237.                                     m.setBackgroundColor(colors.green)
  238.                                     m.setTextColor(colors.white)
  239.                                     m.write("Na")
  240.                                 elseif lolnom == "item.360k_Helium_Coolantcell" then
  241.                                     m.setBackgroundColor(colors.yellow)
  242.                                     m.setTextColor(colors.black)
  243.                                     m.write("He")
  244.                                 elseif lolnom == "item.reactorCoolantSix" then
  245.                                     m.setBackgroundColor(colors.blue)
  246.                                     m.setTextColor(colors.white)
  247.                                     m.write("Co")
  248.                                 elseif lolnom == "item.Quad_Plutoniumcell" then
  249.                                     m.setBackgroundColor(colors.lime)
  250.                                     m.setTextColor(colors.black)
  251.                                     m.write("Pl")
  252.                                 elseif lolnom == "item.reactorUraniumSimple" then
  253.                                     m.setBackgroundColor(colors.lime)
  254.                                     m.setTextColor(colors.black)
  255.                                     m.write("Ur")
  256.                                 end
  257.                             end
  258.                         end
  259.                     end        
  260.                 end
  261.             elseif currentMode == 1 then
  262.                 if cE ~= nil then
  263.                     if cE["nbt"] ~= nil then
  264.                         local h = cE.nbt.heat
  265.                         if h ~= nil then
  266.                             if h > reactemp+(5*reactemp) then
  267.                                 m.setBackgroundColor(colors.red)
  268.                                 m.setTextColor(colors.red)
  269.                             elseif h > reactemp then
  270.                                 m.setBackgroundColor(colors.orange)
  271.                                 m.setTextColor(colors.orange)
  272.                             elseif h < reactemp-(5*reactemp) then
  273.                                 m.setBackgroundColor(colors.blue)
  274.                                 m.setTextColor(colors.blue)
  275.                             elseif h <= reactemp then
  276.                                 m.setBackgroundColor(colors.lightBlue)
  277.                                 m.setTextColor(colors.lightBlue)
  278.                             end
  279.  
  280.                             asd = asd .. h .. " "
  281.                             m.write("--")
  282.                         end
  283.                     end
  284.                 end
  285.             elseif currentMode == 2 then
  286.                 if cE ~= nil then
  287.                     if cE.DamageValue ~= nil then
  288.                         local n = cE.Name
  289.                         local d = cE.DamageValue
  290.                         if n == "item.360k_Helium_Coolantcell" or n == "item.360k_NaK_Coolantcell" then
  291.                             if d < 80 then
  292.                                 m.setBackgroundColor(colors.lightBlue)
  293.                                 m.setTextColor(colors.lightBlue)
  294.                             elseif d >= 80 and d < 90 then
  295.                                 m.setBackgroundColor(colors.green)
  296.                                 m.setTextColor(colors.green)
  297.                             elseif d >= 90 and d < 97 then
  298.                                 m.setBackgroundColor(colors.orange)
  299.                                 m.setTextColor(colors.orange)
  300.                             elseif d >= 97 then
  301.                                 m.setBackgroundColor(colors.red)
  302.                                 m.setTextColor(colors.red)
  303.                             end
  304.                             m.write("--")
  305.                         end
  306.                     end
  307.                 end
  308.             end
  309.         end
  310.     end
  311.     print(asd)
  312.     drawButtons()
  313. end
  314.  
  315. function setTable(name, func, xmin, xmax, ymin, ymax)
  316.     button[name] = {}
  317.     button[name]["func"] = func
  318.     button[name]["active"] = false
  319.     button[name]["valid"] = true
  320.     button[name]["xmin"] = xmin
  321.     button[name]["ymin"] = ymin
  322.     button[name]["xmax"] = xmax
  323.     button[name]["ymax"] = ymax
  324.     redraw()
  325. end
  326.  
  327. function createButtons()
  328.     setTable("Reacteur", toggleReacteur, 3, 19, 2, 6)
  329.     setTable("Ouvrir", openPanel, 3, 19, 8, 12)
  330.     setTable("Sauver", saveReacteur, 23, 31, 10, 10)
  331.     setTable("Mode", modeButton, 34, 42, 10, 10)
  332. end
  333.  
  334. function setColorScheme()
  335.     if alarm then
  336.         m.setBackgroundColor(colors.red)
  337.         m.setTextColor(colors.white)
  338.         buttonInactiveB = colors.black
  339.         buttonActiveB = colors.orange
  340.         buttonInactiveT = colors.white
  341.         buttonActiveT = colors.white
  342.     else
  343.         m.setBackgroundColor(colors.black)
  344.         m.setTextColor(colors.white)
  345.         buttonInactiveB = colors.red
  346.         buttonActiveB = colors.green
  347.         buttonInactiveT = colors.white
  348.         buttonActiveT = colors.white
  349.     end
  350. end
  351.  
  352. function enableAlarm()
  353.     redstone.setOutput("back", true)
  354.     alarm = true
  355.     setTable("Reset alarme", disableAlarm, 3, 19, 14, 18)
  356.     redraw()
  357. end
  358.  
  359. function disableAlarm()
  360.     disableButton("Reset alarme")
  361.     redstone.setOutput("back", false)
  362.     alarm = false
  363.     redraw()
  364. end
  365.  
  366. function saveReacteur()
  367.     local nom = {}
  368.     for k,v in pairs(reacinv) do
  369.         table.insert(nom, v.Name)
  370.     end
  371.     local f = fs.open("nominal","w")
  372.     f.write(textutils.serialize(nom))
  373.     f.close()
  374.     local f = fs.open("nominal","r")
  375.     actualNom = textutils.unserialize(f.readAll())
  376.     f.close()
  377.     logMessage("Sauvegarde effectuee")
  378. end
  379.  
  380. function toggleReacteur(button)
  381.     if not reacteur then
  382.         local nom = {}
  383.         for k,v in pairs(reacinv) do
  384.             table.insert(nom, v.Name)
  385.         end
  386.         local ok = true
  387.         local invalid = {}
  388.         local hecells = 0
  389.         for k,v in pairs(nom) do
  390.             if v == "item.360k_Helium_Coolantcell" then hecells = hecells+1 end
  391.             if v ~= actualNom[k] then
  392.                 ok = false
  393.                 table.insert(invalid, k)
  394.             end
  395.         end
  396.         if not ok then
  397.             logWarning("Configuration invalide")
  398.         end
  399.         print(hecells)
  400.         if hecells < 9 then
  401.             logWarning("Helium manquant")
  402.             return
  403.         end
  404.     end
  405.     if currentStatus == "down" then
  406.         logWarning("Reacteur desactive: ouvert")
  407.         return
  408.     end
  409.     reacteur = not reacteur
  410.     if button then button["active"] = reacteur end
  411.     redstone.setOutput("top", reacteur)
  412.     if reacteur == true then logMessage("Reacteur on")
  413.     else logMessage("Reacteur off") end
  414. end
  415.  
  416. function openPanel()
  417.     if isMoving then return end
  418.     isMoving = true
  419.     disableButton("Ouvrir")
  420.     disableButton("Reacteur")
  421.     if reacteur == true then
  422.         toggleReacteur(nil)
  423.         logWarning("Reacteur desactive auto.")
  424.     end
  425.     rednet.send(frameController, "down")
  426. end
  427.  
  428. function closePanel()
  429.     if isMoving then return end
  430.     isMoving = true
  431.     disableButton("Fermer")
  432.     rednet.send(frameController, "up")
  433. end
  434.  
  435. function doneMoving()
  436.     isMoving = false
  437.     if currentStatus == "up" then
  438.         currentStatus = "down"
  439.         setTable("Fermer", closePanel, 3, 19, 8, 12)
  440.     else
  441.         currentStatus = "up"
  442.         setTable("Ouvrir", openPanel, 3, 19, 8, 12)
  443.         setTable("Reacteur", toggleReacteur, 3, 19, 2, 6)
  444.     end
  445. end
  446.  
  447. function checkxy(x, y)
  448.     for name, data in pairs(button) do
  449.         if y>=data["ymin"] and  y <= data["ymax"] then
  450.             if x>=data["xmin"] and x<= data["xmax"] and data["valid"] == true then
  451.                 data["func"](data)
  452.                 print(name)
  453.                 return
  454.             end
  455.         end
  456.     end
  457. end
  458.  
  459. function modeButton()
  460.     currentMode = currentMode + 1
  461.     if currentMode == 3 then currentMode = 0 end
  462.     redraw()
  463. end
  464.  
  465. createButtons()
  466. while true do
  467.     redraw()
  468.     local e,a,b,c = os.pullEvent()
  469.     isDrawModeMissing = not isDrawModeMissing
  470.     if e == "monitor_touch" then checkxy(b,c) sleep(0.1)
  471.     elseif e == "rednet_message" then
  472.         if a == reactorComputer then
  473.             local t = textutils.unserialize(b)
  474.             reactemp = t.heat
  475.             reacout = t.out
  476.             setColorScheme()
  477.             if t.on and not reacteur and not warnedAboutUnexpectedWork then logWarning("Fonctionnement imprevu") warnedAboutUnexpectedWork = true end
  478.             if not t.on and warnedAboutUnexpectedWork then logWarning("Retour a la normale") warnedAboutUnexpectedWork = false end
  479.             if t.on and warnedAboutUnexpectedWork then m.setCursorPos(mx-6,5) m.write("Erreur") end
  480.             if t.on and currentStatus ~= "up" and not warnedAboutOpenReac then warnedAboutOpenReac = true logAlert("Reacteur ouvert") closePanel() end
  481.             if warnedAboutOpenReac and currentStatus == "up" then logMessage("Panneau ferme") disableAlarm() warnedAboutOpenReac = false end
  482.  
  483.             if reactemp > 3000 and reacteur then
  484.                 logAlert("Reacteur en surchauffe")
  485.                 toggleReacteur(button["Reacteur"])
  486.             end
  487.             reacinv = t.inv
  488.         elseif a == frameController and b == "doneMoving" then
  489.             doneMoving()
  490.         end
  491.     end
  492. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement