Advertisement
Guest User

dc2

a guest
Aug 22nd, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local range = 1
  2. local height = 3
  3. local doorx = nil
  4. local doory = nil
  5. local doorz = nil
  6. local doorlog = 0
  7. local users = {}
  8. local close = 0
  9. rednet.open("bottom")
  10.  
  11. while not doorx do
  12.   doorx,doory,doorz = gps.locate()
  13.  
  14.   if not doorx then
  15.     print("error, cant find doors location")
  16.   else
  17.     print("doors location: "..doorx..", "..doory..", "..doorz)
  18.   end
  19. end
  20.  
  21. function inRange(num,num2,min,max)
  22.   local numdif = num - num2
  23.   if numdif > min and numdif < max then
  24.     return true
  25.   else
  26.     return false
  27.   end
  28. end
  29.  
  30. function checkPos(index)
  31.   --data = textutils.unserialize(users[index])
  32.   local message = users[index]
  33.   data = textutils.unserialize(message)
  34.   --print(message[1])      
  35.   if inRange(data[1],doorx,-range,range) and inRange(data[2],doory,-height,height) and inRange(data[3],doorz,-range,range) then
  36.     close = close +1
  37.     --print("Door opened for "..data[4].." "..close)
  38.      
  39.   end
  40. end
  41.  
  42. function getMessage(timeout)
  43.   --users = {}
  44.   local senderId, packet, protocol = rednet.receive("GCP",timeout)
  45.   if senderId and protocol == "GCP" then
  46.    
  47.     users[senderId] = packet
  48.    
  49.     --print(" "..senderId.."stored to users")
  50.     return true
  51.   else
  52.     return false
  53.   end
  54. end
  55.  
  56. function printsomthing(index)
  57.   --print(index)
  58. end
  59.  
  60. while true do
  61.   if getMessage(5) then
  62.      
  63.     table.foreach(users, checkPos)
  64.     table.foreach(users, printsomthing)
  65.    
  66.     if close > 0 then
  67.       --print("close="..close)
  68.       redstone.setOutput("left",false)
  69.       redstone.setOutput("right",false)
  70.     else
  71.       --print("far="..close)
  72.       redstone.setOutput("left",true)
  73.       redstone.setOutput("right",true)
  74.     end
  75.     close = 0
  76.   else
  77.     print("timeout, no location ping received")
  78.     redstone.setOutput("left",true)
  79.     redstone.setOutput("right",true)
  80.   end
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement