Advertisement
Slinger0001

Mine2

May 18th, 2025 (edited)
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. --[[
  2.     Modular Mining Turtle Program
  3.     Features:
  4.     - Mines a rectangular area (WIDTH x DEPTH)
  5.     - Clears blocks in front and above
  6.     - Tracks position and returns to start
  7.     - Handles fuel needs and movement safely
  8. --]]
  9.  
  10. -- CONFIGURATION
  11. local WIDTH = 50         -- Number of "rows" wide (X)
  12. local DEPTH = 50        -- Number of blocks forward per row (Z)
  13. local MIN_FUEL = 50     -- Minimum fuel buffer
  14.  
  15. -- STATE TRACKING
  16. local currentRow = 0
  17. local currentDepth = 0
  18. local isFacingForward = true
  19.  
  20. -- SAFETY: Ensure enough fuel
  21. local function ensureFuel()
  22.     if turtle.getFuelLevel() < MIN_FUEL then
  23.         for i = 1, 16 do
  24.             turtle.select(i)
  25.             if turtle.refuel(1) then
  26.                 print("Refueled using slot " .. i)
  27.                 break
  28.             end
  29.         end
  30.     end
  31. end
  32.  
  33. -- MOVEMENT WRAPPERS (safe movement)
  34. local function tryForward()
  35.     while not turtle.forward() do
  36.         if turtle.detect() then
  37.             turtle.dig()
  38.         else
  39.             sleep(0.5)
  40.         end
  41.     end
  42. end
  43.  
  44. local function tryUp()
  45.     while not turtle.up() do
  46.         if turtle.detectUp() then
  47.             turtle.digUp()
  48.         else
  49.             sleep(0.5)
  50.         end
  51.     end
  52. end
  53.  
  54. local function tryDown()
  55.     while not turtle.down() do
  56.         if turtle.detectDown() then
  57.             turtle.digDown()
  58.         else
  59.             sleep(0.5)
  60.         end
  61.     end
  62. end
  63.  
  64. -- DIGGING HELPERS
  65. local function clearFront()
  66.     if turtle.detect() then turtle.dig() end
  67. end
  68.  
  69. local function clearUp()
  70.     if turtle.detectUp() then turtle.digUp() end
  71. end
  72.  
  73. -- MOVE FORWARD 1 BLOCK SAFELY WITH CLEARING
  74. local function moveAndClearForward()
  75.     clearFront()
  76.     clearUp()
  77.     tryForward()
  78.     currentDepth = currentDepth + (isFacingForward and 1 or -1)
  79. end
  80.  
  81. -- SHIFT RIGHT TO NEXT ROW
  82. local function shiftRowRight()
  83.     turtle.turnRight()
  84.     clearFront()
  85.     tryForward()
  86.     turtle.turnRight()
  87.     isFacingForward = not isFacingForward
  88.     currentRow = currentRow + 1
  89. end
  90.  
  91. -- SHIFT LEFT TO NEXT ROW
  92. local function shiftRowLeft()
  93.     turtle.turnLeft()
  94.     clearFront()
  95.     tryForward()
  96.     turtle.turnLeft()
  97.     isFacingForward = not isFacingForward
  98.     currentRow = currentRow + 1
  99. end
  100.  
  101. -- MAIN DIG LOOP
  102. local function mineArea(width, depth)
  103.     for row = 1, width do
  104.         for step = 1, depth - 1 do
  105.             ensureFuel()
  106.             moveAndClearForward()
  107.         end
  108.  
  109.         if row < width then
  110.             if isFacingForward then
  111.                 shiftRowRight()
  112.             else
  113.                 shiftRowLeft()
  114.             end
  115.         end
  116.     end
  117. end
  118.  
  119. -- RETURN TO START POSITION
  120. local function returnToOrigin()
  121.     -- Face original direction (forward)
  122.     if not isFacingForward then
  123.         turtle.turnLeft()
  124.         turtle.turnLeft()
  125.     end
  126.  
  127.     -- Back out to 0 depth
  128.     for i = 1, math.abs(currentDepth) do
  129.         tryForward()
  130.     end
  131.  
  132.     -- Face left to return across rows
  133.     turtle.turnLeft()
  134.     for i = 1, currentRow do
  135.         tryForward()
  136.     end
  137.  
  138.     -- Face forward again
  139.     turtle.turnRight()
  140. end
  141.  
  142. -- MAIN
  143. print("Starting mining operation...")
  144. ensureFuel()
  145. mineArea(WIDTH, DEPTH)
  146. print("Mining complete. Returning to start...")
  147. returnToOrigin()
  148. print("All done.")
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement