Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mining Turtle - Diagnostic Version
- -- Shows exactly what the turtle sees at check position
- --=============================================================================
- -- CONFIGURATION
- --=============================================================================
- local travelDirection = "U"
- local miningDirection = "F"
- local triggerDirection = "U" -- Direction to look for trigger
- local checkOffset = {
- {"U", 1},
- }
- local strictMode = true
- local fuelInventoryDirection = "D"
- local minFuelLevel = 100
- local checkInterval = 5
- local initialWait = 5
- local context = {
- modName = "justdirethings",
- prefix = "raw_",
- suffix = "_",
- itemType = "ore",
- itemNames = {
- "ferricore",
- "blazegold",
- "celestigem",
- "eclipsealloy",
- "coal_t1",
- "coal_t2",
- "coal_t3",
- "coal_t4",
- },
- }
- local additionalWhitelist = {}
- local fuelItems = {
- ["minecraft:coal"] = true,
- ["minecraft:charcoal"] = true,
- ["minecraft:coal_block"] = true,
- ["minecraft:oak_planks"] = true,
- ["minecraft:spruce_planks"] = true,
- ["minecraft:birch_planks"] = true,
- ["minecraft:jungle_planks"] = true,
- ["minecraft:acacia_planks"] = true,
- ["minecraft:dark_oak_planks"] = true,
- ["minecraft:blaze_rod"] = true,
- ["minecraft:lava_bucket"] = true,
- }
- --=============================================================================
- -- WHITELIST
- --=============================================================================
- local whitelistedBlocks = {}
- local function buildWhitelist()
- whitelistedBlocks = {}
- for _, itemName in ipairs(context.itemNames) do
- local fullName = context.modName .. ":" .. context.prefix .. itemName .. context.suffix .. context.itemType
- whitelistedBlocks[fullName] = true
- end
- for _, blockName in ipairs(additionalWhitelist) do
- whitelistedBlocks[blockName] = true
- end
- end
- buildWhitelist()
- --=============================================================================
- -- DIRECTION HELPERS
- --=============================================================================
- local function getOppositeDirection(dir)
- local opposites = {
- U = "D", D = "U", F = "B", B = "F", L = "R", R = "L",
- }
- return opposites[dir] or "B"
- end
- local function directionToSide(dir)
- local sides = {
- U = "top", D = "bottom", F = "front", B = "back", L = "left", R = "right",
- }
- return sides[dir] or "front"
- end
- local function moveInDirection(dir)
- if dir == "U" then return turtle.up()
- elseif dir == "D" then return turtle.down()
- elseif dir == "F" then return turtle.forward()
- elseif dir == "B" then return turtle.back()
- elseif dir == "L" then
- turtle.turnLeft()
- local success = turtle.forward()
- turtle.turnRight()
- return success
- elseif dir == "R" then
- turtle.turnRight()
- local success = turtle.forward()
- turtle.turnLeft()
- return success
- end
- return false
- end
- local function digInDirection(dir)
- if dir == "U" then return turtle.digUp()
- elseif dir == "D" then return turtle.digDown()
- elseif dir == "F" then return turtle.dig()
- elseif dir == "B" then
- turtle.turnLeft() turtle.turnLeft()
- local success = turtle.dig()
- turtle.turnLeft() turtle.turnLeft()
- return success
- elseif dir == "L" then
- turtle.turnLeft()
- local success = turtle.dig()
- turtle.turnRight()
- return success
- elseif dir == "R" then
- turtle.turnRight()
- local success = turtle.dig()
- turtle.turnLeft()
- return success
- end
- return false
- end
- local function inspectInDirection(dir)
- if dir == "U" then return turtle.inspectUp()
- elseif dir == "D" then return turtle.inspectDown()
- elseif dir == "F" then return turtle.inspect()
- elseif dir == "B" then
- turtle.turnLeft() turtle.turnLeft()
- local success, data = turtle.inspect()
- turtle.turnLeft() turtle.turnLeft()
- return success, data
- elseif dir == "L" then
- turtle.turnLeft()
- local success, data = turtle.inspect()
- turtle.turnRight()
- return success, data
- elseif dir == "R" then
- turtle.turnRight()
- local success, data = turtle.inspect()
- turtle.turnLeft()
- return success, data
- end
- return false, nil
- end
- local function suckInDirection(dir)
- if dir == "U" then return turtle.suckUp()
- elseif dir == "D" then return turtle.suckDown()
- elseif dir == "F" then return turtle.suck()
- elseif dir == "B" then
- turtle.turnLeft() turtle.turnLeft()
- local success = turtle.suck()
- turtle.turnLeft() turtle.turnLeft()
- return success
- elseif dir == "L" then
- turtle.turnLeft()
- local success = turtle.suck()
- turtle.turnRight()
- return success
- elseif dir == "R" then
- turtle.turnRight()
- local success = turtle.suck()
- turtle.turnLeft()
- return success
- end
- return false
- end
- --=============================================================================
- -- OFFSET MOVEMENT
- --=============================================================================
- local function hasOffset()
- return #checkOffset > 0
- end
- local function moveToCheckPosition()
- for _, move in ipairs(checkOffset) do
- local dir = move[1]
- local dist = move[2]
- for i = 1, dist do
- if not moveInDirection(dir) then
- digInDirection(dir)
- if not moveInDirection(dir) then
- return false
- end
- end
- end
- end
- return true
- end
- local function returnFromCheckPosition()
- for i = #checkOffset, 1, -1 do
- local move = checkOffset[i]
- local dir = getOppositeDirection(move[1])
- local dist = move[2]
- for j = 1, dist do
- if not moveInDirection(dir) then
- digInDirection(dir)
- moveInDirection(dir)
- end
- end
- end
- return true
- end
- --=============================================================================
- -- FUEL MANAGEMENT
- --=============================================================================
- local function isValidFuel(itemName)
- return fuelItems[itemName] == true
- end
- local function findFuelSlot()
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and isValidFuel(item.name) then
- return slot
- end
- end
- return nil
- end
- local function findEmptySlot()
- for slot = 1, 16 do
- if turtle.getItemCount(slot) == 0 then
- return slot
- end
- end
- return nil
- end
- local function refuel()
- if turtle.getFuelLevel() == "unlimited" then return true end
- if turtle.getFuelLevel() >= minFuelLevel then return true end
- -- Try from inventory first
- local fuelSlot = findFuelSlot()
- while fuelSlot and turtle.getFuelLevel() < minFuelLevel do
- turtle.select(fuelSlot)
- turtle.refuel(1)
- fuelSlot = findFuelSlot()
- end
- if turtle.getFuelLevel() >= minFuelLevel then
- turtle.select(1)
- return true
- end
- -- Try peripheral
- local side = directionToSide(fuelInventoryDirection)
- if peripheral.isPresent(side) then
- local inv = peripheral.wrap(side)
- if inv and inv.list and inv.pushItems then
- local emptySlot = findEmptySlot()
- if emptySlot then
- turtle.select(emptySlot)
- local items = inv.list()
- for slot, item in pairs(items) do
- if item and isValidFuel(item.name) then
- local myName = os.getComputerLabel() or ("turtle_" .. os.getComputerID())
- local transferred = 0
- pcall(function()
- transferred = inv.pushItems(myName, slot, 64, emptySlot)
- end)
- if transferred and transferred > 0 then
- turtle.refuel()
- break
- end
- end
- end
- end
- end
- end
- turtle.select(1)
- return turtle.getFuelLevel() > 0
- end
- --=============================================================================
- -- BLOCK DETECTION
- --=============================================================================
- local function isWhitelisted(blockData)
- if not blockData or not blockData.name then return false end
- return whitelistedBlocks[blockData.name] == true
- end
- local function getBlockName(dir)
- local success, data = inspectInDirection(dir)
- if success then
- return data.name
- else
- return "(air/empty)"
- end
- end
- --=============================================================================
- -- MINING LOGIC
- --=============================================================================
- local function mineOres()
- local distanceTraveled = 0
- local stopReason = "complete"
- print("Starting mining...")
- while true do
- if not refuel() then
- stopReason = "fuel"
- break
- end
- local mineSuccess, mineBlockData = inspectInDirection(miningDirection)
- if mineSuccess then
- if isWhitelisted(mineBlockData) then
- print("Mining: " .. mineBlockData.name)
- digInDirection(miningDirection)
- end
- end
- local travelSuccess, travelBlockData = inspectInDirection(travelDirection)
- if travelSuccess then
- if strictMode then
- if isWhitelisted(travelBlockData) then
- digInDirection(travelDirection)
- else
- print("Strict stop: " .. travelBlockData.name)
- stopReason = "strict"
- break
- end
- else
- digInDirection(travelDirection)
- end
- end
- if moveInDirection(travelDirection) then
- distanceTraveled = distanceTraveled + 1
- else
- stopReason = "blocked"
- break
- end
- sleep(0.1)
- end
- print("Returning " .. distanceTraveled .. " blocks")
- local returnDirection = getOppositeDirection(travelDirection)
- for i = 1, distanceTraveled do
- local mineSuccess, mineBlockData = inspectInDirection(miningDirection)
- if mineSuccess and isWhitelisted(mineBlockData) then
- digInDirection(miningDirection)
- end
- if not moveInDirection(returnDirection) then
- digInDirection(returnDirection)
- moveInDirection(returnDirection)
- end
- sleep(0.05)
- end
- if hasOffset() then
- returnFromCheckPosition()
- end
- print("Cycle complete.")
- end
- --=============================================================================
- -- MAIN LOOP
- --=============================================================================
- local function main()
- print("=================================")
- print(" Mining Turtle - Diagnostic")
- print("=================================")
- print("Fuel: " .. turtle.getFuelLevel())
- print("---------------------------------")
- sleep(initialWait)
- while true do
- refuel()
- -- Move to check position
- if hasOffset() then
- print("")
- print("Moving to check position...")
- if not moveToCheckPosition() then
- print("ERROR: Could not move!")
- sleep(checkInterval)
- goto continue
- end
- end
- -- SCAN AND REPORT ALL DIRECTIONS
- print("")
- print("=== BLOCKS AT CHECK POSITION ===")
- print("UP: " .. getBlockName("U"))
- print("DOWN: " .. getBlockName("D"))
- print("FRONT: " .. getBlockName("F"))
- print("BACK: " .. getBlockName("B"))
- print("LEFT: " .. getBlockName("L"))
- print("RIGHT: " .. getBlockName("R"))
- print("=================================")
- print("")
- -- Check trigger direction
- local triggerSuccess, triggerData = inspectInDirection(triggerDirection)
- if triggerSuccess then
- print("TRIGGER FOUND: " .. triggerData.name)
- sleep(0.5)
- mineOres()
- sleep(2)
- else
- print("No trigger (looking " .. triggerDirection .. ")")
- if hasOffset() then
- print("Returning to home...")
- returnFromCheckPosition()
- end
- print("Waiting " .. checkInterval .. " seconds...")
- sleep(checkInterval)
- end
- ::continue::
- end
- end
- main()
Advertisement