Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Stores the turtle's position relative to the starting point
- local x, y, z = 0, 0, 0
- local facing = 0 -- 0 = forward, 1 = right, 2 = backward, 3 = left
- function refuel()
- if turtle.getFuelLevel() < 50 then
- turtle.refuel()
- end
- end
- function digMove()
- refuel()
- turtle.digUp()
- turtle.dig()
- turtle.digDown()
- turtle.forward()
- updatePosition(1)
- if inventoryFull() then
- returnToStart()
- dropOffItems()
- returnToLastPosition()
- end
- end
- function updatePosition(moveForward)
- if moveForward then
- if facing == 0 then x = x + 1
- elseif facing == 1 then z = z + 1
- elseif facing == 2 then x = x - 1
- elseif facing == 3 then z = z - 1
- end
- end
- end
- function turnRight()
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- function turnLeft()
- turtle.turnLeft()
- facing = (facing - 1) % 4
- if facing < 0 then facing = 3 end
- end
- function inventoryFull()
- return turtle.getItemCount(16) > 0 -- Checks if last slot is filled
- end
- function safeMoveBack()
- while not turtle.back() do
- turtle.dig()
- end
- end
- function safeMoveUp()
- while not turtle.up() do
- turtle.digUp()
- end
- end
- function safeMoveDown()
- while not turtle.down() do
- turtle.digDown()
- end
- end
- function returnToStart()
- print("Returning to start...")
- -- Move back to (0,0,0), clearing obstacles
- while x > 0 do safeMoveBack(); x = x - 1 end
- while z > 0 do turnLeft(); safeMoveBack(); z = z - 1; turnRight() end
- while y < 0 do safeMoveUp(); y = y + 1 end
- end
- function dropOffItems()
- print("Dropping off items...")
- for i = 2, 16 do -- Keep only slot 1 (fuel)
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1) -- Re-select fuel slot
- end
- function returnToLastPosition()
- print("Returning to last dig position...")
- -- Move back down
- while y > 0 do safeMoveDown(); y = y - 1 end
- -- Move back on z-axis
- while z < 0 do turnRight(); safeMoveBack(); z = z + 1; turnLeft() end
- -- Move forward to x position
- while x < 0 do turtle.forward(); x = x + 1 end
- print("Resuming excavation.")
- end
- function digLayer(width, length)
- for row = 1, length do
- for col = 1, width - 1 do
- digMove()
- end
- if row < length then
- if row % 2 == 1 then
- turnRight()
- digMove()
- turnRight()
- else
- turnLeft()
- digMove()
- turnLeft()
- end
- end
- end
- if length % 2 == 0 then
- turnRight()
- for i = 1, width - 1 do
- digMove()
- end
- turnRight()
- else
- turnLeft()
- turnLeft()
- end
- end
- function digArea(width, length, depth)
- for level = 1, depth do
- digLayer(width, length)
- if level < depth then
- for _ = 1, 3 do -- Move down 3 blocks per layer
- turtle.digDown()
- turtle.down()
- y = y - 1
- end
- end
- end
- end
- function start()
- print("Enter width, length, and depth:")
- local width = tonumber(io.read())
- local length = tonumber(io.read())
- local depth = tonumber(io.read())
- if width < 1 or length < 1 or depth < 1 then
- print("Dimensions must be positive numbers.")
- return
- end
- digArea(width, length, depth)
- print("Excavation complete!")
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement