FriedToes

Floor Main

Jun 24th, 2022 (edited)
442
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None
  1. term.clear()
  2.  
  3. local id = os.getComputerID()
  4. local modem = peripheral.find("modem") or error("No modem attached", 0)
  5. modem.open(id)
  6.  
  7.  
  8. function requestData()
  9.     while fs.exists("floorData.txt") ~= true do
  10.         term.setCursorPos(1, 4)
  11.         term.write("Floor Number: ")
  12.         floor = tonumber(read())
  13.  
  14.         term.setCursorPos(1, 5)
  15.         term.write("ID Of Elevator Control: ")
  16.         originalComputerID = tonumber(read())
  17.  
  18.         local finalData = {floor, originalComputerID}
  19.         term.setCursorPos(1, 6)
  20.         term.write("Final Data = " .. textutils.serialise(finalData))
  21.  
  22.         if tonumber(floor) ~= nil and tonumber(originalComputerID) ~= nil then
  23.             local file = fs.open("floorData.txt", "w")
  24.  
  25.             file.writeLine(floor)
  26.             file.writeLine(originalComputerID)
  27.  
  28.             file.close()
  29.         end
  30.  
  31.     end
  32. end
  33.  
  34. function readData()
  35.     if fs.exists("floorData.txt") then
  36.         local file = fs.open("floorData.txt", "r")
  37.         importantData = {}
  38.  
  39.         while true do
  40.             local line = file.readLine()
  41.  
  42.             if not line then break end
  43.  
  44.             importantData[#importantData + 1] = tonumber(line)
  45.         end
  46.         file.close()
  47.  
  48.  
  49.         term.setCursorPos(1, 9)
  50.         term.write("Floor: " .. importantData[1])
  51.  
  52.         term.setCursorPos(1, 10)
  53.         term.write("ID: " .. importantData[2])
  54.     end
  55. end
  56.  
  57.  
  58. function floorMain()
  59.     term.setCursorPos(1, 15)
  60.     term.write("Sending Data...")
  61.     while true do
  62.         local eventData = {os.pullEvent()}
  63.         local event = eventData[1]
  64.  
  65.  
  66.         if event == "redstone" and (redstone.getInput("front") or redstone.getInput("left") or redstone.getInput("right")) then
  67.             -- Request elevator on redstone pulse on sides
  68.             print("elevator requested")
  69.             modem.transmit(importantData[2], importantData[1], "call " .. importantData[1])
  70.  
  71.         elseif event == "redstone" and (redstone.getInput("bottom") or redstone.getInput("top"))  then
  72.              -- Send data when on floor
  73.             print("elevator reached floor " .. importantData[1])
  74.  
  75.             -- reference   transmit(send to, reply, message)
  76.             modem.transmit(importantData[2], importantData[1], "normal " .. importantData[1])
  77.         end
  78.  
  79.     end
  80.  
  81. end
  82.  
  83.  
  84. requestData()
  85. readData()
  86. floorMain()
RAW Paste Data Copied