Advertisement
Guest User

doorhandheld

a guest
Feb 27th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. sides = {"right", "left", "top", "bottom", "front", "back" }
  2. REGISTER_PROTOCOL = "REGISTER"
  3. SERVER_PROTOCOL = "SERVER"
  4. GENERAL_PROTOCOL = "GENERAL"
  5. SERVER_CHANNEL = 5
  6. REGISTER_CHANNEL = 3
  7. SERVER_NAME = "Server_hub"
  8.  
  9. DOOR_SERVICE_PROTOCOL = "DOOR_SERVICE"
  10.  
  11. local gModem
  12. local modemSide
  13. local SERVER_ID
  14.  
  15. function openModem()
  16.     for k, side in pairs(sides) do
  17.         if peripheral.isPresent(side) then
  18.             if peripheral.getType(side) == "modem" then
  19.                 gModem = peripheral.wrap(side)
  20.                 modemSide = side
  21.                 rednet.open(modemSide)
  22.             end
  23.         end
  24.     end
  25.    
  26.     SERVER_ID = rednet.lookup(SERVER_PROTOCOL, SERVER_NAME)
  27. end
  28.  
  29.  
  30. function start()
  31.     openModem()
  32.    
  33.     run()
  34. end
  35.  
  36. function run()
  37.     local ID
  38.     local timerID
  39.    
  40.     while true do
  41.         rednet.broadcast("...", DOOR_SERVICE_PROTOCOL)
  42.         timerID = os.startTimer(1)
  43.         local event, param1, param2 = os.pullEvent()
  44.         if event == "rednet_message" then
  45.             ID = param1
  46.             break
  47.         end
  48.     end
  49.    
  50.     os.cancelTimer(timerID)
  51.     clear()
  52.     print("Press enter if you wish to sync with ID: " .. ID)
  53.    
  54.    
  55.     timerID = os.startTimer(5)
  56.     while true do
  57.         local event, param1 = os.pullEvent()
  58.         if event == "key" and param1 == 28 then
  59.             rednet.send(ID, "...", DOOR_SERVICE_PROTOCOL)
  60.             os.cancelTimer(timerID)
  61.             collectBoundaries(ID)
  62.             break
  63.         elseif event == "timer" and param1 == timerID then
  64.             break
  65.         end
  66.     end
  67.    
  68. end
  69.  
  70. function collectBoundaries(ID)
  71.     local count = 0
  72.    
  73.     local coordinates = {}
  74.    
  75.     local quit = false
  76.    
  77.     repeat
  78.         clear()
  79.         print("Current coordinates = ")
  80.         for k, v in ipairs(coordinates) do
  81.             print("["..k.."]".." x= " .. v[1] .." y= " .. v[2].. " z= ".. v[3])
  82.         end
  83.        
  84.         print()
  85.         print("Press [enter] to send coordinate of boundary, [backspace] to quit,")
  86.         print(" [r] to reset all, or [d] to delete one coordinate")
  87.         --while true do
  88.             local event, param1 = os.pullEvent("key")
  89.             if param1 == 28 then
  90.                 local x, y, z = gps.locate()
  91.                 if x ~= nil then
  92.                    
  93.                    
  94.                     local coor = {}
  95.                     coor[1] = math.floor(x)
  96.                     coor[2] = math.floor(y - 1) --This takes into account that the device is off the ground a little
  97.                     coor[3] = math.floor(z)
  98.                    
  99.                     for i = 1, #coor do
  100.                         print(coor[i])
  101.                     end
  102.                    
  103.                     table.insert(coordinates, coor)
  104.                    
  105.                     count = count + 1
  106.                    
  107.                 else
  108.                     print("Cannot determine GPS position!")
  109.                     sleep(1.5)
  110.                     quit = true
  111.                     break
  112.                 end
  113.            
  114.             elseif param1 == 14 then
  115.             --'backspace'
  116.                 rednet.send(ID, "quit", DOOR_SERVICE_PROTOCOL)
  117.                 quit = true
  118.                 break
  119.            
  120.             elseif param1 == 19 then
  121.             --'r'
  122.                 count = 0
  123.                 coordinates = {}
  124.            
  125.             elseif param1 == 32 then
  126.             --'d'
  127.                 local index
  128.                 clear()
  129.                 print("Type the number of the coordinate you'd like deleted")
  130.                 index = tonumber(io.read())
  131.                 table.remove(coordinates, index)
  132.             end
  133.                
  134.         --end
  135.     until count == 4
  136.    
  137.     if quit then
  138.         return nil
  139.     end
  140.    
  141.     for k, v in ipairs(coordinates) do
  142.         local msg = string.format("%d %d %d", v[1], v[2] ,v[3])
  143.        
  144.         repeat
  145.             rednet.send(ID, msg, DOOR_SERVICE_PROTOCOL)
  146.             local fID, fMsg = rednet.receive(DOOR_SERVICE_PROTOCOL)
  147.        
  148.         until fID == ID and fMsg == "confirm"
  149.     end
  150.    
  151.     getHeight(ID)
  152.    
  153.     print("To finish creating the boundary, please go to the main computer")
  154.     sleep(2)
  155. end
  156.  
  157. function getHeight(ID)
  158.     print("=========================")
  159.     print("Enter the height you want for the boundary: ")
  160.     print()
  161.     local height = io.read()
  162.     rednet.send(ID, height, DOOR_SERVICE_PROTOCOL)
  163. end
  164.  
  165. function round(num, idp)
  166.     local mult = 10 ^ (idp or 0)
  167.     return math.floor(num * mult + 0.5) / mult
  168. end
  169.  
  170.  
  171. function clear()
  172.     term.clear()
  173.     term.setCursorPos(1,1)
  174. end
  175.  
  176. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement