Advertisement
ZGAMMAA

Quarry

Apr 10th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. -- Turtle Quarry Script
  2.  
  3. -- User Input
  4. print("Enter Length:")
  5. local length = tonumber(read())
  6. print("Enter Width:")
  7. local width = tonumber(read())
  8. print("Enter Depth:")
  9. local depth = tonumber(read())
  10.  
  11. -- Position and Direction
  12. local x, y, z = 0, 0, 0 -- Relative to start
  13. local dir = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
  14.  
  15. -- Direction helpers
  16. local function turnRight()
  17. turtle.turnRight()
  18. dir = (dir + 1) % 4
  19. end
  20.  
  21. local function turnLeft()
  22. turtle.turnLeft()
  23. dir = (dir - 1) % 4
  24. end
  25.  
  26. local function faceDirection(targetDir)
  27. while dir ~= targetDir do
  28. turnRight()
  29. end
  30. end
  31.  
  32. -- Movement helpers
  33. local function refuelIfNeeded()
  34. if turtle.getFuelLevel() < 50 then
  35. for i = 1, 16 do
  36. turtle.select(i)
  37. if turtle.refuel(1) then
  38. print("Refueled with slot "..i)
  39. return
  40. end
  41. end
  42. print("Out of fuel! Insert fuel and press Enter.")
  43. read()
  44. refuelIfNeeded()
  45. end
  46. end
  47.  
  48. local function tryForward()
  49. refuelIfNeeded()
  50. while not turtle.forward() do
  51. turtle.dig()
  52. sleep(0.5)
  53. end
  54. end
  55.  
  56. local function tryDown()
  57. refuelIfNeeded()
  58. while not turtle.down() do
  59. turtle.digDown()
  60. sleep(0.5)
  61. end
  62. end
  63.  
  64. local function tryUp()
  65. refuelIfNeeded()
  66. while not turtle.up() do
  67. turtle.digUp()
  68. sleep(0.5)
  69. end
  70. end
  71.  
  72. -- Return to origin
  73. local function returnToStart()
  74. -- Go back to X = 0, Z = 0
  75. faceDirection(0) -- North
  76. while z > 0 do turtle.back(); z = z - 1 end
  77. faceDirection(3) -- West
  78. while x > 0 do turtle.back(); x = x - 1 end
  79. -- Go up to surface
  80. while y > 0 do tryUp(); y = y - 1 end
  81. end
  82.  
  83. -- Go to resume point
  84. local function goTo(xTarget, yTarget, zTarget)
  85. while y < yTarget do tryDown(); y = y + 1 end
  86. faceDirection(1) -- East
  87. while x < xTarget do tryForward(); x = x + 1 end
  88. faceDirection(2) -- South
  89. while z < zTarget do tryForward(); z = z + 1 end
  90. end
  91.  
  92. -- Inventory Management
  93. local function isInventoryFull()
  94. for i = 1, 16 do
  95. if turtle.getItemCount(i) == 0 then return false end
  96. end
  97. return true
  98. end
  99.  
  100. local function dumpInventory()
  101. for i = 1, 16 do
  102. turtle.select(i)
  103. turtle.drop()
  104. end
  105. end
  106.  
  107. -- Main quarry function
  108. local function digQuarry()
  109. for d = 1, depth 3 do
  110. for w = 1, width do
  111. for l = 1, length - 1 do
  112. tryForward()
  113. if isInventoryFull() then
  114. local savedX, savedY, savedZ = x, y, z
  115. returnToStart()
  116. dumpInventory()
  117. refuelIfNeeded()
  118. goTo(savedX, savedY, savedZ)
  119. faceDirection(dir)
  120. end
  121. x = x + (dir == 1 and 1 or dir == 3 and -1 or 0)
  122. z = z + (dir == 2 and 1 or dir == 0 and -1 or 0)
  123. end
  124. if w < width then
  125. if (w % 2 == 1) then turnRight(); tryForward(); turnRight()
  126. else turnLeft(); tryForward(); turnLeft()
  127. end
  128. dir = (dir + (w % 2 == 1 and 2 or 6)) % 4
  129. x = x + (dir == 1 and 1 or dir == 3 and -1 or 0)
  130. z = z + (dir == 2 and 1 or dir == 0 and -1 or 0)
  131. end
  132. end
  133. if d < depth then
  134. tryDown()
  135. y = y + 1
  136. turnLeft(); turnLeft()
  137. end
  138. end
  139. returnToStart()
  140. dumpInventory()
  141. end
  142.  
  143. digQuarry()
  144. print("Quarrying complete.")
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement