DorianH935

dig

Jun 5th, 2024
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | Software | 0 0
  1. -- Function to dig forward and move
  2. function digForward()
  3.     while turtle.detect() do
  4.         turtle.dig()
  5.     end
  6.     turtle.forward()
  7. end
  8.  
  9. -- Function to dig down
  10. function digDown()
  11.     while turtle.detectDown() do
  12.         turtle.digDown()
  13.     end
  14.     turtle.down()
  15. end
  16.  
  17. -- Function to return the turtle to the starting point
  18. function returnToSurface(height)
  19.     for i = 1, height do
  20.         turtle.up()
  21.     end
  22. end
  23.  
  24. -- Function to dig a layer
  25. function digLayer(width)
  26.     for i = 1, width do
  27.         for j = 1, width - 1 do
  28.             digForward()
  29.         end
  30.         if i < width then
  31.             if i % 2 == 1 then
  32.                 turtle.turnRight()
  33.                 digForward()
  34.                 turtle.turnRight()
  35.             else
  36.                 turtle.turnLeft()
  37.                 digForward()
  38.                 turtle.turnLeft()
  39.             end
  40.         end
  41.     end
  42.     if width % 2 == 1 then
  43.         turtle.turnRight()
  44.         for i = 1, width - 1 do
  45.             turtle.forward()
  46.         end
  47.         turtle.turnRight()
  48.     else
  49.         turtle.turnLeft()
  50.         for i = 1, width - 1 do
  51.             turtle.forward()
  52.         end
  53.         turtle.turnLeft()
  54.     end
  55. end
  56.  
  57. -- Main function
  58. function digSquareHole(width, depth)
  59.     for level = 1, depth do
  60.         digLayer(width)
  61.         if level < depth then
  62.             digDown()
  63.         end
  64.     end
  65.     returnToSurface(depth)
  66. end
  67.  
  68. -- User input
  69. print("Enter the width of the hole:")
  70. local width = tonumber(read())
  71. print("Enter the depth of the hole:")
  72. local depth = tonumber(read())
  73.  
  74. -- Execute the digging program
  75. digSquareHole(width, depth)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment