Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Fill: Single-Layer Skip, Chest Behind, Robust Home Tracking
- -- == Helpers ==
- heading = 0
- posX, posY, posZ = 0, 0, 0
- function turnRight()
- turtle.turnRight()
- heading = (heading + 1) % 4
- end
- function turnLeft()
- turtle.turnLeft()
- heading = (heading + 3) % 4
- end
- function face(dir)
- local turns = (dir - heading) % 4
- if turns == 1 then turnRight()
- elseif turns == 2 then turnRight() turnRight()
- elseif turns == 3 then turnLeft()
- end
- end
- function forward()
- while not turtle.forward() do turtle.dig() sleep(0.2) end
- if heading == 0 then posX = posX + 1
- elseif heading == 1 then posZ = posZ + 1
- elseif heading == 2 then posX = posX - 1
- else posZ = posZ - 1 end
- end
- function moveUp() while not turtle.up() do turtle.digUp() sleep(0.2) end posY = posY + 1 end
- function moveDown() while not turtle.down() do turtle.digDown() sleep(0.2) end posY = posY - 1 end
- function refuelIfNeeded(needed)
- turtle.select(1)
- if turtle.getFuelLevel() == "unlimited" then return true end
- if turtle.getFuelLevel() < needed then
- print("Refueling from slot 1...")
- turtle.refuel(64)
- end
- return turtle.getFuelLevel() >= needed
- end
- function dumpToChest()
- print("Dumping items to chest behind...")
- turnLeft(); turnLeft()
- for i = 2, 16 do
- turtle.select(i)
- local itm = turtle.getItemDetail()
- if itm and itm.name ~= targetBlockName then turtle.drop() end
- end
- turnLeft(); turnLeft()
- end
- function isInventoryFull()
- for i = 3, 16 do if turtle.getItemCount(i) == 0 then return false end end
- return true
- end
- function placeSlot2Block()
- turtle.digDown()
- if turtle.detectDown() then return true end
- for i = 2, 16 do
- turtle.select(i)
- local itm = turtle.getItemDetail()
- if itm and itm.name == targetBlockName then return turtle.placeDown() end
- end
- return "out_of_blocks"
- end
- function goTo(tx, ty, tz, th)
- while posY < ty do moveUp() end
- while posY > ty do moveDown() end
- if posX < tx then face(0) while posX < tx do forward() end
- elseif posX > tx then face(2) while posX > tx do forward() end end
- if posZ < tz then face(1) while posZ < tz do forward() end
- elseif posZ > tz then face(3) while posZ > tz do forward() end end
- face(th)
- end
- -- == Input ==
- function askNumber(p)
- print(p)
- local n = tonumber(read())
- while not n do print("Enter a number:") n = tonumber(read()) end
- return math.floor(n)
- end
- function askDirection()
- print("Build 'up' or 'down'?")
- local d = read()
- while d ~= "up" and d ~= "down" do print("Type 'up' or 'down'") d = read() end
- return d
- end
- local length = askNumber("Length?")
- local width = askNumber("Width?")
- local height = askNumber("Height?")
- local direction
- if height == 1 then
- direction = "up"
- print("Single layer: defaulting to up.")
- else
- direction = askDirection()
- end
- local est = length * width * height * 3 + (height-1)*(length+width)
- print("Need fuel:", est)
- if not refuelIfNeeded(est) then
- print("Not enough fuel!"); return
- end
- turtle.select(2)
- local blk = turtle.getItemDetail()
- if not blk then print("Put fill block in slot 2."); return end
- targetBlockName = blk.name
- print("Using:", targetBlockName)
- print("Put chest BEHIND turtle before starting.")
- forward()
- function buildLayer()
- for w = 1, width do
- for l = 1, length do
- turtle.dig()
- local pr = placeSlot2Block()
- if pr == "out_of_blocks" then
- goTo(0,0,0,0); dumpToChest()
- print("Out of blocks. Returned home. Refill and rerun.")
- return "stop"
- elseif not pr then return false end
- if isInventoryFull() then
- goTo(0,0,0,0); dumpToChest()
- print("Inventory full. Returned home.")
- return "stop"
- 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
- goTo(0,posY,0,0)
- return true
- end
- -- == Build Main ==
- function build()
- if direction == "up" then
- for h = 1, height do
- print("Layer",h,"/",height)
- local r = buildLayer()
- if r=="stop" then return end
- if h<height then moveUp() end
- end
- else
- for i=1,height-1 do moveUp() end
- for h=height,1,-1 do
- print("Layer",h,"/",height)
- local r = buildLayer()
- if r=="stop" then return end
- if h>1 then moveDown() end
- end
- end
- goTo(0,0,0,0); dumpToChest()
- print("✅ Build complete!")
- end
- build()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement