Advertisement
Agent_Silence

elevator

Oct 8th, 2023 (edited)
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | Gaming | 0 0
  1. local floors = {"Basement", "First Floor", "Roof"}
  2.  
  3. function centerText(text, y)
  4.     local width, height = term.getSize()
  5.     local centerX = width/2
  6.     local startX = math.ceil(centerX - string.len(text)/2)
  7.     term.setCursorPos(startX, y)
  8.     term.write(text)
  9. end
  10.  
  11. function render(selected)
  12.     term.setTextColor(colors.lightBlue)
  13.     term.setBackgroundColor(colors.white)
  14.     term.clear()
  15.     centerText("test",10)
  16.     term.setCursorPos(2,2)
  17.     term.setTextColor(colors.red)
  18.     term.write("COSTCO ")
  19.     term.setTextColor(colors.blue)
  20.     term.write("CLUB")
  21.    
  22.     centerText("Select a Floor",5)
  23.     term.setTextColor(colors.red)
  24.     for i = 1, #floors do
  25.         if i == selected then
  26.             term.setTextColor(colors.black)
  27.             centerText("> "..floors[i].." <", 6+2*i)
  28.             term.setTextColor(colors.red)
  29.         else
  30.             centerText(floors[i], 6+2*i)
  31.         end
  32.     end
  33. end
  34.  
  35. local floor = 1
  36.  
  37. function goToFloor(n)
  38.     diff = n - floor
  39.    
  40.     if diff < 0 then
  41.         redstone.setOuput("back",false)
  42.     else
  43.         redstone.setOuput("back",true)
  44.     end
  45.    
  46.     redstone.setOutput("right",false)
  47.     repeat
  48.         local id, msg = rednet.receive("ElevatorArrival")
  49.     until tonumber(msg) == n
  50.     redstone.setOutput("right",true)
  51. end
  52.  
  53. local selectedFloor = 1
  54. function doInterface()
  55.     while true do
  56.         render(selectedFloor)
  57.         local event, key = os.pullEventRaw("key")
  58.         if key == keys.down then
  59.             selectedFloor = selectedFloor + 1
  60.             if selectedFloor > #floors then
  61.                 selectedFloor = 1
  62.             end
  63.         elseif key == keys.up then
  64.             selectedFloor = selectedFloor - 1
  65.             if selectedFloor < 1 then
  66.                 selectedFloor = #floors
  67.             end
  68.         elseif key == keys.enter then
  69.             rednet.broadcast(selectedFloor, "ElevatorCall")
  70.         end
  71.     end
  72. end
  73.  
  74. function doServer()
  75.     while true do
  76.         local id, msg = rednet.receive("ElevatorCall")
  77.         goToFloor(tonumber(msg))
  78.     end
  79. end
  80.  
  81. function doFloor()
  82.     term.clear()
  83.     term.setCursorPos(1,1)
  84.     term.write("What floor is this?")
  85.     local val = tonumber(read())
  86.    
  87.    
  88.     while true do
  89.         os.pullEvent("redstone");
  90.         rednet.broadcast(val,"ElevatorArrival")
  91.     end
  92. end
  93.  
  94. term.clear()
  95. term.setCursorPos(1,1)
  96. print("1 for Interface, 2 for Server, 3 for Floor")
  97. result = read()
  98.  
  99. if result == "1" then
  100.     doInterface()
  101. elseif result == "2" then
  102.     doServer()
  103. elseif result == "3" then
  104.     doFloor()
  105. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement