Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local io = require("io")
- local term = require("term")
- local event = require("event")
- local component = require("component")
- local gpu = component.gpu
- local modem = component.modem
- local note = require("note")
- local carriage = component.carriage
- local button = require("buttonAPI")
- local touch = true
- local CALLER_PORT = 3538
- modem.open(3539)
- modem.setStrength(100)
- floors = {}
- numFloors = 3
- floors[1] = {label="Library", y=145}
- floors[2] = {label="Workshop (Ground Level)", y=137}
- floors[3] = {label="Industrial Level", y=127}
- local function persistYLevel()
- local file = io.open("currY", "w")
- file:write(currentYLevel)
- file:close()
- end
- local function loadYLevel()
- local file = io.open("currY", "r")
- local prevY = nil
- if file then
- prevY = file:read()
- if prevY ~= nil then
- prevY = tonumber(prevY)
- end
- end
- return prevY
- end
- local function notifyCallers(status, targetFloor)
- modem.broadcast(CALLER_PORT, status, targetFloor)
- end
- local function move(targetY, direction)
- -- 0 is down, 1 is up
- local errMsg = nil
- local done = (targetY == currentYLevel)
- while(not done) do
- carriage.move(direction, false, false)
- os.sleep(.1)
- local energy, wasSuccess, reason, x, y, z = carriage.status()
- if reason then
- errMsg = reason
- if x then
- errMsg = errMsg.."\nx : "..x
- errMsg = errMsg.."\ny : "..y
- errMsg = errMsg.."\nz : "..z
- end
- break
- end
- if (direction == 0) then
- currentYLevel = currentYLevel - 1
- done = (currentYLevel <= targetY)
- else
- currentYLevel = currentYLevel + 1
- done = (currentYLevel >= targetY)
- end
- end
- persistYLevel()
- return errMsg
- end
- local function moveUp(target)
- return move(target, 1)
- end
- local function moveDown(target)
- return move(target, 0)
- end
- local function goToY(yLevel)
- if yLevel > currentYLevel then
- return moveUp(yLevel)
- else
- if yLevel < currentYLevel then
- return moveDown(yLevel)
- end
- end
- end
- local function goToFloor(floorNum)
- return goToY(floors[floorNum].y)
- end
- local function goToFloorByName(name)
- for k,v in pairs(floors) do
- if (v.label == name) then
- notifyCallers("onroute", name)
- local errMsg = goToY(v.y)
- if not errMsg then
- notifyCallers("arrived", name)
- else
- notifyCallers("arrived", "???")
- end
- return errMsg
- end
- end
- return nil
- end
- local function buttonHighlight(name)
- for k,v in pairs(floors) do
- if v.label == name then
- button.setActive(v.label, true)
- else
- button.setActive(v.label, false)
- end
- end
- end
- local function init()
- currentYLevel = loadYLevel()
- term.clear()
- if (currentYLevel == nil) then
- print("Unable to determine current Y level.")
- while(true) do
- io.write("Please provide correct value: ")
- local userYVal = io.read()
- userYVal = tonumber(userYVal)
- if userYVal == nil then
- print("\nNot a valid height value.")
- else
- currentYLevel = userYVal
- break
- end
- end
- end
- local currFloor = nil
- for k,v in pairs(floors) do
- if v.y == currentYLevel then
- currFloor = k
- end
- end
- if currFloor == nil then
- os.sleep(1)
- notifyCallers("arrived", "???")
- if not touch then
- print("Elevator starting between floors, at height " .. currentYLevel)
- end
- else
- if not touch then
- print("Elevator starting on floor: "..floors[currFloor].label)
- else
- buttonHighlight(floors[currFloor].label)
- end
- os.sleep(1)
- notifyCallers("arrived", floors[currFloor].label)
- end
- end
- local function listAvailableFloors()
- for k,v in pairs(floors) do
- io.write("\n"..k..") ".. v.label)
- if v.y == currentYLevel then
- io.write(" {*}")
- end
- end
- end
- local function mainCLI()
- init()
- while(true) do
- print("\n Available Floors: ")
- listAvailableFloors()
- local selection = nil
- while(true) do
- io.write("\n\nEnter desired floor number: ")
- selection = io.read()
- selection = tonumber(selection)
- if not selection or (selection < 1) or (selection > numFloors) then
- print("\n Invalid floor.")
- else
- break
- end
- end
- term.clear()
- local err = goToFloor(selection)
- if err then
- print(err)
- end
- end
- end
- local function eventFilter(id)
- return (id == "touch" or id == "modem_message")
- end
- local function buttonHandler(name)
- buttonHighlight(name)
- os.sleep(1)
- local err = goToFloorByName(name)
- if(err) then
- button.label(1,34,err)
- else
- note.play(72, 0.3)
- note.play(67, 0.3)
- end
- end
- local function getClickOrUpdate()
- local e, _, x, y, _, floorName = event.pullFiltered(1,eventFilter)
- if (e == "touch") then
- if x == nil or y == nil then
- local h, w = gpu.getResolution()
- gpu.set(h,w,".")
- gpu.set(h,w," ")
- else
- button.checkxy(x,y)
- end
- elseif (e == "modem_message") then
- buttonHandler(floorName)
- end
- end
- local function displayAvailableFloors()
- term.setCursorBlink(false)
- button.clear()
- local minX = 8
- local minY = 3
- local buttonHeight = 2
- local buttonWidth = 24
- local buttonSpacing = 2
- for k,v in pairs(floors) do
- button.setTable(v.label, buttonHandler, minX, minX + buttonWidth, minY, minY + buttonHeight)
- minY = minY + buttonHeight+ buttonSpacing
- end
- button.screen()
- end
- function mainTouch()
- gpu.setResolution(40, 40)
- displayAvailableFloors()
- init()
- button.heading("Please Choose A Floor:")
- while(true) do
- getClickOrUpdate()
- end
- end
- if touch then
- mainTouch()
- else
- mainCLI()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement