This morning i was asked a few questions so i'm going to update the code to include comments and to allow arguments. It also has a block counter just for curiosity sake. I can add a few lines to detect when it is finished, if you ask. I thought a manual entry for height allowed for more flexible usage tho(like clearing an underground chamber), so i left it out.
when you run the script, use this syntax: turtleFlatten [length] [width] [height]
like this: turtleFlatten 16 8 20
This will mine 16x8x20 blocks.
--TurtleFlatten
--rich1051414
--------------------------------------------------
--Sends turtle out to flatten a plot land
--------------------------------------------------
--Check arguments for correct formatting
local args = { ... }
if #args ~= 3 then --Check if it's not exactly 3 arguments.
shell.run("clear")
print("Invalid number of arguments")
print("turtleflatten [length] [width] [height]")
do return end
end
for i = 1, 3 do --Step through each argument and make sure it is a number greater than 1.
if tonumber(args[i]) < 2 then
shell.run("clear")
print("Invalid arguments")
print("All measurements must be numbers greater than 1")
do return end
end
end
--------------------------------------------------
--commandline arguments [length] [width] [height]
length = tonumber(args[1]) --length of square
width = tonumber(args[2]) --width of square
height = tonumber(args[3]) --how high it needs to go
--------------------------------------------------
--------------------------------------------------
-- Now for the logic
--------------------------------------------------
-- Boolean to remember which way to turn.
direction = false
LayerBlockCount = 0
TotalBlocksBusted = 0
------------------------------------------
-- Nested loops, 1 each for length width and height.
shell.run("clear")
print("Flatten land initialized.\n")
write("Length: ")
write(tostring(length))
write(" Width: ")
write(tostring(width))
write(" Height: ")
write(tostring(height))
print("\n\n\n")
for x = 1, height do
write("Layer ")
write(tostring(x))
write(" started.\n\n")
for xx = 1, width - 1 do
for xxx = 1, length do
--Digs until the turtle can move forward, to deal with gravel and sand.
repeat
if turtle.detect() then
TotalBlocksBusted = TotalBlocksBusted + 1
end
turtle.dig()
until turtle.forward() == true
--------------------------------------------------
end
-- Orientation.
if direction == false then
direction = true
turtle.turnRight()
repeat
if turtle.detect() then
TotalBlocksBusted = TotalBlocksBusted + 1
end
turtle.dig()
until turtle.forward() == true
turtle.turnRight()
else
direction = false
turtle.turnLeft()
repeat
if turtle.detect() then
TotalBlocksBusted = TotalBlocksBusted + 1
end
turtle.dig()
until turtle.forward() == true
turtle.turnLeft()
end
--------------------------------------------------
end
-- Last line finished outside of loop to remove unneeded steps.
for xxx = 1, length do
repeat
if turtle.detect() then
TotalBlocksBusted = TotalBlocksBusted + 1
end
turtle.dig()
until turtle.forward() == true
end
--------------------------------------------------
--Checks if it needs to go up or just turn around for completion.
if x < height then
repeat
if turtle.detectUp() then
TotalBlocksBusted = TotalBlocksBusted + 1
end
turtle.digUp()
until turtle.up() == true
turtle.turnRight()
turtle.turnRight()
else
turtle.turnRight()
turtle.turnRight()
end
--------------------------------------------------
end
print("Job Completed.\n")
--Return turtle to starting point(if all dimensions are even numbers)
print("Returning...\n")
for a = 1, height do
turtle.down()
end
write(tostring(TotalBlocksBusted))
write(" total blocks busted.\n\n")
print("Powering down.\n")
--------------------------------------------------