Advertisement
ColdIV

doorLock

Jul 12th, 2021 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1. local args = {...}
  2. local protocol = "doorOpener"
  3. local hostname = "doorLock"
  4.  
  5. function main ()
  6.     local detector = peripheral.find("playerDetector")
  7.     rednet.host(protocol, hostname)
  8.    
  9.     parallel.waitForAny(
  10.         -- physical
  11.         function ()
  12.             while true do              
  13.                 -- player input
  14.                 term.clear()
  15.                 term.setCursorPos(1, 1)
  16.                 print ("Password: ")
  17.                 input = read("*")
  18.                 if input == "open" then
  19.                     redstone.setOutput("bottom", true)
  20.                     sleep(3)
  21.                     redstone.setOutput("bottom", false)
  22.                 end
  23.             end
  24.         end,
  25.         -- player detector
  26.         function ()
  27.             while true do
  28.                 local playerInRange = function (range)
  29.                     local players = detector.getPlayersInRange(range)
  30.                     return #players > 0
  31.                 end
  32.                
  33.                 if detector ~= nil then
  34.                     if playerInRange(3) then
  35.                         redstone.setOutput("bottom", true)
  36.                     else
  37.                         redstone.setOutput("bottom", false)
  38.                     end
  39.                 end
  40.             end
  41.         end,
  42.         -- rednet recv
  43.         function ()
  44.             while true do
  45.                 if (peripheral.isPresent("back")) then
  46.                     _, message, _ = rednet.receive(protocol)
  47.                     local m = textutils.unserialize(message)
  48.                     local x, y, z = gps.locate()
  49.                     if m.x and m.y and m.z and math.floor(y) == math.floor(m.y) then
  50.                         if math.floor(math.sqrt(math.pow(m.x - x, 2) + math.pow(m.z - z, 2))) < 10 then
  51.                             redstone.setOutput("bottom", true)
  52.                             sleep(5)
  53.                             redstone.setOutput("bottom", false)
  54.                         end
  55.                     end
  56.                 end
  57.             end
  58.         end
  59.     )
  60.    
  61.    
  62.     rednet.unhost(protocol)
  63. end
  64.  
  65. if args[1] == "update" then
  66.     shell.run("pastebin", "run", "FuQ3WvPs B0699K9y doorLock")
  67. else
  68.     peripheral.wrap("top")
  69.     if (peripheral.isPresent("back")) then
  70.         rednet.open("back")
  71.     end
  72.     main()
  73.     if (peripheral.isPresent("back")) then
  74.         rednet.close("back")
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement