heresjohnny32

simple mineing

Jul 16th, 2026 (edited)
33,070
1
Never
6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | Gaming | 1 0
  1. -- Permanent Home Quarry Script
  2.  
  3. -- Track coordinates relative to start (0,0,0 is the starting point before the 4-block offset)
  4. local x, y, z = 0, 0, 0
  5. local dir = 0 -- 0=forward (+Z), 1=right (+X), 2=back (-Z), 3=left (-X)
  6. local targetX, targetY, targetZ = 0, 0, 0
  7.  
  8. -- Prompt for dimensions
  9. print("Enter Width (X - Right):")
  10. targetX = tonumber(read()) or 32
  11. print("Enter Length (Z - Forward):")
  12. targetZ = tonumber(read()) or 32
  13. print("Enter Height (Y - Down):")
  14. targetY = tonumber(read()) or 32
  15.  
  16. -- Fuel and inventory config
  17. local FUEL_SLOT = 1
  18. local MIN_FUEL = 200
  19.  
  20. -- Navigation helpers
  21. local function turnRight()
  22.     turtle.turnRight()
  23.     dir = (dir + 1) % 4
  24. end
  25.  
  26. local function turnLeft()
  27.     turtle.turnLeft()
  28.     dir = (dir - 1) % 4
  29. end
  30.  
  31. local function face(targetDir)
  32.     while dir ~= targetDir do
  33.         turnRight()
  34.     end
  35. end
  36.  
  37. local function checkFuel()
  38.     if turtle.getFuelLevel() == "unlimited" then return true end
  39.    
  40.     -- Try auto-refueling from inventory slot 1
  41.     if turtle.getFuelLevel() < MIN_FUEL then
  42.         local currentSlot = turtle.getSelectedSlot()
  43.         turtle.select(FUEL_SLOT)
  44.         turtle.refuel(16)
  45.         turtle.select(currentSlot)
  46.     end
  47.    
  48.     -- Return total distance back to absolute (0,0,0) home estimate
  49.     local distHome = math.abs(x) + math.abs(y) + math.abs(z)
  50.     return turtle.getFuelLevel() > (distHome + 50)
  51. end
  52.  
  53. local function isInventoryFull()
  54.     for i = 1, 16 do
  55.         if turtle.getItemCount(i) == 0 then return false end
  56.     end
  57.     return true
  58. end
  59.  
  60. -- Clear block ahead to prevent getting stuck
  61. local function digForward()
  62.     while turtle.detect() do
  63.         turtle.dig()
  64.         os.sleep(0.5)
  65.     end
  66. end
  67.  
  68. -- Move to absolute coordinate system safely
  69. local function goTo(targetX, targetY, targetZ, targetDir)
  70.     -- 1. Always clear height (Y) first to avoid obstacles
  71.     while y < targetY do
  72.         while turtle.detectUp() do turtle.digUp() end
  73.         if turtle.up() then y = y + 1 end
  74.     end
  75.     while y > targetY do
  76.         while turtle.detectDown() do turtle.digDown() end
  77.         if turtle.down() then y = y - 1 end
  78.     end
  79.  
  80.     -- 2. Move along X axis
  81.     if x < targetX then
  82.         face(1)
  83.         while x < targetX do digForward() if turtle.forward() then x = x + 1 end end
  84.     elseif x > targetX then
  85.         face(3)
  86.         while x > targetX do digForward() if turtle.forward() then x = x - 1 end end
  87.     end
  88.  
  89.     -- 3. Move along Z axis
  90.     if z < targetZ then
  91.         face(0)
  92.         while z < targetZ do digForward() if turtle.forward() then z = z + 1 end end
  93.     elseif z > targetZ then
  94.         face(2)
  95.         while z > targetZ do digForward() if turtle.forward() then z = z - 1 end end
  96.     end
  97.  
  98.     -- 4. Set final direction
  99.     face(targetDir)
  100. end
  101.  
  102. -- Return to absolute (0,0,0), dump, and return to exact coordinate
  103. local function returnHomeAndResume()
  104.     print("Returning home...")
  105.     -- Remember current mining state
  106.     local savedX, savedY, savedZ, savedDir = x, y, z, dir
  107.    
  108.     -- Go to absolute 0,0,0 (Home) and face original forward (0)
  109.     -- This lands right back in front of your chests
  110.     goTo(0, 0, 0, 0)
  111.    
  112.     -- Empty inventory into rear chest (turtle faces 0, chest is behind at 2)
  113.     face(2)
  114.     for i = 2, 16 do
  115.         turtle.select(i)
  116.         turtle.drop()
  117.     end
  118.     turtle.select(1)
  119.    
  120.     -- Pull fuel from bottom chest if needed
  121.     if turtle.getFuelLevel() < MIN_FUEL then
  122.         turtle.suckDown()
  123.         turtle.refuel()
  124.     end
  125.    
  126.     print("Resuming mining...")
  127.     -- Fly directly back to the exact saved mining coordinates
  128.     goTo(savedX, savedY, savedZ, savedDir)
  129. end
  130.  
  131. local function moveForward()
  132.     if not checkFuel() or isInventoryFull() then
  133.         returnHomeAndResume()
  134.     end
  135.    
  136.     digForward()
  137.    
  138.     if turtle.forward() then
  139.         if dir == 0 then z = z + 1
  140.         elseif dir == 1 then x = x + 1
  141.         elseif dir == 2 then z = z - 1
  142.         elseif dir == 3 then x = x - 1
  143.         end
  144.         return true
  145.     end
  146.     return false
  147. end
  148.  
  149. -- Main Program Execution
  150.  
  151. -- 1. Initial 4 block home offset (Moves to absolute Z = 4)
  152. print("Moving 4 blocks out...")
  153. while z < 4 do
  154.     moveForward()
  155. end
  156.  
  157. -- Save the starting Z boundary for the mining area
  158. local startZ = 4
  159.  
  160. -- 2. Mining Logic loop
  161. for layer = 1, targetY do
  162.     for row = 1, targetX do
  163.         for col = 1, targetZ - 1 do
  164.             moveForward()
  165.         end
  166.        
  167.         -- Turn to next row logic
  168.         if row < targetX then
  169.             if row % 2 == 1 then
  170.                 if layer % 2 == 1 then turnRight() moveForward() turnRight()
  171.                 else turnLeft() moveForward() turnLeft() end
  172.             else
  173.                 if layer % 2 == 1 then turnLeft() moveForward() turnLeft()
  174.                 else turnRight() moveForward() turnRight() end
  175.             end
  176.         end
  177.     end
  178.    
  179.     -- Move down to next layer if not finished
  180.     if layer < targetY then
  181.         while turtle.detectDown() do turtle.digDown() end
  182.         turtle.down()
  183.         y = y - 1
  184.         -- Turn around to reverse pattern for next layer efficiency
  185.         turnRight()
  186.         turnRight()
  187.     end
  188. end
  189.  
  190. -- Return Home permanently when complete
  191. print("Mining complete! Returning home.")
  192. goTo(0, 0, 0, 0)
  193. print("Done!")
  194.  
Advertisement