Advertisement
meigrafd

mc_oc_client.lua

Apr 30th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.36 KB | None | 0 0
  1. local PORT_FOLLOW = 0xbf01
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local os = require("os")
  6.  
  7. local modem = component.modem
  8. local nav = component.navigation
  9.  
  10. modem.open(PORT_FOLLOW)
  11.  
  12. local function findDrone()
  13.     modem.broadcast(PORT_FOLLOW,"FOLLOW_REQUEST_LINK")
  14.     local _,_,sender,port,_,msg = event.pull("modem_message",nil,nil,PORT_FOLLOW,nil,"FOLLOW_LINK")
  15.     return sender
  16. end
  17.  
  18. local drone = findDrone()
  19.  
  20. local function heartbeatHook(_,_,sender,port,_,msg)
  21.     if sender == drone and port == PORT_FOLLOW and msg == "HEARTBEAT_REQUEST" then
  22.         modem.send(sender,port,"HEARTBEAT_RESPONSE")
  23.     end
  24. end
  25.  
  26.  
  27. event.listen("modem_message",heartbeatHook)
  28.  
  29. modem.send(drone,PORT_FOLLOW,"POS_UPDATE")
  30. local nodes = {select(7,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil,"UPDATE_NODES"))}
  31.  
  32. local function getNode()
  33.     local tbl = nav.findWaypoints(100)
  34.     for i=1,tbl.n do
  35.         local label = tbl[i].label
  36.         for i2=1,#nodes do
  37.             if label == nodes[i2] then
  38.                 return label,table.unpack(tbl[i].position)
  39.             end
  40.         end
  41.     end
  42. end
  43.  
  44. while true do
  45.         local label,x,y,z = getNode()
  46.         print(label,x,y,z)
  47.     modem.send(drone,PORT_FOLLOW,"POS_UPDATE",label,x,y,z)
  48.     local args = {select(6,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil))}
  49.     if table.remove(args,1) == "UPDATE_NODES" then
  50.         nodes = args
  51.     end
  52.     os.sleep(0.1)
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement