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
- local COBBLE_SLOT = 1 -- preferred inventory slot for cobblestone (or chosen material)
- local COBBLE_NAME = "minecraft:cobblestone"
- -- Clearance above the build area: 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
- -- Function to ensure that the active slot has the required material.
- -- Automatically checks all inventory slots.
- function ensureMaterial(materialName, defaultSlot)
- if turtle.getItemCount(defaultSlot) > 0 then
- turtle.select(defaultSlot)
- return true
- end
- 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
- -- 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
- -- New function: fillHole(fillDepth)
- -- Заполняет ямку по вертикали, начиная с нижнего уровня.
- function fillHole(fillDepth)
- if fillDepth <= 1 then
- if isHoleBelow() then
- ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
- turtle.placeDown()
- os.sleep(0.2)
- end
- return
- end
- -- Спускаемся до нижнего уровня ямы (максимум fillDepth-1 раз)
- local descent = 0
- for i = 1, fillDepth - 1 do
- if isHoleBelow() then
- safeDown()
- descent = descent + 1
- else
- break
- end
- end
- -- На нижнем уровне: ставим блок, если там пусто
- if isHoleBelow() then
- ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
- turtle.placeDown()
- os.sleep(0.2)
- end
- -- Поднимаемся обратно, заполняя ямку по ходу подъёма
- for i = 1, descent do
- safeUp()
- if isHoleBelow() then
- ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
- turtle.placeDown()
- os.sleep(0.2)
- 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 moving forward on a sidestep
- 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 a bit
- 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 to level the ground in a snake pattern.
- -- При обнаружении ямы, вызывается функция fillHole() с заданной глубиной.
- 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 to reposition the turtle to the starting point after the snake pattern.
- 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() -- facing east at the starting position
- else
- turtle.turnLeft() -- from west, left -> south
- for i = 1, (length - 1) do
- safeForward()
- end
- turtle.turnLeft() -- now facing east
- end
- end
- -- Main function.
- -- Параметры командной строки:
- -- 1: leveling area width, 2: leveling area 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): " .. clearance .. " block(s)")
- print("Fill depth for holes: " .. fillDepth .. " block(s)")
- levelGround(width, length, clearance, fillDepth)
- print("Leveling complete. The area is ready for building.")
- end
- -- Run the main module with command-line arguments.
- main(...)
Add Comment
Please, Sign In to add comment