Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- peripheral.find("modem", rednet.open) --opens a modem
- local id = os.getComputerID() --gets the unique id of the computer
- term.clear()
- term.setCursorPos(1,1)
- print("A ComputerCraft Program by Cod0fDuty")
- print("Main Elevator Module")
- print("This computer's ID is " .. id)
- local floor = ""
- local current = ""
- local delay = ""
- function getInfo()
- while fs.exists("info") ~= true or floor == nil or current == nil or delay == nil do
- term.setCursorPos(1,7)
- term.clearLine()
- term.setCursorPos(1,6)
- term.clearLine()
- term.setCursorPos(1,5)
- term.clearLine()
- local h = fs.open("info", "w")
- term.write("Floor: ")
- floor = tonumber(read())
- term.write("Current: ")
- current = tonumber(read())
- term.write("Delay: ")
- delay = tonumber(read())
- local infoTable = {floor, current, delay}
- h.write(textutils.serialize(infoTable))
- h.close()
- end
- end
- function readInfo()
- local h = fs.open("info", "r")
- local infoTable = textutils.unserialize(h.readAll())
- if infoTable == nil then
- fs.delete("info")
- os.reboot()
- end
- floor = tonumber(infoTable[1])
- current = tonumber(infoTable[2])
- delay = tonumber(infoTable[3])
- h.close()
- term.setCursorPos(1,5)
- print("Floor: " .. floor)
- print("Current: " .. current)
- print("Delay: " .. delay)
- print("Chosen: ")
- end
- function elevatorModule()
- term.setCursorPos(1,10)
- print("Elevator running!")
- while true do
- local event, sender, message, protocol = os.pullEvent()
- if event == "rednet_message" and protocol == "call" then
- chosen = message
- elseif event == "rednet_message" and protocol == "input" then
- chosen = message
- end
- if (event == "rednet_message" and (protocol == "call" or protocol == "input")) and chosen ~= current and not (chosen > floor) then
- term.setCursorPos(1,8)
- print("Chosen: " .. chosen)
- rednet.broadcast(current, tostring(id)) --broadcasts "current" to all floor modules with the protocol of main computer's id
- sleep(delay) --pauses program for a set amount of time, useful for doors
- if chosen < current then
- rs.setOutput("left", true) --true = rope pulley down, false = rope pulley up
- end
- rs.setOutput("front", true) --outputs a 2-tick redstone signal to the front
- sleep(0.1)
- rs.setOutput("front", false)
- while true do
- local event, sender, message, protocol = os.pullEvent()
- if event == "rednet_message" and protocol == "elevator" then --changes "current" everytime the elevator car passes a floor
- current = message
- if message == chosen then --breaks the loop when the elevator car reaches its destination
- break
- end
- elseif event == "redstone" and rs.getInput("right")then --breaks when car reaches the top floor
- current = floor
- break
- end
- if event == "rednet_message" and protocol == "call" and message > math.min(current, chosen) and message < math.max(current, chosen) then --changes "chosen" to message, without breaking the loop
- chosen = message
- end
- end
- local h = fs.open("info", "w")
- local infoTable = {floor, current, delay}
- h.write(textutils.serialize(infoTable))
- h.close()
- rs.setOutput("left", false) --set left output to false
- rs.setOutput("front", true)
- rednet.broadcast(current, tostring(id))
- term.setCursorPos(1,6)
- print("Current: " .. current)
- end
- end
- end
- function deleteInfo()
- term.setCursorPos(1,11)
- print("Press E to clear data...")
- while true do
- local event, key = os.pullEvent("key")
- if event == "key" and key == keys.e then
- fs.delete("info")
- term.setCursorPos(1,13)
- print("Cleared!")
- sleep(1)
- os.reboot()
- end
- end
- end
- getInfo()
- readInfo()
- parallel.waitForAny(elevatorModule, deleteInfo)
- -- Define a function to get the IDs of the computers in the elevator system
- function getComputerIds()
- local ids = {}
- local peripherals = peripheral.getNames()
- for i, peripheralName in ipairs(peripherals) do
- if peripheral.getType(peripheralName) == "computer" then
- table.insert(ids, peripheralName)
- end
- end
- return ids
- end
- -- Define a function to check the status of a computer
- function checkComputerStatus(computerId)
- if os.ping(computerId) then
- print("Computer " .. computerId .. " is online.")
- else
- print("Computer " .. computerId .. " is offline. Rebooting...")
- os.reboot()
- end
- end
- -- Define a loop to run every 5 minutes and check the status of the computers
- while true do
- local computerIds = getComputerIds()
- for i, computerId in ipairs(computerIds) do
- checkComputerStatus(computerId)
- end
- -- Wait for 5 minutes before checking again
- sleep(300)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement