Advertisement
Mojokojo69

elevator lobby display

Aug 30th, 2023 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. local monitor = peripheral.find("monitor")
  3. local speaker = peripheral.find("speaker")
  4.  
  5. monitor.setTextScale(1)
  6. monitor.setTextColour(colours.green)
  7.  
  8. local configFileName = "lobby_display_config.txt"
  9. local computerID = os.getComputerID()
  10. local config = {channel = "", currentFloor = "", computerID = ""}
  11.  
  12. local elevatorFloor = 0
  13. local prevFloor = 0
  14.  
  15. -- Save Config to file
  16. local function saveConfig(config)
  17. local configData = textutils.serializeJSON(config)
  18. local file = fs.open(configFileName, "w")
  19. file.write(configData)
  20. file.close()
  21. end
  22.  
  23. -- Load Config from file
  24. local function loadConfig()
  25. if fs.exists(configFileName) then
  26. local file = fs.open(configFileName, "r")
  27. local configData = file.readAll()
  28. file.close()
  29. config = textutils.unserializeJSON(configData)
  30. end
  31. end
  32.  
  33. local function refreshMonitor()
  34. monitor.clear()
  35. monitor.setCursorPos(1, 1)
  36. end
  37.  
  38. local function animateFloorChange(newFloor)
  39. local width, height = monitor.getSize()
  40. local x = math.ceil(width / 2)
  41. local startY, endY
  42.  
  43. if newFloor > prevFloor then
  44.  
  45. startY = height
  46. endY = math.ceil(height / 2)
  47. else
  48. startY = 1
  49. endY = math.ceil(height / 2)
  50. end
  51.  
  52. for y = startY, endY, newFloor > prevFloor and -1 or 1 do
  53. refreshMonitor()
  54. monitor.setCursorPos(x, y)
  55. monitor.write(tostring(newFloor))
  56. sleep(0.1)
  57. end
  58.  
  59. prevFloor = newFloor
  60. end
  61.  
  62. local function mainLoop(config)
  63. modem.open(tonumber(config.channel))
  64. while true do
  65. local event, modem, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  66. if event == "modem_message" and senderChannel == tonumber(config.channel) then -- Use config.channel here
  67. if string.sub(message, 1, 5) == "idle:" then
  68. elevatorFloor = tonumber(string.sub(message, 6))
  69. animateFloorChange(elevatorFloor)
  70. print("Elevator floor: " .. elevatorFloor)
  71. if elevatorFloor == tonumber(config.currentFloor) then -- Use config.currentFloor here
  72. if prevFloor ~= elevatorFloor then
  73. speaker.playSound("minecraft:block.note_block.pling")
  74. else
  75. speaker.playSound("minecraft:block.note_block.bell", 0.5, 0.3)
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end
  82.  
  83. -- Initialization
  84. local function initializeClient()
  85. loadConfig()
  86. if not config.channel or config.channel == "" then
  87. print("Please enter the channel to use:")
  88. config.channel = tonumber(read())
  89. end
  90. if not config.currentFloor or config.currentFloor == "" then
  91. print("Please enter the current floor:")
  92. config.currentFloor = tonumber(read())
  93. end
  94. if not config.computerID then
  95. config.computerID = tostring(computerID) -- Convert to string if needed
  96. end
  97. saveConfig(config)
  98. mainLoop(config)
  99. end
  100.  
  101. -- Entry Point
  102. initializeClient()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement