Forte40

Radial Miner

Aug 19th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.81 KB | None | 0 0
  1. loadfile("move")()
  2. loadfile("config")()
  3.  
  4. local mineConfig = config.load("miner.dat")
  5.  
  6. local radiusFirst = true
  7. local enderChest = false
  8. local enderFuel = false
  9.  
  10. local dataFile = "radialminer.dat"
  11.  
  12. local fuelSlot = nil
  13. local noiseSlot = {}
  14. local useNoise = false
  15.  
  16. function checkInventory()
  17.   for i = 1, 16 do
  18.     if turtle.getItemCount(i) > 0 then
  19.       turtle.select(i)
  20.       if turtle.refuel(0) then
  21.         if fuelSlot == nil then
  22.           fuelSlot = i
  23.         else
  24.           return
  25.         end
  26.       else
  27.         noiseSlot[i] = true
  28.         useNoise = true
  29.       end
  30.     else
  31.       return
  32.     end
  33.   end
  34. end
  35.  
  36. function loadWork()
  37.   local work = config.load(dataFile)
  38.   if work == nil then
  39.     work = createWork()
  40.     config.save(dataFile, work)
  41.   end
  42.   return work
  43. end
  44.  
  45. function createWork()
  46.   local work = {}
  47.   work.startRadius = work.startRadius or config.getNumber("startRadius", 1)
  48.   work.endRadius = work.endRadius or config.getNumber("endRadius", 2000)
  49.   work.startHeight = work.startHeight or config.getNumber("startHeight", 4)
  50.   work.endHeight = work.endHeight or config.getNumber("endHeight", 57)
  51.   work.currentHeight = work.currentHeight or config.getNumber("currentHeight")
  52.   turtle.y = work.currentHeight
  53.   for r = work.startRadius, work.endRadius do
  54.     work[r] = {}
  55.     for h = work.startHeight + 1, work.endHeight, 3 do
  56.       work[r][h] = false
  57.     end
  58.   end
  59.   return work
  60. end
  61.  
  62. function getNextRadius(work, height, turtleId)
  63.   for r = work.startRadius, work.endRadius do
  64.     for h = height or work.startHeight + 1, height or work.endHeight, 3 do
  65.       if work[r][h] == false
  66.           or (turtleId ~= nil and work[r][h] == turtleId) then
  67.         return r
  68.       end
  69.     end
  70.   end
  71. end
  72.  
  73. function getNextHeight(work, radius, turtleId)
  74.   for h = work.startHeight, work.endHeight do
  75.     for r = radius or work.startRadius, radius or work.endRadius do
  76.       if work[r][h] == false
  77.           or (turtleId ~= nil and work[r][h] == turtleId) then
  78.         return h
  79.       end
  80.     end
  81.   end
  82. end
  83.  
  84. function getWork(work, turtleId)
  85.   local radius
  86.   local height
  87.   if radiusFirst then
  88.      radius = getNextRadius(work, height, turtleId)
  89.      height = getNextHeight(work, radius, turtleId)
  90.   else
  91.      height = getNextHeight(work, radius, turtleId)  
  92.      radius = getNextRadius(work, height, turtleId)
  93.   end
  94.   if radius ~= nil then
  95.     work[radius][height] = turtleId
  96.     config.save(dataFile, work)
  97.   end
  98.   return radius, height
  99. end
  100.  
  101. function finishWork(work, radius, height)
  102.   work[radius][height] = true
  103.   config.save(dataFile, work)
  104. end
  105.  
  106. function unload()
  107.   turtle.moveToWaypoint("home", "xzy")
  108.   for i = 1, 16 do
  109.     local amount = turtle.getItemCount(i)
  110.     if amount > 0 then
  111.       -- keep full stack of fuel and at least 1 noise block
  112.       if i ~= fuelSlot then
  113.         turtle.select(i)
  114.         if noiseSlot[i] then
  115.           if not turtle.dropUp(turtle.getItemCount(i) - 1) then
  116.             print("chest is full")
  117.             while not turtle.dropUp(turtle.getItemCount(i) - 1) do
  118.               sleep(10)
  119.             end
  120.           end
  121.         else
  122.           if not turtle.dropUp() then
  123.             print("chest is full")            
  124.             while not turtle.dropUp() do
  125.               sleep(10)
  126.             end
  127.           end
  128.         end
  129.       end
  130.     end
  131.   end
  132.   turtle.select(1)
  133. end
  134.  
  135. function unloadReturn()
  136.   print("inventory full, returning items early")
  137.   turtle.setWaypoint("mine")
  138.   unload()
  139.   turtle.moveToWaypoint("mine", "yxz")
  140. end
  141.  
  142.  
  143. function isFull()
  144.   local full = true
  145.   for i = 1, 16 do
  146.     if turtle.getItemCount(i) == 0 then
  147.       full = false
  148.       break
  149.     end
  150.   end
  151.   return full
  152. end
  153.  
  154. function isNoiseUp()
  155.   for slot in pairs(noiseSlot) do
  156.     turtle.select(slot)
  157.     if turtle.compareUp() then
  158.       turtle.select(1)
  159.       return true
  160.     end
  161.   end
  162.   turtle.select(1)
  163.   return false
  164. end
  165.  
  166. function isNoiseDown()
  167.   for slot in pairs(noiseSlot) do
  168.     turtle.select(slot)
  169.     if turtle.compareDown() then
  170.       turtle.select(1)
  171.       return true
  172.     end
  173.   end
  174.   turtle.select(1)
  175.   return false
  176. end
  177.  
  178. function mineForward3()
  179.   turtle.dig()
  180.   if isFull() then
  181.     unloadReturn()
  182.   end
  183.   turtle.moveForward()
  184.   if isFull() then
  185.     unloadReturn()
  186.   end
  187.   if not useNoise or not isNoiseUp() then
  188.     turtle.digUp()
  189.     if isFull() then
  190.       unloadReturn()
  191.     end
  192.   end
  193.   if not useNoise or not isNoiseDown() then
  194.     turtle.digDown()
  195.     if isFull() then
  196.       unloadReturn()
  197.     end
  198.   end
  199. end
  200.  
  201. function ensureFueled(requiredFuel)
  202.   if fuelSlot ~= nil then
  203.     turtle.select(fuelSlot)
  204.     while turtle.getFuelLevel() < requiredFuel do
  205.       if turtle.getItemCount(fuelSlot) > 1 then
  206.         turtle.refuel(1)
  207.       else
  208.         -- not enough fuel
  209.         return false
  210.       end
  211.     end
  212.     turtle.select(1)
  213.     return true
  214.   else
  215.     return (turtle.getFuelLevel() >= requiredFuel)
  216.   end
  217. end
  218.  
  219. function mine(radius, height)
  220.   turtle.setWaypoint("home")
  221.   turtle.setWaypoint("mine", turtle.x, height, turtle.z + radius, 1)
  222.   checkInventory()
  223.   -- calculate fuel
  224.   local requiredFuel = radius * 8 + turtle.distanceTo("mine") * 2
  225.   while not ensureFueled(requiredFuel) do
  226.     print("waiting for fuel...")
  227.     io.read()
  228.     checkInventory()
  229.   end
  230.   print("mining radius ", radius, " at level ", height, " with ", requiredFuel, " fuel")  
  231.   turtle.moveToWaypoint("mine", "yxz")
  232.   -- mine ring
  233.   for i = 1, 4 do
  234.     for j = 1, radius do
  235.       mineForward3()
  236.     end
  237.     turtle.turnRight()
  238.     for j = 1, radius do
  239.       mineForward3()
  240.     end
  241.   end
  242.   unload()
  243. end
  244.  
  245. local work = loadWork()
  246.  
  247. while true do
  248.   local radius, height = getWork(work, 1)
  249.   if radius ~= nil then
  250.     mine(radius, height)
  251.     finishWork(work, radius, height)
  252.   else
  253.     print("finished")
  254.     break
  255.   end
  256. end
Advertisement
Add Comment
Please, Sign In to add comment