Hemius

Untitled

Oct 4th, 2025 (edited)
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.77 KB | None | 0 0
  1. -- quarry.lua (2025 optimized + fixed return logic)
  2. -- Fully autonomous vertical quarry with resume, fuel, dump, trash filter
  3. -- Author: ChatGPT (2025 edition, optimized & corrected)
  4.  
  5. -- === CONFIG ===
  6. local STATE_FILE = "quarry_state.txt"
  7. local FUEL_SLOT = 1
  8. local SAFE_FUEL_MARGIN = 200
  9. local SAVE_INTERVAL = 50
  10. local STATUS_INTERVAL = 25
  11.  
  12. -- === Trash filter ===
  13. local trashList = {
  14.     ["minecraft:stone"]=true, ["minecraft:cobblestone"]=true,
  15.     ["minecraft:andesite"]=true, ["minecraft:diorite"]=true,
  16.     ["minecraft:granite"]=true, ["minecraft:dirt"]=true,
  17.     ["minecraft:coarse_dirt"]=true, ["minecraft:gravel"]=true,
  18.     ["minecraft:sand"]=true, ["minecraft:tuff"]=true,
  19.     ["minecraft:deepslate"]=true, ["minecraft:cobbled_deepslate"]=true
  20. }
  21.  
  22. -- === UTILITIES ===
  23. local function saveState(s)
  24.     local f = fs.open(STATE_FILE, "w")
  25.     f.write(textutils.serialize(s))
  26.     f.close()
  27. end
  28.  
  29. local function loadState()
  30.     if not fs.exists(STATE_FILE) then return nil end
  31.     local f = fs.open(STATE_FILE, "r")
  32.     local d = textutils.unserialize(f.readAll())
  33.     f.close()
  34.     return d
  35. end
  36.  
  37. local function maxFuelLevel()
  38.     local limit = turtle.getFuelLimit()
  39.     return (limit == "unlimited") and math.huge or limit
  40. end
  41.  
  42. -- === FUEL ===
  43. local function refuelSlot()
  44.     if turtle.getFuelLevel() == "unlimited" then return end
  45.     turtle.select(FUEL_SLOT)
  46.     while turtle.getItemCount() > 0 and turtle.getFuelLevel() < maxFuelLevel() do
  47.         turtle.refuel(1)
  48.     end
  49. end
  50.  
  51. local function autoRefuelFromChest()
  52.     turtle.turnRight(); turtle.turnRight()
  53.     turtle.select(FUEL_SLOT)
  54.     local found = false
  55.     while turtle.getFuelLevel() < maxFuelLevel() do
  56.         if not turtle.suck(64) then break end
  57.         found = true
  58.         refuelSlot()
  59.     end
  60.     turtle.turnRight(); turtle.turnRight()
  61.     if not found then
  62.         print("⚠️ Add fuel to chest behind and press Enter.")
  63.         read()
  64.         autoRefuelFromChest()
  65.     end
  66. end
  67.  
  68. -- === INVENTORY ===
  69. local function cleanTrash()
  70.     for i=2,16 do
  71.         local d = turtle.getItemDetail(i)
  72.         if d and trashList[d.name] then
  73.             turtle.select(i)
  74.             turtle.drop()
  75.         end
  76.     end
  77. end
  78.  
  79. local function dumpInventory()
  80.     turtle.turnRight(); turtle.turnRight()
  81.     for i=2,16 do
  82.         turtle.select(i)
  83.         turtle.drop()
  84.     end
  85.     turtle.turnRight(); turtle.turnRight()
  86. end
  87.  
  88. -- === HUD ===
  89. local startTime = os.clock()
  90. local movedBlocks = 0
  91. local lastStatus = 0
  92.  
  93. local function formatTime(sec)
  94.     local m=math.floor(sec/60)
  95.     local s=math.floor(sec%60)
  96.     return string.format("%02d:%02d",m,s)
  97. end
  98.  
  99. local function showStatus(state, totalLayers, totalBlocks)
  100.     term.clear()
  101.     term.setCursorPos(1,1)
  102.     local perc = math.min(100, (movedBlocks / totalBlocks) * 100)
  103.     local elapsed = os.clock() - startTime
  104.     local eta = (perc > 0) and (elapsed / perc) * (100 - perc) or 0
  105.     print("==============================")
  106.     print("         QUARRY STATUS        ")
  107.     print("==============================")
  108.     print(string.format("Layer %d / %d", state.layer, totalLayers))
  109.     print(string.format("Fuel: %d", turtle.getFuelLevel()))
  110.     print(string.format("Progress: %.1f%% (%d/%d)", perc, movedBlocks, totalBlocks))
  111.     print(string.format("Time: %s | ETA: %s", formatTime(elapsed), formatTime(eta)))
  112.     print("==============================")
  113. end
  114.  
  115. -- === SETUP ===
  116. local args={...}
  117. local length,width
  118. if #args<2 then
  119.     if fs.exists(STATE_FILE) then
  120.         local s=loadState()
  121.         length=s.length; width=s.width
  122.         print(string.format("📁 Resuming quarry %dx%d",length,width))
  123.     else
  124.         print("📏 Enter quarry dimensions:")
  125.         io.write("  Length: "); length=tonumber(read())
  126.         io.write("  Width: "); width=tonumber(read())
  127.     end
  128. else
  129.     length=tonumber(args[1]); width=tonumber(args[2])
  130. end
  131. if not length or not width or length<=0 or width<=0 then
  132.     print("❌ Invalid dimensions")
  133.     return
  134. end
  135.  
  136. -- === DIRECTION + POSITION TRACKING ===
  137. -- dir: 0=north, 1=east, 2=south, 3=west
  138. local state = loadState() or {
  139.     x=1, z=1, y=0, dir=1, layer=0,
  140.     length=length, width=width, finished=false
  141. }
  142. local dir = state.dir or 1
  143.  
  144. local function turnRight()
  145.     turtle.turnRight()
  146.     dir = (dir + 1) % 4
  147.     state.dir = dir
  148. end
  149.  
  150. local function turnLeft()
  151.     turtle.turnLeft()
  152.     dir = (dir + 3) % 4
  153.     state.dir = dir
  154. end
  155.  
  156. local function forward()
  157.     while turtle.detect() do turtle.dig() end
  158.     while not turtle.forward() do turtle.dig(); sleep(0.1) end
  159.     if dir==0 then state.z = state.z - 1
  160.     elseif dir==1 then state.x = state.x + 1
  161.     elseif dir==2 then state.z = state.z + 1
  162.     elseif dir==3 then state.x = state.x - 1 end
  163. end
  164.  
  165. local function back()
  166.     turtle.back()
  167.     if dir==0 then state.z = state.z + 1
  168.     elseif dir==1 then state.x = state.x - 1
  169.     elseif dir==2 then state.z = state.z - 1
  170.     elseif dir==3 then state.x = state.x + 1 end
  171. end
  172.  
  173. local function down()
  174.     while turtle.detectDown() do turtle.digDown() end
  175.     turtle.down()
  176.     state.y = state.y - 1
  177. end
  178.  
  179. -- === RETURN TO BASE ===
  180. local function goHome()
  181.     -- Face west (reverse of start)
  182.     while dir ~= 3 do turnRight() end
  183.     while state.x > 1 do back() end
  184.     -- Face north
  185.     while dir ~= 0 do turnLeft() end
  186.     while state.z > 1 do back() end
  187.     -- Face east again (toward quarry)
  188.     while dir ~= 1 do turnRight() end
  189.     -- Now at (1,1), facing east, chest behind
  190. end
  191.  
  192. local function resumePosition()
  193.     -- move from home to saved coords
  194.     while dir ~= 1 do turnRight() end
  195.     for z=2,state.z do forward() end
  196.     for x=2,state.x do forward() end
  197. end
  198.  
  199. -- === INITIAL PREP ===
  200. autoRefuelFromChest()
  201. refuelSlot()
  202. showStatus(state, 60, length*width*60) -- estimated start
  203.  
  204. local totalLayers = 60  -- adjust if desired
  205. local totalBlocks = length*width*totalLayers
  206.  
  207. -- === MAIN LAYER MINING ===
  208. local function mineLayer()
  209.     for z=1,state.width do
  210.         for x=1,state.length-1 do
  211.             forward()
  212.             movedBlocks=movedBlocks+1
  213.  
  214.             if movedBlocks % SAVE_INTERVAL == 0 then saveState(state) end
  215.             if movedBlocks - lastStatus >= STATUS_INTERVAL then
  216.                 showStatus(state,totalLayers,totalBlocks)
  217.                 lastStatus = movedBlocks
  218.             end
  219.  
  220.             -- Occasional maintenance
  221.             if movedBlocks % 128 == 0 then
  222.                 if turtle.getFuelLevel() < SAFE_FUEL_MARGIN then
  223.                     print("⛽ Refueling...")
  224.                     goHome()
  225.                     dumpInventory()
  226.                     autoRefuelFromChest()
  227.                     resumePosition()
  228.                 end
  229.                 local fullCount=0
  230.                 for i=2,16 do if turtle.getItemCount(i)>0 then fullCount=fullCount+1 end end
  231.                 if fullCount>=14 then
  232.                     print("📦 Dumping inventory...")
  233.                     goHome(); dumpInventory(); resumePosition()
  234.                 else
  235.                     cleanTrash()
  236.                 end
  237.             end
  238.         end
  239.         if z < state.width then
  240.             if z % 2 == 1 then
  241.                 turnRight(); forward(); turnRight()
  242.             else
  243.                 turnLeft(); forward(); turnLeft()
  244.             end
  245.         end
  246.     end
  247.     -- Return to start of layer
  248.     goHome()
  249. end
  250.  
  251. -- === MAIN LOOP ===
  252. local function quarry()
  253.     while not state.finished do
  254.         mineLayer()
  255.         down()
  256.         state.layer = state.layer + 1
  257.         saveState(state)
  258.         showStatus(state, totalLayers, totalBlocks)
  259.         if state.layer >= totalLayers then state.finished = true end
  260.     end
  261. end
  262.  
  263. -- === RUN ===
  264. quarry()
  265. goHome()
  266. dumpInventory()
  267. fs.delete(STATE_FILE)
  268. print("✅ Quarry finished successfully.")
  269.  
Advertisement