Advertisement
Guest User

KMMS

a guest
Oct 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. local PORT_FOLLOW = 0xbf01
  2. local MAX_DIST = 100
  3.  
  4. local drone = component.proxy(component.list("drone")())
  5. local modem = component.proxy(component.list("modem")())
  6. local nav = component.proxy(component.list("navigation")())
  7.  
  8. modem.open(PORT_FOLLOW)
  9.  
  10. local function findClient()
  11. while true do
  12. local evt,_,sender,port,dist,msg = computer.pullSignal()
  13. if evt == "modem_message" and port == PORT_FOLLOW and dist < MAX_DIST and msg == "FOLLOW_REQUEST_LINK" then
  14. drone.setStatusText("Linked: "..sender:sub(1,3))
  15. modem.send(sender,port,"FOLLOW_LINK")
  16. return sender
  17. end
  18. end
  19. end
  20.  
  21. local function getNearbyNodes(justLabels)
  22. local waypoints = nav.findWaypoints(MAX_DIST)
  23. local nodes = {}
  24. for i = 1,waypoints.n do
  25. if justLabels then
  26. nodes[i] = waypoints[i].label
  27. else
  28. local wp = waypoints[i]
  29. nodes[wp.label] = wp.position
  30. end
  31. end
  32. return nodes
  33. end
  34.  
  35. local client,nodes,noResponse = findClient(),getNearbyNodes()
  36. local timeout = computer.uptime() + 10
  37.  
  38. while true do
  39. local evt,_,sender,port,dist,msg,label,ox,oy,oz = computer.pullSignal(timeout - computer.uptime())
  40. if moving and drone.getOffset() < 0.5 then
  41. moving = false
  42. nodes = getNearbyNodes()
  43. end
  44. if not evt then
  45. if noResponse then
  46. return
  47. end
  48. noResponse = true;
  49. modem.send(client,PORT_FOLLOW,"HEARTBEAT_REQUEST")
  50. timeout = timeout + 1
  51. elseif evt == "modem_message" and sender == client and port == PORT_FOLLOW and dist < MAX_DIST then
  52. if msg == "HEARTBEAT_RESPONSE" then
  53. noResponse = false
  54. elseif msg == "HEARTBEAT_REQUEST" then
  55. modem.send(sender,port,"HEARTBEAT_RESPONSE")
  56. elseif msg == "POS_UPDATE" and not moving then
  57. local node = nodes[label]
  58. if not node then
  59. modem.send(sender,port,"UPDATE_NODES",label,table.unpack(getNearbyNodes(true)))
  60. else
  61. drone.move(node[1] - ox, node[2] - oy, node[3] - oz)
  62. moving = true
  63. modem.send(sender,port,"OK")
  64. end
  65. end
  66. timeout = computer.uptime() + 10
  67. end
  68. end
  69. Client
  70.  
  71. --Follow Script Client
  72.  
  73. local PORT_FOLLOW = 0xbf01
  74.  
  75. local component = require("component")
  76. local event = require("event")
  77. local os = require("os")
  78.  
  79. local modem = component.modem
  80. local nav = component.navigation
  81.  
  82. modem.open(PORT_FOLLOW)
  83.  
  84. local function findDrone()
  85. modem.broadcast(PORT_FOLLOW,"FOLLOW_REQUEST_LINK")
  86. local _,_,sender,port,_,msg = event.pull("modem_message",nil,nil,PORT_FOLLOW,nil,"FOLLOW_LINK")
  87. return sender
  88. end
  89.  
  90. local drone = findDrone()
  91.  
  92. local function heartbeatHook(_,_,sender,port,_,msg)
  93. if sender == drone and port == PORT_FOLLOW and msg == "HEARTBEAT_REQUEST" then
  94. modem.send(sender,port,"HEARTBEAT_RESPONSE")
  95. end
  96. end
  97.  
  98.  
  99. event.listen("modem_message",heartbeatHook)
  100.  
  101. modem.send(drone,PORT_FOLLOW,"POS_UPDATE")
  102. local nodes = {select(7,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil,"UPDATE_NODES"))}
  103.  
  104. local function getNode()
  105. local tbl = nav.findWaypoints(100)
  106. for i=1,tbl.n do
  107. local label = tbl[i].label
  108. for i2=1,#nodes do
  109. if label == nodes[i2] then
  110. return label,table.unpack(tbl[i].position)
  111. end
  112. end
  113. end
  114. end
  115.  
  116. while true do
  117. local label,x,y,z = getNode()
  118. print(label,x,y,z)
  119. modem.send(drone,PORT_FOLLOW,"POS_UPDATE",label,x,y,z)
  120. local args = {select(6,event.pull("modem_message",nil,drone,PORT_FOLLOW,nil))}
  121. if table.remove(args,1) == "UPDATE_NODES" then
  122. nodes = args
  123. end
  124. os.sleep(0.1)
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement