Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Quarry Script
- -- User Input
- print("Enter Length:")
- local length = tonumber(read())
- print("Enter Width:")
- local width = tonumber(read())
- print("Enter Depth:")
- local depth = tonumber(read())
- -- Position and Direction
- local x, y, z = 0, 0, 0 -- Relative to start
- local dir = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
- -- Direction helpers
- local function turnRight()
- turtle.turnRight()
- dir = (dir + 1) % 4
- end
- local function turnLeft()
- turtle.turnLeft()
- dir = (dir - 1) % 4
- end
- local function faceDirection(targetDir)
- while dir ~= targetDir do
- turnRight()
- end
- end
- -- Movement helpers
- local function refuelIfNeeded()
- if turtle.getFuelLevel() < 50 then
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- print("Refueled with slot "..i)
- return
- end
- end
- print("Out of fuel! Insert fuel and press Enter.")
- read()
- refuelIfNeeded()
- end
- end
- local function tryForward()
- refuelIfNeeded()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.5)
- end
- end
- local function tryDown()
- refuelIfNeeded()
- while not turtle.down() do
- turtle.digDown()
- sleep(0.5)
- end
- end
- local function tryUp()
- refuelIfNeeded()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.5)
- end
- end
- -- Return to origin
- local function returnToStart()
- -- Go back to X = 0, Z = 0
- faceDirection(0) -- North
- while z > 0 do turtle.back(); z = z - 1 end
- faceDirection(3) -- West
- while x > 0 do turtle.back(); x = x - 1 end
- -- Go up to surface
- while y > 0 do tryUp(); y = y - 1 end
- end
- -- Go to resume point
- local function goTo(xTarget, yTarget, zTarget)
- while y < yTarget do tryDown(); y = y + 1 end
- faceDirection(1) -- East
- while x < xTarget do tryForward(); x = x + 1 end
- faceDirection(2) -- South
- while z < zTarget do tryForward(); z = z + 1 end
- end
- -- Inventory Management
- local function isInventoryFull()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then return false end
- end
- return true
- end
- local function dumpInventory()
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- end
- -- Main quarry function
- local function digQuarry()
- for d = 1, depth 3 do
- for w = 1, width do
- for l = 1, length - 1 do
- tryForward()
- if isInventoryFull() then
- local savedX, savedY, savedZ = x, y, z
- returnToStart()
- dumpInventory()
- refuelIfNeeded()
- goTo(savedX, savedY, savedZ)
- faceDirection(dir)
- end
- x = x + (dir == 1 and 1 or dir == 3 and -1 or 0)
- z = z + (dir == 2 and 1 or dir == 0 and -1 or 0)
- end
- if w < width then
- if (w % 2 == 1) then turnRight(); tryForward(); turnRight()
- else turnLeft(); tryForward(); turnLeft()
- end
- dir = (dir + (w % 2 == 1 and 2 or 6)) % 4
- x = x + (dir == 1 and 1 or dir == 3 and -1 or 0)
- z = z + (dir == 2 and 1 or dir == 0 and -1 or 0)
- end
- end
- if d < depth then
- tryDown()
- y = y + 1
- turnLeft(); turnLeft()
- end
- end
- returnToStart()
- dumpInventory()
- end
- digQuarry()
- print("Quarrying complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement