Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Helper: Select next non-empty slot
- function selectNextSlot()
- for i = 1, 16 do
- local slot = (turtle.getSelectedSlot() % 16) + 1
- turtle.select(slot)
- if turtle.getItemCount() > 0 then
- return true
- end
- end
- return false
- end
- -- Helper: Try placing a block in a direction, handling slot changes
- function placeBlockDown()
- while not turtle.placeDown() do
- if turtle.getItemCount() == 0 then
- if not selectNextSlot() then error("Out of blocks!") end
- else
- sleep(0.2)
- end
- end
- end
- function placeBlockUp()
- while not turtle.placeUp() do
- if turtle.getItemCount() == 0 then
- if not selectNextSlot() then error("Out of blocks!") end
- else
- sleep(0.2)
- end
- end
- end
- function placeBlockForward()
- while not turtle.place() do
- if turtle.getItemCount() == 0 then
- if not selectNextSlot() then error("Out of blocks!") end
- else
- sleep(0.2)
- end
- end
- end
- -- Helpers: Movement (with digging if needed)
- function forwardSafe()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.2)
- end
- end
- function upSafe()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.2)
- end
- end
- function downSafe()
- while not turtle.down() do
- turtle.digDown()
- sleep(0.2)
- end
- end
- -- Build the floor at the current level
- function buildFloor(length, width)
- for w = 1, width do
- for l = 1, length do
- placeBlockDown()
- if l < length then forwardSafe() end
- end
- if w < width then
- if w % 2 == 1 then
- turtle.turnRight()
- forwardSafe()
- turtle.turnRight()
- else
- turtle.turnLeft()
- forwardSafe()
- turtle.turnLeft()
- end
- end
- end
- -- Return to bottom-left corner facing original direction
- if width % 2 == 1 then
- turtle.turnRight()
- for i = 1, length - 1 do turtle.back() end
- turtle.turnRight()
- else
- turtle.turnLeft()
- for i = 1, length - 1 do turtle.back() end
- turtle.turnLeft()
- end
- for i = 1, width - 1 do turtle.back() end
- end
- -- Build walls around the structure
- function buildWalls(length, width, height)
- local function buildWallSegment()
- for h = 1, height - 2 do
- upSafe()
- placeBlockForward()
- end
- for i = 1, height - 2 do downSafe() end
- end
- local function moveAndWall(dist)
- for i = 1, dist - 1 do
- forwardSafe()
- buildWallSegment()
- end
- end
- buildWallSegment()
- moveAndWall(length)
- turtle.turnRight()
- buildWallSegment()
- moveAndWall(width)
- turtle.turnRight()
- buildWallSegment()
- moveAndWall(length)
- turtle.turnRight()
- buildWallSegment()
- moveAndWall(width)
- turtle.turnRight()
- end
- -- Build ceiling (go to height, then build floor again at top)
- function buildCeiling(length, width, height)
- for i = 1, height - 1 do upSafe() end
- buildFloor(length, width)
- for i = 1, height - 1 do downSafe() end
- end
- -- Main function
- function buildHollowBox(length, width, height)
- if length < 2 or width < 2 or height < 2 then
- error("Dimensions must be at least 2x2x2.")
- end
- -- Ensure a valid slot is selected
- if turtle.getItemCount() == 0 then
- if not selectNextSlot() then
- error("No blocks in inventory!")
- end
- end
- buildFloor(length, width)
- buildWalls(length, width, height)
- buildCeiling(length, width, height)
- print("✅ Hollow structure complete.")
- end
- -- Example call:
- -- Uncomment and customize the below line to run directly:
- -- buildHollowBox(5, 4, 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement