Advertisement
eshipplosk

Turtle Quarry v.2

Apr 20th, 2025 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. -- USER INPUT
  2. print("Enter length:")
  3. local length = tonumber(read())
  4. print("Enter width:")
  5. local width = tonumber(read())
  6. print("Enter depth:")
  7. local depth = tonumber(read())
  8.  
  9. local fuelSlot = 16
  10.  
  11. -- STATE
  12. local x, y, z = 0, 0, 0 -- relative coordinates
  13. local dir = 0           -- 0=N, 1=E, 2=S, 3=W
  14.  
  15. -- === MOVEMENT HELPERS ===
  16. function refuel()
  17.     if turtle.getFuelLevel() < 10 then
  18.         turtle.select(fuelSlot)
  19.         turtle.refuel(1)
  20.     end
  21. end
  22.  
  23. function tryDig() while turtle.detect() do turtle.dig(); sleep(0.4) end end
  24. function tryDigDown() while turtle.detectDown() do turtle.digDown(); sleep(0.4) end end
  25. function tryDigUp() while turtle.detectUp() do
  26. turtle.digUp(); sleep(0.4) end end
  27.  
  28. function forward()
  29.     refuel()
  30.     tryDig()
  31.     while not turtle.forward() do sleep(0.2) end
  32.     if dir == 0 then z = z + 1
  33.     elseif dir == 1 then x = x + 1
  34.     elseif dir == 2 then z = z - 1
  35.     elseif dir == 3 then x = x - 1 end
  36. end
  37.  
  38. function back()
  39.     refuel()
  40.     while not turtle.back() do sleep(0.2) end
  41.     if dir == 0 then z = z - 1
  42.     elseif dir == 1 then x = x - 1
  43.     elseif dir == 2 then z = z + 1
  44.     elseif dir == 3 then x = x + 1 end
  45. end
  46.  
  47. function down()
  48.     refuel()
  49.     tryDigDown()
  50.     while not turtle.down() do sleep(0.2) end
  51.     y = y - 1
  52. end
  53.  
  54. function up()
  55.     refuel()
  56.     tryDigUp()
  57.     while not turtle.up() do sleep(0.2) end
  58.     y = y + 1
  59. end
  60.  
  61. function turnLeft()
  62.     turtle.turnLeft()
  63.     dir = (dir - 1) % 4
  64.     if dir < 0 then dir = dir + 4 end
  65. end
  66.  
  67. function turnRight()
  68.     turtle.turnRight()
  69.     dir = (dir + 1) % 4
  70. end
  71.  
  72. function face(target)
  73.     while dir ~= target do turnRight() end
  74. end
  75.  
  76. -- === GO TO COORDINATE ===
  77. function goTo(targetX, targetY, targetZ, targetDir)
  78.     -- Go up first (safety)
  79.     while y < targetY do up() end
  80.     while y > targetY do down() end
  81.  
  82.     if x < targetX then face(1) while x < targetX do forward() end
  83.     elseif x > targetX then face(3) while x > targetX do forward() end end
  84.  
  85.     if z < targetZ then face(0) while z < targetZ do forward() end
  86.     elseif z > targetZ then face(2) while z > targetZ do forward() end end
  87.  
  88.     face(targetDir or 0)
  89. end
  90.  
  91. -- === INVENTORY HANDLING ===
  92. function isInventoryFull()
  93.     for i = 1, 15 do
  94.         if turtle.getItemCount(i) == 0 then return false end
  95.     end
  96.     return true
  97. end
  98.  
  99. function dropItems()
  100.     local saveX, saveY, saveZ, saveDir = x, y, z, dir
  101.     goTo(0, 0, 0, 2)
  102.     for i = 1, 15 do
  103.         turtle.select(i)
  104.         turtle.drop()
  105.     end
  106.     turtle.select(1)
  107.     goTo(saveX, saveY, saveZ, saveDir)
  108. end
  109.  
  110. -- === MINING FUNCTION ===
  111. function mineLayer()
  112.     for row = 1, width do
  113.         for col = 1, length - 1 do
  114.             forward()
  115.             if isInventoryFull() then dropItems() end
  116.         end
  117.         if row < width then
  118.             if row % 2 == 1 then turnRight(); forward(); turnRight()
  119.             else turnLeft(); forward(); turnLeft() end
  120.         end
  121.     end
  122.     -- Return to top-left corner of next layer
  123.     if width % 2 == 1 then face(2) for i = 1, length - 1 do forward() end end
  124.     face(3) for i = 1, width - 1 do forward() end
  125.     face(0)
  126. end
  127.  
  128. -- === MAIN EXECUTION ===
  129. print("Starting mining...")
  130. turtle.digDown()
  131. down()
  132.  
  133. for d = 1, depth do
  134.     mineLayer()
  135.     if d < depth then down() end
  136. end
  137.  
  138. -- Final return to origin, face chest
  139. goTo(0, 0, 0, 2)
  140. for i = 1, 15 do
  141.     turtle.select(i)
  142.     turtle.drop()
  143. end
  144. turtle.select(1)
  145. goTo(0, 0, 0, 0)
  146. print("Mining complete. Ready to unload into chest.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement