Advertisement
tman1123

ElevatorController.lua

Apr 1st, 2024 (edited)
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Adjust as needed for your setup:
  2.     --Modem
  3.     local modemSide = "top"
  4.     --Bundled Cable
  5.     local cableSide = "bottom"
  6.  
  7. --Code starts here
  8. rednet.open(modemSide)
  9.  
  10. local floorsFile = "floors.txt"
  11.  
  12. function loadFloors()
  13.     if not fs.exists(floorsFile) then
  14.         return {}
  15.     else
  16.         local file = fs.open(floorsFile, "r")
  17.         local data = file.readAll()
  18.         file.close()
  19.         return textutils.unserialize(data)
  20.     end
  21. end
  22.  
  23. function findFloor(cable)
  24.     local floors = loadFloors()
  25.     for _, floor in ipairs(floors) do
  26.         if cable == floor.col then
  27.             return floor
  28.         end
  29.     end
  30.     return {num = "--", name = "Moving", col = 0}
  31. end
  32.  
  33. function centerText(text, line)
  34.     local width, height = term.getSize()
  35.     term.setCursorPos((width - string.len(text)) / 2, line)
  36.     print(text)
  37. end
  38.  
  39. function displayUI()
  40.     term.clear()
  41.     centerText("Monitoring Elevator", 1)
  42.     centerText("Current Floor", 4)
  43. end
  44.  
  45. displayUI() -- Initial UI setup
  46.  
  47. while true do
  48.     local cable = rs.getBundledInput(cableSide)
  49.     local floor = findFloor(cable)
  50.     rednet.broadcast(floor, "ElevatorFloor")
  51.    
  52.     -- Update the current floor display
  53.     term.setCursorPos(1, 6) -- Adjust Y to match layout
  54.     term.clearLine()
  55.     centerText(floor.num .. " - " .. floor.name, 6)
  56.    
  57.     sleep(.5) -- Adjust sleep time as needed
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement