Advertisement
Josqus

Untitled

Nov 6th, 2023
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. -- Recursive function to dig a line
  2. function digLine(blocks)
  3. if blocks == 0 then
  4. return
  5. else
  6. -- Check if there is a block to dig
  7. if turtle.detect() then
  8. turtle.dig()
  9. end
  10. -- Move forward
  11. if turtle.forward() then
  12. digLine(blocks - 1) -- Recurse for the next block
  13. else
  14. -- If the turtle can't move forward, it might be because it needs to dig or because it's out of fuel
  15. if not turtle.dig() then
  16. print("Turtle is unable to move forward and can't dig. Check for obstacles or refuel.")
  17. return
  18. end
  19. -- Try to move forward again after digging
  20. if turtle.forward() then
  21. digLine(blocks - 1)
  22. else
  23. print("Turtle is really stuck and can't move forward after digging. Check for obstacles.")
  24. return
  25. end
  26. end
  27. end
  28. end
  29.  
  30. -- Recursive function to dig the entire field
  31. function digField(length, width)
  32. if width == 0 then
  33. return
  34. else
  35. -- Dig a line
  36. digLine(length)
  37. -- Turn around to start the next line
  38. if width % 2 == 0 then
  39. turtle.turnLeft()
  40. if turtle.forward() then
  41. turtle.turnLeft()
  42. else
  43. return -- End if the turtle cannot move to start a new line
  44. end
  45. else
  46. turtle.turnRight()
  47. if turtle.forward() then
  48. turtle.turnRight()
  49. else
  50. return -- End if the turtle cannot move to start a new line
  51. end
  52. end
  53. -- Recurse for the next line
  54. digField(length, width - 1)
  55. end
  56. end
  57.  
  58. -- Start the digging process
  59. digField(6, 6)
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement