Advertisement
ApexdaUser

Drone for OpenComputers (Test)

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