TechManDylan

CustomExcavate3

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