Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants definition
- local WIDTH = 15 -- horizontal dimension of the house floor
- local LENGTH = 20 -- vertical dimension of the house floor (use even number for better repositioning)
- local WALL_HEIGHT = 5 -- number of wall layers
- local OPENING_BOTTOM = 2 -- opening (window/door) starts at level 2
- local OPENING_TOP = 4 -- opening ends at level 4 (i.e. 3 blocks tall)
- local COBBLE_SLOT = 1 -- default inventory slot for cobblestone
- local PLANK_SLOT = 2 -- default inventory slot for roof material
- -- Expected material names (adjust if needed)
- local COBBLE_NAME = "minecraft:cobblestone"
- local PLANK_NAME = "minecraft:planks"
- -- Walls table in clockwise order.
- -- Assumes that after repositioning the turtle is at the southwest corner of the floor, facing east.
- -- Then its right side will be south, followed by west, north, and east.
- local walls = {
- {length = WIDTH, openingStart = math.floor((WIDTH - 3) / 2) + 1, door = true}, -- south wall (door)
- {length = LENGTH, openingStart = math.floor((LENGTH - 3) / 2) + 1, door = false}, -- west wall (window)
- {length = WIDTH, openingStart = math.floor((WIDTH - 3) / 2) + 1, door = false}, -- north wall (window)
- {length = LENGTH, openingStart = math.floor((LENGTH - 3) / 2) + 1, door = false} -- east wall (window)
- }
- -- Function to ensure that the active slot for a given material is not empty.
- -- If the default slot is empty, search all slots for an item matching materialName.
- function ensureMaterial(materialName, defaultSlot)
- if turtle.getItemCount(defaultSlot) > 0 then
- turtle.select(defaultSlot)
- return
- else
- for i = 1, 16 do
- local detail = turtle.getItemDetail(i)
- if detail and detail.name == materialName and detail.count > 0 then
- turtle.select(i)
- return
- end
- end
- end
- error("Out of material: " .. materialName)
- end
- -- New function to check if there is a hole (air) below the turtle.
- function isHoleBelow()
- if not turtle.detectDown() then
- return true
- end
- local success, data = turtle.inspectDown()
- if not success then
- return true
- else
- return data.name == "minecraft:air"
- end
- end
- -- Safe movement functions with obstacle removal
- function safeForward()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig() -- dig block in front if present
- else
- turtle.attack() -- attack mob if present
- end
- os.sleep(0.5)
- end
- end
- function safeUp()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp() -- clear block above
- else
- turtle.attackUp() -- attack entity above if needed
- end
- os.sleep(0.5)
- end
- end
- function safeDown()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown() -- clear block below if present
- else
- os.sleep(0.5)
- end
- end
- end
- -- Movement function used during ground leveling and roof building that does not remove obstacles.
- function forwardNoDig()
- while not turtle.forward() do
- os.sleep(0.5)
- end
- end
- -- Level the ground by traversing a WIDTH x LENGTH area in a snake pattern.
- -- Assumes the turtle starts at the southwest corner of the target area, facing east.
- function levelGround(width, length)
- for row = 1, length do
- for col = 1, width do
- if isHoleBelow() then
- ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
- turtle.placeDown()
- os.sleep(0.2) -- delay to allow block placement
- end
- if col < width then
- safeForward()
- end
- end
- if row < length then
- if row % 2 == 1 then
- turtle.turnLeft() -- from east, left -> north
- safeForward()
- turtle.turnLeft() -- now facing west
- else
- turtle.turnRight() -- from west, right -> north
- safeForward()
- turtle.turnRight() -- now facing east
- end
- end
- end
- -- Reposition the turtle to the starting corner (southwest) with correct orientation.
- repositionToStart(width, length)
- end
- -- Reposition the turtle to the southwest corner facing east.
- -- Based on the snake fill pattern, final position depends on whether length is odd or even.
- function repositionToStart(width, length)
- if length % 2 == 1 then
- -- For odd number of rows, final position is at (width, length) facing east (northeast corner).
- turtle.turnRight()
- turtle.turnRight() -- now facing west
- for i = 1, (width - 1) do
- safeForward()
- end
- turtle.turnLeft() -- now facing south
- for i = 1, (length - 1) do
- safeForward()
- end
- turtle.turnLeft() -- now facing east at (1,1)
- else
- -- For even number of rows, final position is at (1, length) facing west (northwest corner).
- turtle.turnLeft() -- from west, left -> south
- for i = 1, (length - 1) do
- safeForward()
- end
- turtle.turnLeft() -- from south, left -> east
- end
- end
- -- Function to build one wall segment along the current edge.
- -- The turtle places a block on its right if the current position is not within the opening.
- function buildWallSegmentRight(wallLength, openingStart, currentLevel)
- for pos = 1, wallLength do
- if not (currentLevel >= OPENING_BOTTOM and currentLevel <= OPENING_TOP and pos >= openingStart and pos < openingStart + 3) then
- turtle.turnRight() -- face right (exterior side)
- ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
- if turtle.detect() then
- turtle.dig() -- clear obstacle if needed
- end
- turtle.place() -- place block on exterior side
- turtle.turnLeft() -- return to original direction
- os.sleep(0.1) -- short delay after placing
- end
- if pos < wallLength then
- safeForward()
- end
- end
- end
- -- Build all wall layers by traversing the interior perimeter.
- -- Assumes the turtle starts at the southwest interior corner facing east.
- function buildWalls()
- for level = 1, WALL_HEIGHT do
- for i = 1, 4 do
- local wall = walls[i]
- buildWallSegmentRight(wall.length, wall.openingStart, level)
- turtle.turnRight() -- prepare for next wall segment
- end
- if level < WALL_HEIGHT then
- safeUp() -- move up to next layer
- -- After ascending, the turtle is assumed to be in the correct corner.
- end
- end
- end
- -- New roof building function.
- -- Instead of placing blocks above the turtle (which blocks its movement),
- -- we ascend one level and build the roof by placing blocks below the turtle.
- function buildRoof()
- print("Ascending to roof build level...")
- safeUp() -- move one block up so that the roof is built beneath the turtle
- for row = 1, LENGTH do
- for col = 1, WIDTH do
- ensureMaterial(PLANK_NAME, PLANK_SLOT)
- if turtle.detectDown() then
- -- Do not dig if the block below is already correct; assume it's our roof block.
- local success, data = turtle.inspectDown()
- if not (success and data.name == PLANK_NAME) then
- turtle.digDown()
- end
- end
- turtle.placeDown() -- place roof block below
- os.sleep(0.1) -- short delay after placing
- if col < WIDTH then
- safeForward()
- end
- end
- if row < LENGTH then
- if row % 2 == 1 then
- turtle.turnRight()
- safeForward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- safeForward()
- turtle.turnLeft()
- end
- end
- end
- print("Roof build complete. Descending back to roof level...")
- safeDown() -- optionally, return to roof level
- end
- -- Main function executing the building process; accepts varargs for command-line parameters.
- function main(...)
- local args = {...}
- local quickMode = false
- if args[1] and args[1] == "quick" then
- quickMode = true
- end
- if not quickMode then
- print("Starting ground leveling...")
- levelGround(WIDTH, LENGTH)
- else
- print("Quick mode activated: skipping ground leveling.")
- end
- print("Starting wall building...")
- buildWalls()
- print("Building roof...")
- buildRoof()
- print("Construction complete.")
- end
- -- Execute main program with command-line arguments
- main(...)
Add Comment
Please, Sign In to add comment