SY573M_404

Untitled

May 28th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. FLOOR = 1
  2.  
  3. function rednet_or_redstone(protocol_filter, timeout)
  4. -- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
  5. if type(protocol_filter) == "number" and timeout == nil then
  6. protocol_filter, timeout = nil, protocol_filter
  7. end
  8. expect(1, protocol_filter, "string", "nil")
  9. expect(2, timeout, "number", "nil")
  10.  
  11. -- Start the timer
  12. local timer = nil
  13. if timeout then
  14. timer = os.startTimer(timeout)
  15. end
  16.  
  17. -- Wait for events
  18. while true do
  19. local event, p1, p2, p3 = os.pullEvent()
  20. if event == "rednet_message" then
  21. -- Return the first matching rednet_message
  22. local sender_id, message, protocol = p1, p2, p3
  23. if protocol_filter == nil or protocol == protocol_filter then
  24. return "rednet_message", sender_id, message, protocol
  25. end
  26. elseif event == "timer" then
  27. -- Return nil if we timeout
  28. if p1 == timer then
  29. return nil
  30. end
  31. elseif event == "redstone"
  32. return "redstone"
  33. end
  34. end
  35. end
  36.  
  37. local monitor = peripheral.find("monitor")
  38.  
  39. monitor.setBackgroundColor(colors.white)
  40. monitor.setTextColor(colors.black)
  41. monitor.setTextScale(5)
  42. monitor.clear()
  43.  
  44. function display_floor(floor)
  45. monitor.clear()
  46. monitor.setCursorPos(1, 1)
  47. monitor.write(floor)
  48. end
  49.  
  50. peripheral.find("modem", rednet.open)
  51.  
  52. while true do
  53. local type, id, message = rednet_or_redstone()
  54.  
  55. if type == "redstone" then
  56. if redstone.getInput("back") then
  57. local data = {
  58. event = "arrived",
  59. floor = FLOOR
  60. }
  61.  
  62. rednet.broadcast(textutils.serialise(data))
  63.  
  64. display_floor(FLOOR)
  65. end
  66. elseif type == "rednet_message" then
  67. local data = textutils.unserialise(message)
  68. local event = data["event"]
  69.  
  70. if event == "arrived" then
  71. local floor = data["floor"]
  72.  
  73. display_floor(floor)
  74. end
  75. end
  76. end
  77.  
Advertisement
Add Comment
Please, Sign In to add comment