Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local mainTunnelLength = 1000
- local branchLength = 20
- local branchSpacing = 5
- local fuelLimit = 100
- local maxOreSearchDepth = 30
- -- Position tracking
- local pos = {x=0, y=0, z=0}
- local dir = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
- local resumePoint = nil
- -- Movement helpers
- local function turnLeft()
- turtle.turnLeft()
- dir = (dir - 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- dir = (dir + 1) % 4
- end
- local function faceDirection(targetDir)
- while dir ~= targetDir do
- turnRight()
- end
- end
- local function moveForward()
- refuelIfNeeded()
- while turtle.detect() do
- turtle.dig()
- sleep(0.5)
- end
- while not turtle.forward() do
- turtle.dig()
- sleep(0.5)
- end
- if dir == 0 then pos.z = pos.z - 1
- elseif dir == 1 then pos.x = pos.x + 1
- elseif dir == 2 then pos.z = pos.z + 1
- elseif dir == 3 then pos.x = pos.x - 1 end
- end
- local function moveBack()
- while not turtle.back() do
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- sleep(0.2)
- end
- if dir == 0 then pos.z = pos.z + 1
- elseif dir == 1 then pos.x = pos.x - 1
- elseif dir == 2 then pos.z = pos.z - 1
- elseif dir == 3 then pos.x = pos.x + 1 end
- end
- local function moveUp()
- while not turtle.up() do turtle.digUp(); sleep(0.2) end
- pos.y = pos.y + 1
- end
- local function moveDown()
- while not turtle.down() do turtle.digDown(); sleep(0.2) end
- pos.y = pos.y - 1
- end
- -- Refuel
- function refuelIfNeeded()
- if turtle.getFuelLevel() < fuelLimit then
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(1) then
- print("Refueled with slot " .. i)
- break
- end
- end
- turtle.select(1)
- end
- end
- -- Inventory management
- local function keepItem(name)
- local patterns = {"cobblestone", "gravel", "flint", "marble", "dirt", "apatite", "thaumcraft", "xycronium", "thermalfoundation:material"}
- for _, pattern in ipairs(patterns) do
- if string.find(name, pattern) then return false end
- end
- return true
- end
- local function cleanInventory()
- for i = 1, 16 do
- turtle.select(i)
- local item = turtle.getItemDetail()
- if item and not keepItem(item.name) then
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- local function isInventoryFull()
- for i = 2, 16 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- -- Position restore
- local function savePosition()
- resumePoint = {x = pos.x, y = pos.y, z = pos.z, dir = dir}
- end
- local function resumePosition()
- if not resumePoint then return end
- if pos.x < resumePoint.x then faceDirection(1)
- elseif pos.x > resumePoint.x then faceDirection(3) end
- while pos.x ~= resumePoint.x do moveForward() end
- if pos.z < resumePoint.z then faceDirection(2)
- elseif pos.z > resumePoint.z then faceDirection(0) end
- while pos.z ~= resumePoint.z do moveForward() end
- while pos.y < resumePoint.y do moveUp() end
- while pos.y > resumePoint.y do moveDown() end
- faceDirection(resumePoint.dir)
- end
- -- Ore detection
- local function isOre(name)
- local patterns = {"ore", "Ore", "ic2", "thermal", "gregtech", "forestry", "redpower"}
- for _, pattern in ipairs(patterns) do
- if string.find(name, pattern) then return true end
- end
- return false
- end
- local function inspectBlock(inspectFunc)
- local success, data = inspectFunc()
- return success and data.name and isOre(data.name)
- end
- local function mineOreVein(depth)
- if depth > maxOreSearchDepth then return end
- local function tryMine(dirFunc, moveFunc, returnFunc)
- if dirFunc() then
- moveFunc()
- mineOreVein(depth + 1)
- returnFunc()
- end
- end
- tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
- turnLeft()
- tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
- turnRight()
- turnRight()
- tryMine(function() return inspectBlock(turtle.inspect) end, moveForward, moveBack)
- turnLeft()
- tryMine(function() return inspectBlock(turtle.inspectUp) end, moveUp, moveDown)
- tryMine(function() return inspectBlock(turtle.inspectDown) end, moveDown, moveUp)
- end
- -- Mining
- local function mineBranch()
- for i = 1, branchLength do
- moveForward()
- mineOreVein(0)
- end
- turnLeft()
- turnLeft()
- for i = 1, branchLength do
- moveForward()
- end
- if isInventoryFull() then
- savePosition()
- returnToStart()
- dropAllItems()
- resumePosition()
- end
- cleanInventory()
- turnLeft()
- turnLeft()
- end
- local function stripMine()
- for i = 0, mainTunnelLength - 1 do
- moveForward()
- mineOreVein(0)
- if i % branchSpacing == 0 then
- turnLeft()
- mineBranch()
- turnRight()
- turnRight()
- mineBranch()
- turnLeft()
- end
- end
- end
- -- Return and unload
- function returnToStart()
- print("Returning to start...")
- if pos.x > 0 then faceDirection(3)
- elseif pos.x < 0 then faceDirection(1) end
- while pos.x ~= 0 do moveForward() end
- if pos.z > 0 then faceDirection(0)
- elseif pos.z < 0 then faceDirection(2) end
- while pos.z ~= 0 do moveForward() end
- while pos.y > 0 do moveDown() end
- while pos.y < 0 do moveUp() end
- faceDirection(0)
- end
- function dropAllItems()
- print("Dropping off items...")
- for i = 2, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- end
- -- Start
- print("Starting enhanced strip mining...")
- stripMine()
- returnToStart()
- dropAllItems()
- print("Mining complete. Turtle has returned.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement