Advertisement
MasterKyle08

elevator main CC updated

Mar 20th, 2023 (edited)
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.11 KB | None | 0 0
  1. peripheral.find("modem", rednet.open)   --opens a modem
  2. local id = os.getComputerID()   --gets the unique id of the computer
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. print("A ComputerCraft Program by Cod0fDuty")
  6. print("Main Elevator Module")
  7. print("This computer's ID is " .. id)
  8.  
  9. local floor = ""  
  10. local current = ""
  11. local delay = ""
  12.  
  13. function getInfo()
  14.    while fs.exists("info") ~= true or floor == nil or current == nil or delay == nil do  
  15.       term.setCursorPos(1,7)
  16.       term.clearLine()
  17.       term.setCursorPos(1,6)
  18.       term.clearLine()
  19.       term.setCursorPos(1,5)
  20.       term.clearLine()
  21.       local h = fs.open("info", "w")  
  22.       term.write("Floor: ")
  23.       floor = tonumber(read())  
  24.       term.write("Current: ")
  25.       current = tonumber(read())
  26.       term.write("Delay: ")
  27.       delay = tonumber(read())
  28.       local infoTable = {floor, current, delay}
  29.       h.write(textutils.serialize(infoTable))  
  30.       h.close()
  31.    end
  32. end
  33.  
  34. function readInfo()
  35.    local h = fs.open("info", "r")  
  36.    local infoTable = textutils.unserialize(h.readAll())  
  37.    if infoTable == nil then
  38.       fs.delete("info")
  39.       os.reboot()
  40.    end
  41.    floor = tonumber(infoTable[1])
  42.    current = tonumber(infoTable[2])
  43.    delay = tonumber(infoTable[3])
  44.    h.close()
  45.    term.setCursorPos(1,5)
  46.    print("Floor: " .. floor)
  47.    print("Current: " .. current)
  48.    print("Delay: " .. delay)
  49.    print("Chosen: ")
  50. end
  51.  
  52. function elevatorModule()
  53.    term.setCursorPos(1,10)
  54.    print("Elevator running!")
  55.    while true do
  56.       local event, sender, message, protocol = os.pullEvent()  
  57.       if event == "rednet_message" and protocol == "call" then  
  58.          chosen = message
  59.       elseif event == "rednet_message" and protocol == "input" then  
  60.          chosen = message
  61.       end
  62.       if (event == "rednet_message" and (protocol == "call" or protocol == "input"))  and chosen ~= current and not (chosen > floor) then  
  63.          term.setCursorPos(1,8)
  64.          print("Chosen: " .. chosen)  
  65.          rednet.broadcast(current, tostring(id))   --broadcasts "current" to all floor modules with the protocol of main computer's id
  66.          sleep(delay)   --pauses program for a set amount of time, useful for doors
  67.          if chosen < current then
  68.             rs.setOutput("left", true)   --true = rope pulley down, false = rope pulley up
  69.          end
  70.          rs.setOutput("front", true)   --outputs a 2-tick redstone signal to the front
  71.          sleep(0.1)
  72.          rs.setOutput("front", false)
  73.          while true do
  74.             local event, sender, message, protocol = os.pullEvent()
  75.             if event == "rednet_message" and protocol == "elevator" then   --changes "current" everytime the elevator car passes a floor
  76.                current = message
  77.                if message == chosen then   --breaks the loop when the elevator car reaches its destination
  78.                   break
  79.                end
  80.             elseif event == "redstone" and rs.getInput("right")then   --breaks when car reaches the top floor
  81.                current = floor
  82.                break
  83.             end
  84.             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
  85.                chosen = message
  86.             end
  87.          end
  88.          local h = fs.open("info", "w")
  89.          local infoTable = {floor, current, delay}
  90.          h.write(textutils.serialize(infoTable))  
  91.          h.close()
  92.          rs.setOutput("left", false)   --set left output to false
  93.          rs.setOutput("front", true)
  94.          rednet.broadcast(current, tostring(id))
  95.          term.setCursorPos(1,6)
  96.          print("Current: " .. current)  
  97.       end
  98.    end
  99. end
  100.  
  101. function deleteInfo()
  102.    term.setCursorPos(1,11)
  103.    print("Press E to clear data...")
  104.    while true do
  105.       local event, key = os.pullEvent("key")
  106.       if event == "key" and key == keys.e then
  107.          fs.delete("info")  
  108.          term.setCursorPos(1,13)
  109.          print("Cleared!")
  110.          sleep(1)
  111.          os.reboot()  
  112.       end
  113.    end
  114. end
  115.  
  116.  
  117. getInfo()
  118. readInfo()
  119. parallel.waitForAny(elevatorModule, deleteInfo)
  120.  
  121.  
  122. -- Define a function to get the IDs of the computers in the elevator system
  123. function getComputerIds()
  124.   local ids = {}
  125.   local peripherals = peripheral.getNames()
  126.   for i, peripheralName in ipairs(peripherals) do
  127.     if peripheral.getType(peripheralName) == "computer" then
  128.       table.insert(ids, peripheralName)
  129.     end
  130.   end
  131.   return ids
  132. end
  133.  
  134. -- Define a function to check the status of a computer
  135. function checkComputerStatus(computerId)
  136.   if os.ping(computerId) then
  137.     print("Computer " .. computerId .. " is online.")
  138.   else
  139.     print("Computer " .. computerId .. " is offline. Rebooting...")
  140.     os.reboot()
  141.   end
  142. end
  143.  
  144. -- Define a loop to run every 5 minutes and check the status of the computers
  145. while true do
  146.   local computerIds = getComputerIds()
  147.   for i, computerId in ipairs(computerIds) do
  148.     checkComputerStatus(computerId)
  149.   end
  150.  
  151.   -- Wait for 5 minutes before checking again
  152.   sleep(300)
  153. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement