Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local WIDTH = 32
- local LENGTH = 32
- local DEPTH = 32
- -- State Tracking
- local x, y, z = 0, 0, 0
- local facing = 0 -- 0: Forward, 1: Right, 2: Back, 3: Left
- local returnX, returnY, returnZ, returnFacing
- -- UI Functions
- local function drawUI(status)
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Advanced Mining Turtle ===")
- print("Status: " .. status)
- print(string.format("Pos : X:%d Y:%d Z:%d", x, y, z))
- print("Fuel : " .. turtle.getFuelLevel() .. "/" .. turtle.getFuelLimit())
- end
- -- Movement & Positioning
- local function turnRight()
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- local function turnLeft()
- turtle.turnLeft()
- facing = (facing - 1) % 4
- end
- local function moveForward()
- while turtle.detect() do turtle.dig() end
- if turtle.forward() then
- if facing == 0 then z = z + 1
- elseif facing == 1 then x = x + 1
- elseif facing == 2 then z = z - 1
- elseif facing == 3 then x = x - 1
- end
- return true
- end
- return false
- end
- local function moveUp()
- while turtle.detectUp() do turtle.digUp() end
- if turtle.up() then y = y + 1; return true end
- return false
- end
- local function moveDown()
- while turtle.detectDown() do turtle.digDown() end
- if turtle.down() then y = y - 1; return true end
- return false
- end
- local function turnTo(targetFacing)
- while facing ~= targetFacing do
- turnRight()
- end
- end
- -- Maintenance Checks
- local function needsFuel()
- local distanceHome = math.abs(x) + math.abs(y) + math.abs(z) + 5
- return turtle.getFuelLevel() < distanceHome
- end
- local function isFull()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then return false end
- end
- return true
- end
- -- Navigation Home and Back
- local function goHome()
- returnX, returnY, returnZ, returnFacing = x, y, z, facing
- drawUI("Returning Home...")
- -- Rise to safety height first to avoid walls, then go home
- while y < 0 do moveUp() end
- -- Align and move X
- if x > 0 then turnTo(3) while x > 0 do moveForward() end
- elseif x < 0 then turnTo(1) while x < 0 do moveForward() end
- end
- -- Align and move Z
- if z > 0 then turnTo(2) while z > 0 do moveForward() end
- elseif z < 0 then turnTo(0) while z < 0 do moveForward() end
- end
- -- Drop items behind
- turnTo(2)
- for i = 1, 15 do
- turtle.select(i)
- turtle.drop()
- end
- -- Refuel from chest above
- turtle.select(16)
- turtle.suckUp()
- turtle.refuel()
- turtle.select(1)
- end
- local function resumePosition()
- drawUI("Returning to Work...")
- -- Move Z
- if returnZ > 0 then turnTo(0) while z < returnZ do moveForward() end
- end
- -- Move X
- if returnX > 0 then turnTo(1) while x < returnX do moveForward() end
- end
- -- Move Down
- while y > returnY do moveDown() end
- turnTo(returnFacing)
- end
- local function checkMaintenance()
- if needsFuel() or isFull() then
- goHome()
- resumePosition()
- end
- end
- -- Core Mining Logic
- local function mineLayer()
- for i = 1, LENGTH do
- for j = 1, WIDTH - 1 do
- checkMaintenance()
- moveForward()
- end
- if i < LENGTH then
- if i % 2 == 1 then
- turnRight() moveForward() turnRight()
- else
- turnLeft() moveForward() turnLeft()
- end
- end
- end
- end
- local function main()
- turtle.select(1)
- turtle.refuel() -- Initial fuel check
- for layer = 1, DEPTH do
- drawUI("Mining Layer " .. layer)
- mineLayer()
- if layer < DEPTH then
- -- Return to start column of the layer before dropping down
- drawUI("Resetting Column...")
- while x > 0 do turnTo(3) moveForward() end
- while z > 0 do turnTo(2) moveForward() end
- turnTo(0)
- moveDown()
- end
- end
- drawUI("Job Finished! Going Home.")
- goHome()
- turnTo(0)
- drawUI("Done!")
- end
- main()
Advertisement