Advertisement
Dafmak

turt

May 10th, 2025 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. -- Modified for mirrored dual-turtle deployment
  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. print("Is this the LEFT turtle? (y/n)")
  10. local isLeft = read():lower() == "y"
  11.  
  12. local fuelSlot = 16
  13.  
  14. -- STATE
  15. local x, y, z = 0, 0, 0 -- relative coordinates
  16. local dir = 0           -- 0=N, 1=E, 2=S, 3=W
  17.  
  18. -- === MOVEMENT HELPERS ===
  19. function refuel()
  20.     if turtle.getFuelLevel() < 10 then
  21.         turtle.select(fuelSlot)
  22.         if not turtle.refuel(1) then
  23.             print("⚠️ Low fuel and no coal in slot 16!")
  24.             sleep(5)
  25.         end
  26.     end
  27. end
  28.  
  29. function tryDig() while turtle.detect() do turtle.dig(); sleep(0.4) end end
  30. function tryDigDown() while turtle.detectDown() do turtle.digDown(); sleep(0.4) end end
  31. function tryDigUp() while turtle.detectUp() do turtle.digUp(); sleep(0.4) end end
  32.  
  33. function forward()
  34.     refuel()
  35.     tryDig()
  36.     while not turtle.forward() do sleep(0.2) end
  37.     if dir == 0 then z = z + 1
  38.     elseif dir == 1 then x = x + 1
  39.     elseif dir == 2 then z = z - 1
  40.     elseif dir == 3 then x = x - 1 end
  41. end
  42.  
  43. function back()
  44.     refuel()
  45.     while not turtle.back() do sleep(0.2) end
  46.     if dir == 0 then z = z - 1
  47.     elseif dir == 1 then x = x - 1
  48.     elseif dir == 2 then z = z + 1
  49.     elseif dir == 3 then x = x + 1 end
  50. end
  51.  
  52. function down()
  53.     refuel()
  54.     tryDigDown()
  55.     while not turtle.down() do sleep(0.2) end
  56.     y = y - 1
  57. end
  58.  
  59. function up()
  60.     refuel()
  61.     tryDigUp()
  62.     while not turtle.up() do sleep(0.2) end
  63.     y = y + 1
  64. end
  65.  
  66. function turnLeft()
  67.     turtle.turnLeft()
  68.     dir = (dir - 1) % 4
  69.     if dir < 0 then dir = dir + 4 end
  70. end
  71.  
  72. function turnRight()
  73.     turtle.turnRight()
  74.     dir = (dir + 1) % 4
  75. end
  76.  
  77. function face(target)
  78.     while dir ~= target do turnRight() end
  79. end
  80.  
  81. -- === GO TO COORDINATE ===
  82. function goTo(targetX, targetY, targetZ, targetDir)
  83.     while y < targetY do up() end
  84.     while y > targetY do down() end
  85.  
  86.     if x < targetX then face(1) while x < targetX do forward() end
  87.     elseif x > targetX then face(3) while x > targetX do forward() end end
  88.  
  89.     if z < targetZ then face(0) while z < targetZ do forward() end
  90.     elseif z > targetZ then face(2) while z > targetZ do forward() end end
  91.  
  92.     face(targetDir or 0)
  93. end
  94.  
  95. -- === INVENTORY HANDLING ===
  96. function isInventoryFull()
  97.     for i = 1, 15 do
  98.         if turtle.getItemCount(i) == 0 then return false end
  99.     end
  100.     return true
  101. end
  102.  
  103. function sortAndDropItems()
  104.     for i = 1, 15 do
  105.         if i ~= fuelSlot then
  106.             turtle.select(i)
  107.             local item = turtle.getItemDetail()
  108.             if item then
  109.                 if item.name == "minecraft:coal" then
  110.                     turtle.transferTo(fuelSlot)
  111.                 else
  112.                     turtle.drop()
  113.                 end
  114.             end
  115.         end
  116.     end
  117.     turtle.select(1)
  118. end
  119.  
  120. -- === MINING FUNCTION ===
  121. function mineLayer()
  122.     local primary = isLeft and length or width
  123.     local secondary = isLeft and width or length
  124.     for row = 1, secondary do
  125.         for col = 1, primary - 1 do
  126.             forward()
  127.             if isInventoryFull() then sortAndDropItems() end
  128.         end
  129.         if row < secondary then
  130.             if row % 2 == 1 then
  131.                 if isLeft then turnRight(); forward(); turnRight()
  132.                 else turnLeft(); forward(); turnLeft() end
  133.             else
  134.                 if isLeft then turnLeft(); forward(); turnLeft()
  135.                 else turnRight(); forward(); turnRight() end
  136.             end
  137.         end
  138.     end
  139.     if secondary % 2 == 1 then face(isLeft and 2 or 0) for i = 1, primary - 1 do forward() end end
  140.     face(isLeft and 3 or 1) for i = 1, secondary - 1 do forward() end
  141.     face(0)
  142. end
  143.  
  144. -- === MAIN EXECUTION ===
  145. print("Starting mining...")
  146. turtle.digDown()
  147. down()
  148.  
  149. for d = 1, depth do
  150.     mineLayer()
  151.     if d < depth then down() end
  152. end
  153.  
  154. -- Final sort and drop of remaining unwanted blocks
  155. sortAndDropItems()
  156.  
  157. -- Final return to origin
  158. goTo(0, 0, 0, 0)
  159. print("Mining complete.")
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement