hhhzzzsss

flatmine.lua

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