Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SkunkWorks Logistics
- -- Elevator control API
- -- Defining all common functions and variables
- elevCtrl = "right" -- Bundle leading to elevator controls
- doorCtrl = "back" -- Bundle leading to door controls
- liteCtrl = "bottom" -- Bundle for controlling call lights
- incSig = "left" -- Bundle leading to call signals
- monitor = "top" -- Local monitor location
- elevHome = 1 -- From the bottom, which floor is home? Will be significant in later development
- elevFloor = 1 -- Elevator's current position, by floor. Used only for signaling/door control purposes, not motion.
- elevPos = 1 -- Elevator's current position, in BLOCKS, not floors
- floorLev = {1, 7, 13} -- Table of floor elevations.
- elevUp = colors.white -- Upwards frame motor
- elevDown = colors.orange -- Downwards frame moter
- elevDep = colors.magenta -- Frame deployer
- elevBrk = colors.lightBlue -- Frame breaker
- floorCol = {colors.orange, colors.magenta, colors.lightBlue} -- Set up the table of per-floor color codes
- maint = colors.white -- Motor room door and calibration remote
- floorCol[0] = maint -- Put it into the main array as floor 0
- doorsOpen = {true, false, false} -- Default configuration starts on floor 1 with that door (only) open
- doorsOpen[0] = false -- Maintenance room door
- -- Note that the first attempt to open/close a door will put it in sync, as it explicitly turns channels on/off
- function elevLower( times )
- for i = 1, times do
- rs.setBundledOutput(elevCtrl, elevDown)
- sleep(0.1)
- rs.setBundledOutput(elevCtrl, 0)
- elevPos = elevPos - 1
- sleep(0.7)
- rs.setBundledOutput(elevCtrl, elevBrk)
- sleep(0.1)
- rs.setBundledOutput(elevCtrl, 0)
- sleep(0.1)
- end
- end
- function elevRaise( times )
- for i = 1, times do
- rs.setBundledOutput(elevCtrl, elevDep)
- sleep(0.1)
- rs.setBundledOutput(elevCtrl, elevUp)
- elevPos = elevPos + 1
- sleep(0.1)
- rs.setBundledOutput(elevCtrl, 0)
- sleep(0.8)
- end
- end
- function monPrint( msg )
- term.redirect(mon)
- print(msg)
- term.restore()
- end
- function toggleMaint() -- Maintenance room can be accessed by the appropriate remote
- local doors = rs.getBundledOutput(doorCtrl)
- if doorsOpen[0] then -- Maintenance door is already open
- doors = colors.subtract(doors, maint)
- rs.setBundledOutput(doorCtrl, doors)
- lightOff(0) -- Maintenance lights indicate door status alone
- doorsOpen[0] = false
- else
- doors = colors.combine(doors, maint)
- rs.setBundledOutput(doorCtrl, doors)
- lightOn(0) -- Shows the door's open and/or gives light to see by
- doorsOpen[0] = true
- end
- end
- function doorClose(floor) -- Technically works for maintenance room, but see toggleMaint()
- local doors=rs.getBundledOutput(doorCtrl)
- doors = colors.subtract(doors,floorCol[floor])
- rs.setBundledOutput(doorCtrl, doors)
- doorsOpen[floor]=false
- end
- function doorOpen(floor) -- See notes on doorClose()
- local doors=rs.getBundledOutput(doorCtrl)
- doors = colors.combine(doors,floorCol[floor])
- rs.setBundledOutput(doorCtrl, doors)
- doorsOpen[floor]=true
- end
- function lightOn(floor)
- local lights=rs.getBundledOutput(liteCtrl)
- lights=colors.combine(lights,floorCol[floor])
- rs.setBundledOutput(liteCtrl, lights)
- end
- function lightOff(floor)
- local lights=rs.getBundledOutput(liteCtrl)
- lights=colors.subtract(lights,floorCol[floor])
- rs.setBundledOutput(liteCtrl, lights)
- end
- function lightWarn(floor) -- Used to warn the floor the elevator's at that it's closing and preparing to move
- for i=1, 4 do
- lightOn(floor)
- sleep(0.25)
- lightOff(floor)
- sleep(0.25)
- end
- end
- function moveToFloor(target) -- Move elevator to the given floor in the array
- -- Returns success/failure as a Boolean, then how far it moved as an int
- target = math.floor(target) -- Ensure we're working with an integer value
- if target == 0 then -- Floors are numbered from 1, so 0 is out of bounds
- return false, 0
- elseif target > #floorLev then -- Listing a floor that's beyond the reach of the array
- return false, 0
- else
- local newPos = floorLev[target]
- if newPos == elevPos then -- Already there
- return true, 0
- elseif newPos > elevPos then -- We need to go up
- elevRaise(newPos - elevPos) -- Move the difference
- return true, newPos - elevPos -- Upwards is a positive change
- else -- We must need to go down
- elevLower(elevPos - newPos) -- Move the difference
- return true, newPos - elevPos -- Downwards is a negative change
- end
- end
- end
- function elevCall(target) -- This floor has called, or the up/down buttons been pressed on floor below/above
- if target == 0 then -- Out of bounds. Maintenance room can't call the elevator.
- return false, 0
- elseif target > #floorLev then -- Too high. Shouldn't happen.
- return false, 0
- else
- local newPos = floorLev[target]
- if elevPos == newPos then -- Are we already there?
- elevFloor = target -- Update the elevator's floor location, just in case
- return true, 0 -- We're done
- else
- lightOn(target)
- lightWarn(elevFloor)
- doorClose(elevFloor)
- local succ=moveToFloor(target)
- lightOff(target)
- doorOpen(target)
- elevFloor = target
- return succ -- Pass along the output of moveToFloor()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement