Advertisement
Cod0fDuty

floor

Mar 20th, 2022
7,591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.75 KB | None | 0 0
  1. --[[MIT License
  2.  
  3. Copyright (c) 2022 Cod0fDuty
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.]]--
  22.  
  23.  
  24. --If you use this program in a video/post, I would greatly appreciate it if you would credit my Youtube channel https://www.youtube.com/c/Cod0fDuty.
  25. --Please let me know if you have any questions or suggestions, feel free to contact me at [email protected].
  26.  
  27.  
  28. peripheral.find("modem", rednet.open)   --opens a modem
  29. term.clear()
  30. term.setCursorPos(1,1)
  31. print("A ComputerCraft Program by Cod0fDuty")
  32. print("Floor/Call Module")
  33.  
  34. local floor = ""   --empty string, so the while loop below is ignored if the file "info" already exists
  35. local id = ""
  36.  
  37. function getInfo()
  38.    while fs.exists("info") ~= true or floor == nil or id == nil do   --repeats if the file "info" doesn't exist or any of the variables are nil
  39.       term.setCursorPos(1,5)
  40.       term.clearLine()
  41.       term.setCursorPos(1,4)
  42.       term.clearLine()
  43.       local h = fs.open("info", "w")   --opens a file for writing
  44.       term.write("Floor: ")
  45.       floor = tonumber(read())   --string containing non-numeric characters will be converted to nil
  46.       term.write("ID of recipient: ")
  47.       id = tonumber(read())
  48.       h.writeLine(floor)
  49.       h.writeLine(id)
  50.       h.close()
  51.    end
  52. end
  53.  
  54. function readInfo()
  55.    local h = fs.open("info", "r")   --opens a file for reading
  56.    floor = h.readLine()
  57.    id = h.readLine()
  58.    h.close()
  59.    if floor == nil or id == nil then
  60.       fs.delete("info")
  61.       os.reboot()
  62.    end
  63.    floor = tonumber(floor)
  64.    id = tonumber(id)
  65.    term.setCursorPos(1,4)
  66.    print("Floor: " .. floor)
  67.    print("ID of recipient: " .. id)
  68. end
  69.  
  70. function floorModule()
  71.    term.setCursorPos(1,7)
  72.    print("Standby!")
  73.    while true do
  74.       local event, sender, message, protocol = os.pullEvent()   --wait for events
  75.       if event == "redstone" and rs.getInput("front") then   --detects redstone input from front
  76.          rednet.send(id, floor, "elevator")   --sends a message with the protocol "elevator" to the specified computer
  77.       elseif event == "redstone" and rs.getInput("right") or rs.getInput("left") then   --detects redstone input from either left or right
  78.          rednet.send(id, floor, "call")   --sends a message with the protocol "call" to the specified computer
  79.       elseif event == "rednet_message" and message == floor and protocol == tostring(id) then   --detects rednet message with the protocol of the specified id
  80.          rs.setOutput("back", true)   --outputs a 2-tick redstone signal to the back
  81.          sleep(0.1)
  82.          rs.setOutput("back", false)
  83.       end
  84.    end
  85. end
  86.  
  87. function deleteInfo()
  88.    term.setCursorPos(1,8)
  89.    print("Press E to clear data...")
  90.    while true do
  91.       local event, key = os.pullEvent("key")
  92.       if event == "key" and key == keys.e then
  93.          fs.delete("info")   --deletes the file
  94.          term.setCursorPos(1,10)
  95.          print("Cleared!")
  96.          sleep(1)
  97.          os.reboot()   --restarts the program
  98.       end
  99.    end
  100. end
  101.  
  102. getInfo()
  103. readInfo()
  104. parallel.waitForAny(floorModule, deleteInfo)
  105.  
  106. --[[
  107. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  108. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣶⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  109. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⠿⠟⠛⠻⣿⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  110. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣆⣀⣀⠀⣿⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  111. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠻⣿⣿⣿⠅⠛⠋⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  112. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢼⣿⣿⣿⣃⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  113. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣟⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  114. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣛⣛⣫⡄⠀⢸⣦⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  115. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⡆⠸⣿⣿⣿⡷⠂⠨⣿⣿⣿⣿⣶⣦⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  116. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣤⣾⣿⣿⣿⣿⡇⢀⣿⡿⠋⠁⢀⡶⠪⣉⢸⣿⣿⣿⣿⣿⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  117. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣿⣿⡏⢸⣿⣷⣿⣿⣷⣦⡙⣿⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  118. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣿⣿⣿⣿⣿⣿⣇⢸⣿⣿⣿⣿⣿⣷⣦⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  119. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  120. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
  121. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀
  122. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀
  123. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀
  124. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣿⣵⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  125. --]]
  126.  
  127. --https://www.youtube.com/watch?v=dQw4w9WgXcQ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement