Advertisement
SemlerPDX

Minecraft ComputerCraft Reactor Shutdown

Aug 27th, 2021 (edited)
1,962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. --Mekanism Fission Reactor Emergency Shutdown Monitor
  2. --v0.91 by SemlerPDX Aug26 2021
  3.  
  4. --Reactor Control & Monitor Scripts
  5. -- Control PC: https://pastebin.com/2VrXwGNH
  6. --  and
  7. -- Emergency Monitor PC (this script):  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. dmgSide = "top"
  15. tempSide = "right"
  16. wasteSide = "left"
  17. fuelSide = "back"
  18. reactorShutdown = "bottom"
  19.  
  20. --Peripheral Side
  21. modemSide = "front" --wireless modem side
  22.  
  23. hazardPC = 22 --id of (this) critical shutdown monitor PC
  24. remotePC = 21 --id of remote display status monitor PC
  25. controlPC = 20 --id of reactor control PC
  26.  
  27. --id of rednet protocols
  28. protocol = "PowerSys"
  29.  
  30. --Remote Command Keywords
  31. msgConfirmed = "copycopy"
  32. faultCleared = "FaultCleared"
  33. --Remote Command Receive Timeout (in seconds)
  34. timeout = 5
  35. sendCount = 0
  36.  
  37. --Display Text and Shutdown Reasons
  38. shutdownReasons = {"Damage","Heat","Waste","Fuel"}
  39. monitorActive = "Monitor is active"
  40. emergencyShutdown = "EMERGENCY SHUTDOWN!"
  41. standbyShutdown = "Standby Shutdown"
  42. faultyStandby = "Fault Standby Mode"
  43.  
  44. --Init
  45. modem = peripheral.wrap(modemSide)
  46. rednet.open(modemSide)
  47.  
  48. local function fSet()
  49.   term.clear()
  50.   y = 1
  51. end
  52.  
  53. local function fWrite(msg)
  54.   if y >= 19 then
  55.     fSet()
  56.   end
  57.   term.setCursorPos(1,y)
  58.   term.write(msg)
  59.   y = y + 1
  60. end
  61.  
  62. --Rednet Recieve Requested Message
  63. local function fRcv(msg)
  64.   id,message = rednet.receive(protocol,timeout)
  65.   if message ~= nil then
  66.     if msg == message then
  67.       return true
  68.     else
  69.       return false
  70.     end
  71.   end  
  72. end
  73.  
  74. --Rednet Send with Receipt Confirmation
  75. local function fSend(toID,msg,rcv,ccode)
  76.   rednet.send(toID,msg,protocol)
  77.   if rcv then
  78.     returnReceipt = fRcv(ccode)
  79.     if returnReceipt then
  80.       return true
  81.     else
  82.       return false
  83.     end
  84.   end
  85. end
  86.  
  87.  
  88. --MAIN LOOP
  89. while true do
  90.   fSet()
  91.   fWrite(monitorActive)
  92.  
  93.   --Check State and Clear State Functions
  94.   local function fCheckDamage()
  95.     while true do
  96.       sleep(0.05)
  97.       dmgStable = redstone.getInput(dmgSide)
  98.       if not dmgStable then
  99.         redstone.setOutput(reactorShutdown,true)
  100.         dmgStable = nil
  101.         return true
  102.       end
  103.     end
  104.   end
  105.  
  106.   local function fClearDamage()
  107.     while true do
  108.       sleep(5)
  109.       local dmgStable = redstone.getInput(dmgSide)
  110.       if dmgStable then
  111.         dmgStable = nil
  112.         return true
  113.       end
  114.     end
  115.   end
  116.  
  117.   local function fCheckTemperature()
  118.     while true do
  119.       sleep(0.05)
  120.       tempStable = redstone.getInput(tempSide)
  121.       if not tempStable then
  122.         redstone.setOutput(reactorShutdown,true)
  123.         tempStable = nil
  124.         return true
  125.       end
  126.     end
  127.   end
  128.  
  129.   local function fClearTemperature()
  130.     while true do
  131.       sleep(5)
  132.       local tempStable = redstone.getInput(tempSide)
  133.       if tempStable then
  134.         tempStable = nil
  135.         return true
  136.       end
  137.     end
  138.   end
  139.  
  140.   local function fCheckWaste()
  141.     while true do
  142.       sleep(0.05)
  143.       wasteStable = redstone.getInput(wasteSide)
  144.       if not wasteStable then
  145.         redstone.setOutput(reactorShutdown,true)
  146.         wasteStable = nil
  147.         return true
  148.       end
  149.     end
  150.   end
  151.  
  152.   local function fClearWaste()
  153.     while true do
  154.       sleep(5)
  155.       local wasteStable = redstone.getInput(wasteSide)
  156.       if wasteStable then
  157.         wasteStable = nil
  158.         return true
  159.       end
  160.     end
  161.   end
  162.  
  163.   local function fCheckFuel()
  164.     while true do
  165.       sleep(0.05)
  166.       local fuelLow = redstone.getInput(fuelSide)
  167.       if fuelLow then
  168.         redstone.setOutput(reactorShutdown,true)
  169.         fuelLow = nil
  170.         return true
  171.       end
  172.     end
  173.   end
  174.  
  175.   local function fClearFuel()
  176.     while true do
  177.       sleep(5)
  178.       fuelLow = redstone.getInput(fuelSide)
  179.       if not fuelLow then
  180.         fuelLow = nil
  181.         return true
  182.       end
  183.     end
  184.   end
  185.  
  186.   local function fSendFault()
  187.     sendCount = 0
  188.     fSet()
  189.     fWrite("Sent MSG: '"..shutdownReasons[shutdownReason].."' to PCID: "..controlPC)
  190.     while true do
  191.       local messageReceived = fSend(controlPC,shutdownReasons[shutdownReason],true,msgConfirmed)
  192.       sleep(1)
  193.       sendCount = sendCount + 1
  194.       fWrite("Reason Send Count: "..sendCount)
  195.       if messageReceived then
  196.         messageReceived = nil
  197.         return true
  198.       end
  199.     end
  200.   end
  201.  
  202.   local function fClearFault()
  203.     fWrite("Fault Monitor in Standby...")
  204.     while true do
  205.       id,message = rednet.receive(protocol)
  206.       if message == faultCleared then
  207.         local checkDmgStable = redstone.getInput(dmgSide)
  208.         local checkTempStable = redstone.getInput(tempSide)
  209.         local checkWasteStable = redstone.getInput(wasteSide)
  210.         local checkFuelLow = redstone.getInput(fuelSide)
  211.         if checkDmgStable and checkTempStable and checkWasteStable and not checkFuelLow then
  212.           redstone.setOutput(reactorShutdown,false)
  213.           sleep(1)
  214.           rednet.send(id,faultCleared,protocol)
  215.           return true
  216.         end
  217.       end
  218.     end
  219.   end
  220.  
  221.  
  222.   local function fCheckReacting()
  223.     while true do
  224.       sleep(0.05)
  225.       reacting = redstone.getInput(reactorShutdown)
  226.       if not reacting then
  227.         sleep(5)
  228.         reacting = nil
  229.         return true
  230.       end
  231.     end
  232.   end
  233.  
  234.   local function fStandby()
  235.     while true do
  236.       sleep(5)
  237.       local reacting = redstone.getInput(reactorShutdown)
  238.       if reacting then
  239.         reacting = nil
  240.         return true
  241.       end
  242.     end
  243.   end
  244.  
  245.  
  246.   shutdownReason = parallel.waitForAny(fCheckDamage,fCheckTemperature,fCheckWaste,fCheckFuel,fCheckReacting)
  247.  
  248.   if shutdownReason <= 4 then
  249.     --send fault shutdown reason to control PC
  250.     fWrite(emergencyShutdown)
  251.     faultAcknowledge = parallel.waitForAll(fSendFault,fCheckReacting)
  252.     --enter fault standby loop
  253.     fWrite(faultyStandby)
  254.     faultStandby = parallel.waitForAll(fClearDamage,fClearTemperature,fClearWaste,fClearFuel,fClearFault,fStandby)
  255.   elseif shutdownReason == 5 then
  256.     --enter standby loop
  257.     fWrite(standbyShutdown)
  258.     monitorStandby = parallel.waitForAll(fClearDamage,fClearTemperature,fClearWaste,fClearFuel,fStandby)
  259.   end
  260.  
  261.   --confirm standby exit and reset for active monitoring
  262.   fWrite("'Copy' to Control")
  263.   fSend(controlPC,msgConfirmed)
  264.   sleep(1)
  265.  
  266. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement