Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Default settings.
- local HOUSE_WIDTH = 15 -- House width (excluding borders)
- local HOUSE_LENGTH = 20 -- House length (excluding borders)
- -- Leveling area: house dimensions + 1 block border on each side
- local LEVEL_WIDTH = HOUSE_WIDTH + 2
- local LEVEL_LENGTH = HOUSE_LENGTH + 2
- -- Vertical clearance: only the immediate block by default.
- local CLEARANCE = 1
- -- FILL_DEPTH determines how many blocks downward to fill when a hole is encountered.
- local FILL_DEPTH = 2 -- Default fill depth; can be overridden via command-line argument
- -- Global variable to store the desired material name.
- local desiredMaterial = nil
- -- Function to get the desired material from the inventory.
- -- It first checks slot 1; if that is empty, it searches all slots.
- local function getDesiredMaterial()
- local detail = turtle.getItemDetail(1)
- if detail then
- desiredMaterial = detail.name
- return desiredMaterial
- else
- for i = 2, 16 do
- local d = turtle.getItemDetail(i)
- if d then
- desiredMaterial = d.name
- return desiredMaterial
- end
- end
- end
- error("No material found in inventory. Please add the desired material.")
- end
- -- Function that searches the inventory for the desired material.
- -- It selects the first slot containing that material.
- function ensureMaterialFromSlot1()
- local materialName = getDesiredMaterial()
- 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 true
- end
- end
- error("Out of material: " .. materialName)
- end
- -- Function to remove tall grass below the turtle.
- -- It checks the block below, and if it is "minecraft:grass" or "minecraft:tall_grass", it digs it.
- function removeTallGrassDown()
- local success, block = turtle.inspectDown()
- if success and (block.name == "minecraft:grass" or block.name == "minecraft:tall_grass") then
- turtle.digDown()
- os.sleep(0.2)
- end
- end
- -- Function to check if the block below should be replaced.
- -- Returns true if no block is detected or if the block below is air.
- function isHoleBelow()
- if not turtle.detectDown() then
- return true
- end
- local success, data = turtle.inspectDown()
- if not success then
- return true
- else
- if data.name == "minecraft:air" then
- return true
- else
- return false
- end
- end
- end
- -- Safe forward movement with obstacle removal and mob detection.
- function safeForward()
- local attempts = 0
- while not turtle.forward() do
- if turtle.detect() then
- local success, data = turtle.inspect()
- if success then
- turtle.dig()
- else
- turtle.attack()
- end
- else
- turtle.attack()
- end
- attempts = attempts + 1
- if attempts >= 5 then
- print("Persistent obstacle detected in front. Attempting to bypass...")
- turtle.turnLeft()
- safeForward() -- try sidestepping
- turtle.turnRight()
- attempts = 0
- end
- os.sleep(0.5)
- end
- end
- -- Safe upward movement with obstacle removal and mob detection.
- function safeUp()
- local attempts = 0
- while not turtle.up() do
- if turtle.detectUp() then
- local success, data = turtle.inspectUp()
- if success then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- else
- turtle.attackUp()
- end
- attempts = attempts + 1
- if attempts >= 5 then
- print("Persistent obstacle detected above. Attempting alternative maneuver...")
- turtle.turnLeft()
- safeForward() -- move sideways
- turtle.turnRight()
- attempts = 0
- end
- os.sleep(0.5)
- end
- end
- -- Safe downward movement with obstacle removal and basic mob check.
- function safeDown()
- local attempts = 0
- while not turtle.down() do
- if turtle.detectDown() then
- local success, data = turtle.inspectDown()
- if success then
- turtle.digDown()
- else
- turtle.attack()
- end
- else
- turtle.attack()
- end
- attempts = attempts + 1
- if attempts >= 5 then
- print("Persistent obstacle detected below. Waiting before retrying...")
- attempts = 0
- end
- os.sleep(0.5)
- end
- end
- -- Function to check vertical clearance above the turtle.
- function checkVerticalClearance(clearance)
- if turtle.detectUp() then
- return false
- end
- if clearance > 1 then
- for i = 2, clearance do
- safeUp()
- if turtle.detectUp() then
- for j = 1, i do safeDown() end
- return false
- end
- end
- for i = 2, clearance do safeDown() end
- end
- return true
- end
- -- Function fillHole(fillDepth)
- -- Fills a hole downward to the specified depth, starting from the lowest level.
- -- Before placing a block, it calls removeTallGrassDown() so that only individual tall grass is removed.
- function fillHole(fillDepth)
- if fillDepth <= 1 then
- if isHoleBelow() then
- removeTallGrassDown()
- ensureMaterialFromSlot1()
- turtle.placeDown()
- os.sleep(0.2)
- end
- return
- end
- -- Descend to the lowest level (up to fillDepth-1 times)
- local descent = 0
- for i = 1, fillDepth - 1 do
- if isHoleBelow() then
- removeTallGrassDown()
- safeDown()
- descent = descent + 1
- else
- break
- end
- end
- -- At the lowest level, place a block after removing tall grass if present
- if isHoleBelow() then
- removeTallGrassDown()
- ensureMaterialFromSlot1()
- turtle.placeDown()
- os.sleep(0.2)
- end
- -- Ascend back, filling the hole on the way up
- for i = 1, descent do
- safeUp()
- if isHoleBelow() then
- removeTallGrassDown()
- ensureMaterialFromSlot1()
- turtle.placeDown()
- os.sleep(0.2)
- end
- end
- end
- -- Function levelGround()
- -- Traverses the leveling area in a snake pattern.
- -- When a hole is detected, fillHole(fillDepth) is called.
- function levelGround(width, length, clearance, fillDepth)
- clearance = clearance or CLEARANCE
- fillDepth = fillDepth or FILL_DEPTH
- for row = 1, length do
- for col = 1, width do
- if isHoleBelow() then
- fillHole(fillDepth)
- end
- if not checkVerticalClearance(clearance) then
- turtle.digUp()
- os.sleep(0.1)
- end
- 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
- repositionToStart(width, length)
- end
- -- Function repositionToStart()
- -- Returns the turtle to the starting position after traversing the area.
- function repositionToStart(width, length)
- if length % 2 == 1 then
- 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() -- returns to original orientation (east)
- else
- turtle.turnLeft() -- turn from west to south
- for i = 1, (length - 1) do
- safeForward()
- end
- turtle.turnLeft() -- now facing east
- end
- end
- -- Main function.
- -- Command-line parameters:
- -- 1: Leveling area width, 2: length, 3: vertical clearance, 4: fill depth.
- function main(...)
- local args = {...}
- local width = tonumber(args[1]) or LEVEL_WIDTH
- local length = tonumber(args[2]) or LEVEL_LENGTH
- local clearance = tonumber(args[3]) or CLEARANCE
- local fillDepth = tonumber(args[4]) or FILL_DEPTH
- print("Leveling area size: " .. width .. " x " .. length)
- print("Vertical clearance: " .. clearance .. " block(s)")
- print("Fill depth: " .. fillDepth .. " block(s)")
- levelGround(width, length, clearance, fillDepth)
- print("Leveling complete. The area is ready for building.")
- end
- -- Run main function with command-line parameters.
- main(...)
Add Comment
Please, Sign In to add comment