Second_Cup

Untitled

Jul 8th, 2025 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. -- Turtle Dig Down Script with Full Perimeter Safety and Return Logic
  2. -- Digs down while maintaining blocks in all 4 horizontal directions
  3. -- Removes any liquids encountered below
  4. -- Returns to original position when finished
  5. -- Uses any available blocks for placement
  6.  
  7. local tArgs = {...}
  8. local depth = tonumber(tArgs[1])
  9. local shouldReturn = tArgs[2] == "return" or tArgs[2] == "true"
  10.  
  11. if not depth or depth < 1 then
  12. print("Usage: digdown <depth> [return]")
  13. print("Example: digdown 20")
  14. print("Example with return: digdown 20 return")
  15. return
  16. end
  17.  
  18. -- Track movements for return trip
  19. local movements = {}
  20. local currentY = 0
  21.  
  22. -- Function to check if block is liquid
  23. local function isLiquid(block)
  24. return block and (block.name:find("water") or block.name:find("lava"))
  25. end
  26.  
  27. -- Function to find any placeable block in inventory
  28. local function findPlaceableBlock()
  29. for slot = 1, 16 do
  30. turtle.select(slot)
  31. if turtle.getItemCount(slot) > 0 then
  32. return true
  33. end
  34. end
  35. return false
  36. end
  37.  
  38. -- Function to ensure all 4 directions have blocks
  39. local function ensurePerimeter()
  40. -- Check and place blocks in all 4 directions
  41. for i = 1, 4 do
  42. local success, data = turtle.inspect()
  43. if not success or isLiquid(data) then
  44. -- Try to place any available block
  45. if findPlaceableBlock() then
  46. if not turtle.place() then
  47. print("Warning: Failed to place block in slot "..turtle.getSelectedSlot())
  48. end
  49. else
  50. print("Error: No placeable blocks in inventory!")
  51. return false
  52. end
  53. end
  54. turtle.turnRight() -- Rotate to next direction
  55. end
  56. return true
  57. end
  58.  
  59. -- Function to safely dig down and maintain perimeter
  60. local function safeDigDown()
  61. -- First ensure perimeter is solid
  62. if not ensurePerimeter() then
  63. return false
  64. end
  65.  
  66. -- Check block below
  67. local success, data = turtle.inspectDown()
  68. if success then
  69. if isLiquid(data) then
  70. -- Handle liquid below with any available block
  71. if findPlaceableBlock() then
  72. if not turtle.placeDown() then
  73. print("Warning: Failed to place block below")
  74. end
  75. else
  76. print("Error: No blocks to place below!")
  77. return false
  78. end
  79. end
  80. turtle.digDown()
  81. end
  82.  
  83. -- Move down and record movement
  84. if turtle.down() then
  85. currentY = currentY - 1
  86. table.insert(movements, "down")
  87. -- Ensure new level has perimeter
  88. return ensurePerimeter()
  89. end
  90. return false
  91. end
  92.  
  93. -- Function to return to surface
  94. local function returnToSurface()
  95. print("Returning to surface...")
  96.  
  97. -- Reverse movement log
  98. for i = #movements, 1, -1 do
  99. local move = movements[i]
  100. if move == "down" then
  101. while not turtle.up() do
  102. turtle.digUp()
  103. sleep(0.5)
  104. end
  105. currentY = currentY + 1
  106. end
  107. end
  108.  
  109. print("Returned to surface successfully! Current Y: "..currentY)
  110. end
  111.  
  112. -- Check initial fuel requirements
  113. local requiredFuel = depth * 3 -- Extra for return trip and perimeter building
  114. if turtle.getFuelLevel() < requiredFuel then
  115. print("Need at least "..requiredFuel.." fuel units")
  116. print("Current fuel: "..turtle.getFuelLevel())
  117. return
  118. end
  119.  
  120. -- Check for any blocks at all
  121. local hasBlocks = false
  122. for i = 1, 16 do
  123. if turtle.getItemCount(i) > 0 then
  124. hasBlocks = true
  125. break
  126. end
  127. end
  128.  
  129. if not hasBlocks then
  130. print("Warning: No blocks in inventory for perimeter building!")
  131. print("Recommend bringing at least "..depth.." blocks")
  132. end
  133.  
  134. -- Main digging loop
  135. print("Starting descent to depth "..depth.."...")
  136. for i = 1, depth do
  137. if not safeDigDown() then
  138. print("Stopped at level "..i.." due to error")
  139. if shouldReturn then returnToSurface() end
  140. return
  141. end
  142.  
  143. print("Level "..i.." - Fuel: "..turtle.getFuelLevel())
  144.  
  145. -- Periodic status report
  146. if i % 5 == 0 then
  147. local remaining = depth - i
  148. if turtle.getFuelLevel() < remaining * 3 then
  149. print("Warning: Low fuel! "..turtle.getFuelLevel().." left, needs ~"..remaining*3)
  150. end
  151. end
  152. end
  153.  
  154. print("Successfully reached depth "..depth.." with full perimeter safety!")
  155.  
  156. -- Return to surface if requested
  157. if shouldReturn then
  158. returnToSurface()
  159. end
Advertisement
Add Comment
Please, Sign In to add comment