Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Fill Area with Dirt using a Turtle
- -- Function to ask for a number
- local function askForNumber(prompt)
- print(prompt)
- local input = read()
- return tonumber(input)
- end
- -- Prompt user for dimensions
- local length = askForNumber("Enter length:")
- local width = askForNumber("Enter width:")
- local height = askForNumber("Enter height:")
- -- Select dirt in inventory (first found slot)
- local function selectDirt()
- for i = 1, 16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item and item.name:find("dirt") then
- return true
- end
- end
- return false
- end
- -- Place a block below the turtle
- local function placeDown()
- if not turtle.detectDown() then
- if not turtle.placeDown() then
- if not selectDirt() then
- print("Out of dirt!")
- return false
- end
- if not turtle.placeDown() then
- print("Failed to place dirt.")
- return false
- end
- end
- end
- return true
- end
- -- Move forward with obstacle handling
- local function forward()
- while not turtle.forward() do
- turtle.dig()
- sleep(0.5)
- end
- end
- -- Build one horizontal layer
- local function buildLayer()
- for w = 1, width do
- for l = 1, length do
- placeDown()
- if l < length then
- forward()
- end
- end
- -- Move to next row
- if w < width then
- if w % 2 == 1 then
- turtle.turnRight()
- forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- forward()
- turtle.turnLeft()
- end
- end
- end
- -- Return to starting position of the layer
- if width % 2 == 1 then
- turtle.turnRight()
- for i = 1, width - 1 do forward() end
- turtle.turnRight()
- else
- if width > 1 then
- if width % 2 == 0 then
- turtle.turnLeft()
- for i = 1, width - 1 do forward() end
- turtle.turnLeft()
- end
- end
- end
- end
- -- Main loop: build each layer
- for h = 1, height do
- buildLayer()
- if h < height then
- while not turtle.up() do
- turtle.digUp()
- sleep(0.5)
- end
- end
- end
- print("Done building!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement