Advertisement
Guest User

eeprom

a guest
May 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1.  
  2. local PORT_FOLLOW = 0xbf01
  3. local MAX_DIST = 100
  4.  
  5. local drone = component.proxy(component.list("drone")())
  6. local modem = component.proxy(component.list("modem")())
  7. local nav = component.proxy(component.list("navigation")())
  8.  
  9. modem.open(PORT_FOLLOW)
  10.  
  11. local function findClient()
  12. while true do
  13. local evt,_,sender,port,dist,msg = computer.pullSignal()
  14. if evt == "modem_message" and port == PORT_FOLLOW and dist < MAX_DIST and msg == "FOLLOW_REQUEST_LINK" then
  15. drone.setStatusText("Linked: "..sender:sub(1,3))
  16. modem.send(sender,port,"FOLLOW_LINK")
  17. return sender
  18. end
  19. end
  20. end
  21.  
  22. local function getNearbyNodes(justLabels)
  23. local waypoints = nav.findWaypoints(MAX_DIST)
  24. local nodes = {}
  25. for i = 1,waypoints.n do
  26. if justLabels then
  27. nodes[i] = waypoints[i].label
  28. else
  29. local wp = waypoints[i]
  30. nodes[wp.label] = wp.position
  31. end
  32. end
  33. return nodes
  34. end
  35.  
  36. local client,nodes,noResponse = findClient(),getNearbyNodes()
  37. local timeout = computer.uptime() + 10
  38.  
  39. while true do
  40. local evt,_,sender,port,dist,msg,label,ox,oy,oz = computer.pullSignal(timeout - computer.uptime())
  41. if moving and drone.getOffset() < 0.5 then
  42. moving = false
  43. nodes = getNearbyNodes()
  44. end
  45. if not evt then
  46. if noResponse then
  47. return
  48. end
  49. noResponse = true;
  50. modem.send(client,PORT_FOLLOW,"HEARTBEAT_REQUEST")
  51. timeout = timeout + 1
  52. elseif evt == "modem_message" and sender == client and port == PORT_FOLLOW and dist < MAX_DIST then
  53. if msg == "HEARTBEAT_RESPONSE" then
  54. noResponse = false
  55. elseif msg == "HEARTBEAT_REQUEST" then
  56. modem.send(sender,port,"HEARTBEAT_RESPONSE")
  57. elseif msg == "POS_UPDATE" and not moving then
  58. local node = nodes[label]
  59. if not node then
  60. modem.send(sender,port,"UPDATE_NODES",label,table.unpack(getNearbyNodes(true)))
  61. else
  62. drone.move(node[1] - ox, node[2] - oy, node[3] - oz)
  63. moving = true
  64. modem.send(sender,port,"OK")
  65. end
  66. end
  67. timeout = computer.uptime() + 10
  68. end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement