jille_Jr icon

CC: Mining program (turtle) using inspect

jille_Jr | PRO | 11/15/20 02:30:48 PM UTC (Edited) | 0 ⭐ | 432 👁️ | Never ⏰ | []
Lua |

3.34 KB

|

None

|

0 👍

/

0 👎

--(( Settings ))--
 
local fuelcap = 120
local shaftoffset = 5
 
local junk = {
    "minecraft:stone",
    "minecraft:cobblestone",
    "minecraft:dirt",
    "minecraft:grass",
    "minecraft:gravel",
    --"minecraft:sand",
    "chisel:marble1",
    "chisel:marble2",
    "chisel:limestone1",
    "chisel:limestone2",
    "chisel:basalt1",
    "chisel:basalt2",
}
 
local fuel = {
    "minecraft:coal",
    "minecraft:charcoal",
    "minecraft:planks",
    "minecraft:wood",
}
 
--(( Variables ))--
 
local depth = 0
 
--(( Functions ))--
 
 
 
local function selectfromlist(list)
    for slot=1,16 do
        local data = turtle.getItemDetail(slot)
 
        if data then
            for _,block in ipairs(list) do
                if block == data.name then
                    turtle.select(slot)
                    return true
                end
            end
        end
    end
    return false
end
 
local function selectfuel()
    return selectfromlist(fuel)
end
 
local function selectjunk()
    return selectfromlist(junk)
end
 
-- side = "", "up", or "down"
local function isvaluable()
    local succ,data = turtle.inspect()
 
    if not succ then
        return false
    end
 
    for _,block in ipairs(junk) do
        if data.name == block then
            return false
        end
    end
 
    return true
end
 
local function refuel()
    local fuel = turtle.getFuelLevel()
    while fuel < fuelcap do
        print("Searching for fuel...")
        if selectfuel() and turtle.refuel(1) then
            return true
        end
        print("No fuel found. Press SPACE to search again!")
        local key
        repeat _,key=os.pullEvent("key")
        until key==keys.space
    end
end
 
local function dig(dir)
    if not dir or dir == "forward" then
        dir = ""
    end
    dir = dir:sub(1,1):upper() .. dir:sub(2):lower()
    turtle.select(1)
    if not turtle["detect"..dir]() then return true end
    while not turtle["dig"..dir]() do
        if not turtle["attack"..dir]() then
            return false
        end
    end
    return true
end
 
local function move(dir)
    refuel()
    while not turtle[dir]() do
        if dig(dir) == false then
            return false
        end
    end
    return true
end
 
local function down()
    if move("down") then
        depth = depth + 1
        return true
    end
    return false
end
 
local function up()
    if move("up") then
        depth = depth - 1
        return true
    end
    return false
end
 
local function forward()
    return move("forward")
end
 
local function checksides()
    local turns = {turtle.turnRight,turtle.turnLeft}
    local turn = turns[math.random(1,#turns)]
    for count = 1,4 do
        if isvaluable() then
            dig()
        end
        turn()
    end
end
 
local function mine()
    -- go down little first
    repeat until down()
    repeat until down()
    if selectjunk() then
        turtle.placeUp()
    end
 
    -- down to bottom
    while true do
        checksides()
        if not down() then
            print("Bedrock after " .. depth .. " blocks!")
            break
        end
    end
 
    -- up to miss bedrock noise
    for i=1,5 do
        repeat until up()
    end
 
    -- move to next shaft
    for i=1,shaftoffset do
        repeat until forward()
    end
 
    -- up to surface
    while depth > 0 do
        checksides()
        repeat until up()
    end
 
    -- fill hole
    if selectjunk() then
        turtle.placeDown()
    end
end
 
local function formatTime(sec)
    if sec > 60 then
        return math.floor(sec/60).." min "..math.floor(sec%60).." sec"
    end
    return math.floor(sec) .. " sec"
end
 
print("Commencing mining operation!")
local start = os.clock()
 
mine()
 
local took = os.clock() - start
print("Operation complete! Took " .. formatTime(took))

Comments