Advertisement
Guest User

train

a guest
Jan 18th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. --Credits PeachMaster 2019
  2. --Haha just kidding it only example
  3. --It's pretty basic, you can set static speed limits
  4. --CTRL + T to exit
  5.  
  6. --Change these to your likings
  7. local monitor = peripheral.find("monitor");
  8. term.redirect(monitor);
  9. local theSpeedLimit = 200
  10. local theNextSpeedLimit = 0
  11.  
  12.  
  13. local radio = peripheral.find("wirelessMTCRadio") -- Finds a radio peripheral
  14. os.loadAPI("json")
  15. radio.activate() -- Activates the Radio
  16.  
  17. print("The server UUID is: "..radio.getSelfUUID())
  18.  
  19. while true do
  20.     print("Waiting for response..")
  21.     local event, side, id, uuid, message, system = os.pullEvent("radio_message") -- Waits for a radio message
  22.  
  23.     --Prints important stuff
  24.     print("Side it came from: "..side)
  25.     print("Train UUID: "..id)
  26.     print("Raw Message: "..message)
  27.  
  28.     if string.find(message, "speed") then
  29.         speed = string.gsub(message, "speed ", "");
  30.         theSpeedLimit = speed
  31.     end
  32.    
  33.     local theMessage = json.decode(message) -- Decodes the message from JSON to Lua Table
  34.  
  35.      print(theMessage)
  36.      
  37.      print(string.len(id))
  38.    if string.len(id) <  8 then
  39.     if theMessage.funct == "attemptconnection" then
  40.         --Alright, yay! The train is attempting to connect! Send a startlevel2 to turn on W-MTC
  41.        print("1")
  42.          radio.sendMessage(id, json.encode({funct = "startlevel2", speedLimit = theSpeedLimit, nextSpeedLimit = theNextSpeedLimit, speedChange = false, endSoon = false, stationStopSoon = false, mtcStatus = 1, atoStatus = 1, destination = ""}))
  43.         print("Sending a start level 2 request to "..uuid)
  44.     end
  45.  
  46.     if theMessage.funct == "update" then
  47.     print("2")
  48.    
  49.     if theMessage.signalBlock == "SCML-1" then
  50.     radio.sendMessage(id, json.encode({funct = "response", speedLimit = 280, nextSpeedLimit = 100, speedChange = true, endSoon = false, stationStopSoon = false, atoStatus = 1}))
  51.    elseif theMessage.signalBlock == "SCML-2" then
  52.    
  53.    end
  54.      end
  55.         --The train sends updates to the server every second or so
  56.        -- radio.sendMessage(id, json.encode({funct = "response", speedlimit = theSpeedLimit, nextSpeedLimit = theNextSpeedLimit, speedChange = false, endSoon = false, stationStopSoon = false, mtcStatus = 1, atoStatus = 1, destination = ""}))
  57.     end
  58. end
  59.  
  60.  
  61. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement