Advertisement
Fusion1227

[SINGLE PLAYER] Half Network

Oct 27th, 2023 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. --[[
  2.     A complete train signal system has two on sensors (one per side), two off sensors (one per side),
  3.     and two signal lights. Given a signal light A and a signal light B, this script handles
  4.     the on sensor for B and the off sensor for A. Since these two sensors are close to A,
  5.     it is efficient to link them and A to a single computer, and to link the rest of the components
  6.     on the other side to another computer.
  7.  
  8.     Each train signal network should have two computers, and each computer should use this script
  9.     and name it "startup".
  10. ]]
  11.  
  12.  
  13. ----| config |----------------------------
  14.  
  15.  
  16. local MODEM_SIDE = "top"
  17. local LEFT_SENSOR_SIDE = "left"
  18. local RIGHT_SENSOR_SIDE = "right"
  19. local SIGNAL_SIDE = "back"
  20. local CENTRAL_CPU_ID = 4 -- the id of the computer at spawn
  21.  
  22.  
  23. ----| setup |----------------------------
  24.  
  25.  
  26. local protocolEnum = {
  27.     GET_SIGNAL_STATE = "GET_SIGNAL_STATE",
  28.     SENSOR_CHANGE = "SENSOR_CHANGE",
  29. }
  30.  
  31. local sensorStateEnum = { -- compatible with bitwise operations
  32.     off = 0,
  33.     leftOn = 1,
  34.     rightOn = 2,
  35.     bothOn = 3,
  36. }
  37.  
  38. local lastSensorStates = sensorStateEnum.off
  39.  
  40.  
  41. ----| functions |----------------------------
  42.  
  43.  
  44. function getSensorStates()
  45.     local leftValue = redstone.getInput(LEFT_SENSOR_SIDE) and sensorStateEnum.leftOn or sensorStateEnum.off -- 1 if on, 0 if off
  46.     local rightValue = redstone.getInput(RIGHT_SENSOR_SIDE) and sensorStateEnum.rightOn or sensorStateEnum.off -- 2 if on, 0 if off
  47.     return leftValue + rightValue -- corresponds to one of the sensorStateEnum values
  48. end
  49.  
  50.  
  51. function sendSensorStates()
  52.     local states = getSensorStates()
  53.     if states ~= lastSensorStates and states ~= sensorStateEnum.off and states ~= sensorStateEnum.bothOn then -- if the state changed and just one of the sensors is on
  54.         rednet.send(CENTRAL_CPU_ID, states, protocolEnum.SENSOR_CHANGE)
  55.     end
  56.     lastSensorStates = states -- record the state change
  57. end
  58.  
  59.  
  60. function setSignalState()
  61.     rednet.send(CENTRAL_CPU_ID, nil, protocolEnum.GET_SIGNAL_STATE)
  62.     local id, signalState = rednet.receive(protocolEnum.GET_SIGNAL_STATE)
  63.     print("Received message from computer " .. id .. "\n\t> Message: " .. tostring(signalState))
  64.     if id ~= CENTRAL_CPU_ID then return end
  65.  
  66.     if signalState == true then -- activate signal
  67.         print("Activating signal...")
  68.         redstone.setOutput(SIGNAL_SIDE, true)
  69.     elseif signalState == false then -- deactivate signal
  70.         redstone.setOutput(SIGNAL_SIDE, false)
  71.         print("Deactivating signal...")
  72.     end
  73. end
  74.  
  75.  
  76. ----| main |----------------------------
  77.  
  78.  
  79. rednet.open(MODEM_SIDE)
  80. print("Rednet opened.")
  81.  
  82.  
  83. while true do
  84.     sendSensorStates()
  85.     setSignalState()
  86.     sleep(1)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement