Advertisement
meigrafd

mc_oc_eeeprom.lua

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