Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Rectangular Quarry Program for ComputerCraft Turtles (with Whitelist)
- Description:
- This program instructs a mining turtle to dig a rectangular area (a quarry).
- It includes a whitelist to keep valuable items and automatically retains fuel.
- All other items are deposited into a chest.
- How to Use:
- 1. Configure the 'itemWhitelist' table below with the item IDs you want to keep.
- You can find item IDs in-game (often by pressing F3+H).
- 2. Place this file on a mining turtle (e.g., 'quarry.lua').
- 3. Place a chest directly behind the turtle's starting position. This is where
- it will deposit all non-whitelisted items.
- 4. Make sure the turtle has some fuel. It will automatically find and use it
- from its inventory, and it will never drop fuel items.
- 5. Run the program with two arguments: width and length.
- Example: quarry.lua 10 20
- ]]
- -- =================================================================
- -- Configuration
- -- =================================================================
- -- Add the string IDs of items you want the turtle to KEEP.
- -- The turtle will automatically keep any fuel source, so you don't need to add coal.
- -- Example Format: ["mod_name:item_name"] = true,
- local itemWhitelist = {
- ["minecraft:diamond"] = true,
- ["minecraft:emerald"] = true,
- ["minecraft:raw_gold"] = true,
- ["minecraft:raw_iron"] = true,
- ["minecraft:lapis_lazuli"] = true,
- ["minecraft:redstone"] = true,
- -- Example for a modded ore from AllTheMods
- ["allthemodium:raw_allthemodium"] = true,
- ["allthemodium:raw_vibranium"] = true,
- ["allthemodium:raw_unobtainium"] = true,
- ["alltheores:raw_tin"] = true,
- ["alltheores:raw_lead"] = true,
- }
- -- =================================================================
- -- Helper Functions
- -- =================================================================
- -- Function to check fuel and refuel if necessary
- local function ensureFuel()
- if turtle.getFuelLevel() < 2 then
- print("Low on fuel. Attempting to refuel...")
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- print("Refueled successfully.")
- turtle.select(1) -- Select the first slot again for digging
- return true
- end
- end
- print("ERROR: Out of fuel! Please add fuel to my inventory.")
- return false
- end
- return true
- end
- -- Function to check if inventory is full and empty non-whitelisted items.
- local function manageInventory()
- local isFull = true
- for i = 1, 15 do -- Check first 15 slots
- if turtle.getItemCount(i) == 0 then
- isFull = false
- break
- end
- end
- if isFull then
- print("Inventory full. Dropping non-whitelisted items.")
- -- Turn around to face the chest
- turtle.turnLeft()
- turtle.turnLeft()
- -- Iterate through inventory and drop unwanted items
- for i = 1, 16 do
- turtle.select(i)
- local itemDetail = turtle.getItemDetail(i)
- if itemDetail then
- local itemName = itemDetail.name
- -- turtle.refuel(0) checks if the item is fuel without consuming it
- local isFuel = turtle.refuel(0)
- local isWhitelisted = itemWhitelist[itemName]
- -- If the item is NOT fuel and is NOT on the whitelist, drop it.
- if not isFuel and not isWhitelisted then
- turtle.drop()
- end
- end
- end
- turtle.select(1) -- Reselect the first slot
- -- Turn back to the original direction
- turtle.turnLeft()
- turtle.turnLeft()
- print("Inventory managed. Resuming mining.")
- end
- end
- -- A robust function for moving forward.
- local function moveForwardAndDig()
- if not ensureFuel() then
- return false
- end
- while turtle.detect() do
- if not turtle.dig() then
- print("Waiting for obstruction to clear...")
- os.sleep(1)
- end
- end
- while not turtle.forward() do
- print("Movement blocked. Re-digging...")
- turtle.dig()
- os.sleep(0.5)
- end
- return true
- end
- -- Function to dig a single row of a given length
- local function digRow(length)
- for i = 1, length - 1 do
- manageInventory()
- turtle.digUp()
- turtle.digDown()
- if not moveForwardAndDig() then
- return false
- end
- end
- manageInventory()
- turtle.digUp()
- turtle.digDown()
- return true
- end
- -- Function to turn the turtle right
- local function turnRight()
- ensureFuel()
- turtle.turnRight()
- end
- -- Function to turn the turtle left
- local function turnLeft()
- ensureFuel()
- turtle.turnLeft()
- end
- -- =================================================================
- -- Main Program Logic
- -- =================================================================
- local args = { ... }
- if #args ~= 2 then
- print("Usage: quarry <width> <length>")
- print("Example: quarry 10 20")
- return
- end
- local width = tonumber(args[1])
- local length = tonumber(args[2])
- if not width or not length or width <= 0 or length <= 0 then
- print("Error: Width and length must be positive numbers.")
- return
- end
- print("Starting quarry of size " .. width .. "x" .. length)
- print("Whitelist is active.")
- os.sleep(2)
- -- Main loop for digging rows
- for i = 1, width do
- print("Digging row " .. i .. "/" .. width)
- if not digRow(length) then
- print("Halting program due to movement failure.")
- return
- end
- if i < width then
- if i % 2 ~= 0 then
- turnRight()
- moveForwardAndDig()
- turnRight()
- else
- turnLeft()
- moveForwardAndDig()
- turnLeft()
- end
- end
- end
- -- Final inventory drop
- manageInventory()
- print("Quarry complete!")
Add Comment
Please, Sign In to add comment