Necrotico

CC Train System - Station

Feb 12th, 2022 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.04 KB | None | 0 0
  1. -- Station name should be saved in "station.txt" file!
  2.  
  3. local TRAIN_COMM_PORT = 101
  4.  
  5. local modem = peripheral.wrap("left")
  6.  
  7. if modem.isOpen(TRAIN_COMM_PORT) then
  8.     modem.close(TRAIN_COMM_PORT)
  9. end
  10.  
  11. modem.open(TRAIN_COMM_PORT)
  12.  
  13. local function getStationName()
  14.     local sfh = fs.open("station.txt", "r")
  15.     local station_name = sfh.readLine()
  16.     sfh.close()
  17.     return station_name
  18. end
  19.  
  20. while true do
  21.     local event, modemSide, senderChannel,
  22.     replyChannel, message, senderDistance = os.pullEvent("modem_message")
  23.     local this_station_name = getStationName()
  24.     if message == this_station_name then
  25.         if replyChannel == 102 then -- This station is where the train is coming from
  26.             redstone.setAnalogOutput("right", 10)
  27.             redstone.setAnalogOutput("back", 0)
  28.             redstone.setAnalogOutput("front", 0)
  29.             print("Sending train to target station...")
  30.             sleep(20)
  31.             redstone.setAnalogOutput("right", 0)
  32.         elseif replyChannel == 103 then -- This station is the target, and train is coming from behind the computer
  33.             redstone.setAnalogOutput("right", 0)
  34.             redstone.setAnalogOutput("front", 10)
  35.             print("Receiving train from up the line...")
  36.             sleep(40)
  37.             redstone.setAnalogOutput("right", 10)
  38.             redstone.setAnalogOutput("front", 0)
  39.         elseif replyChannel == 104 then -- This station is the target, and train is coming from in front of the computer
  40.             redstone.setAnalogOutput("right", 0)
  41.             redstone.setAnalogOutput("back", 10)
  42.             print("Receiving train from down the line...")
  43.             sleep(40)
  44.             redstone.setAnalogOutput("right", 10)
  45.             redstone.setAnalogOutput("back", 0)
  46.         end
  47.     elseif not replyChannel == 102 then
  48.         redstone.setAnalogOutput("right", 10)
  49.         redstone.setAnalogOutput("back", 0)
  50.         redstone.setAnalogOutput("front", 0)
  51.         print("This station is not the train's target, letting it pass...")
  52.     end
  53. end
  54.  
Add Comment
Please, Sign In to add comment