Advertisement
DanFrmSpace

quar2day

May 30th, 2025 (edited)
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.11 KB | None | 0 0
  1. -- Advanced Quarry Turtle for CC: Tweaked
  2. -- Mines 5x5 area down to bedrock with state persistence
  3.  
  4. -- Configuration
  5. local config = {
  6.     quarryWidth = 5,       -- Width of quarry
  7.     quarryLength = 5,      -- Length of quarry
  8.     minFuelLevel = 500,    -- Minimum fuel before returning
  9.     torchInterval = 5,     -- Place torch every N layers
  10.     chestCheckInterval = 5 -- Check inventory every N layers
  11. }
  12.  
  13. -- State tracking
  14. local state = {
  15.     x = 0, y = 0, z = 0,
  16.     facing = 0, -- 0=north, 1=east, 2=south, 3=west
  17.     layer = 0,
  18.     rowInLayer = 0,
  19.     blockInRow = 0,
  20.     startX = 0, startY = 0, startZ = 0,
  21.     quarrying = true,
  22.     returningForFuel = false,
  23.     miningPattern = "rows" -- rows or spiral
  24. }
  25.  
  26. -- Utility functions
  27. local function saveState()
  28.     local file = fs.open("quarry_state", "w")
  29.     file.write(textutils.serialize(state))
  30.     file.close()
  31. end
  32.  
  33. local function loadState()
  34.     if fs.exists("quarry_state") then
  35.         local file = fs.open("quarry_state", "r")
  36.         state = textutils.unserialize(file.readAll())
  37.         file.close()
  38.         return true
  39.     end
  40.     return false
  41. end
  42.  
  43. local function deleteState()
  44.     if fs.exists("quarry_state") then
  45.         fs.delete("quarry_state")
  46.     end
  47. end
  48.  
  49. -- Movement tracking functions
  50. local function forward()
  51.     if turtle.forward() then
  52.         if state.facing == 0 then state.z = state.z - 1
  53.         elseif state.facing == 1 then state.x = state.x + 1
  54.         elseif state.facing == 2 then state.z = state.z + 1
  55.         elseif state.facing == 3 then state.x = state.x - 1
  56.         end
  57.         return true
  58.     end
  59.     return false
  60. end
  61.  
  62. local function back()
  63.     if turtle.back() then
  64.         if state.facing == 0 then state.z = state.z + 1
  65.         elseif state.facing == 1 then state.x = state.x - 1
  66.         elseif state.facing == 2 then state.z = state.z - 1
  67.         elseif state.facing == 3 then state.x = state.x + 1
  68.         end
  69.         return true
  70.     end
  71.     return false
  72. end
  73.  
  74. local function up()
  75.     if turtle.up() then
  76.         state.y = state.y + 1
  77.         return true
  78.     end
  79.     return false
  80. end
  81.  
  82. local function down()
  83.     if turtle.down() then
  84.         state.y = state.y - 1
  85.         return true
  86.     end
  87.     return false
  88. end
  89.  
  90. local function turnLeft()
  91.     turtle.turnLeft()
  92.     state.facing = (state.facing - 1) % 4
  93. end
  94.  
  95. local function turnRight()
  96.     turtle.turnRight()
  97.     state.facing = (state.facing + 1) % 4
  98. end
  99.  
  100. local function turnAround()
  101.     turnRight()
  102.     turnRight()
  103. end
  104.  
  105. -- Safe digging functions
  106. local function digSafe()
  107.     while turtle.detect() do
  108.         if not turtle.dig() then
  109.             return false -- Hit bedrock or unbreakable block
  110.         end
  111.         sleep(0.5) -- Handle falling blocks
  112.     end
  113.     return true
  114. end
  115.  
  116. local function digUpSafe()
  117.     while turtle.detectUp() do
  118.         if not turtle.digUp() then
  119.             return false
  120.         end
  121.         sleep(0.5)
  122.     end
  123.     return true
  124. end
  125.  
  126. local function digDownSafe()
  127.     while turtle.detectDown() do
  128.         if not turtle.digDown() then
  129.             return false -- Hit bedrock
  130.         end
  131.         sleep(0.5)
  132.     end
  133.     return true
  134. end
  135.  
  136. -- Movement with digging
  137. local function forwardDig()
  138.     digSafe()
  139.     local attempts = 0
  140.     while not forward() and attempts < 10 do
  141.         digSafe()
  142.         turtle.attack() -- Clear mobs
  143.         attempts = attempts + 1
  144.         sleep(0.2)
  145.     end
  146.     return attempts < 10
  147. end
  148.  
  149. local function upDig()
  150.     digUpSafe()
  151.     local attempts = 0
  152.     while not up() and attempts < 10 do
  153.         digUpSafe()
  154.         turtle.attackUp()
  155.         attempts = attempts + 1
  156.         sleep(0.2)
  157.     end
  158.     return attempts < 10
  159. end
  160.  
  161. local function downDig()
  162.     if not digDownSafe() then
  163.         return false -- Hit bedrock
  164.     end
  165.     local attempts = 0
  166.     while not down() and attempts < 10 do
  167.         if not digDownSafe() then
  168.             return false -- Hit bedrock
  169.         end
  170.         turtle.attackDown()
  171.         attempts = attempts + 1
  172.         sleep(0.2)
  173.     end
  174.     return attempts < 10
  175. end
  176.  
  177. -- Inventory management
  178. local function findItem(name)
  179.     for i = 1, 16 do
  180.         local item = turtle.getItemDetail(i)
  181.         if item and item.name == name then
  182.             return i
  183.         end
  184.     end
  185.     return nil
  186. end
  187.  
  188. local function countEmptySlots()
  189.     local empty = 0
  190.     for i = 1, 16 do
  191.         if turtle.getItemCount(i) == 0 then
  192.             empty = empty + 1
  193.         end
  194.     end
  195.     return empty
  196. end
  197.  
  198. local function depositItems()
  199.     print("Depositing items...")
  200.     for i = 1, 16 do
  201.         turtle.select(i)
  202.         local item = turtle.getItemDetail()
  203.         -- Keep fuel and torches
  204.         if item and
  205.            item.name ~= "minecraft:coal" and
  206.            item.name ~= "minecraft:charcoal" and
  207.            item.name ~= "minecraft:lava_bucket" and
  208.            item.name ~= "minecraft:torch" then
  209.             turtle.dropUp()
  210.         end
  211.     end
  212.     turtle.select(1)
  213. end
  214.  
  215. -- Fuel management
  216. local function getFuelLevel()
  217.     local level = turtle.getFuelLevel()
  218.     if level == "unlimited" then
  219.         return 999999
  220.     end
  221.     return level
  222. end
  223.  
  224. local function refuel()
  225.     print("Checking fuel...")
  226.     local fuelLevel = getFuelLevel()
  227.    
  228.     if fuelLevel < config.minFuelLevel then
  229.         print("Fuel low. Refueling...")
  230.         for i = 1, 16 do
  231.             turtle.select(i)
  232.             if turtle.refuel(0) then -- Check if item is fuel
  233.                 local needed = config.minFuelLevel * 2 - getFuelLevel()
  234.                 turtle.refuel(math.ceil(needed / 80)) -- Coal gives 80 fuel
  235.                 if getFuelLevel() >= config.minFuelLevel then
  236.                     break
  237.                 end
  238.             end
  239.         end
  240.        
  241.         -- Check for lava buckets
  242.         local lavaBucket = findItem("minecraft:lava_bucket")
  243.         if lavaBucket and getFuelLevel() < config.minFuelLevel then
  244.             turtle.select(lavaBucket)
  245.             turtle.refuel(1)
  246.         end
  247.     end
  248.    
  249.     turtle.select(1)
  250.     return getFuelLevel() >= config.minFuelLevel
  251. end
  252.  
  253. -- Torch placement
  254. local function placeTorch()
  255.     local torchSlot = findItem("minecraft:torch")
  256.     if torchSlot then
  257.         local currentSlot = turtle.getSelectedSlot()
  258.         turtle.select(torchSlot)
  259.        
  260.         -- Try to place on wall
  261.         turnLeft()
  262.         if not turtle.place() then
  263.             turnRight()
  264.             turnRight()
  265.             turtle.place()
  266.             turnLeft()
  267.         else
  268.             turnRight()
  269.         end
  270.        
  271.         turtle.select(currentSlot)
  272.     end
  273. end
  274.  
  275. -- Navigation functions
  276. local function goToPosition(targetX, targetY, targetZ, targetFacing)
  277.     print(string.format("Going to position: X:%d Y:%d Z:%d", targetX, targetY, targetZ))
  278.    
  279.     -- Move to Y level first (go up if needed)
  280.     while state.y < targetY do
  281.         upDig()
  282.     end
  283.    
  284.     -- Move X
  285.     while state.x < targetX do
  286.         while state.facing ~= 1 do turnRight() end
  287.         forwardDig()
  288.     end
  289.     while state.x > targetX do
  290.         while state.facing ~= 3 do turnRight() end
  291.         forwardDig()
  292.     end
  293.    
  294.     -- Move Z
  295.     while state.z < targetZ do
  296.         while state.facing ~= 2 do turnRight() end
  297.         forwardDig()
  298.     end
  299.     while state.z > targetZ do
  300.         while state.facing ~= 0 do turnRight() end
  301.         forwardDig()
  302.     end
  303.    
  304.     -- Move to Y level (go down if needed)
  305.     while state.y > targetY do
  306.         downDig()
  307.     end
  308.    
  309.     -- Face correct direction
  310.     if targetFacing then
  311.         while state.facing ~= targetFacing do
  312.             turnRight()
  313.         end
  314.     end
  315. end
  316.  
  317. local function returnToStart()
  318.     print("Returning to start position...")
  319.     local savedState = {
  320.         x = state.x,
  321.         y = state.y,
  322.         z = state.z,
  323.         facing = state.facing
  324.     }
  325.    
  326.     goToPosition(state.startX, state.startY, state.startZ, 0)
  327.    
  328.     return savedState
  329. end
  330.  
  331. local function returnToMining(savedPosition)
  332.     print("Returning to mining position...")
  333.     goToPosition(savedPosition.x, savedPosition.y, savedPosition.z, savedPosition.facing)
  334. end
  335.  
  336. -- Quarry patterns
  337. local function mineLayer()
  338.     -- Always start from the same corner with same orientation
  339.     local layerStartX = state.startX + 1  -- One block forward from start
  340.     local layerStartZ = state.startZ
  341.     local layerY = state.y
  342.    
  343.     -- Ensure we're at the correct starting position and facing
  344.     goToPosition(layerStartX, layerY, layerStartZ, 0)
  345.    
  346.     for row = 1, config.quarryLength do
  347.         -- Mine one row
  348.         for block = 1, config.quarryWidth - 1 do
  349.             forwardDig()
  350.             state.blockInRow = block
  351.             saveState()
  352.         end
  353.        
  354.         -- Move to next row if not last
  355.         if row < config.quarryLength then
  356.             if row % 2 == 1 then
  357.                 -- Odd row, turn right
  358.                 turnRight()
  359.                 forwardDig()
  360.                 turnRight()
  361.             else
  362.                 -- Even row, turn left
  363.                 turnLeft()
  364.                 forwardDig()
  365.                 turnLeft()
  366.             end
  367.         end
  368.        
  369.         state.rowInLayer = row
  370.         state.blockInRow = 0
  371.         saveState()
  372.     end
  373.    
  374.     -- Always return to the starting corner of this layer facing north
  375.     goToPosition(layerStartX, layerY, layerStartZ, 0)
  376. end
  377.  
  378. -- Main quarry function
  379. local function quarry()
  380.     print("Starting quarry operation...")
  381.     print(string.format("Size: %dx%d", config.quarryWidth, config.quarryLength))
  382.     print("Mining down to bedrock...")
  383.    
  384.     while state.quarrying do
  385.         -- Check fuel before each layer
  386.         if getFuelLevel() < config.minFuelLevel then
  387.             print("Low fuel! Current level: " .. getFuelLevel())
  388.             state.returningForFuel = true
  389.             saveState()
  390.            
  391.             local miningPos = returnToStart()
  392.            
  393.             -- Try to refuel from inventory
  394.             if not refuel() then
  395.                 print("Cannot refuel from inventory!")
  396.                 print("Please add fuel to the turtle")
  397.                 print("Fuel needed: " .. config.minFuelLevel)
  398.                 print("Current fuel: " .. getFuelLevel())
  399.                 print("Press any key after adding fuel...")
  400.                 os.pullEvent("key")
  401.                 refuel()
  402.             end
  403.            
  404.             -- Deposit items while we're here
  405.             depositItems()
  406.            
  407.             -- Return to mining
  408.             returnToMining(miningPos)
  409.             state.returningForFuel = false
  410.             saveState()
  411.         end
  412.        
  413.         -- Mine current layer
  414.         print("Mining layer " .. state.layer)
  415.         mineLayer()
  416.        
  417.         -- Place torch if needed
  418.         if state.layer % config.torchInterval == 0 then
  419.             placeTorch()
  420.         end
  421.        
  422.         -- Check inventory every few layers
  423.         if state.layer % config.chestCheckInterval == 0 and countEmptySlots() <= 2 then
  424.             print("Inventory nearly full, returning to deposit...")
  425.             local miningPos = returnToStart()
  426.             depositItems()
  427.             returnToMining(miningPos)
  428.         end
  429.        
  430.         -- Try to go down
  431.         if not downDig() then
  432.             print("Hit bedrock at layer " .. state.layer)
  433.             state.quarrying = false
  434.         else
  435.             state.layer = state.layer + 1
  436.             state.rowInLayer = 0
  437.             state.blockInRow = 0
  438.             saveState()
  439.         end
  440.     end
  441.    
  442.     -- Return to start and deposit final items
  443.     returnToStart()
  444.     depositItems()
  445.    
  446.     print("Quarry complete!")
  447.     print("Mined " .. state.layer .. " layers")
  448.     deleteState()
  449. end
  450.  
  451. -- Resume function for interrupted mining
  452. local function resume()
  453.     print("Resuming quarry operation...")
  454.     print(string.format("Current position: X:%d Y:%d Z:%d", state.x, state.y, state.z))
  455.     print("Current layer: " .. state.layer)
  456.    
  457.     if state.returningForFuel then
  458.         print("Was returning for fuel...")
  459.         returnToStart()
  460.         refuel()
  461.         depositItems()
  462.        
  463.         -- Go back to the starting position of the current layer
  464.         local layerStartX = state.startX + 1
  465.         local layerStartZ = state.startZ
  466.         local layerY = state.startY - state.layer
  467.         goToPosition(layerStartX, layerY, layerStartZ, 0)
  468.         state.returningForFuel = false
  469.     end
  470.    
  471.     quarry()
  472. end
  473.  
  474. -- Setup function
  475. local function setup()
  476.     print("Advanced Quarry Turtle")
  477.     print("===========================")
  478.     print("Setup Instructions:")
  479.     print("1. Place chest ABOVE turtle")
  480.     print("2. Add fuel (coal/lava buckets)")
  481.     print("3. Add torches (optional)")
  482.     print("4. Turtle will mine 5x5 area")
  483.     print("   in front of it (north)")
  484.     print("   extending to the right (east)")
  485.     print("===========================")
  486.     print("Current fuel: " .. getFuelLevel())
  487.     print("Press any key to start...")
  488.     os.pullEvent("key")
  489.    
  490.     -- Check if resuming
  491.     if loadState() then
  492.         print("Found saved state!")
  493.         resume()
  494.     else
  495.         -- New quarry
  496.         state.startX = state.x
  497.         state.startY = state.y
  498.         state.startZ = state.z
  499.        
  500.         -- Check initial fuel
  501.         if not refuel() then
  502.             print("ERROR: Not enough fuel!")
  503.             print("Add at least " .. config.minFuelLevel .. " fuel")
  504.             return
  505.         end
  506.        
  507.         -- Move forward to start quarry
  508.         print("Moving to quarry start position...")
  509.         forwardDig()
  510.         state.startX = state.x - 1  -- Store original position (one block back)
  511.         state.startY = state.y
  512.         state.startZ = state.z
  513.         downDig() -- Start first layer
  514.         state.layer = 1
  515.         saveState()
  516.        
  517.         quarry()
  518.     end
  519. end
  520.  
  521. -- Error handling wrapper
  522. local function main()
  523.     local success, error = pcall(setup)
  524.     if not success then
  525.         print("ERROR: " .. error)
  526.         print("State saved. Run program again to resume.")
  527.         saveState()
  528.     end
  529. end
  530.  
  531. -- Run the program
  532. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement