Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Smart Fill with True Robust Layer Return and Chest Dump
- local function askNumber(prompt)
- print(prompt)
- local val = tonumber(read())
- while not val do
- print("Please enter a valid number:")
- val = tonumber(read())
- end
- return math.floor(val)
- end
- local function askDirection()
- print("Build direction? Type 'up' for bottom-to-top or 'down' for top-to-bottom:")
- local dir = read()
- while dir ~= "up" and dir ~= "down" do
- print("Please type 'up' or 'down':")
- dir = read()
- end
- return dir
- end
- local moves = {}
- local heading = 0 -- 0: forward, 1: right, 2: back, 3: left
- local function forward()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.2)
- end
- table.insert(moves, {action="fwd"})
- end
- local function moveUp()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.2)
- end
- end
- local function moveDown()
- while not turtle.down() do
- turtle.digDown()
- sleep(0.2)
- end
- end
- local function turnRight()
- turtle.turnRight()
- heading = (heading + 1) % 4
- table.insert(moves, {action="tr"})
- end
- local function turnLeft()
- turtle.turnLeft()
- heading = (heading + 3) % 4
- table.insert(moves, {action="tl"})
- end
- local function turnRightRaw() turtle.turnRight(); heading = (heading + 1) % 4 end
- local function turnLeftRaw() turtle.turnLeft(); heading = (heading + 3) % 4 end
- local function refuelIfNeeded(needed)
- turtle.select(1)
- if turtle.getFuelLevel() == "unlimited" then return true end
- if turtle.getFuelLevel() < needed then
- print("Refueling from slot 1...")
- if not turtle.refuel(64) then
- print("⚠️ Could not refuel from slot 1.")
- else
- print("Fuel level: "..turtle.getFuelLevel())
- end
- end
- if turtle.getFuelLevel() < needed then
- print("❌ Not enough fuel. Need at least "..needed..".")
- return false
- end
- return true
- end
- local function dumpToChest()
- print("Dumping items to chest...")
- for i = 2, 16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item then
- if not (item.name == targetBlockName) then
- turtle.drop()
- end
- end
- end
- end
- local function isInventoryFull()
- for i = 3, 16 do
- if turtle.getItemCount(i) == 0 then return false end
- end
- return true
- end
- local function placeSlot2Block()
- turtle.digDown()
- if turtle.detectDown() then return true end
- for i = 2, 16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item and item.name == targetBlockName then
- return turtle.placeDown()
- end
- end
- print("⚠️ No matching block left to place.")
- return false
- end
- -- Ask user
- local length = askNumber("Enter length:")
- local width = askNumber("Enter width:")
- local height = askNumber("Enter height:")
- local direction = askDirection()
- -- Fuel estimate/check
- local estimatedMoves = length * width * height * 3 + (height - 1) * (length + width)
- print("Estimated fuel needed: "..estimatedMoves)
- if not refuelIfNeeded(estimatedMoves) then return end
- -- Block type from slot 2
- turtle.select(2)
- local slot2Block = turtle.getItemDetail()
- if not slot2Block then
- print("❌ No block found in slot 2. Please load desired block.")
- return
- end
- targetBlockName = slot2Block.name
- print("📦 Using only block type: "..targetBlockName)
- turtle.select(2)
- print("Place a chest in front of turtle's starting position for dumps.")
- -- Move forward to first block (starting reference)
- forward()
- -- True robust: return to origin of the layer and clear moves
- local function returnToOrigin()
- -- Face forward
- while heading ~= 0 do
- turnRightRaw()
- end
- -- Undo all moves in reverse order
- for i = #moves, 1, -1 do
- local move = moves[i]
- if move.action == "fwd" then
- turtle.back()
- elseif move.action == "up" then
- turtle.down()
- elseif move.action == "down" then
- turtle.up()
- elseif move.action == "tr" then
- turtle.turnLeft()
- heading = (heading + 3) % 4
- elseif move.action == "tl" then
- turtle.turnRight()
- heading = (heading + 1) % 4
- end
- end
- moves = {}
- end
- -- Build one layer, robust return at end
- local function buildLayer()
- for w = 1, width do
- for l = 1, length do
- turtle.dig()
- if not placeSlot2Block() then return false end
- if isInventoryFull() then
- print("Inventory full! Returning to dump in chest.")
- return "dump"
- end
- if l < length then forward() end
- end
- if w < width then
- if w % 2 == 1 then
- turnRight()
- forward()
- turnRight()
- else
- turnLeft()
- forward()
- turnLeft()
- end
- end
- end
- -- Always return to start of layer (lower/upper left corner, facing original)
- returnToOrigin()
- return true
- end
- -- Main build logic, moving up/down *after* return, clearing moves each time
- local function build()
- if direction == "up" then
- for h = 1, height do
- print("Building layer "..h.." / "..height)
- local layerResult = buildLayer()
- if layerResult == "dump" then
- dumpToChest()
- print("Please clear inventory and re-run script to finish the area!")
- return
- elseif not layerResult then return end
- if h < height then
- moveUp()
- moves = {} -- CLEAR move log at start of new layer
- end
- end
- else
- for i = 1, height - 1 do moveUp() end
- for h = height, 1, -1 do
- print("Building layer "..h.." / "..height)
- local layerResult = buildLayer()
- if layerResult == "dump" then
- dumpToChest()
- print("Please clear inventory and re-run script to finish the area!")
- return
- elseif not layerResult then return end
- if h > 1 then
- moveDown()
- moves = {} -- CLEAR move log at start of new layer
- end
- end
- end
- dumpToChest()
- -- At very end, turtle is back at original start, facing forward.
- end
- build()
- print("✅ Build complete! Returned to starting point and dumped inventory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement