Advertisement
Guest User

Amazon

a guest
Apr 26th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. local drone = component.proxy(component.list("drone")())
  2. local nav = component.proxy(component.list("navigation")())
  3. local modem = component.proxy(component.list("modem")())
  4.  
  5. local waypointLookRadius = 64
  6. local flightAltitude = 80
  7. local commsPort = 1337
  8. local colors = {["travelling"] = 0xFFFFFF, ["charging"] = 0xE6E600, ["charged"] = 0x00FF00, ["waiting"] = 0xFF9900, ["error"] = 0xFF0000, ["active"] = 0x0000FF }
  9. local cx, cy, cz
  10. local BASE
  11. function callibrate()
  12.   cx, cy, cz = nav:getPosition()
  13. end
  14. function getWaypoints()
  15.   BASE = {}
  16.   callibrate()
  17.   local waypoints = nav.findWaypoints(waypointLookRadius)
  18.   for i=1, waypoints.n do
  19.     if waypoints[i].label == "Charger" then
  20.       BASE.x = cx+waypoints[i].position[1]
  21.       BASE.y = cy+waypoints[i].position[2]
  22.       BASE.z = cz+waypoints[i].position[3]
  23.     end
  24.   end
  25.   if BASE == nil then
  26.     error("Missing waypoints")
  27.   end
  28. end
  29.  
  30. function getCharge()
  31.   return computer.energy()/computer.maxEnergy()
  32. end
  33.  
  34. function color(state)
  35.   drone.setLightColor(colors[state] or 0x000000)
  36. end
  37.  
  38. function move(tx, ty, tz)
  39.   local dx = tx - cx
  40.   local dy = ty - cy
  41.   local dz = tz - cz
  42.   drone.move(dx, dy, dz)
  43.   while drone.getOffset() > 0.5 and drone.getVelocity() > 0.2 do
  44.     computer.pullSignal(0.2)
  45.   end
  46.   cx, cy, cz = tx, ty, tz
  47. end
  48.  
  49. function fly(tx, ty, tz)
  50.   move(cx, flightAltitude, cz)
  51.   move(tx, flightAltitude, tz)
  52.   move(tx, ty, tz)
  53.   callibrate()
  54.   if cx ~= tx or cy ~= ty or cz ~= tz then
  55.     computer.pullSignal(1)
  56.     callibrate()
  57.     move(tx,ty,tz)
  58.   end
  59. end
  60.  
  61. function goHome()
  62.     color("travelling")
  63.     fly(BASE.x, BASE.y+1, BASE.z)
  64.     color("charging")
  65.     while getCharge() <= .95 do computer.pullSignal(.1) end
  66.     color("charged")
  67.     computer.beep(1000, 1)
  68. end
  69.  
  70. function runDelivery(ds)
  71.   color("travelling")
  72.   fly(ds.gx,ds.gy+1,ds.gz)
  73.   color("active")
  74.   for i = 1, ds.sx do
  75.     drone.suck(0)
  76.   end
  77.   color("travelling")
  78.   fly(ds.dx, ds.dy+1, ds.dz)
  79.   color("active")
  80.   for i = 1, drone.inventorySize() do
  81.     drone.select(i)
  82.     drone.drop(0)
  83.   end
  84. end
  85.  
  86. function waitForCommand()
  87.     color("waiting")
  88.     while true do
  89.       evt, self, requester, _, _, request = computer.pullSignal()
  90.       if evt == "modem_message" then
  91.         if request == "getDrone" then
  92.           modem.send(requester, commsPort, "confirmed", drone.inventorySize())
  93.           local confirmed = false
  94.           local start = computer.uptime()
  95.           while computer.uptime() - start < 3 do
  96.             evt, _, check, _, _, msg = computer.pullSignal(1)
  97.             if evt == "modem_message" and check == requester then
  98.               if msg == self then
  99.                 confirmed = true
  100.               end
  101.               break
  102.             end
  103.           end
  104.           if confirmed then
  105.             color("active")
  106.             modem.send(requester, commsPort, "sendData")
  107.             local ds = {}
  108.             local start = computer.uptime()
  109.             while computer.uptime() - start < 10 do
  110.               evt, _, check, _, _, entry, data = computer.pullSignal(1)
  111.               if evt == "modem_message" and check == requester then
  112.                 if entry == "end" then
  113.                   break
  114.                 else
  115.                   ds[entry] = data
  116.                 end
  117.               end            
  118.             end
  119.             if ds.sx and ds.gx and ds.gy and ds.gz and ds.dx and ds.dy and ds.dz then
  120.               modem.close(commsPort)
  121.               runDelivery(ds)
  122.               modem.open(commsPort)
  123.             else
  124.               color("error")
  125.               computer.beep(1000, 2)
  126.             end
  127.             break
  128.           end
  129.         end
  130.       end
  131.     end
  132. end
  133.  
  134. function init()
  135.   modem.open(commsPort)
  136.   getWaypoints()
  137.   while true do
  138.     goHome()
  139.     getWaypoints()
  140.     waitForCommand()  
  141.   end
  142. end
  143.  
  144. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement