Advertisement
Fusion1227

Signal CPU

Oct 26th, 2023 (edited)
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- this is the startup script for signal computers
  2.  
  3.  
  4. ----| config |----------------------------
  5.  
  6.  
  7. local MODEM_SIDE = "top"
  8. local SIGNAL_SIDE = "back" -- the side connected to the signal
  9. local CENTRAL_CPU_ID = 125 -- the id of the computer at spawn
  10.  
  11.  
  12.  
  13. ----| setup |----------------------------
  14.  
  15.  
  16. local actionEnum = {
  17.     GET_SIGNAL_STATE = "GET_SIGNAL_STATE",
  18.     SENSOR_CHANGE = "SENSOR_CHANGE",
  19. }
  20.  
  21.  
  22. ----| functions |----------------------------
  23.  
  24.  
  25. function receiveSignal() -- waits for commands from central cpu
  26.     while true do
  27.         print("Waiting for a command...")
  28.         local id, state = rednet.receive()
  29.         if id == CENTRAL_CPU_ID then
  30.             if state == true then -- activate signal
  31.                 print("Activating signal...")
  32.                 redstone.setOutput(SIGNAL_SIDE, true)
  33.             elseif state == false then -- deactivate signal
  34.                 redstone.setOutput(SIGNAL_SIDE, false)
  35.                 print("Deactivating signal...")
  36.             end
  37.         end
  38.     end
  39. end
  40.  
  41.  
  42. ----| main |----------------------------
  43.  
  44.  
  45. rednet.open(MODEM_SIDE)
  46. print("Rednet opened.")
  47.  
  48.  
  49. -- get the immediate signal state
  50. print("Getting the immediate state...")
  51. rednet.send(CENTRAL_CPU_ID, actionEnum.GET_SIGNAL_STATE)
  52. local id, immediateState = rednet.receive()
  53. redstone.setOutput(SIGNAL_SIDE, immediateState)
  54. print("The signal state is currently set to " .. tostring(immediateState))
  55.  
  56.  
  57. -- listen for signal state changes
  58. receiveSignal()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement