Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Recursive function to dig a line
- function digLine(blocks)
- if blocks == 0 then
- return
- else
- -- Check if there is a block to dig
- if turtle.detect() then
- turtle.dig()
- end
- -- Move forward
- if turtle.forward() then
- digLine(blocks - 1) -- Recurse for the next block
- else
- -- If the turtle can't move forward, it might be because it needs to dig or because it's out of fuel
- if not turtle.dig() then
- print("Turtle is unable to move forward and can't dig. Check for obstacles or refuel.")
- return
- end
- -- Try to move forward again after digging
- if turtle.forward() then
- digLine(blocks - 1)
- else
- print("Turtle is really stuck and can't move forward after digging. Check for obstacles.")
- return
- end
- end
- end
- end
- -- Recursive function to dig the entire field
- function digField(length, width)
- if width == 0 then
- return
- else
- -- Dig a line
- digLine(length)
- -- Turn around to start the next line
- if width % 2 == 0 then
- turtle.turnLeft()
- if turtle.forward() then
- turtle.turnLeft()
- else
- return -- End if the turtle cannot move to start a new line
- end
- else
- turtle.turnRight()
- if turtle.forward() then
- turtle.turnRight()
- else
- return -- End if the turtle cannot move to start a new line
- end
- end
- -- Recurse for the next line
- digField(length, width - 1)
- end
- end
- -- Start the digging process
- digField(6, 6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement