Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --v1.0
- --A "Swiss Cheese" Turtle Mining Program that leaves the environment relatively undamaged by skipping junk and dangerous blocks
- --by hornedcommando
- --Known issues:
- --Turtle is not optimized to keep only one slot occupied with fuel, also if chests are before the fuel slot it will burn chests for fuel
- --Runtime can be very long, and if you log off / chunk is unloaded the turtle will be at the bottom of a deep deep hole
- --Sometimes (somehow) the turtle returns to the surface without cobblestone, and doesn't cover the hole, probably could just have it use junk rather than hard coded cobblestone
- --Could dig up at Surface + New Mineshaft to make the trail human walkable
- --Torches are spread far enough that spawns will happen in the mine.
- --Turtles (STILL) will just go back to mining after returning home
- --Import Relative Location Libraries (run this on the turtle: pastebin get KFa0XQqf TrackingTurtle )
- local tTurtle = require("TrackingTurtle")
- local myTurtle = tTurtle.create()
- --Global Variables
- --Start Location (so the turtle can return home
- startx, starty, startz = myTurtle.getPosition()
- --Starting face
- startFace = myTurtle.getFacing()
- --Max distance (to prevent the turtle from getting lost)
- MAX_DISTANCE = 150
- --Flag to check if lootchest needs to be placed
- isJunkShuffle = false
- -- Function to check if a block is forbidden
- local function isForbiddenBlock(blockName)
- local forbiddenBlocks = {
- "forbidden_arcanus:stella_arcanum",
- "minecraft:bedrock"
- }
- for _, forbiddenBlock in ipairs(forbiddenBlocks) do
- if blockName == forbiddenBlock then
- return true
- end
- end
- return false
- end
- --Function to help the turtle navigate to given coordinates (mostly home for now)
- function navigate(x, y, z, face)
- local currentX, currentY, currentZ = myTurtle.getPosition()
- local currentFace = myTurtle.getFacing()
- local xDiff = x - currentX
- local yDiff = y - currentY
- local zDiff = z - currentZ
- local faceDiff = face - currentFace
- print("I'm at: " .. currentX .. ", " .. currentY .. ", " .. currentZ .. " facing: " .. currentFace)
- if xDiff > 0 then
- print("I'm west of target, heading east")
- myTurtle.face(1)
- for i = 1, xDiff do
- myTurtle.forward()
- end
- elseif xDiff < 0 then
- print("I'm east of target, heading west")
- myTurtle.face(3)
- for i = 1, math.abs(xDiff) do
- myTurtle.forward()
- end
- end
- if yDiff > 0 then
- print("I'm below target, heading up")
- for i = 1, yDiff do
- myTurtle.up()
- end
- elseif yDiff < 0 then
- print("I'm above target, heading down")
- for i = 1, math.abs(yDiff) do
- myTurtle.down()
- end
- end
- if zDiff > 0 then
- print("I'm north of target, heading south")
- myTurtle.face(2)
- for i = 1, zDiff do
- myTurtle.forward()
- end
- elseif zDiff < 0 then
- print("I'm south of target, heading north")
- myTurtle.face(0)
- for i = 1, math.abs(zDiff) do
- myTurtle.forward()
- end
- end
- myTurtle.face(face)
- print("I'm home!")
- os.shutdown()
- end
- --Strong up, checks block, digs, then goes up
- function gu()
- print("Going up")
- local success, block = turtle.inspectUp()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- else
- while not myTurtle.up() do
- print("Digging up")
- turtle.digUp()
- end
- end
- end
- --Strong down, checks block, digs, then goes down
- function gd()
- print("Going down")
- local success, block = turtle.inspectDown()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- return false
- else
- while not myTurtle.down() do
- print("Digging down")
- turtle.digDown()
- end
- return true
- end
- end
- --Strong forward, checks block, digs, then goes forward
- function gf()
- print("Going forward")
- local success, block = turtle.inspect()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- return false
- else
- while not myTurtle.forward() do
- print("Digging forward")
- turtle.dig()
- end
- end
- end
- --Check if any slot is empty
- local function inventoryFull()
- print("Checking inventory")
- for slot = 1, 16 do
- local count = turtle.getItemCount(slot)
- if turtle.getItemCount(slot) == 0 then
- print("Slot: " .. slot .. " is empty. Inventory is not full.")
- return false -- If any slot is empty, return false
- end
- end
- return true -- If all slots are occupied, return true
- end
- -- Function to search the inventory for an item
- local function searchInventory(name)
- for slot = 1, 16 do
- turtle.select(slot)
- local slotDetail = turtle.getItemDetail()
- if slotDetail and slotDetail.name:find(name) then
- return true, slot
- -- Return the slot number if item found
- end
- end
- return false -- Return false if item not found
- end
- -- Dictionary of items which the turtle will drop
- local junk = {
- "minecraft:dirt",
- "minecraft:cobblestone",
- "create:dolomite_cobblestone",
- "create:limestone_cobblestone",
- "create:gabbro_cobblestone",
- "create:granite_cobblestone",
- "forbidden_arcanus:darkstone",
- "extcaves:sedimentstone",
- "extcaves:lavastone",
- "extcaves:pebble_sedimentstone",
- "create:andesite_cobblestone",
- "create:diorite_cobblestone",
- "minecraft:gravel",
- "occultism:datura_seeds",
- "minecraft:wheat_seeds",
- "extcaves:pebble_stone",
- "minecraft:stone",
- "minecraft:granite",
- "minecraft:diorite",
- "minecraft:andesite",
- "minecraft:gravel",
- "minecraft:deepslate",
- "railcraft:quarried_stone",
- "minecraft:cobbled_deepslate",
- "minecraft:tuff",
- "minecraft:basalt",
- "minecraft:blackstone",
- "minecraft:calcite",
- "minecraft:smooth_basalt",
- "minecraft:sand",
- "minecraft:gravel",
- "minecraft:deepslate",
- "minecraft:cobbled_deepslate",
- "darkerdepths:cobbled_sandstone",
- "minecraft:sandstone"
- -- Add more junk items as needed
- }
- --Function to check if a block is junk
- local function isJunk(blockName)
- for _, junkBlock in ipairs(junk) do
- if blockName == junkBlock then
- return true
- end
- end
- return false
- end
- --Function to drop junk from inventory
- local function dropJunk()
- print("Dropping junk")
- for slot = 1, 16 do
- turtle.select(slot)
- local itemDetail = turtle.getItemDetail()
- if itemDetail and isJunk(itemDetail.name) then
- turtle.drop()
- end
- end
- end
- --Because the turtle digs down, there is a tendency, as inventory fills up, for good items to fall on top of the turtle. If the turtle detects it is full it will attempt to drop junk, and pickup loot
- local function junkShuffle()
- print("Shuffling junk")
- myTurtle.face(0)
- local success, block = turtle.inspect()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- else
- turtle.dig()
- dropJunk()
- while turtle.suckUp() do
- turtle.suckUp()
- dropJunk()
- end
- isJunkShuffle = true
- end
- end
- --Function to refuel the turtle, attempts to maintain a specified fuel level, this could be adjusted based on abundance of fuel or time between fuel calls
- local function smartRefuel()
- print("Checking Fuel")
- -- Check if fuel level is below 1000
- while turtle.getFuelLevel() < 1000 do
- print("Refueling")
- for slot = 1, 16 do
- turtle.select(slot) -- Select the slot
- if turtle.refuel(0) then
- -- Check if the selected item is a fuel
- turtle.refuel(1) -- Refuel with the selected item
- break -- Stop searching for fuel after refueling
- end
- end
- end
- end
- --If a Junk Shuffle has been necessary, the turtle will place a chest and torch down to offload extra items at the top of the mineshaft
- function lootChest()
- print("storing goodies")
- dropJunk()
- myTurtle.face(1)
- local success, block = turtle.inspect()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- else
- turtle.dig()
- searchInventory("minecraft:chest")
- if turtle.place() then
- for slot = 1, 16 do
- turtle.select(slot)
- local itemdetail = turtle.getItemDetail()
- if itemdetail and itemdetail.name ~= "minecraft:chest" and itemdetail.name ~= "thermal:charcoal_block" and itemdetail.name ~= "minecraft:torch" and itemdetail.name ~= "minecraft:coal" then
- turtle.drop()
- isJunkShuffle = false
- end
- end
- else
- print("No chest found, going home")
- navigate(startx, starty, startz, startFace)
- return nil
- end
- end
- myTurtle.face(3)
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- else
- turtle.dig()
- searchInventory("minecraft:torch")
- turtle.place()
- end
- myTurtle.face(0)
- newMineshaft()
- end
- -- Function to create a new mineshaft
- function newMineshaft()
- print("Creating new mineshaft")
- local currentX, currentY, currentZ = myTurtle.getPosition()
- local distanceX = math.abs(currentX)
- local distanceY = math.abs(currentY)
- local distanceZ = math.abs(currentZ)
- if distanceX > MAX_DISTANCE or distanceZ > MAX_DISTANCE then
- print("I'm far away, heading home")
- navigate(startx, starty, startz, startFace)
- return nil
- end
- if isJunkShuffle == true then
- lootChest()
- end
- for _ = 1, 3 do
- local success, block = turtle.inspectUp()
- if isForbiddenBlock(block.name) then
- print("Detected block is forbidden")
- else
- turtle.digUp()
- end
- gf()
- end
- mine()
- end
- function riseToSurface()
- local currentX, currentY, currentZ = myTurtle.getPosition()
- print("surfacing")
- -- Loop until the turtle reaches Y=0
- while currentY < 0 do
- gu()
- -- Update the current position
- currentX, currentY, currentZ = myTurtle.getPosition()
- end
- -- find cobblestone in the turtle's inventory
- -- Loop through all inventory slots
- for slot = 1, 16 do
- -- Select the current slot
- turtle.select(slot)
- -- Get information about the item in the selected slot
- local itemDetail = turtle.getItemDetail()
- -- Check if an item is present in the selected slot and it is cobblestone
- if itemDetail and itemDetail.name == "minecraft:cobblestone" then
- turtle.placeDown()
- end
- end
- -- Cobblestone not found in the inventory, return nil
- smartRefuel()
- dropJunk()
- myTurtle.face(startFace)
- newMineshaft()
- end
- -- Function to mine
- function mine()
- print("Mining")
- gd()
- print("polite hole dug")
- while true do
- if inventoryFull() then
- junkShuffle()
- end
- if not gd() then
- print("can not descend, returning to surface")
- riseToSurface()
- break
- else
- for i = 1, 4 do
- local success, block = turtle.inspect()
- if isForbiddenBlock(block.name) or isJunk(block.name) then
- myTurtle.turnRight()
- else
- turtle.dig()
- myTurtle.turnRight()
- end
- end
- end
- end
- end
- smartRefuel()
- mine() -- Call the mine function to start mining
Add Comment
Please, Sign In to add comment