Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- 3D Rectangular Mining Program for ComputerCraft Turtles (with Whitelist)
- Description:
- This program instructs a mining turtle to dig a rectangular area down to a specified depth.
- 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., 'down.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 three arguments: width, length, and depth.
- Example: down 10 20 5
- ]]
- -- =================================================================
- -- 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
- -- A robust function for moving down.
- local function moveDownAndDig()
- if not ensureFuel() then
- return false
- end
- while turtle.detectDown() do
- if not turtle.digDown() then
- print("Waiting for obstruction below to clear...")
- os.sleep(1)
- end
- end
- while not turtle.down() do
- print("Downward movement blocked. Re-digging...")
- turtle.digDown()
- os.sleep(0.5)
- end
- return true
- end
- -- A robust function for moving up.
- local function moveUpAndDig()
- if not ensureFuel() then
- return false
- end
- while turtle.detectUp() do
- if not turtle.digUp() then
- print("Waiting for obstruction above to clear...")
- os.sleep(1)
- end
- end
- while not turtle.up() do
- print("Upward movement blocked. Re-digging...")
- turtle.digUp()
- os.sleep(0.5)
- end
- return true
- end
- -- Function to dig a single row of a given length at current level
- 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
- -- Function to return to surface from any depth
- local function returnToSurface(currentDepth)
- print("Returning to surface from depth " .. currentDepth)
- for i = 1, currentDepth do
- if not moveUpAndDig() then
- print("ERROR: Failed to return to surface!")
- return false
- end
- end
- print("Reached surface.")
- return true
- end
- -- Function to dig a complete level (width x length rectangle)
- local function digLevel(width, length)
- -- Main loop for digging rows at this level
- for i = 1, width do
- if not digRow(length) then
- print("Halting program due to movement failure.")
- return false
- end
- if i < width then
- if i % 2 ~= 0 then
- turnRight()
- moveForwardAndDig()
- turnRight()
- else
- turnLeft()
- moveForwardAndDig()
- turnLeft()
- end
- end
- end
- return true
- end
- -- Function to return to start position of current level
- local function returnToLevelStart(width, length)
- -- If we're at an odd row, we need to go back to start
- if width % 2 ~= 0 then
- -- We're facing the same direction as we started
- -- Turtle is at (width-1, length-1) facing original direction
- turnLeft()
- turnLeft()
- for i = 1, length - 1 do
- moveForwardAndDig()
- end
- turnLeft()
- for i = 1, width - 1 do
- moveForwardAndDig()
- end
- turnLeft()
- else
- -- We're facing opposite to start direction
- -- Turtle is at (width-1, 0) facing opposite to start direction
- -- Just need to move back to (0, 0) and turn around
- for i = 1, width - 1 do
- moveForwardAndDig()
- end
- -- Now at (0, 0) facing opposite direction, turn around
- turnLeft()
- turnLeft()
- end
- end
- -- =================================================================
- -- Main Program Logic
- -- =================================================================
- local args = { ... }
- if #args ~= 3 then
- print("Usage: down <width> <length> <depth>")
- print("Example: down 10 20 5")
- return
- end
- local width = tonumber(args[1])
- local length = tonumber(args[2])
- local depth = tonumber(args[3])
- if not width or not length or not depth or width <= 0 or length <= 0 or depth <= 0 then
- print("Error: Width, length, and depth must be positive numbers.")
- return
- end
- print("Starting 3D mining operation: " .. width .. "x" .. length .. "x" .. depth)
- print("Whitelist is active.")
- os.sleep(2)
- -- Main loop for digging levels
- for level = 1, depth do
- print("Mining level " .. level .. "/" .. depth .. " (depth " .. (level - 1) .. ")")
- if not digLevel(width, length) then
- print("Mining failed at level " .. level)
- returnToSurface(level - 1)
- return
- end
- -- Don't move down after the last level
- if level < depth then
- returnToLevelStart(width, length)
- if not moveDownAndDig() then
- print("Failed to move to next level!")
- returnToSurface(level - 1)
- return
- end
- end
- end
- -- Return to surface
- returnToLevelStart(width, length)
- returnToSurface(depth)
- -- Final inventory drop
- manageInventory()
- print("3D mining operation complete!")
Add Comment
Please, Sign In to add comment