Advertisement
OttomateN

DigAlpha

Mar 4th, 2025 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.58 KB | None | 0 0
  1. -- Stores the turtle's position relative to the starting point
  2. local x, y, z = 0, 0, 0  
  3. local facing = 0  -- 0 = forward, 1 = right, 2 = backward, 3 = left
  4.  
  5. function refuel()
  6.     if turtle.getFuelLevel() < 50 then
  7.         turtle.refuel()
  8.     end
  9. end
  10.  
  11. function digMove()
  12.     refuel()
  13.     turtle.digUp()
  14.     turtle.dig()
  15.     turtle.digDown()
  16.     turtle.forward()
  17.     updatePosition(1)
  18.    
  19.     if inventoryFull() then
  20.         returnToStart()
  21.         dropOffItems()
  22.         returnToLastPosition()
  23.     end
  24. end
  25.  
  26. function updatePosition(moveForward)
  27.     if moveForward then
  28.         if facing == 0 then x = x + 1
  29.         elseif facing == 1 then z = z + 1
  30.         elseif facing == 2 then x = x - 1
  31.         elseif facing == 3 then z = z - 1
  32.         end
  33.     end
  34. end
  35.  
  36. function turnRight()
  37.     turtle.turnRight()
  38.     facing = (facing + 1) % 4
  39. end
  40.  
  41. function turnLeft()
  42.     turtle.turnLeft()
  43.     facing = (facing - 1) % 4
  44.     if facing < 0 then facing = 3 end
  45. end
  46.  
  47. function inventoryFull()
  48.     return turtle.getItemCount(16) > 0  -- Checks if last slot is filled
  49. end
  50.  
  51. function safeMoveBack()
  52.     while not turtle.back() do
  53.         turtle.dig()
  54.     end
  55. end
  56.  
  57. function safeMoveUp()
  58.     while not turtle.up() do
  59.         turtle.digUp()
  60.     end
  61. end
  62.  
  63. function safeMoveDown()
  64.     while not turtle.down() do
  65.         turtle.digDown()
  66.     end
  67. end
  68.  
  69. function returnToStart()
  70.     print("Returning to start...")
  71.    
  72.     -- Move back to (0,0,0), clearing obstacles
  73.     while x > 0 do safeMoveBack(); x = x - 1 end
  74.     while z > 0 do turnLeft(); safeMoveBack(); z = z - 1; turnRight() end
  75.     while y < 0 do safeMoveUp(); y = y + 1 end
  76. end
  77.  
  78. function dropOffItems()
  79.     print("Dropping off items...")
  80.     for i = 2, 16 do  -- Keep only slot 1 (fuel)
  81.         turtle.select(i)
  82.         turtle.drop()
  83.     end
  84.     turtle.select(1)  -- Re-select fuel slot
  85. end
  86.  
  87. function returnToLastPosition()
  88.     print("Returning to last dig position...")
  89.  
  90.     -- Move back down
  91.     while y > 0 do safeMoveDown(); y = y - 1 end
  92.     -- Move back on z-axis
  93.     while z < 0 do turnRight(); safeMoveBack(); z = z + 1; turnLeft() end
  94.     -- Move forward to x position
  95.     while x < 0 do turtle.forward(); x = x + 1 end
  96.  
  97.     print("Resuming excavation.")
  98. end
  99.  
  100. function digLayer(width, length)
  101.     for row = 1, length do
  102.         for col = 1, width - 1 do
  103.             digMove()
  104.         end
  105.  
  106.         if row < length then
  107.             if row % 2 == 1 then
  108.                 turnRight()
  109.                 digMove()
  110.                 turnRight()
  111.             else
  112.                 turnLeft()
  113.                 digMove()
  114.                 turnLeft()
  115.             end
  116.         end
  117.     end
  118.  
  119.     if length % 2 == 0 then
  120.         turnRight()
  121.         for i = 1, width - 1 do
  122.             digMove()
  123.         end
  124.         turnRight()
  125.     else
  126.         turnLeft()
  127.         turnLeft()
  128.     end
  129. end
  130.  
  131. function digArea(width, length, depth)
  132.     for level = 1, depth do
  133.         digLayer(width, length)
  134.  
  135.         if level < depth then
  136.             for _ = 1, 3 do  -- Move down 3 blocks per layer
  137.                 turtle.digDown()
  138.                 turtle.down()
  139.                 y = y - 1
  140.             end
  141.         end
  142.     end
  143. end
  144.  
  145. function start()
  146.     print("Enter width, length, and depth:")
  147.     local width = tonumber(io.read())
  148.     local length = tonumber(io.read())
  149.     local depth = tonumber(io.read())
  150.  
  151.     if width < 1 or length < 1 or depth < 1 then
  152.         print("Dimensions must be positive numbers.")
  153.         return
  154.     end
  155.  
  156.     digArea(width, length, depth)
  157.     print("Excavation complete!")
  158. end
  159.  
  160. start()
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement