svdragster

quarry final

Aug 11th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. --[[
  2.   Rectangular Quarry Program for ComputerCraft Turtles (with Whitelist)
  3.  
  4.   Description:
  5.   This program instructs a mining turtle to dig a rectangular area (a quarry).
  6.   It includes a whitelist to keep valuable items and automatically retains fuel.
  7.   All other items are deposited into a chest.
  8.  
  9.   How to Use:
  10.   1. Configure the 'itemWhitelist' table below with the item IDs you want to keep.
  11.      You can find item IDs in-game (often by pressing F3+H).
  12.  
  13.   2. Place this file on a mining turtle (e.g., 'quarry.lua').
  14.  
  15.   3. Place a chest directly behind the turtle's starting position. This is where
  16.     it will deposit all non-whitelisted items.
  17.  
  18.  4. Make sure the turtle has some fuel. It will automatically find and use it
  19.     from its inventory, and it will never drop fuel items.
  20.  
  21.  5. Run the program with two arguments: width and length.
  22.     Example: quarry.lua 10 20
  23. ]]
  24.  
  25. -- =================================================================
  26. -- Configuration
  27. -- =================================================================
  28.  
  29. -- Add the string IDs of items you want the turtle to KEEP.
  30. -- The turtle will automatically keep any fuel source, so you don't need to add coal.
  31. -- Example Format: ["mod_name:item_name"] = true,
  32. local itemWhitelist = {
  33.   ["minecraft:diamond"] = true,
  34.   ["minecraft:emerald"] = true,
  35.   ["minecraft:raw_gold"] = true,
  36.   ["minecraft:raw_iron"] = true,
  37.   ["minecraft:lapis_lazuli"] = true,
  38.   ["minecraft:redstone"] = true,
  39.  
  40.   -- Example for a modded ore from AllTheMods
  41.   ["allthemodium:raw_allthemodium"] = true,
  42.   ["allthemodium:raw_vibranium"] = true,
  43.   ["allthemodium:raw_unobtainium"] = true,
  44.   ["alltheores:raw_tin"] = true,
  45.   ["alltheores:raw_lead"] = true,
  46. }
  47.  
  48. -- =================================================================
  49. -- Helper Functions
  50. -- =================================================================
  51.  
  52. -- Function to check fuel and refuel if necessary
  53. local function ensureFuel()
  54.   if turtle.getFuelLevel() < 2 then
  55.     print("Low on fuel. Attempting to refuel...")
  56.     for i = 1, 16 do
  57.       turtle.select(i)
  58.       if turtle.refuel(1) then
  59.         print("Refueled successfully.")
  60.         turtle.select(1) -- Select the first slot again for digging
  61.         return true
  62.       end
  63.     end
  64.     print("ERROR: Out of fuel! Please add fuel to my inventory.")
  65.     return false
  66.   end
  67.   return true
  68. end
  69.  
  70. -- Function to check if inventory is full and empty non-whitelisted items.
  71. local function manageInventory()
  72.   local isFull = true
  73.   for i = 1, 15 do -- Check first 15 slots
  74.     if turtle.getItemCount(i) == 0 then
  75.       isFull = false
  76.       break
  77.     end
  78.   end
  79.  
  80.   if isFull then
  81.     print("Inventory full. Dropping non-whitelisted items.")
  82.     -- Turn around to face the chest
  83.     turtle.turnLeft()
  84.     turtle.turnLeft()
  85.  
  86.     -- Iterate through inventory and drop unwanted items
  87.     for i = 1, 16 do
  88.       turtle.select(i)
  89.       local itemDetail = turtle.getItemDetail(i)
  90.  
  91.       if itemDetail then
  92.         local itemName = itemDetail.name
  93.         -- turtle.refuel(0) checks if the item is fuel without consuming it
  94.         local isFuel = turtle.refuel(0)
  95.         local isWhitelisted = itemWhitelist[itemName]
  96.  
  97.         -- If the item is NOT fuel and is NOT on the whitelist, drop it.
  98.         if not isFuel and not isWhitelisted then
  99.           turtle.drop()
  100.         end
  101.       end
  102.     end
  103.  
  104.     turtle.select(1) -- Reselect the first slot
  105.     -- Turn back to the original direction
  106.     turtle.turnLeft()
  107.     turtle.turnLeft()
  108.     print("Inventory managed. Resuming mining.")
  109.   end
  110. end
  111.  
  112. -- A robust function for moving forward.
  113. local function moveForwardAndDig()
  114.   if not ensureFuel() then
  115.     return false
  116.   end
  117.  
  118.   while turtle.detect() do
  119.     if not turtle.dig() then
  120.       print("Waiting for obstruction to clear...")
  121.       os.sleep(1)
  122.     end
  123.   end
  124.  
  125.   while not turtle.forward() do
  126.     print("Movement blocked. Re-digging...")
  127.     turtle.dig()
  128.     os.sleep(0.5)
  129.   end
  130.  
  131.   return true
  132. end
  133.  
  134. -- Function to dig a single row of a given length
  135. local function digRow(length)
  136.   for i = 1, length - 1 do
  137.     manageInventory()
  138.  
  139.     turtle.digUp()
  140.     turtle.digDown()
  141.  
  142.     if not moveForwardAndDig() then
  143.       return false
  144.     end
  145.   end
  146.   manageInventory()
  147.   turtle.digUp()
  148.   turtle.digDown()
  149.   return true
  150. end
  151.  
  152. -- Function to turn the turtle right
  153. local function turnRight()
  154.   ensureFuel()
  155.   turtle.turnRight()
  156. end
  157.  
  158. -- Function to turn the turtle left
  159. local function turnLeft()
  160.   ensureFuel()
  161.   turtle.turnLeft()
  162. end
  163.  
  164. -- =================================================================
  165. -- Main Program Logic
  166. -- =================================================================
  167.  
  168. local args = { ... }
  169. if #args ~= 2 then
  170.   print("Usage: quarry <width> <length>")
  171.   print("Example: quarry 10 20")
  172.   return
  173. end
  174.  
  175. local width = tonumber(args[1])
  176. local length = tonumber(args[2])
  177.  
  178. if not width or not length or width <= 0 or length <= 0 then
  179.   print("Error: Width and length must be positive numbers.")
  180.   return
  181. end
  182.  
  183. print("Starting quarry of size " .. width .. "x" .. length)
  184. print("Whitelist is active.")
  185. os.sleep(2)
  186.  
  187. -- Main loop for digging rows
  188. for i = 1, width do
  189.   print("Digging row " .. i .. "/" .. width)
  190.  
  191.   if not digRow(length) then
  192.     print("Halting program due to movement failure.")
  193.     return
  194.   end
  195.  
  196.   if i < width then
  197.     if i % 2 ~= 0 then
  198.       turnRight()
  199.       moveForwardAndDig()
  200.       turnRight()
  201.     else
  202.       turnLeft()
  203.       moveForwardAndDig()
  204.       turnLeft()
  205.     end
  206.   end
  207. end
  208.  
  209. -- Final inventory drop
  210. manageInventory()
  211.  
  212. print("Quarry complete!")
  213.  
Add Comment
Please, Sign In to add comment