Advertisement
Guest User

quarry

a guest
May 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.32 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 3 then
  3.     error("Usage: quarry <minY> <maxY> <maxRadius>")
  4. end
  5.  
  6. local discardItems = {"chisel:", "Thaumcraft:", "BiomesOPlenty:", "Forestry:"}
  7.  
  8. local minY = tonumber(tArgs[1])
  9. local maxY = tonumber(tArgs[2])
  10. local maxRadius = tonumber(tArgs[3])
  11. if minY < 1 then
  12.     error("minY must be >0")
  13. end
  14. if maxY > 255 then
  15.     error("minY must be <256")
  16. end
  17. if maxY < minY then
  18.     error("minY > maxY")
  19. end
  20. if maxRadius < 1 then
  21.     error("maxRadius must be >0")
  22. end
  23.  
  24.  
  25. local xPos, yPos, zPos = 0, 0, 0
  26. local xDir, zDir = 0, 1
  27. local ownOrientation = false
  28.  
  29. local function move(moveFn, detFn, digFn, attFn, xd, yd, zd, dig, count)
  30.     for pos = 1, count do
  31.         while not moveFn() do
  32.             if detFn() then
  33.                 if not dig or not digFn() then
  34.                     return pos - 1
  35.                 end
  36.             elseif not attFn() then
  37.                 sleep(0.5)
  38.             end
  39.         end
  40.         xPos = xPos + xd
  41.         yPos = yPos + yd
  42.         zPos = zPos + zd
  43.     end
  44.     return count
  45. end
  46.  
  47. local function forward(count, dig)
  48.     return move(turtle.forward, turtle.detect, turtle.dig, turtle.attack, xDir, 0, zDir, dig, count)
  49. end
  50.  
  51. local function up(count, dig)
  52.     return move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp, 0, 1, 0, dig, count)
  53. end
  54.  
  55. local function down(count, dig)
  56.     return move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown, 0, -1, 0, dig, count)
  57. end
  58.  
  59. local function left()
  60.     turtle.turnLeft()
  61.     xDir, zDir = -zDir, xDir
  62. end
  63.  
  64. local function right()
  65.     turtle.turnRight()
  66.     xDir, zDir = zDir, -xDir
  67. end
  68.  
  69. local function orient(xd, zd)
  70.     while xd ~= xDir or zd ~= zDir do
  71.         if xDir == zd and zDir == -xd then
  72.             left()
  73.         else
  74.             right()
  75.         end
  76.     end
  77. end
  78.  
  79. local function goTo(x, y, z, xd, zd)
  80.     while xPos ~= x or yPos ~= y or zPos ~= z do
  81.         down(yPos - y, true)
  82.         if x < xPos then
  83.             orient(-1, 0)
  84.             forward(xPos - x, true)
  85.         end
  86.         if z < zPos then
  87.             orient(0, -1)
  88.             forward(zPos - z, true)
  89.         elseif z > zPos then
  90.             orient(0, 1)
  91.             forward(z - zPos, true)
  92.         end
  93.         if x > xPos then
  94.             orient(1, 0)
  95.             forward(x - xPos, true)
  96.         end
  97.         up(y - yPos, true)
  98.     end
  99.     orient(xd, zd)
  100. end
  101.  
  102. local function inPrefixList(item, list)
  103.     for _, prefix in pairs(list) do
  104.         if string.sub(item, 1, string.len(prefix)) == prefix then
  105.             return true
  106.         end
  107.     end
  108.     return false
  109. end
  110.  
  111. local function checkInventoryAndFuel(neededFuel)
  112.     local fuel = turtle.getFuelLevel()
  113.     local invFull = true
  114.     for n = 1, 16 do
  115.         if turtle.getItemCount(n) == 0 then
  116.             invFull = false
  117.         elseif fuel < neededFuel then
  118.             turtle.select(n)
  119.             if turtle.refuel(1) then
  120.                 local newFuel = turtle.getFuelLevel()
  121.                 turtle.refuel((neededFuel - newFuel) / (newFuel - fuel) + 1)
  122.                 fuel = newFuel
  123.             end
  124.         end
  125.     end
  126.     return invFull
  127. end
  128.  
  129. local function unloadInventory(keepFuel)
  130.     local fuelSlot = 0
  131.     for slot = 1, 16 do
  132.         if turtle.getItemCount(slot) > 0 then
  133.             turtle.select(slot)        
  134.             local keep = false
  135.             if keepFuel and fuelSlot == 0 and turtle.refuel(0) then
  136.                 keep = true
  137.                 fuelSlot = slot
  138.             end        
  139.             if not keep and not turtle.drop() then
  140.                 print("Chest is full, waiting for space")
  141.                 while not turtle.drop() do
  142.                     sleep(1)
  143.                 end
  144.             end
  145.         end
  146.     end
  147.     if keepFuel then
  148.         if fuelSlot == 0 then
  149.             fuelSlot = 1
  150.         end
  151.         local space = turtle.getItemSpace(fuelSlot)
  152.         local chest = peripheral.wrap("front")
  153.         if ownOrientation and chest and chest.getAllStacks then
  154.             for slot, stack in pairs(chest.getAllStacks()) do
  155.                 if stack.all().id == "minecraft:coal" then
  156.                     space = space - chest.pushItem(ownOrientation, slot, space, fuelSlot)
  157.                     if space <= 0 then
  158.                         break
  159.                     end
  160.                 end
  161.             end
  162.         end
  163.     end
  164.     turtle.select(1)
  165. end
  166.  
  167. local function doMaintenance()
  168.     print("Going home for maintenance...")
  169.     ox, oy, oz, oxd, ozd = xPos, yPos, zPos, xDir, zDir
  170.     goTo(0, 0, 0, 0, -1)
  171.     unloadInventory(true)
  172.     local neededFuel = 2 * (math.abs(ox) + math.abs(oy) + math.abs(oz)) + 100
  173.     checkInventoryAndFuel(neededFuel)
  174.     if turtle.getFuelLevel() < neededFuel then
  175.         print("Waiting for fuel")
  176.         while turtle.getFuelLevel() < neededFuel do
  177.             os.pullEvent("turtle_inventory")
  178.             checkInventoryAndFuel(neededFuel)
  179.         end
  180.         unloadInventory(true)
  181.     end
  182.     print("Resuming operations...")
  183.     goTo(ox, oy, oz, oxd, ozd)
  184. end
  185.  
  186. local function checkMaintenance()
  187.     local neededFuel = math.abs(xPos) + math.abs(yPos) + math.abs(zPos) + 10
  188.     local invFull = checkInventoryAndFuel(neededFuel)
  189.     if invFull then
  190.         print("Inventory full, cleaning up...")
  191.         for slot = 1, 16 do
  192.             local stack = turtle.getItemDetail(slot)
  193.             while stack and inPrefixList(stack.name, discardItems) do
  194.                 turtle.select(slot)
  195.                 turtle.dropUp()
  196.                 invFull = false
  197.                 for i = slot + 1, 16 do
  198.                     turtle.select(i)
  199.                     turtle.transferTo(slot)
  200.                 end
  201.                 stack = turtle.getItemDetail(slot)
  202.             end
  203.         end
  204.     end
  205.     if invFull or turtle.getFuelLevel() < neededFuel then
  206.         doMaintenance()
  207.     end
  208.     turtle.select(1)
  209. end
  210.  
  211. local function dig(distance)
  212.     for i = 1, distance do
  213.         forward(1, true)
  214.         turtle.digUp()
  215.         turtle.digDown()
  216.         checkMaintenance()
  217.     end
  218. end
  219.  
  220. local function digLayer(radius)
  221.     dig(radius)
  222.     right()
  223.     for j = 1, 3 do
  224.         dig(2 * radius)
  225.         right()
  226.     end
  227.     dig(radius - 1)
  228.     forward(1, true)
  229. end
  230.  
  231. local function digRadius(radius, numLayers, vertMoveFn, vertDigFn)
  232.     print("Digging radius ", radius, "...")
  233.     for i = 1, numLayers do
  234.         digLayer(radius)
  235.         if i < numLayers then
  236.             vertMoveFn(3, true)
  237.             vertDigFn()
  238.         else
  239.             left()
  240.             dig(1)
  241.             right()
  242.         end
  243.     end
  244. end
  245.  
  246.  
  247. local chest = peripheral.wrap("back")
  248. if chest then
  249.     for _, dir in pairs({"north", "east", "south", "west"}) do
  250.         local status, result = pcall(chest.pushItem, dir, 1, 0)
  251.         if status then
  252.             ownOrientation = dir
  253.             break
  254.         end
  255.     end
  256. end
  257.  
  258. doMaintenance()
  259. while down(1, true) == 1 do
  260.     checkMaintenance()
  261. end
  262. print("Base is at Y=", 1 - yPos)
  263. up(minY)
  264.  
  265. while forward(1, false) == 1 do
  266.     checkMaintenance()
  267. end
  268. dig(1)
  269. local radius = zPos
  270. right()
  271.  
  272. local numLayers = math.floor((maxY - minY + 3) / 3)
  273. while radius <= maxRadius do
  274.     digRadius(radius, numLayers, up, turtle.digUp)
  275.     radius = radius + 1
  276.     if radius > maxRadius then
  277.         break
  278.     end
  279.     digRadius(radius, numLayers, down, turtle.digDown)
  280.     radius = radius + 1
  281. end
  282.  
  283. goTo(0, 0, 0, 0, -1)
  284. unloadInventory(false)
  285. goTo(0, 0, 0, 0, 1)
  286. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement