Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Modular Mining Turtle Program
- Features:
- - Mines a rectangular area (WIDTH x DEPTH)
- - Clears blocks in front and above
- - Tracks position and returns to start
- - Handles fuel needs and movement safely
- --]]
- -- CONFIGURATION
- local WIDTH = 50 -- Number of "rows" wide (X)
- local DEPTH = 50 -- Number of blocks forward per row (Z)
- local MIN_FUEL = 50 -- Minimum fuel buffer
- -- STATE TRACKING
- local currentRow = 0
- local currentDepth = 0
- local isFacingForward = true
- -- SAFETY: Ensure enough fuel
- local function ensureFuel()
- if turtle.getFuelLevel() < MIN_FUEL then
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- print("Refueled using slot " .. i)
- break
- end
- end
- end
- end
- -- MOVEMENT WRAPPERS (safe movement)
- local function tryForward()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- sleep(0.5)
- end
- end
- end
- local function tryUp()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- sleep(0.5)
- end
- end
- end
- local function tryDown()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- sleep(0.5)
- end
- end
- end
- -- DIGGING HELPERS
- local function clearFront()
- if turtle.detect() then turtle.dig() end
- end
- local function clearUp()
- if turtle.detectUp() then turtle.digUp() end
- end
- -- MOVE FORWARD 1 BLOCK SAFELY WITH CLEARING
- local function moveAndClearForward()
- clearFront()
- clearUp()
- tryForward()
- currentDepth = currentDepth + (isFacingForward and 1 or -1)
- end
- -- SHIFT RIGHT TO NEXT ROW
- local function shiftRowRight()
- turtle.turnRight()
- clearFront()
- tryForward()
- turtle.turnRight()
- isFacingForward = not isFacingForward
- currentRow = currentRow + 1
- end
- -- SHIFT LEFT TO NEXT ROW
- local function shiftRowLeft()
- turtle.turnLeft()
- clearFront()
- tryForward()
- turtle.turnLeft()
- isFacingForward = not isFacingForward
- currentRow = currentRow + 1
- end
- -- MAIN DIG LOOP
- local function mineArea(width, depth)
- for row = 1, width do
- for step = 1, depth - 1 do
- ensureFuel()
- moveAndClearForward()
- end
- if row < width then
- if isFacingForward then
- shiftRowRight()
- else
- shiftRowLeft()
- end
- end
- end
- end
- -- RETURN TO START POSITION
- local function returnToOrigin()
- -- Face original direction (forward)
- if not isFacingForward then
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Back out to 0 depth
- for i = 1, math.abs(currentDepth) do
- tryForward()
- end
- -- Face left to return across rows
- turtle.turnLeft()
- for i = 1, currentRow do
- tryForward()
- end
- -- Face forward again
- turtle.turnRight()
- end
- -- MAIN
- print("Starting mining operation...")
- ensureFuel()
- mineArea(WIDTH, DEPTH)
- print("Mining complete. Returning to start...")
- returnToOrigin()
- print("All done.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement