Advertisement
chopstyix

Elevator_Startup

Feb 1st, 2022 (edited)
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.70 KB | None | 0 0
  1. local DEF_FLOORS = { -- {Numerical Value, Assigned Redpower Color, etc}
  2.   --["Basement_Two] = {},
  3.   --["Basement_One] = {},
  4.   ["First_Floor"] = {1,colors.black,"monitor_5",peripheral.wrap("monitor_5"),peripheral.wrap("speaker_2"),7,8},
  5.   ["Second_Floor"] = {2,colors.green,"monitor_4",peripheral.wrap("monitor_4"),peripheral.wrap("speaker_3"),7,7},
  6.   ["Third_Floor"] = {3,colors.lightBlue,"monitor_3",peripheral.wrap("monitor_3"),peripheral.wrap("speaker_4"),7,6},
  7.   ["Fourth_Floor"] = {4,colors.white,"monitor_2",peripheral.wrap("monitor_2"),peripheral.wrap("speaker_1"),7,5},
  8.  
  9. }
  10. local currentFloor
  11. local activeWire = 16 -- Safe guard
  12. local activeWire = redstone.getBundledInput("back")
  13. local touchSensitivity = 1
  14. local floorRequest
  15. --local activeSignal = tostring(redstone.getBundledInput("left"))
  16. function playElevatorMusic()
  17.   for k,v in pairs(DEF_FLOORS) do
  18.     print("Attempting to play music at:",k)
  19.     local speaker = DEF_FLOORS[k][5]
  20.     speaker.playSound("music_disc.chirp",.25)
  21.   end
  22. end
  23.  
  24. function stopElevatorMusic()
  25.   for k,v in pairs(DEF_FLOORS) do
  26.     local speaker = DEF_FLOORS[k][5]
  27.     speaker.stop()
  28.   end
  29. end
  30.  
  31. function findVariableFromSide(side)
  32.   local side = tostring(side)
  33.   for k,v in pairs(DEF_FLOORS) do -- Go through the values
  34.     for k2,v2 in ipairs(DEF_FLOORS[k]) do -- Go through arrays
  35.       if k2 == 3 then
  36.         if side == v2 then
  37.           --print("Found key at",k)
  38.           return tostring(k)
  39.         end
  40.       end
  41.     end
  42.   end
  43.   --("Done with findVariableSide!")
  44. end
  45.  
  46. function findVariableFromNumber(number)
  47.   -- local side = tostring(side)
  48.   for k,v in pairs(DEF_FLOORS) do -- Go through the values
  49.     for k2,v2 in ipairs(DEF_FLOORS[k]) do -- Go through arrays
  50.       if k2 == 1 then
  51.         if number == v2 then
  52.           --print("Found key at",k)
  53.           return tostring(k)
  54.         end
  55.       end
  56.     end
  57.   end
  58.   --print("Done with findVariableNumber!")
  59. end
  60.  
  61. function openDoor()
  62.   redstone.setOutput("right",true)
  63. end
  64.  
  65. function closeDoor()
  66.   redstone.setOutput("right",false)
  67. end
  68.  
  69. function goingUp() -- Signal should be on
  70.   if redstone.testBundledInput("back",colors.purple) then
  71.     --print("goingUp is already active!")
  72.   else  
  73.     activeWire = activeWire + colors.purple
  74.     redstone.setBundledOutput("back",activeWire)
  75.   end
  76. end
  77.  
  78. function goingDown() -- Signal should be off
  79.   if redstone.testBundledInput("back",colors.purple) then
  80.     activeWire = activeWire - colors.purple
  81.     redstone.setBundledOutput("back",activeWire)
  82.   else
  83.     --print("goingDown is already active!")
  84.   end
  85. end
  86.  
  87. function enableClutch()
  88.   if redstone.testBundledInput("back",colors.yellow) then
  89.     --print("Clutch is already Enabled!")
  90.   else
  91.     --print("Clutch Enabled!")
  92.     activeWire = activeWire + colors.yellow  
  93.     redstone.setBundledOutput("back",activeWire)
  94.   end
  95. end
  96.  
  97. function disableClutch()
  98.   if redstone.testBundledInput("back",colors.yellow) then
  99.     --print("Clutch Disabled!")
  100.     activeWire = activeWire - colors.yellow
  101.     redstone.setBundledOutput("back",activeWire)
  102.   else
  103.     --print("Clutch is already disabled!")
  104.   end
  105. end
  106.  
  107. function getCurrentFloor()
  108.   local activeSignal = tostring(redstone.getBundledInput("left"))
  109.   -- print(activeSignal) -- Should expect a number
  110.   for k,v in pairs(DEF_FLOORS) do
  111.     for k2,v2 in ipairs(DEF_FLOORS[k]) do
  112.       if k2 == 2 then
  113.         if activeSignal == tostring(v2) then
  114.           --print("Currently at:",k)
  115.           return k
  116.         end
  117.       end      
  118.     end
  119.   end  
  120. end
  121.  
  122. function travelTo(floor)
  123.   -- Get current floor and convert it to a number
  124.   local selectedFloor = DEF_FLOORS[floor][1]
  125.   local currentFloor = DEF_FLOORS[getCurrentFloor()][1]
  126.   -- local selectedFloor = floor
  127.   local waitForColor
  128.  
  129.   --playElevatorMusic()
  130.   closeDoor()
  131.   sleep(1)
  132.  
  133.   --print("Current Floor:",currentFloor)
  134.   -- Determine if floor is above or below
  135.   if currentFloor > selectedFloor then
  136.     --print("I should go down!")
  137.     goingDown()
  138.     disableClutch()
  139.   elseif currentFloor < selectedFloor then
  140.     --print("I should go up!")
  141.     goingUp()
  142.     disableClutch()
  143.   else
  144.     --print("I'm at the selectedFloor!")
  145.     return
  146.     enableClutch()
  147.   end
  148.   -- Assign destination
  149.   for k,v in pairs(DEF_FLOORS) do
  150.     for k2,v2 in ipairs(DEF_FLOORS[k]) do
  151.       if k2 == 1 then
  152.         print("k2:",k2,"v2:",v2)
  153.         if selectedFloor == v2 then
  154.           waitForColor = DEF_FLOORS[k][2]
  155.           --print("Assigning waitForColor variable to:",waitForColor)
  156.         end
  157.       end
  158.     end
  159.   end
  160.  
  161.   -- enableClutch()
  162.   local search = true
  163.   while search do
  164.     local event = os.pullEvent("redstone")
  165.     if event then  
  166.       --print("Redstone event detected!")  
  167.       --print("Waiting for color",waitForColor)
  168.       if redstone.testBundledInput("left",waitForColor) then
  169.         --print("Found floor!")
  170.         search = false
  171.         enableClutch()
  172.       end
  173.     end  
  174.   end
  175.   --print(findVariableFromNumber(selectedFloor))
  176.   --stopElevatorMusic()
  177.   --chime(findVariableFromNumber(selectedFloor))
  178.   return
  179. end
  180.  
  181. function drawScreenRequest(floor)
  182.   local mon = DEF_FLOORS[floor][4]
  183.   local oldTerm = term.redirect(mon)
  184.   mon.setTextScale(.5)
  185.   term.clear()
  186.   term.setCursorPos(4,1)
  187.   term.write("The Oilrig")
  188.   term.setCursorPos(4,4)
  189.   term.write("[Tap Here]")
  190.   term.setCursorPos(4,5)
  191.   term.write("[  For   ]")
  192.   term.setCursorPos(4,6)
  193.   term.write("[Elevator]")
  194.   --term.setCursorPos(5,3)
  195.   --term.write("\30 \31")
  196.   term.redirect(oldTerm)
  197.   return
  198. end
  199.  
  200. function drawScreenSelect(floor,selected)
  201.   if selected == nil then
  202.     selected = 0
  203.   end
  204.   local mon = DEF_FLOORS[floor][4]
  205.   local oldTerm = term.redirect(mon)
  206.   mon.setTextScale(.5)
  207.   term.clear()
  208.   term.setCursorPos(2,1)
  209.   term.write("Select Floor")
  210.   term.setCursorPos(DEF_FLOORS["Fourth_Floor"][6],DEF_FLOORS["Fourth_Floor"][7])
  211.   if selected == 4 then term.setCursorPos(DEF_FLOORS["Fourth_Floor"][6]-1,DEF_FLOORS["Fourth_Floor"][7]) term.write(">[4]<") else term.write("[4]") end
  212.   term.setCursorPos(DEF_FLOORS["Third_Floor"][6],DEF_FLOORS["Third_Floor"][7])
  213.   if selected == 3 then term.setCursorPos(DEF_FLOORS["Third_Floor"][6]-1,DEF_FLOORS["Third_Floor"][7]) term.write(">[3]<") else term.write("[3]") end
  214.   term.setCursorPos(DEF_FLOORS["Second_Floor"][6],DEF_FLOORS["Second_Floor"][7])
  215.   if selected == 2 then term.setCursorPos(DEF_FLOORS["Second_Floor"][6]-1,DEF_FLOORS["Second_Floor"][7]) term.write(">[2]<") else term.write("[2]") end
  216.   term.setCursorPos(DEF_FLOORS["First_Floor"][6],DEF_FLOORS["First_Floor"][7])
  217.   if selected == 1 then term.setCursorPos(DEF_FLOORS["First_Floor"][6]-1,DEF_FLOORS["First_Floor"][7]) term.write(">[1]<") else term.write("[1]") end
  218.   term.redirect(oldTerm)
  219. end
  220.  
  221. function chime(floor)
  222.   local speaker = DEF_FLOORS[floor][5] -- Find speaker in index
  223.   speaker.playNote("pling",0.5,23)
  224.   sleep(.35)
  225.   speaker.playNote("pling",0.5,16)
  226. end
  227.  
  228. function buttonClick(floor)
  229.   local speaker = DEF_FLOORS[floor][5] -- Find speaker in index
  230.   speaker.playNote("hat",0.5,10)
  231. end
  232.  
  233.  
  234. function waitForSelect(floor) -- Wait's for user to request a cabin and request's for a floor selection
  235.   while true do
  236.     drawScreenSelect(floor)
  237.     local event,side,xPos,yPos = os.pullEvent("monitor_touch")
  238.     local floor = findVariableFromSide(tostring(side))
  239.     print(event,"=> Side:",floor,",","X:",tostring(xPos),"Y:",tostring(yPos))
  240.     buttonClick(floor)
  241.     if (xPos > (DEF_FLOORS["First_Floor"][6]-touchSensitivity)) and (xPos < (DEF_FLOORS["First_Floor"][6]+3+touchSensitivity)) and yPos == (DEF_FLOORS["First_Floor"][7]) then
  242.       --print("First_Floor Selected") -- 7,6
  243.       drawScreenSelect(floor,1)
  244.       floorRequest = "First_Floor"
  245.       break
  246.     elseif (xPos > (DEF_FLOORS["Second_Floor"][6]-touchSensitivity)) and (xPos < (DEF_FLOORS["Second_Floor"][6]+3+touchSensitivity)) and yPos == (DEF_FLOORS["Second_Floor"][7]) then
  247.       --print("Second_Floor Selected")
  248.       drawScreenSelect(floor,2)  
  249.       floorRequest = "Second_Floor"
  250.       break
  251.     elseif (xPos > (DEF_FLOORS["Third_Floor"][6]-touchSensitivity)) and (xPos < (DEF_FLOORS["Third_Floor"][6]+3+touchSensitivity)) and yPos == (DEF_FLOORS["Third_Floor"][7]) then
  252.       --print("Third_Floor selected")
  253.       drawScreenSelect(floor,3)
  254.       floorRequest = "Third_Floor"
  255.       break
  256.     elseif (xPos > (DEF_FLOORS["Fourth_Floor"][6]-touchSensitivity)) and (xPos < (DEF_FLOORS["Fourth_Floor"][6]+3+touchSensitivity)) and yPos == (DEF_FLOORS["Fourth_Floor"][7]) then
  257.       --print("Fourth_Floor selected")
  258.       drawScreenSelect(floor,4)
  259.       floorRequest = "Fourth_Floor"
  260.       break
  261.     else
  262.       print("Nothing Selected, retrying...")  
  263.     end
  264.     -- Draw something so user know's input was accepted
  265.   end
  266.   return
  267. end
  268.  
  269. function waitForRequest() -- Wait's for user to request a cabin and request's for a floor selection
  270.   local event,side,xPos,yPos = os.pullEvent("monitor_touch")
  271.   local floor = findVariableFromSide(tostring(side))
  272.   print(event,"=> Side:",floor,",","X:",tostring(xPos),"Y:",tostring(yPos))
  273.   chime(floor)
  274.   playElevatorMusic()
  275.   parallel.waitForAll(function() travelTo(floor) end,function() waitForSelect(floor) end)
  276.   openDoor()
  277.   sleep(3)
  278.   closeDoor()
  279.   return floor
  280. end
  281. -- MAIN CODE BEGINS --
  282. stopElevatorMusic()
  283. shell.run("reset")
  284. sleep(10)
  285. print("Elevator Active")
  286.  
  287. enableClutch()
  288.  
  289. openDoor()
  290.  
  291. while true do
  292.   for k,v in pairs(DEF_FLOORS) do
  293.     drawScreenRequest(k)
  294.   end
  295.   waitForRequest()
  296.   --playElevatorMusic()
  297.   travelTo(floorRequest)
  298.   sleep(.5)
  299.   stopElevatorMusic()
  300.   chime(getCurrentFloor())
  301.   openDoor()
  302.   sleep(3)
  303.   closeDoor()
  304.   --drawScreenSelect(atFloor)
  305.   --parallel.waitForAny(waitForRequest())
  306. end
  307.  
  308. print("Done")
  309.  
  310. -- MAIN CODE ENDS--
  311.  
  312.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement