SemlerPDX

Minecraft ComputerCraft Reactor Monitor

Aug 28th, 2021 (edited)
2,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. --Mekanism Fission Reactor Remote Status Monitor
  2. --v0.91 by SemlerPDX Aug27 2021
  3.  
  4. --Reactor Control & Monitor Scripts
  5. -- Control PC: https://pastebin.com/2VrXwGNH
  6. --  and
  7. -- Emergency Monitor PC:  https://pastebin.com/uxE2jE2B
  8. --  and
  9. -- Remote Status Monitor PC (this script): https://pastebin.com/59EWW86J
  10. --
  11. -- example image of setup:  https://imgur.com/a/nrKeBcH
  12.  
  13. --Peripheral Sides
  14. monitorSide = "back"
  15. modemSide = "left"
  16.  
  17. --id of rednet protocols
  18. protocol = "PowerSys"
  19. protocolCMD = "PowerCMD"
  20.  
  21. controlPC = 20 --id of reactor control PC
  22. remotePC = 21 --id of (this) remote display status monitor PC
  23. hazardPC = 22 --id of critical shutdown monitor PC
  24.  
  25. --Remote Command Keywords
  26. msgOpenComms = "opencomms"
  27. msgConfirmed = "copycopy"
  28. commandON = "on"
  29. commandOFF = "off"
  30. remoteClear = "clear"
  31. sysOnline = "Systems Online"
  32. sysOffline = "Systems Offline"
  33. --Remote Command Receive Timeout (in seconds)
  34. timeout = 5
  35.  
  36. --Init
  37. online = false
  38. monitor = peripheral.wrap(monitorSide)
  39. modem = peripheral.wrap(modemSide)
  40. rednet.open(modemSide)
  41.  
  42. local function fSet()
  43.   term.clear()
  44.   monitor.clear()
  45.   term.setCursorPos(1,1)
  46.   monitor.setCursorPos(1,1)
  47.   y = 1
  48. end
  49.  
  50. local function fWrite(msg)
  51.   y = y + 1
  52.   term.write(msg)
  53.   monitor.write(msg)
  54.   term.setCursorPos(1,y)
  55.   monitor.setCursorPos(1,y)
  56. end
  57.  
  58. local function fToggleTxt(state)
  59.   monitor.setCursorPos(1,5)
  60.   monitor.write("                  ")
  61.   monitor.setCursorPos(1,5)
  62.   if state == commandON then
  63.     monitor.write(" -Tap to Disable- ")
  64.     online = true
  65.   elseif state == commandOFF then
  66.     monitor.write(" -Tap to Enable- ")
  67.     online = false
  68.   end
  69.   monitor.setCursorPos(1,y)
  70. end
  71.  
  72. --Rednet Recieve Requested Message
  73. local function fRcv(msg)
  74.   id,message = rednet.receive(protocolCMD,timeout)
  75.   if message ~= nil then
  76.     if msg == message then
  77.       return true
  78.     else
  79.       return false
  80.     end
  81.   end  
  82. end
  83.  
  84. --Rednet Send with Receipt Confirmation
  85. local function fSend(toID,msg,rcv,ccode)
  86.   rednet.send(toID,msg,protocolCMD)
  87.   if rcv then
  88.     returnReceipt = fRcv(ccode)
  89.     if returnReceipt then
  90.       return true
  91.     else
  92.       return false
  93.     end
  94.   end
  95. end
  96.  
  97. --Rednet Send Remote Toggle Command and Return Confirmation
  98. local function fRemoteToggle(state)
  99.   while true do
  100.     commsOpen = fSend(controlPC,msgOpenComms,true,msgConfirmed)
  101.     if commsOpen then
  102.       commsOut = fSend(controlPC,state,true,msgConfirmed)
  103.       return commsOut
  104.     end
  105.     sleep(1)
  106.   end
  107. end
  108.  
  109.  
  110. --MAIN LOOP
  111. while true do
  112.  
  113.   --Rednet Remote System Status Update
  114.   local function fReactorStatus()
  115.     while true do
  116.       id,message = rednet.receive(protocol)
  117.       if stateNew and stateChange then
  118.         fToggleTxt(stateNew)
  119.         stateChange = nil
  120.       end
  121.       if id == controlPC then
  122.         if message == remoteClear then
  123.           fSet()
  124.         else
  125.           if message == commandON then
  126.             online = true
  127.             fToggleTxt(commandON)
  128.           elseif message == commandOFF then
  129.             online = false
  130.             fToggleTxt(commandOFF)
  131.           else
  132.             fWrite(message)
  133.           end
  134.         end
  135.       end
  136.     end
  137.   end
  138.  
  139.   --Touchscreen Systems Online Toggle
  140.   local function fTouch()
  141.     while true do
  142.       sleep(6)
  143.       event,side,xPos,yPos = os.pullEvent("monitor_touch")
  144.       if not online then
  145.         stateNew = commandON
  146.       elseif online then
  147.         stateNew = commandOFF
  148.       end
  149.       stateChange = fRemoteToggle(stateNew)
  150.     end
  151.   end
  152.  
  153.   --Main Function should loop indefinitely
  154.   monitorStatus = parallel.waitForAny(fReactorStatus,fTouch)
  155.  
  156. end
Add Comment
Please, Sign In to add comment