Theshadow989

Elevator V1

Jul 2nd, 2022 (edited)
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.25 KB | None | 0 0
  1. --[[
  2. Elevator Floor Selection
  3. Written by TheShadow989
  4. ]]--
  5.  
  6. -- Reference Images used to build: https://imgur.com/a/laqDQ9x
  7.  
  8. --[[
  9. Directional Cable Colors
  10.  
  11. upCable = Used to move the frame elevator up.
  12. downCable = Used to move the frame elevator down.
  13. ]]--
  14.  
  15. local upCable = colors.white -- Cable color for the elevator to go up
  16. local downCable = colors.black -- Cable color for the elevator to go down
  17.  
  18. --[[
  19. Current Computer Index
  20.  
  21. Set this value to the floorOptions index of the current floor that the computer is on.
  22. Without this value the 'Call' button will not work correctly.
  23. ]]--
  24.  
  25. local currentComputerIndex = 1
  26.  
  27. --[[
  28. Floor Options Array
  29.  
  30. NOTE: This array is going to be the same on every computer, only change the 'Current Computer Index' value above for each computer.
  31.  
  32. ID         = Floor array index (Must be in numerical order)
  33. floorName  = Custom floor name (examples: FL1, GRD, Floor 1, F1, 1)
  34. floorCable = Cable color that is used to track where the elevator is.
  35. doorCable  = Cable color that is used to open and close the elevator doors (Not required)
  36. ]]--
  37.  
  38. local floorOptions =
  39. {
  40.     [1] = {floorName="  1  ", floorCable=colors.yellow, doorCable=colors.orange},
  41.     [2] = {floorName="  2  ", floorCable=colors.magenta, doorCable=colors.purple},
  42.     [3] = {floorName="  3  ", floorCable=colors.lightBlue, doorCable=colors.blue},
  43.     [4] = {floorName="  4  ", floorCable=colors.lime, doorCable=colors.green},
  44.     [5] = {floorName="  5  ", floorCable=colors.pink, doorCable=colors.red},
  45.     [6] = {floorName="  6  ", floorCable=colors.lightGray, doorCable=colors.gray},
  46.     [7] = {floorName="  7  ", floorCable=colors.cyan, doorCable=colors.brown},
  47. }
  48.  
  49. -- Working Variables (DO NOT CHANGE)
  50. local sides = {"top", "bottom", "left", "right", "front", "back"} -- Used the find the monitor side.
  51. local mon = "" -- Monitor
  52. local currentFloorIndex = 1 -- The floor that the elevator is on. This value will change as the elevator moves.
  53. local targetFloorIndex = -1 -- The target floor from the selection menu.
  54. local positionReset = true; -- If elevator is between floors on logout (server close). When player logs back in the elevator will go up a floor until a floor cable is triggered. Only happens once on program startup.
  55.  
  56. --Find Monitor Side
  57. for i=1, #sides do
  58.     if peripheral.isPresent(sides[i]) then
  59.         if peripheral.getType(sides[i]) == "monitor" then
  60.             mon = peripheral.wrap(sides[i])
  61.         end
  62.     end
  63. end
  64.  
  65. --Reset Program
  66. term.clear()
  67. mon.clear()
  68. redstone.setBundledOutput("top", 0)
  69.  
  70. while true do
  71.     os.queueEvent("fakeEvent")
  72.     os.pullEvent()
  73.  
  74.     for index, data in pairs(floorOptions) do
  75.         if redstone.testBundledInput("top", data.floorCable) then
  76.             positionReset = false
  77.         end
  78.     end
  79.  
  80.     if positionReset then
  81.         redstone.setBundledOutput("top", colors.combine(redstone.getBundledOutput("top"), upCable))
  82.     else
  83.         break
  84.     end
  85. end
  86.  
  87. --------------------------------------------------------------------------
  88. --  Center Monitor Text Function
  89. --------------------------------------------------------------------------
  90. local function centerMonitorText(text, xcor, ycor)
  91.     mon.setCursorPos(xcor,ycor)
  92.     local x,y = mon.getSize()
  93.     local x2,y2 = mon.getCursorPos()
  94.  
  95.     if x == 7 or x == 29 or x == 39 then
  96.         mon.setCursorPos(math.ceil((x / 2) - (text:len() / 2)) + 1, y2)
  97.     else
  98.         mon.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  99.     end
  100.  
  101.     mon.write(text)
  102. end
  103.  
  104. --------------------------------------------------------------------------
  105. --  Draw Current Floor Menu Function
  106. --------------------------------------------------------------------------
  107. local function drawCurrentFloorMenu(index)
  108.     mon.clear()
  109.     mon.setCursorPos(0,0)
  110.     mon.setBackgroundColor(colors.black)
  111.  
  112.     local x, y = mon.getSize()
  113.  
  114.     mon.setTextScale(1)
  115.     mon.setCursorPos(1,1)
  116.     mon.write("--------------------------------------------------------------------------------------------------------")
  117.     mon.setCursorPos(x/2-(#floorOptions[index].floorName / 2) + 1, 3)
  118.     mon.write(floorOptions[index].floorName)
  119.     mon.setCursorPos(1,5)
  120.     mon.write("--------------------------------------------------------------------------------------------------------")
  121. end
  122.  
  123. --------------------------------------------------------------------------
  124. --  Draw Call Elevator Menu Function
  125. --------------------------------------------------------------------------
  126. local function drawCallMenu()
  127.     mon.setBackgroundColor(colors.gray)
  128.     centerMonitorText("     ", 2, 8)
  129.     centerMonitorText("Call ", 2, 9)
  130.     centerMonitorText("     ", 2, 10)
  131.     mon.setBackgroundColor(colors.black)
  132. end
  133.  
  134. --------------------------------------------------------------------------
  135. --  Draw Selection Menu Function
  136. --------------------------------------------------------------------------
  137. local function drawSelectMenu()
  138.     local x, y = mon.getSize()
  139.  
  140.     for index, data in pairs(floorOptions) do
  141.         mon.setCursorPos(x/2-(#data.floorName / 2) + 1, 5 + index)
  142.         mon.setBackgroundColor(colors.gray)
  143.         mon.write(data.floorName)
  144.         mon.setBackgroundColor(colors.black)
  145.     end
  146. end
  147.  
  148. --------------------------------------------------------------------------
  149. --  Check Elevator Location
  150. --------------------------------------------------------------------------
  151. local function checkElevatorLocation()
  152.      for index, data in pairs(floorOptions) do
  153.         if redstone.testBundledInput("top", data.floorCable) then
  154.             currentFloorIndex = index
  155.  
  156.             if not(redstone.testBundledInput("top", upCable) or redstone.testBundledInput("top", downCable)) then
  157.                 redstone.setBundledOutput("top", colors.subtract(redstone.getBundledOutput("top"), data.doorCable))
  158.             end
  159.         else
  160.             redstone.setBundledOutput("top", colors.combine(redstone.getBundledOutput("top"), data.doorCable))
  161.         end
  162.     end
  163.  
  164.     drawCurrentFloorMenu(currentFloorIndex)
  165.  
  166.     if not(redstone.testBundledInput("top", upCable) or redstone.testBundledInput("top", downCable)) then
  167.         if redstone.getInput("front") then
  168.             drawSelectMenu()
  169.         else
  170.             drawCallMenu()
  171.         end
  172.     else
  173.         centerMonitorText("Please", 0, 8)
  174.         centerMonitorText("Wait..", 0, 9)
  175.         mon.setBackgroundColor(colors.black)
  176.     end
  177.  
  178.     -- Display up arrow if elevator going up
  179.     if redstone.testBundledInput("top", upCable) then
  180.         centerMonitorText("^", 0, 2)
  181.     end
  182.  
  183.     -- Display down arrow if elevator going down
  184.     if redstone.testBundledInput("top", downCable) then
  185.         centerMonitorText("v", 0, 4)
  186.     end
  187. end
  188.  
  189. --------------------------------------------------------------------------
  190. --  Check Click Function
  191. --------------------------------------------------------------------------
  192. local function checkClick(x,y)
  193.     if redstone.getInput("front") then
  194.         for index, data in pairs(floorOptions) do
  195.             if y == index + 5 then
  196.                 targetFloorIndex = index
  197.                 print(index)
  198.                 os.sleep(2)
  199.             end
  200.         end
  201.     else
  202.         targetFloorIndex = currentComputerIndex
  203.     end
  204.     return false
  205. end
  206.  
  207. checkElevatorLocation()
  208.  
  209. --------------------------------------------------------------------------
  210. --  PROGRAM LOOP
  211. --------------------------------------------------------------------------
  212. while true do
  213.     local e = {os.pullEvent()}
  214.  
  215.     --Check Elevator Location
  216.     checkElevatorLocation()
  217.  
  218.     if not(redstone.testBundledInput("top", upCable) or redstone.testBundledInput("top", downCable)) then
  219.         if e[1] == "monitor_touch" then
  220.             checkClick(e[3], e[4])
  221.         end
  222.     end
  223.  
  224.     if targetFloorIndex ~= -1 and currentFloorIndex ~= targetFloorIndex then
  225.         if currentFloorIndex < targetFloorIndex  then -- Down
  226.             print("Going Down" .. " curIndex: " .. currentFloorIndex .. " tarIndex: " .. targetFloorIndex)
  227.             redstone.setBundledOutput("top", colors.combine(redstone.getBundledOutput("top"), downCable))
  228.         elseif currentFloorIndex > targetFloorIndex  then -- Up
  229.             print("Going Up" .. " curIndex: " .. currentFloorIndex .. " tarIndex: " .. targetFloorIndex)
  230.             redstone.setBundledOutput("top", colors.combine(redstone.getBundledOutput("top"), upCable))
  231.         end
  232.     else -- Stop Elevator
  233.         print("Stopping" .. " curIndex: " .. currentFloorIndex .. " tarIndex: " .. targetFloorIndex)
  234.         targetFloorIndex = -1
  235.         redstone.setBundledOutput("top", colors.subtract(redstone.getBundledOutput("top"), downCable))
  236.         redstone.setBundledOutput("top", colors.subtract(redstone.getBundledOutput("top"), upCable))
  237.     end
  238. end
Add Comment
Please, Sign In to add comment