Advertisement
ZGAMMAA

Chunk Eater

Apr 10th, 2025 (edited)
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. -- ✅ FINAL CHUNK EATER: Mines 16x16, returns to start to unload/refuel
  2.  
  3. -- === CONFIG ===
  4. local CHUNK_SIZE = 16
  5. local LAYER_HEIGHT = 3
  6. local FUEL_BUFFER = 100
  7.  
  8. -- === POSITION TRACKING ===
  9. local x, y, z = 0, 0, 0
  10. local dir = 1 -- 0=north, 1=east, 2=south, 3=west
  11. local startX, startY, startZ, startDir = x, y, z, dir
  12.  
  13. -- === DIRECTION HELPERS ===
  14. local function turnRight() turtle.turnRight(); dir = (dir + 1) % 4 end
  15. local function turnLeft() turtle.turnLeft(); dir = (dir - 1) % 4 end
  16. local function face(target) while dir ~= target do turnRight() end end
  17. local function faceExact(targetDir) while dir ~= targetDir do turnRight() end end
  18. local function updatePosition(dx, dy, dz) x, y, z = x + dx, y + dy, z + dz end
  19.  
  20. -- === INVENTORY CHECK ===
  21. local function isFull()
  22.   for i = 1, 16 do
  23.     if turtle.getItemCount(i) == 0 then return false end
  24.   end
  25.   return true
  26. end
  27.  
  28. -- === GO TO COORDINATES ===
  29. local function goTo(xT, yT, zT)
  30.   while y < yT do turtle.down(); updatePosition(0, 1, 0) end
  31.   while y > yT do turtle.up(); updatePosition(0, -1, 0) end
  32.  
  33.   face(1) while x < xT do turtle.forward(); updatePosition(1, 0, 0) end
  34.   face(3) while x > xT do turtle.forward(); updatePosition(-1, 0, 0) end
  35.  
  36.   face(2) while z < zT do turtle.forward(); updatePosition(0, 0, 1) end
  37.   face(0) while z > zT do turtle.forward(); updatePosition(0, 0, -1) end
  38. end
  39.  
  40. -- === RETURN TO START AND UNLOAD BEHIND ===
  41. local function returnToStartAndUnload()
  42.   print("📦 Returning to original start point to dump items...")
  43.  
  44.   goTo(startX, startY, startZ)
  45.   faceExact(startDir)
  46.  
  47.   -- Dump behind
  48.   turnLeft(); turnLeft()
  49.   for i = 1, 16 do
  50.     turtle.select(i)
  51.     turtle.drop()
  52.   end
  53.   turnLeft(); turnLeft()
  54.  
  55.   print("✅ Dump complete. Returning to work...")
  56. end
  57.  
  58. -- === REFUELING ===
  59. local function refuelFromInventory()
  60.   for i = 1, 16 do
  61.     turtle.select(i)
  62.     if turtle.refuel(0) then
  63.       turtle.refuel()
  64.       return true
  65.     end
  66.   end
  67.   return false
  68. end
  69.  
  70. local function checkFuel()
  71.   local estReturn = math.abs(x - startX) + math.abs(y - startY) + math.abs(z - startZ) + FUEL_BUFFER
  72.   if turtle.getFuelLevel() < estReturn then
  73.     local sx, sy, sz, sd = x, y, z, dir
  74.     print("⚠️ Low fuel. Returning to refuel...")
  75.     returnToStartAndUnload()
  76.     while not refuelFromInventory() do
  77.       print("⌛ Waiting for fuel in inventory...")
  78.       sleep(2)
  79.     end
  80.     goTo(sx, sy, sz)
  81.     faceExact(sd)
  82.   end
  83. end
  84.  
  85. -- === MOVEMENT ===
  86. local function tryForward()
  87.   if isFull() then
  88.     print("📦 Inventory full before move. Returning to dump...")
  89.     local sx, sy, sz, sd = x, y, z, dir
  90.     returnToStartAndUnload()
  91.     checkFuel()
  92.     goTo(sx, sy, sz)
  93.     faceExact(sd)
  94.   end
  95.  
  96.   checkFuel()
  97.   while not turtle.forward() do
  98.     turtle.dig()
  99.     sleep(0.3)
  100.   end
  101.   turtle.digUp()
  102.   turtle.digDown()
  103.   if dir == 0 then updatePosition(0, 0, -1)
  104.   elseif dir == 1 then updatePosition(1, 0, 0)
  105.   elseif dir == 2 then updatePosition(0, 0, 1)
  106.   elseif dir == 3 then updatePosition(-1, 0, 0)
  107.   end
  108. end
  109.  
  110. local function tryUp()
  111.   checkFuel()
  112.   while not turtle.up() do
  113.     turtle.digUp()
  114.     sleep(0.3)
  115.   end
  116.   updatePosition(0, -1, 0)
  117. end
  118.  
  119. local function tryDown()
  120.   checkFuel()
  121.   while not turtle.down() do
  122.     turtle.digDown()
  123.     sleep(0.3)
  124.   end
  125.   updatePosition(0, 1, 0)
  126. end
  127.  
  128. -- === DIG LAYER ===
  129. local function digLayer()
  130.   for row = 1, CHUNK_SIZE do
  131.     for col = 1, CHUNK_SIZE - 1 do
  132.       tryForward()
  133.     end
  134.     if row < CHUNK_SIZE then
  135.       if row % 2 == 1 then
  136.         turnRight(); tryForward(); turnRight()
  137.       else
  138.         turnLeft(); tryForward(); turnLeft()
  139.       end
  140.     end
  141.   end
  142.  
  143.   -- Return to origin column
  144.   if x > 0 then face(3) while x > 0 do turtle.forward(); updatePosition(-1, 0, 0) end end
  145.   if x < 0 then face(1) while x < 0 do turtle.forward(); updatePosition(1, 0, 0) end end
  146.   if z > 0 then face(0) while z > 0 do turtle.forward(); updatePosition(0, 0, -1) end end
  147.   if z < 0 then face(2) while z < 0 do turtle.forward(); updatePosition(0, 0, 1) end end
  148.   face(1)
  149. end
  150.  
  151. -- === MAIN ===
  152. startX, startY, startZ, startDir = x, y, z, dir
  153.  
  154. print("Enter depth to dig:")
  155. local depth = tonumber(read())
  156. if not depth or depth <= 0 then
  157.   print("❌ Invalid depth.")
  158.   return
  159. end
  160.  
  161. for layer = 1, depth, LAYER_HEIGHT do
  162.   digLayer()
  163.   if layer + LAYER_HEIGHT <= depth then
  164.     print("⬇️ Descending to next layer...")
  165.     for i = 1, LAYER_HEIGHT do tryDown() end
  166.   end
  167. end
  168.  
  169. returnToStartAndUnload()
  170. print("✅ Chunk excavation complete. Turtle returned to start.")
  171.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement