Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DEF_FLOORS = { -- {Numerical Value, Assigned Redpower Color, etc}
- --["Basement_Two] = {},
- --["Basement_One] = {},
- ["First_Floor"] = {1,colors.black,"monitor_5",peripheral.wrap("monitor_5"),peripheral.wrap("speaker_2"),7,8},
- ["Second_Floor"] = {2,colors.green,"monitor_4",peripheral.wrap("monitor_4"),peripheral.wrap("speaker_3"),7,7},
- ["Third_Floor"] = {3,colors.lightBlue,"monitor_3",peripheral.wrap("monitor_3"),peripheral.wrap("speaker_4"),7,6},
- ["Fourth_Floor"] = {4,colors.white,"monitor_2",peripheral.wrap("monitor_2"),peripheral.wrap("speaker_1"),7,5},
- }
- local currentFloor
- local activeWire = 16 -- Safe guard
- local activeWire = redstone.getBundledInput("back")
- local touchSensitivity = 1
- local floorRequest
- --local activeSignal = tostring(redstone.getBundledInput("left"))
- function playElevatorMusic()
- for k,v in pairs(DEF_FLOORS) do
- print("Attempting to play music at:",k)
- local speaker = DEF_FLOORS[k][5]
- speaker.playSound("music_disc.chirp",.25)
- end
- end
- function stopElevatorMusic()
- for k,v in pairs(DEF_FLOORS) do
- local speaker = DEF_FLOORS[k][5]
- speaker.stop()
- end
- end
- function findVariableFromSide(side)
- local side = tostring(side)
- for k,v in pairs(DEF_FLOORS) do -- Go through the values
- for k2,v2 in ipairs(DEF_FLOORS[k]) do -- Go through arrays
- if k2 == 3 then
- if side == v2 then
- --print("Found key at",k)
- return tostring(k)
- end
- end
- end
- end
- --("Done with findVariableSide!")
- end
- function findVariableFromNumber(number)
- -- local side = tostring(side)
- for k,v in pairs(DEF_FLOORS) do -- Go through the values
- for k2,v2 in ipairs(DEF_FLOORS[k]) do -- Go through arrays
- if k2 == 1 then
- if number == v2 then
- --print("Found key at",k)
- return tostring(k)
- end
- end
- end
- end
- --print("Done with findVariableNumber!")
- end
- function openDoor()
- redstone.setOutput("right",true)
- end
- function closeDoor()
- redstone.setOutput("right",false)
- end
- function goingUp() -- Signal should be on
- if redstone.testBundledInput("back",colors.purple) then
- --print("goingUp is already active!")
- else
- activeWire = activeWire + colors.purple
- redstone.setBundledOutput("back",activeWire)
- end
- end
- function goingDown() -- Signal should be off
- if redstone.testBundledInput("back",colors.purple) then
- activeWire = activeWire - colors.purple
- redstone.setBundledOutput("back",activeWire)
- else
- --print("goingDown is already active!")
- end
- end
- function enableClutch()
- if redstone.testBundledInput("back",colors.yellow) then
- --print("Clutch is already Enabled!")
- else
- --print("Clutch Enabled!")
- activeWire = activeWire + colors.yellow
- redstone.setBundledOutput("back",activeWire)
- end
- end
- function disableClutch()
- if redstone.testBundledInput("back",colors.yellow) then
- --print("Clutch Disabled!")
- activeWire = activeWire - colors.yellow
- redstone.setBundledOutput("back",activeWire)
- else
- --print("Clutch is already disabled!")
- end
- end
- function getCurrentFloor()
- local activeSignal = tostring(redstone.getBundledInput("left"))
- -- print(activeSignal) -- Should expect a number
- for k,v in pairs(DEF_FLOORS) do
- for k2,v2 in ipairs(DEF_FLOORS[k]) do
- if k2 == 2 then
- if activeSignal == tostring(v2) then
- --print("Currently at:",k)
- return k
- end
- end
- end
- end
- end
- function travelTo(floor)
- -- Get current floor and convert it to a number
- local selectedFloor = DEF_FLOORS[floor][1]
- local currentFloor = DEF_FLOORS[getCurrentFloor()][1]
- -- local selectedFloor = floor
- local waitForColor
- --playElevatorMusic()
- closeDoor()
- sleep(1)
- --print("Current Floor:",currentFloor)
- -- Determine if floor is above or below
- if currentFloor > selectedFloor then
- --print("I should go down!")
- goingDown()
- disableClutch()
- elseif currentFloor < selectedFloor then
- --print("I should go up!")
- goingUp()
- disableClutch()
- else
- --print("I'm at the selectedFloor!")
- return
- enableClutch()
- end
- -- Assign destination
- for k,v in pairs(DEF_FLOORS) do
- for k2,v2 in ipairs(DEF_FLOORS[k]) do
- if k2 == 1 then
- print("k2:",k2,"v2:",v2)
- if selectedFloor == v2 then
- waitForColor = DEF_FLOORS[k][2]
- --print("Assigning waitForColor variable to:",waitForColor)
- end
- end
- end
- end
- -- enableClutch()
- local search = true
- while search do
- local event = os.pullEvent("redstone")
- if event then
- --print("Redstone event detected!")
- --print("Waiting for color",waitForColor)
- if redstone.testBundledInput("left",waitForColor) then
- --print("Found floor!")
- search = false
- enableClutch()
- end
- end
- end
- --print(findVariableFromNumber(selectedFloor))
- --stopElevatorMusic()
- --chime(findVariableFromNumber(selectedFloor))
- return
- end
- function drawScreenRequest(floor)
- local mon = DEF_FLOORS[floor][4]
- local oldTerm = term.redirect(mon)
- mon.setTextScale(.5)
- term.clear()
- term.setCursorPos(4,1)
- term.write("The Oilrig")
- term.setCursorPos(4,4)
- term.write("[Tap Here]")
- term.setCursorPos(4,5)
- term.write("[ For ]")
- term.setCursorPos(4,6)
- term.write("[Elevator]")
- --term.setCursorPos(5,3)
- --term.write("\30 \31")
- term.redirect(oldTerm)
- return
- end
- function drawScreenSelect(floor,selected)
- if selected == nil then
- selected = 0
- end
- local mon = DEF_FLOORS[floor][4]
- local oldTerm = term.redirect(mon)
- mon.setTextScale(.5)
- term.clear()
- term.setCursorPos(2,1)
- term.write("Select Floor")
- term.setCursorPos(DEF_FLOORS["Fourth_Floor"][6],DEF_FLOORS["Fourth_Floor"][7])
- 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
- term.setCursorPos(DEF_FLOORS["Third_Floor"][6],DEF_FLOORS["Third_Floor"][7])
- 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
- term.setCursorPos(DEF_FLOORS["Second_Floor"][6],DEF_FLOORS["Second_Floor"][7])
- 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
- term.setCursorPos(DEF_FLOORS["First_Floor"][6],DEF_FLOORS["First_Floor"][7])
- 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
- term.redirect(oldTerm)
- end
- function chime(floor)
- local speaker = DEF_FLOORS[floor][5] -- Find speaker in index
- speaker.playNote("pling",0.5,23)
- sleep(.35)
- speaker.playNote("pling",0.5,16)
- end
- function buttonClick(floor)
- local speaker = DEF_FLOORS[floor][5] -- Find speaker in index
- speaker.playNote("hat",0.5,10)
- end
- function waitForSelect(floor) -- Wait's for user to request a cabin and request's for a floor selection
- while true do
- drawScreenSelect(floor)
- local event,side,xPos,yPos = os.pullEvent("monitor_touch")
- local floor = findVariableFromSide(tostring(side))
- print(event,"=> Side:",floor,",","X:",tostring(xPos),"Y:",tostring(yPos))
- buttonClick(floor)
- 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
- --print("First_Floor Selected") -- 7,6
- drawScreenSelect(floor,1)
- floorRequest = "First_Floor"
- break
- 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
- --print("Second_Floor Selected")
- drawScreenSelect(floor,2)
- floorRequest = "Second_Floor"
- break
- 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
- --print("Third_Floor selected")
- drawScreenSelect(floor,3)
- floorRequest = "Third_Floor"
- break
- 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
- --print("Fourth_Floor selected")
- drawScreenSelect(floor,4)
- floorRequest = "Fourth_Floor"
- break
- else
- print("Nothing Selected, retrying...")
- end
- -- Draw something so user know's input was accepted
- end
- return
- end
- function waitForRequest() -- Wait's for user to request a cabin and request's for a floor selection
- local event,side,xPos,yPos = os.pullEvent("monitor_touch")
- local floor = findVariableFromSide(tostring(side))
- print(event,"=> Side:",floor,",","X:",tostring(xPos),"Y:",tostring(yPos))
- chime(floor)
- playElevatorMusic()
- parallel.waitForAll(function() travelTo(floor) end,function() waitForSelect(floor) end)
- openDoor()
- sleep(3)
- closeDoor()
- return floor
- end
- -- MAIN CODE BEGINS --
- stopElevatorMusic()
- shell.run("reset")
- sleep(10)
- print("Elevator Active")
- enableClutch()
- openDoor()
- while true do
- for k,v in pairs(DEF_FLOORS) do
- drawScreenRequest(k)
- end
- waitForRequest()
- --playElevatorMusic()
- travelTo(floorRequest)
- sleep(.5)
- stopElevatorMusic()
- chime(getCurrentFloor())
- openDoor()
- sleep(3)
- closeDoor()
- --drawScreenSelect(atFloor)
- --parallel.waitForAny(waitForRequest())
- end
- print("Done")
- -- MAIN CODE ENDS--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement