Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- === Utility Functions ===
- 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
- function placeBlockDown()
- while not turtle.placeDown() do
- if turtle.getItemCount() == 0 and not selectNextSlot() then
- error("Out of blocks!")
- end
- sleep(0.1)
- end
- end
- function placeBlockUp()
- while not turtle.placeUp() do
- if turtle.getItemCount() == 0 and not selectNextSlot() then
- error("Out of blocks!")
- end
- sleep(0.1)
- end
- end
- function placeBlockForward()
- while not turtle.place() do
- if turtle.getItemCount() == 0 and not selectNextSlot() then
- error("Out of blocks!")
- end
- sleep(0.1)
- end
- end
- function forwardSafe()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.1)
- end
- end
- function upSafe()
- while not turtle.up() do
- turtle.digUp()
- sleep(0.1)
- end
- end
- function downSafe()
- while not turtle.down() do
- turtle.digDown()
- sleep(0.1)
- end
- end
- -- === Structure Building Functions ===
- 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 starting corner
- 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
- function buildWalls(length, width, height)
- local function wallSegment()
- for h = 1, height - 1 do
- placeBlockForward()
- if h < height - 1 then upSafe() end
- end
- for h = 1, height - 2 do downSafe() end
- end
- local function moveAndWall(dist)
- for i = 1, dist - 1 do
- forwardSafe()
- wallSegment()
- end
- end
- wallSegment()
- moveAndWall(length)
- turtle.turnRight()
- wallSegment()
- moveAndWall(width)
- turtle.turnRight()
- wallSegment()
- moveAndWall(length)
- turtle.turnRight()
- wallSegment()
- moveAndWall(width)
- turtle.turnRight()
- end
- 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
- function buildHollowBox(length, width, height)
- if length < 2 or width < 2 or height < 2 then
- error("❌ Dimensions must be at least 2x2x2.")
- end
- if turtle.getFuelLevel() == 0 then
- error("❌ Turtle needs fuel! Use turtle.refuel().")
- end
- if turtle.getItemCount() == 0 and not selectNextSlot() then
- error("❌ No blocks in inventory.")
- end
- print("▶ Building floor...")
- buildFloor(length, width)
- print("▶ Building walls...")
- buildWalls(length, width, height)
- print("▶ Building ceiling...")
- buildCeiling(length, width, height)
- print("✅ Done!")
- end
- -- === Menu Prompt ===
- function promptNumber(prompt)
- while true do
- io.write(prompt)
- local input = read()
- local number = tonumber(input)
- if number and number >= 2 then
- return math.floor(number)
- else
- print("Please enter a number ≥ 2.")
- end
- end
- end
- -- === Main Program ===
- print("=== Build Hollow Structure ===")
- local len = promptNumber("Enter length (≥2): ")
- local wid = promptNumber("Enter width (≥2): ")
- local hei = promptNumber("Enter height (≥2): ")
- print("▶ Starting build: " .. len .. " x " .. wid .. " x " .. hei)
- buildHollowBox(len, wid, hei)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement