ItsNoah

exfm

Apr 19th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.73 KB | None | 0 0
  1. local tArgs = {...}
  2. -- Mine in a quarry pattern until we hit something we can't dig
  3. local size = tonumber(tArgs[1])
  4. local mdepth = tonumber(tArgs[2])
  5. if size < 1 then
  6.     print("Excavate diameter must be positive")
  7.     return
  8. end
  9. if mdepth < 1 then
  10.     print("Excavate depth must be positive")
  11.     return
  12. end
  13.  
  14. local depth = 0
  15. local unloaded = 0
  16. local collected = 0
  17.  
  18. local xPos, zPos = 0, 0
  19. local xDir, zDir = 0, 1
  20.  
  21. local goTo  -- Filled in further down
  22. local refuel  -- Filled in further down
  23.  
  24. local function unload(_bKeepOneFuelStack)
  25.     print("Unloading items...")
  26.     for n = 1, 16 do
  27.         local nCount = turtle.getItemCount(n)
  28.         if nCount > 0 then
  29.             turtle.select(n)
  30.             local bDrop = true
  31.             if _bKeepOneFuelStack and turtle.refuel(0) then
  32.                 bDrop = false
  33.                 _bKeepOneFuelStack = false
  34.             end
  35.             if bDrop then
  36.                 turtle.drop()
  37.                 unloaded = unloaded + nCount
  38.             end
  39.         end
  40.     end
  41.     collected = 0
  42.     turtle.select(1)
  43. end
  44.  
  45. local function returnSupplies()
  46.     local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  47.     print("Returning to surface...")
  48.     goTo(0, 0, 0, 0, -1)
  49.  
  50.     local fuelNeeded = 2 * (x + y + z) + 1
  51.     if not refuel(fuelNeeded) then
  52.         unload(true)
  53.         print("Waiting for fuel")
  54.         while not refuel(fuelNeeded) do
  55.             os.pullEvent("turtle_inventory")
  56.         end
  57.     else
  58.         unload(true)
  59.     end
  60.  
  61.     print("Resuming mining...")
  62.     goTo(x, y, z, xd, zd)
  63. end
  64.  
  65. local function collect()
  66.     local bFull = true
  67.     local nTotalItems = 0
  68.     for n = 1, 16 do
  69.         local nCount = turtle.getItemCount(n)
  70.         if nCount == 0 then
  71.             bFull = false
  72.         end
  73.         nTotalItems = nTotalItems + nCount
  74.     end
  75.  
  76.     if nTotalItems > collected then
  77.         collected = nTotalItems
  78.         if math.fmod(collected + unloaded, 50) == 0 then
  79.             print("Mined " .. (collected + unloaded) .. " items.")
  80.         end
  81.     end
  82.  
  83.     if bFull then
  84.         print("No empty slots left.")
  85.         return false
  86.     end
  87.     return true
  88. end
  89.  
  90. function refuel(ammount)
  91.     local fuelLevel = turtle.getFuelLevel()
  92.     if fuelLevel == "unlimited" then
  93.         return true
  94.     end
  95.  
  96.     local needed = ammount or (xPos + zPos + depth + 2)
  97.     if turtle.getFuelLevel() < needed then
  98.         local fueled = false
  99.         for n = 1, 16 do
  100.             if turtle.getItemCount(n) > 0 then
  101.                 turtle.select(n)
  102.                 if turtle.refuel(1) then
  103.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  104.                         turtle.refuel(1)
  105.                     end
  106.                     if turtle.getFuelLevel() >= needed then
  107.                         turtle.select(1)
  108.                         return true
  109.                     end
  110.                 end
  111.             end
  112.         end
  113.         turtle.select(1)
  114.         return false
  115.     end
  116.  
  117.     return true
  118. end
  119.  
  120. local function dropCobble()
  121.     for i = 1, 16 do
  122.         local currentItem = turtle.getItemDetail(i)
  123.         if currentItem ~= nil then
  124.             if currentItem.name == "minecraft:cobblestone" then
  125.                 turtle.select(i)
  126.                 turtle.dropUp()
  127.             end
  128.         end
  129.     end
  130. end
  131.  
  132. local function tryForwards()
  133.     if not refuel() then
  134.         print("Not enough Fuel")
  135.         returnSupplies()
  136.     end
  137.  
  138.     while not turtle.forward() do
  139.         if turtle.detect() then
  140.             if turtle.dig() then
  141.                 if not collect() then
  142.                     dropCobble()
  143.                     if not collect() then
  144.                         returnSupplies()
  145.                     end
  146.                 end
  147.             else
  148.                 return false
  149.             end
  150.         elseif turtle.attack() then
  151.             if not collect() then
  152.                 returnSupplies()
  153.             end
  154.         else
  155.             sleep(0.5)
  156.         end
  157.     end
  158.  
  159.     xPos = xPos + xDir
  160.     zPos = zPos + zDir
  161.     return true
  162. end
  163.  
  164. local function tryDown()
  165.     if not refuel() then
  166.         print("Not enough Fuel")
  167.         returnSupplies()
  168.     end
  169.  
  170.     while not turtle.down() do
  171.         if turtle.detectDown() then
  172.             if turtle.digDown() then
  173.                 dropCobble()
  174.                 if not collect() then
  175.                     returnSupplies()
  176.                 end
  177.             else
  178.                 return false
  179.             end
  180.         elseif turtle.attackDown() then
  181.             if not collect() then
  182.                 returnSupplies()
  183.             end
  184.         else
  185.             sleep(0.5)
  186.         end
  187.     end
  188.  
  189.     depth = depth + 1
  190.     if math.fmod(depth, 10) == 0 then
  191.         print("Descended " .. depth .. " metres.")
  192.     end
  193.  
  194.     return true
  195. end
  196.  
  197. local function turnLeft()
  198.     turtle.turnLeft()
  199.     xDir, zDir = -zDir, xDir
  200. end
  201.  
  202. local function turnRight()
  203.     turtle.turnRight()
  204.     xDir, zDir = zDir, -xDir
  205. end
  206.  
  207. function goTo(x, y, z, xd, zd)
  208.     while depth > y do
  209.         if turtle.up() then
  210.             depth = depth - 1
  211.         elseif turtle.digUp() or turtle.attackUp() then
  212.             dropCobble()
  213.             collect()
  214.         else
  215.             sleep(0.5)
  216.         end
  217.     end
  218.  
  219.     if xPos > x then
  220.         while xDir ~= -1 do
  221.             turnLeft()
  222.         end
  223.         while xPos > x do
  224.             if turtle.forward() then
  225.                 xPos = xPos - 1
  226.             elseif turtle.dig() or turtle.attack() then
  227.                 dropCobble()
  228.                 collect()
  229.             else
  230.                 sleep(0.5)
  231.             end
  232.         end
  233.     elseif xPos < x then
  234.         while xDir ~= 1 do
  235.             turnLeft()
  236.         end
  237.         while xPos < x do
  238.             if turtle.forward() then
  239.                 xPos = xPos + 1
  240.             elseif turtle.dig() or turtle.attack() then
  241.                 dropCobble()
  242.                 collect()
  243.             else
  244.                 sleep(0.5)
  245.             end
  246.         end
  247.     end
  248.  
  249.     if zPos > z then
  250.         while zDir ~= -1 do
  251.             turnLeft()
  252.         end
  253.         while zPos > z do
  254.             if turtle.forward() then
  255.                 zPos = zPos - 1
  256.             elseif turtle.dig() or turtle.attack() then
  257.                 dropCobble()
  258.                 collect()
  259.             else
  260.                 sleep(0.5)
  261.             end
  262.         end
  263.     elseif zPos < z then
  264.         while zDir ~= 1 do
  265.             turnLeft()
  266.         end
  267.         while zPos < z do
  268.             if turtle.forward() then
  269.                 zPos = zPos + 1
  270.             elseif turtle.dig() or turtle.attack() then
  271.                 dropCobble()
  272.                 collect()
  273.             else
  274.                 sleep(0.5)
  275.             end
  276.         end
  277.     end
  278.  
  279.     while depth == y do
  280.         if turtle.down() then
  281.             depth = depth + 1
  282.         elseif turtle.digDown() or turtle.attackDown() then
  283.             dropCobble()
  284.             collect()
  285.         else
  286.             sleep(0.5)
  287.         end
  288.     end
  289.  
  290.     while zDir ~= zd or xDir ~= xd do
  291.         turnLeft()
  292.     end
  293. end
  294.  
  295. if not refuel() then
  296.     print("Out of Fuel")
  297.     return
  298. end
  299.  
  300. print("Excavating...")
  301.  
  302. local reseal = false
  303. turtle.select(1)
  304. if turtle.digDown() then
  305.     reseal = true
  306. end
  307.  
  308. local alternate = 0
  309. local done = false
  310. while not done do
  311.     for n = 1, size do
  312.         for m = 1, size - 1 do
  313.             if not tryForwards() then
  314.                 done = true
  315.                 break
  316.             end
  317.         end
  318.         if done then
  319.             break
  320.         end
  321.         if n < size then
  322.             if math.fmod(n + alternate, 2) == 0 then
  323.                 turnLeft()
  324.                 if not tryForwards() then
  325.                     done = true
  326.                     break
  327.                 end
  328.                 turnLeft()
  329.             else
  330.                 turnRight()
  331.                 if not tryForwards() then
  332.                     done = true
  333.                     break
  334.                 end
  335.                 turnRight()
  336.             end
  337.         end
  338.     end
  339.     if done then
  340.         break
  341.     end
  342.     if depth == mdepth then
  343.         break
  344.     end
  345.  
  346.     if size > 1 then
  347.         if math.fmod(size, 2) == 0 then
  348.             turnRight()
  349.         else
  350.             if alternate == 0 then
  351.                 turnLeft()
  352.             else
  353.                 turnRight()
  354.             end
  355.             alternate = 1 - alternate
  356.         end
  357.     end
  358.  
  359.     if not tryDown() then
  360.         done = true
  361.         break
  362.     end
  363. end
  364.  
  365. print("Returning to surface...")
  366.  
  367. -- Return to where we started
  368. goTo(0, 0, 0, 0, -1)
  369. unload(false)
  370. goTo(0, 0, 0, 0, 1)
  371.  
  372. -- Seal the hole
  373. if reseal then
  374.     turtle.placeDown()
  375. end
  376.  
  377. print("Mined " .. (collected + unloaded) .. " items total.")
  378.  
Add Comment
Please, Sign In to add comment