Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- local PACKET_TYPE_FLOOR_LIST = "PACKET_TYPE_FLOOR_LIST"
- local PACKET_TYPE_REGISTER_FLOOR = "PACKET_TYPE_REGISTER_FLOOR"
- local PACKET_TYPE_GOTO_FLOOR = "PACKET_TYPE_GOTO_FLOOR"
- local PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER = "PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER"
- -- open rednet
- local modem = peripheral.find("modem");
- -- can change if elevator uses different configuration
- local motor = peripheral.wrap("left")
- local minFloorWaitTime = 2
- -- check if settings have already been configured
- if not fs.exists("config.settings") or args[1] == "-config" then
- -- prompt for config
- print("--- ELEVATOR CONFIG ---")
- print("these settings may be changed at any time by editing config.settings")
- print("")
- print("Enter the channel for this elevator to use:")
- settings.set("channel", tonumber(read()))
- print("")
- print("Enter the y coordinate of the floor of the elevator when the rope is fully retracted:")
- settings.set("top", tonumber(read()))
- print("")
- print("Enter the regular travel speed of the elevator:")
- settings.set("speed", tonumber(read()))
- print("")
- print("Enter the maximum speed that the elevator can reliably go:")
- settings.set("maxSpeed", tonumber(read()))
- print("")
- print("saving config...")
- settings.save("config.settings")
- end
- term.clear()
- print("loading config...")
- -- load the settings file
- settings.load("config.settings")
- print("")
- print("reading channel: ")
- local channel = settings.get("channel", 65535)
- print(channel)
- sleep(0.1)
- print("")
- print("reading top: ")
- local top = settings.get("top", 310)
- local currentY = 0
- print(top)
- sleep(0.1)
- print("")
- print("reading speed: ")
- local speed = settings.get("speed", 32)
- print(speed)
- sleep(0.1)
- print("")
- print("reading maxSpeed: ")
- local maxSpeed = settings.get("maxSpeed", 32)
- print(maxSpeed)
- sleep(0.1)
- sleep(0.2)
- term.clear()
- sleep(0.1)
- local numFloors = 0
- local floors = {}
- local moveQueue = {}
- -- now the program actually STARTS...
- -- go to specified y at specified speed
- function goToY(newY, moveSpeed)
- if newY > top then
- deltaY = newY - currentY
- currentY = newY
- sleep(motor.translate(deltaY, moveSpeed))
- else
- print("ERROR: attempt to move to height above max height")
- end
- end
- -- re-center the elevator
- function reCenter()
- currentY = 0
- goToY(top, maxSpeed)
- end
- -- go to the specified floor
- function goToFloor(floorNum)
- if floorNum < numFloors then
- goToY(floors[floorNum], speed)
- else
- print("ERROR: attempt to go to invalid floor")
- end
- end
- function addFloorToQueue(floorNum)
- table.insert(floorQueue, floorNum)
- end
- -- broadcast the list of floors to all button panels
- function broadcastFloorList()
- local msg = {}
- msg.type = PACKET_TYPE_FLOOR_LIST
- msg.floors = floors
- modem.transmit(channel, channel, msg)
- end
- function reRegisterFloors()
- local msg = {}
- msg.type = PACKET_TYPE_REQUEST_FLOOR_RE_REGISTER
- modem.transmit(channel, channel, msg)
- floors = {}
- end
- -- add a floor given a messageData from a PACKET_TYPE_REGISTER_FLOOR packet
- function registerFloor(dat)
- if dat.y == nil then
- print("ERROR: bad packet")
- return
- end
- -- check if the floor already exists
- for i = 0, numFloors do
- if floors[i].y == dat.y then
- return
- end
- end
- floors[numFloors] = dat
- numFloors = numFloors + 1
- -- sort the floors by y axis
- table.sort(floors, function(lhs, rhs)
- return lhs.y < rhs.y
- end)
- broadcastFloorList()
- end
- -- open the modem for channel
- modem.closeAll()
- modem.open(channel)
- -- program loops
- -- messages:
- -- type(string) type of message
- -- data(any) date that the message contains
- -- wait for and process packets from button panels
- function networkPacketManager()
- while true do
- --pull modem_message event
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if message.type ~= nil then
- local msgType = message.type
- local msgData = message.data
- if msgType == PACKET_TYPE_REGISTER_FLOOR then
- registerFloor(msgData)
- elseif msgType == PACKET_TYPE_GOTO_FLOOR then
- if msgData.floorNum ~= nil then
- addFloorToQueue(msgData.floorNum)
- else
- print("ERROR: bad packet")
- end
- end
- else
- print("ERROR: bad packet")
- end
- end while
- end
- -- move the elevator and sutch (must by async ass it will sleep when moving)
- function main()
- while true do
- -- check if there is a floor in the queue
- local numFloorsInQueue = table.getn(floorQueue)
- if numFloorsInQueue ~= 0 then
- local floorNum = floorQueue[0]
- table.remove(floorQueue, 1)
- goToFloor(floorNum)
- -- wait so the player can get on/off the elevator
- sleep(minFloorWaitTime)
- end
- sleep(0)
- end while
- end
- -- requrest all floors be re-registered
- reRegisterFloors()
- parallel.waitForAny(networkPacketManager, main)
- modem.closeAll()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement