Advertisement
SemlerPDX

Minecraft ComputerCraft Reactor Control

Aug 27th, 2021 (edited)
2,909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.03 KB | None | 0 0
  1. --Mekanism Fission Reactor Control w/Remote Emergency Monitor
  2. --v0.92 by SemlerPDX Aug21-26 2021
  3.  
  4. --Reactor Control & Monitor Scripts
  5. -- Control PC (this script): https://pastebin.com/2VrXwGNH
  6. --  and
  7. -- Emergency Monitor PC:  https://pastebin.com/uxE2jE2B
  8. --  and
  9. -- Remote Status Monitor PC: https://pastebin.com/59EWW86J
  10. --
  11. -- example image of setup:  https://imgur.com/a/nrKeBcH
  12.  
  13. --Redstone I/O Sides
  14. battery = "left"
  15. reactor = "right"
  16. monitoring = "top"
  17. --Peripheral Sides
  18. monitorSide = "bottom"
  19. modemSide = "back"
  20.  
  21. --Battery Monitor Redstone Min/Max Levels (0-15)
  22. minBank = 7
  23. maxBank = 15
  24.  
  25. --id of rednet protocols
  26. protocol = "PowerSys"
  27. protocolCMD = "PowerCMD"
  28.  
  29. controlPC = 20 --id of (this) reactor control PC
  30. remotePC = 21 --id of remote display status monitor PC
  31. hazardPC = 22 --id of critical shutdown monitor PC
  32.  
  33. --Remote Command Keywords
  34. msgConfirmed = "copycopy"
  35. msgDenied = "samestate"
  36. faultClear = "FaultCleared"
  37. clearFaults = "ClearFault"
  38. commandClear = "enable"
  39. commandON = "on"
  40. commandOFF = "off"
  41. --Remote Command Receive Timeout (in seconds)
  42. timeout = 5
  43.  
  44. --Array of Redstone (0-15) into % of 100 for Text Display
  45. batteryLevels = {7,13,20,27,33,40,47,53,60,67,73,80,87,93,100}
  46.  
  47. --Init
  48. y = 1
  49. online = false
  50. reacting = false
  51. shutdownReason = "unknown"
  52. redstone.setOutput(reactor,false)
  53. redstone.setOutput(monitoring,false)
  54. monitor = peripheral.wrap(monitorSide)
  55. modem = peripheral.wrap(modemSide)
  56. rednet.open(modemSide)
  57.  
  58. --Remote Reactor Status Monitor Send Function
  59. local function fStatusUpdate(msg)
  60.   rednet.send(remotePC,msg,protocol)
  61. end
  62.  
  63. --Refresh/Clear Display(s) Function
  64. local function fSet(remoteClear)
  65.   y = 1
  66.   term.clear()
  67.   monitor.clear()
  68.   term.setCursorPos(1,1)
  69.   monitor.setCursorPos(1,1)
  70.   if remoteClear then
  71.     fStatusUpdate("clear")
  72.   end
  73. end
  74.  
  75. --Direct Monitor Screen Write Function
  76. local function fDisplay(txt)
  77.   monitor.write(txt)
  78.   monitor.setCursorPos(1,y)
  79. end
  80.  
  81. --Combined Term/Monitor/Remote Write Function
  82. local function fWrite(msg,display,send)
  83.   y = y + 1
  84.   term.write(msg)
  85.   term.setCursorPos(1,y)
  86.   if display then
  87.     fDisplay(msg)
  88.   end
  89.   if send then
  90.     fStatusUpdate(msg)
  91.   end
  92. end
  93.  
  94. --Change Redstone Input (0-15) into Percentage for Text Display
  95. local function fBankStatus(cellLevel)
  96.   bankCapacity = "Bank Capacity < 7%"
  97.   if cellLevel == 1 then
  98.     bankCapacity = "Bank Capacity > "..batteryLevels[cellLevel].."%"
  99.   elseif cellLevel >= 2 and cellLevel <= 14 then
  100.     bankCapacity = "Bank Capacity >"..batteryLevels[cellLevel].."%"
  101.   else
  102.     bankCapacity = "Bank Capacity 100%"
  103.   end
  104.   return bankCapacity
  105. end
  106.  
  107. --Acknowledge Emergency Shutdown Fault and Get Reason from Monitoring PC
  108. local function fAcknowledge()
  109.   shutdownReason = "unknown"
  110.   id,reason = rednet.receive(protocol)
  111.   rednet.send(id,msgConfirmed,protocol)
  112.   if reason ~= nil then
  113.     shutdownReason = reason
  114.   end
  115. end
  116.  
  117.  
  118. --MAIN LOOP
  119. while true do
  120.  
  121.   --Emergency Shutdown Monitor Function
  122.   local function fShutdown()
  123.     local criticalShutdown = redstone.getInput(monitoring)
  124.     while true do
  125.       sleep(0.05)
  126.       criticalShutdown = redstone.getInput(monitoring)
  127.       if criticalShutdown then
  128.         redstone.setOutput(reactor,false)
  129.         criticalShutdown = nil
  130.         return true
  131.       end
  132.     end
  133.   end
  134.  
  135.   --Master Online State & Battery Monitor Function
  136.   local function fBatteryMonitor()
  137.     while true do
  138.       sleep(5)
  139.       fSet(true)
  140.       currBank = redstone.getAnalogInput(battery)
  141.       reacting = redstone.getOutput(reactor)
  142.       currStatus = fBankStatus(currBank)
  143.       fWrite(currStatus,true,true)
  144.       if currBank == maxBank and reacting then
  145.         fWrite("Bank at capacity",true,true)
  146.         fWrite("Shutting down...",true,true)
  147.         redstone.setOutput(monitoring,false)
  148.         redstone.setOutput(reactor,false)
  149.         sleep(10)
  150.       elseif currBank <= minBank and not reacting and online then
  151.         fWrite("Bank below min",true,true)
  152.         fWrite("Enabling reaction",true,true)
  153.         redstone.setOutput(monitoring,true)
  154.         sleep(5)
  155.         redstone.setOutput(reactor,true)
  156.       elseif currBank >= minBank and not reacting and online then
  157.         fWrite("Bank at capacity",true,true)
  158.         fWrite("Reactor in standby",true,true)
  159.       elseif reacting and not online then
  160.         fWrite("Command Offline",true,true)
  161.         fWrite("Shutting down...",true,true)
  162.         redstone.setOutput(monitoring,false)
  163.         redstone.setOutput(reactor,false)
  164.         sleep(10)
  165.       elseif not reacting and not online then
  166.         fWrite("Systems Offline",true,true)
  167.         fWrite("Reactor in standby",true,true)
  168.       else
  169.         fWrite("Systems Online",true,true)
  170.         if reacting then
  171.           fWrite("Reactor operating",true,true)
  172.         else
  173.           fWrite("Reactor in standby",true,true)
  174.         end
  175.       end
  176.       monitor.setCursorPos(1,5)
  177.       if online then
  178.         fDisplay(" -Tap to Disable- ")
  179.         fStatusUpdate(commandON)
  180.       else
  181.         fDisplay(" -Tap to Enable- ")
  182.         fStatusUpdate(commandOFF)
  183.       end
  184.     end
  185.   end
  186.  
  187.   --Touchscreen Systems Online Toggle
  188.   local function fTouch()
  189.     while true do
  190.       sleep(3)
  191.       event,side,xPos,yPos = os.pullEvent("monitor_touch")
  192.       if not online then
  193.         online = true
  194.       elseif online then
  195.         online = false
  196.       end
  197.     end
  198.   end  
  199.  
  200.   --Rednet Systems Online Toggle
  201.   local function fListen()
  202.     while true do
  203.       id,message = rednet.receive(protocolCMD)
  204.       rednet.send(id,msgConfirmed,protocolCMD)
  205.       id,message = rednet.receive(protocolCMD,timeout)
  206.       if message ~= nil then
  207.         if message == commandON and not online then
  208.           online = true
  209.           rednet.send(id,msgConfirmed,protocolCMD)
  210.         elseif message == commandOFF and online then
  211.           online = false
  212.           rednet.send(id,msgConfirmed,protocolCMD)
  213.         else
  214.           rednet.send(id,msgDenied,protocolCMD)
  215.         end
  216.       end
  217.     end
  218.   end
  219.  
  220.   --Emergency Shutdown Fault Acknowledgment
  221.   local function fClearFault()
  222.     while true do
  223.       sleep(5)
  224.       local faultsActive  = redstone.getInput(monitoring)
  225.       if not faultsActive then
  226.         faultsActive = nil
  227.         return true
  228.       end
  229.     end
  230.   end
  231.  
  232.   --Fault Clear Terminal Input Confirmation Function
  233.   local function fConfirmCleared()
  234.     while true do
  235.       fSet(true)
  236.       currBank = redstone.getAnalogInput(battery)
  237.       currStatus = fBankStatus(currBank)
  238.       fWrite(currStatus,true,true)
  239.       fWrite("Critical "..shutdownReason.."!",true,true)
  240.       fWrite("EMERGENCY SHUTDOWN",true,true)
  241.       fWrite("Clear faults, then type 'enable' and press Enter")
  242.       term.write("> ")
  243.       local confirmCleared = read()
  244.       fSet()
  245.       if confirmCleared == commandClear then
  246.         fWrite("Confirming Cleared")
  247.         rednet.send(hazardPC,faultClear,protocol)
  248.         id,message = rednet.receive(protocol,15)
  249.         if message == faultCleared then
  250.           sleep(5)
  251.         end
  252.         faultsActive = redstone.getInput(monitoring)
  253.         fSet()
  254.         if not faultsActive then
  255.           fWrite("Clear Confirmed")
  256.           fWrite("Zero Faults Detected")
  257.           fWrite("Enabling Operations")
  258.           return true
  259.         else
  260.           fWrite("Clear Disregarded")
  261.           fWrite("Reactor Faults Detected!")
  262.           fWrite("Cannot Enable Operations!")
  263.         end
  264.       else
  265.         fWrite("Invalid Command...")
  266.         fWrite("Reactor Faults Detected!")
  267.         fWrite("Clear faults and enter 'enable'")
  268.       end
  269.       sleep(4)
  270.     end
  271.   end
  272.  
  273.  
  274.   --MAIN Reactor Function monitoring battery, cmd input, and shutdown signal
  275.   reactorFunction = parallel.waitForAny(fShutdown,fBatteryMonitor,fTouch,fListen)
  276.  
  277.   --Emergency Shutdown, output to screens/rednet
  278.   if reactorFunction == 1 then
  279.     redstone.setOutput(monitoring,false)
  280.     fAcknowledge()
  281.     --Force Terminal Input that faults cleared
  282.     faultAcknowledged = parallel.waitForAll(fClearFault,fConfirmCleared)
  283.   end
  284.  
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement