Advertisement
Axten2

ManaBattery-drone

Jan 25th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. -- Author: Axten
  2. -- Simple drone program to move a drone from a Botania pool to a set of pools for storing
  3.  
  4. -- Waypoint range
  5. local range = 16
  6.  
  7. -- Colors
  8. local colorFill = 0x69fcff
  9. local colorWaiting = 0x9ab7b8
  10. local colorCharging = 0x8de354
  11. local colorMoving = 0xc2a3cf
  12.  
  13. local function proxyFor(name, required)
  14.     local address = component and component.list(name)()
  15.     if not address and required then
  16.         error("missing component '" .. name .. "'")
  17.     end
  18.     return address and component.proxy(address) or nil
  19. end
  20.  
  21. local drone = proxyFor("drone", true)
  22. local nav = proxyFor("navigation", true)
  23. local chatbox = proxyFor("chat")
  24.  
  25. --tracking local position
  26. local px, py, pz = 0, 0, 0
  27.  
  28. local function moveTo(x, y, z)
  29.     drone.setLightColor(colorMoving)
  30.     if type(x) == "table" then
  31.         x, y, z = x[1], x[2], x[3]
  32.     end
  33.     local rx, ry, rz = x - px, y - py, z - pz
  34.     drone.move(rx, ry, rz)
  35.     while drone.getOffset() > 0.5 or drone.getVelocity() > 0.5 do
  36.         computer.pullSignal(0.5)
  37.     end
  38.     px, py, pz = x, y, z
  39. end
  40.  
  41. local function moveX(x)
  42.     moveTo(px +x, py, pz)
  43. end
  44.  
  45. local function moveZ(z)
  46.     moveTo(px, py, pz+z)
  47. end
  48.  
  49. local function recharge()
  50.     --io.write("recharging")
  51.     moveTo(0, 0, 0)
  52.     drone.setLightColor(colorCharging)
  53.     if computer.energy() < computer.maxEnergy() * 0.1 then
  54.         while computer.energy() < computer.maxEnergy() * 0.9 do
  55.             computer.pullSignal(1)
  56.         end
  57.     end
  58. end
  59.  
  60. local function getCurrentMana()
  61.     return drone.durability()
  62. end
  63.  
  64. local function fillBattery(mX, mZ)
  65.     for i= 0, mX  do
  66.         for j= 0, mZ do
  67.             drone.setLightColor(colorWaiting)
  68.             if not dip() then --if ring empty start over
  69.                 moveTo(0,0,0)
  70.                 return true
  71.             end
  72.             moveZ(1)
  73.         end
  74.         moveX(1)
  75.         moveZ(-j)
  76.     end
  77. end
  78.  
  79. local function dip()
  80.     repeat
  81.         local prevMana = getCurrentMana()
  82.         drop()
  83.         os.sleep(5)
  84.         suck()
  85.     until getCurrentMana() == 0 or getCurrentMana() == prevMana
  86.     if getCurrentMana() == 0 then
  87.         return false --ring is empty
  88.     else
  89.         return true --pool is full
  90.     end
  91. end
  92.  
  93.  
  94. local function suck()
  95.     if  drone.suck() == false then
  96.         chatError("can't suck item")
  97.         return false
  98.     end
  99. end
  100.  
  101. local function drop()
  102.     if drone.drop(0) == false then
  103.         chatError("can't drop item")
  104.         return false
  105.     end
  106. end
  107.  
  108. local function chatError(message)
  109.     --io.write("Error: " .. message)
  110.     moveTo(0,0,0)
  111. end
  112.  
  113. local function updateWaypoints()
  114.     --io.write("updating waypoints")
  115.     waypoints = nav.findWaypoints(range)
  116. end
  117.  
  118. local function filterWaypoints(filter)
  119.     local result = {}
  120.     for _, w in ipairs(waypoints) do
  121.         if filter(w) then
  122.             table.insert(result, w)
  123.         end
  124.     end
  125.     return result
  126. end
  127.  
  128.  
  129. while true do
  130.     recharge()
  131.     updateWaypoints()
  132.     local generators = filterWaypoints(function(w) return w.redstone > 0 end)
  133.     local    battery = filterWaypoints(function(w) return w.redstone < 1 end)
  134.  
  135.     for _, pool in ipairs(battery) do
  136.         if pool.getLabel == "start" then
  137.         local batteryStartPos=pool.position
  138.         elseif pool.getLabel == "end" then
  139.         local batteryEndPos=pool.position
  140.         end
  141.     end
  142.  
  143.     if batteryStartPos == nil or batteryEndPos == nil then
  144.         chatError("battery start and end waypoints not defined")
  145.         return false;
  146.     else
  147.         batteryX= math.abs(batteryStartPos[1] - batteryEndPos[1])
  148.         batteryZ= math.abs(batteryStartPos[3] - batteryEndPos[3])
  149.     end
  150.  
  151.     for _, generator in ipairs(generators) do
  152.         moveTo(generator.position)
  153.         dip()
  154.         drone.setLightColor(colorFill)
  155.     end
  156.  
  157.     moveTo(batteryStartPos)
  158.     fillBattery()
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement