ZGAMMAA

Chunk Eater

Apr 10th, 2025 (edited)
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.37 KB | None | 0 0
  1. -- === CONFIG ===
  2. local CHUNK_SIZE = 16
  3. local LAYER_HEIGHT = 3
  4. local FUEL_BUFFER = 100
  5. local SAVE_FILE = "resume.dat"
  6.  
  7. -- Whitelist mode: only keep these blocks
  8. local WHITELISTED_BLOCKS = {
  9.   ["minecraft:coal_ore"] = true,
  10.   ["minecraft:iron_ore"] = true,
  11.   ["minecraft:copper_ore"] = true,
  12.   ["minecraft:gold_ore"] = true,
  13.   ["minecraft:redstone_ore"] = true,
  14.   ["minecraft:lapis_ore"] = true,
  15.   ["minecraft:diamond_ore"] = true,
  16.   ["minecraft:emerald_ore"] = true,
  17.   ["minecraft:deepslate_coal_ore"] = true,
  18.   ["minecraft:deepslate_iron_ore"] = true,
  19.   ["minecraft:deepslate_copper_ore"] = true,
  20.   ["minecraft:deepslate_gold_ore"] = true,
  21.   ["minecraft:deepslate_redstone_ore"] = true,
  22.   ["minecraft:deepslate_lapis_ore"] = true,
  23.   ["minecraft:deepslate_diamond_ore"] = true,
  24.   ["minecraft:deepslate_emerald_ore"] = true
  25. }
  26.  
  27. -- === STATE ===
  28. local x, y, z = 0, 0, 0
  29. local dir = 1
  30. local layer = 1
  31. local startX, startY, startZ, startDir = x, y, z, dir
  32.  
  33. -- === SAVE / LOAD ===
  34. local function saveState()
  35.   local f = fs.open(SAVE_FILE, "w")
  36.   f.write(textutils.serialize({x=x, y=y, z=z, dir=dir, layer=layer}))
  37.   f.close()
  38. end
  39.  
  40. local function loadState()
  41.   if not fs.exists(SAVE_FILE) then return false end
  42.   local f = fs.open(SAVE_FILE, "r")
  43.   local data = textutils.unserialize(f.readAll())
  44.   f.close()
  45.   x, y, z = data.x, data.y, data.z
  46.   dir = data.dir
  47.   layer = data.layer
  48.   startX, startY, startZ, startDir = 0, 0, 0, 1
  49.   return true
  50. end
  51.  
  52. local function clearState()
  53.   if fs.exists(SAVE_FILE) then fs.delete(SAVE_FILE) end
  54. end
  55.  
  56. -- === DIRECTION HELPERS ===
  57. local function turnRight() turtle.turnRight(); dir = (dir + 1) % 4 end
  58. local function turnLeft() turtle.turnLeft(); dir = (dir - 1) % 4 end
  59. local function face(target) while dir ~= target do turnRight() end end
  60. local function faceExact(targetDir) while dir ~= targetDir do turnRight() end end
  61. local function updatePosition(dx, dy, dz) x, y, z = x + dx, y + dy, z + dz; saveState() end
  62.  
  63. -- === INVENTORY ===
  64. local function isFull()
  65.   for i = 1, 16 do
  66.     if turtle.getItemCount(i) == 0 then return false end
  67.   end
  68.   return true
  69. end
  70.  
  71. local function dropUnlistedBlocks()
  72.   for i = 1, 16 do
  73.     turtle.select(i)
  74.     local item = turtle.getItemDetail()
  75.     if item and not WHITELISTED_BLOCKS[item.name] then
  76.       turtle.drop()
  77.     end
  78.   end
  79. end
  80.  
  81. -- === POSITIONING ===
  82. local function goTo(xT, yT, zT)
  83.   while y < yT do turtle.down(); updatePosition(0, 1, 0) end
  84.   while y > yT do turtle.up(); updatePosition(0, -1, 0) end
  85.   face(1) while x < xT do turtle.forward(); updatePosition(1, 0, 0) end
  86.   face(3) while x > xT do turtle.forward(); updatePosition(-1, 0, 0) end
  87.   face(2) while z < zT do turtle.forward(); updatePosition(0, 0, 1) end
  88.   face(0) while z > zT do turtle.forward(); updatePosition(0, 0, -1) end
  89. end
  90.  
  91. -- === UNLOAD AT ORIGIN ===
  92. local function returnToStartAndUnload()
  93.   goTo(startX, startY, startZ)
  94.   faceExact(startDir)
  95.   turnLeft(); turnLeft()
  96.   for i = 1, 16 do
  97.     turtle.select(i)
  98.     turtle.drop()
  99.   end
  100.   turnLeft(); turnLeft()
  101. end
  102.  
  103. -- === FUEL ===
  104. local function refuelFromInventory()
  105.   for i = 1, 16 do
  106.     turtle.select(i)
  107.     if turtle.refuel(0) then
  108.       turtle.refuel()
  109.       return true
  110.     end
  111.   end
  112.   return false
  113. end
  114.  
  115. local function refuelFromLeftChest()
  116.   turnLeft()
  117.   for i = 1, 16 do
  118.     turtle.select(i)
  119.     if turtle.getFuelLevel() < turtle.getFuelLimit() then
  120.       turtle.suck()
  121.       if turtle.refuel(0) then
  122.         turtle.refuel()
  123.       else
  124.         turtle.drop()
  125.       end
  126.     end
  127.   end
  128.   turnRight()
  129. end
  130.  
  131. local function checkFuel()
  132.   local dist = math.abs(x - startX) + math.abs(y - startY) + math.abs(z - startZ)
  133.   if turtle.getFuelLevel() < dist + FUEL_BUFFER then
  134.     local sx, sy, sz, sd = x, y, z, dir
  135.     returnToStartAndUnload()
  136.     if not refuelFromInventory() then refuelFromLeftChest() end
  137.     goTo(sx, sy, sz)
  138.     faceExact(sd)
  139.   end
  140. end
  141.  
  142. -- === BEDROCK CHECK ===
  143. local function reachedBedrock()
  144.   local success, data = turtle.inspectDown()
  145.   return success and data.name == "minecraft:bedrock"
  146. end
  147.  
  148. -- === MOVEMENT ===
  149. local function tryForward()
  150.   if isFull() then
  151.     local sx, sy, sz, sd = x, y, z, dir
  152.     returnToStartAndUnload()
  153.     checkFuel()
  154.     goTo(sx, sy, sz)
  155.     faceExact(sd)
  156.   end
  157.   checkFuel()
  158.   while not turtle.forward() do
  159.     turtle.dig()
  160.     sleep(0.3)
  161.   end
  162.   turtle.digUp()
  163.   turtle.digDown()
  164.   dropUnlistedBlocks()
  165.   if dir == 0 then updatePosition(0, 0, -1)
  166.   elseif dir == 1 then updatePosition(1, 0, 0)
  167.   elseif dir == 2 then updatePosition(0, 0, 1)
  168.   elseif dir == 3 then updatePosition(-1, 0, 0) end
  169. end
  170.  
  171. local function tryDown()
  172.   checkFuel()
  173.   while not turtle.down() do
  174.     turtle.digDown()
  175.     sleep(0.3)
  176.   end
  177.   updatePosition(0, 1, 0)
  178. end
  179.  
  180. -- === DIG LAYER ===
  181. local function digLayer()
  182.   print("⛏ Digging layer " .. layer .. " at y=" .. y)
  183.   for row = 1, CHUNK_SIZE do
  184.     for col = 1, CHUNK_SIZE - 1 do
  185.       tryForward()
  186.     end
  187.     if row < CHUNK_SIZE then
  188.       if row % 2 == 1 then turnRight(); tryForward(); turnRight()
  189.       else turnLeft(); tryForward(); turnLeft() end
  190.     end
  191.   end
  192.   -- Return to origin column
  193.   if x > 0 then face(3) while x > 0 do turtle.forward(); updatePosition(-1, 0, 0) end end
  194.   if x < 0 then face(1) while x < 0 do turtle.forward(); updatePosition(1, 0, 0) end end
  195.   if z > 0 then face(0) while z > 0 do turtle.forward(); updatePosition(0, 0, -1) end end
  196.   if z < 0 then face(2) while z < 0 do turtle.forward(); updatePosition(0, 0, 1) end end
  197.   face(1)
  198. end
  199.  
  200. -- === MAIN ===
  201. if fs.exists(SAVE_FILE) then
  202.   print("Resume previous run? (y/n)")
  203.   local input = read()
  204.   if input:lower() == "y" then
  205.     loadState()
  206.     print("📍 Resumed at: x="..x.." y="..y.." z="..z.." dir="..dir.." layer="..layer)
  207.   else
  208.     clearState()
  209.     x, y, z = 0, 0, 0
  210.     dir = 1
  211.     layer = 1
  212.   end
  213. end
  214.  
  215. print("📦 Starting Chunk Eater. Will stop at bedrock.")
  216.  
  217. while true do
  218.   digLayer()
  219.   layer = layer + LAYER_HEIGHT
  220.   saveState()
  221.   if reachedBedrock() then
  222.     print("🛑 Bedrock detected. Stopping dig.")
  223.     break
  224.   end
  225.   print("⬇️ Descending to next layer...")
  226.   for i = 1, LAYER_HEIGHT do
  227.     if not reachedBedrock() then
  228.       tryDown()
  229.     else
  230.       print("🛑 Bedrock reached during descent.")
  231.       break
  232.     end
  233.   end
  234. end
  235.  
  236. returnToStartAndUnload()
  237. clearState()
  238. print("✅ Chunk excavation complete.")
Advertisement
Add Comment
Please, Sign In to add comment