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 vertical column at current position
- local function digVerticalColumn(depth)
- print("Digging vertical column to depth " .. depth)
- -- Dig current position
- manageInventory()
- turtle.digUp()
- turtle.digDown()
- -- Dig down to specified depth
- for level = 1, depth do
- if not moveDownAndDig() then
- print("Failed to dig down at level " .. level)
- -- Try to return to surface
- for i = 1, level - 1 do
- moveUpAndDig()
- end
- return false
- end
- -- Dig up and down at this level
- manageInventory()
- turtle.digUp()
- turtle.digDown()
- end
- -- Return to surface
- for i = 1, depth do
- if not moveUpAndDig() then
- print("ERROR: Failed to return to surface from level " .. i)
- return false
- end
- end
- return true
- end
- -- Function to move to next position in grid (snake pattern)
- local function moveToNextPosition(currentX, currentY, width, length, direction)
- if direction == "right" then
- if currentY < length then
- -- Move forward (right)
- if not moveForwardAndDig() then
- return false, currentX, currentY, direction
- end
- return true, currentX, currentY + 1, direction
- else
- -- At end of row, move down and change direction
- if currentX < width then
- turtle.turnRight()
- if not moveForwardAndDig() then
- return false, currentX, currentY, direction
- end
- turtle.turnRight()
- return true, currentX + 1, currentY, "left"
- else
- -- Finished all positions
- return false, currentX, currentY, direction
- end
- end
- else -- direction == "left"
- if currentY > 1 then
- -- Move forward (left)
- if not moveForwardAndDig() then
- return false, currentX, currentY, direction
- end
- return true, currentX, currentY - 1, direction
- else
- -- At end of row, move down and change direction
- if currentX < width then
- turtle.turnLeft()
- if not moveForwardAndDig() then
- return false, currentX, currentY, direction
- end
- turtle.turnLeft()
- return true, currentX + 1, currentY, "right"
- else
- -- Finished all positions
- return false, currentX, currentY, direction
- end
- end
- 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 vertical mining
- local totalPositions = width * length
- local currentPosition = 1
- local currentX = 1
- local currentY = 1
- local direction = "right"
- print("Starting vertical mining: " .. totalPositions .. " positions total")
- -- Mine the first position
- if not digVerticalColumn(depth) then
- print("Mining failed at first position!")
- return
- end
- -- Mine remaining positions
- while currentPosition < totalPositions do
- local success, newX, newY, newDirection = moveToNextPosition(currentX, currentY, width, length, direction)
- if not success then
- if newX == currentX and newY == currentY then
- print("Movement failed at position (" .. currentX .. ", " .. currentY .. ")")
- return
- else
- print("Finished mining all positions!")
- break
- end
- end
- currentX = newX
- currentY = newY
- direction = newDirection
- currentPosition = currentPosition + 1
- print("Mining position " .. currentPosition .. "/" .. totalPositions .. " at (" .. currentX .. ", " .. currentY .. ")")
- if not digVerticalColumn(depth) then
- print("Mining failed at position (" .. currentX .. ", " .. currentY .. ")")
- return
- end
- end
- -- Final inventory drop
- manageInventory()
- print("3D mining operation complete!")
Add Comment
Please, Sign In to add comment