hhhzzzsss

cuboid.lua

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