Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Hallway Builder Program (Updated with Slots 2 and 3 for Wall Blocks)
- -- Ensure the turtle has enough fuel
- local function ensureFuel(needed)
- if turtle.getFuelLevel() < needed then
- print("Not enough fuel. Please refuel the turtle.")
- error()
- end
- end
- -- Check if the turtle can move forward
- local function safeForward()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- sleep(0.5)
- end
- end
- end
- -- Return to the starting position
- local function returnToStart(steps)
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, steps do
- safeForward()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Select a slot containing wall blocks (Slots 2 and 3)
- local function selectWallBlock()
- if turtle.getItemCount(2) > 0 then
- turtle.select(2)
- elseif turtle.getItemCount(3) > 0 then
- turtle.select(3)
- else
- print("Out of wall blocks!")
- error()
- end
- end
- -- Build the floor and first layer of walls
- local function buildFirstLayer(length)
- for i = 1, length do
- -- Place floor block
- turtle.select(1) -- Floor blocks
- turtle.placeDown()
- -- Place wall blocks to the left and right
- selectWallBlock()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- turtle.place()
- turtle.turnLeft()
- -- Move forward
- ensureFuel(1)
- safeForward()
- end
- end
- -- Build wall layers
- local function buildWallLayer(length)
- for i = 1, length do
- selectWallBlock()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- turtle.place()
- turtle.turnLeft()
- -- Move forward
- ensureFuel(1)
- safeForward()
- end
- end
- -- Build the ceiling with half slabs
- local function buildCeiling(length)
- for i = 1, length do
- selectWallBlock()
- turtle.turnLeft()
- turtle.place()
- turtle.turnRight()
- turtle.turnRight()
- turtle.place()
- turtle.turnLeft()
- -- Place ceiling half slab
- turtle.select(4) -- Ceiling half slabs
- turtle.placeUp()
- -- Move forward
- ensureFuel(1)
- safeForward()
- end
- end
- -- Main Program
- local hallwayLength = 20
- -- Calculate required fuel
- local totalMoves = hallwayLength * 6 -- Approximate total movements
- ensureFuel(totalMoves)
- -- Build the first layer (floor and bottom walls)
- buildFirstLayer(hallwayLength)
- returnToStart(hallwayLength)
- -- Move up to the second layer
- ensureFuel(1)
- turtle.up()
- -- Build the second layer of walls
- buildWallLayer(hallwayLength)
- returnToStart(hallwayLength)
- -- Move up to the third layer
- ensureFuel(1)
- turtle.up()
- -- Build the third layer of walls and the ceiling
- buildCeiling(hallwayLength)
- returnToStart(hallwayLength)
- -- Move back down to the ground level
- ensureFuel(2)
- turtle.down()
- turtle.down()
- print("Hallway construction complete!")
Add Comment
Please, Sign In to add comment