jille_Jr icon

CC: Mining program (turtle)

jille_Jr | PRO | 11/20/13 04:45:04 PM UTC | 0 ⭐ | 747 👁️ | Never ⏰ | []
Lua |

2.51 KB

|

None

|

0 👍

/

0 👎

 
--(( Settings ))--
 
local fuelcap = 50
 
--(( Variables ))--
 
local depth = 0
local items = {}
 
--(( Functions ))--
 
local function claim(slot)
    items[slot] = true
end
 
-- side = "", "up", or "down"
local function isjunk(side)
    side = side or ""
    side = side:sub(1,1):upper() .. side:sub(2):lower()
    for slot,value in pairs(items) do
        turtle.select(slot)
        if turtle["compare"..side]() then
            return true
        end
    end
    return false
end
 
local function refuel()
    local fuel = turtle.getFuelLevel()
    while fuel < fuelcap do
        print("Searching for fuel..")
        for slot = 1,16 do
            if items[slot] ~= true then
                turtle.select(slot)
                if turtle.refuel(1) then return true end
            end
        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)
    dir = dir or ""
    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 down()
    refuel()
    while not turtle.down() do
        if dig("down") == false then
            return false
        end
    end
    return true
end
 
local function up()
    refuel()
    while not turtle.up() do
        if dig("up") == false then
            return false
        end
    end
    return true
end
 
local function checksides()
    local turns = {turtle.turnRight,turtle.turnLeft,}
    local turn = turns[math.random(1,#turns)]
    for count = 1,4 do
        if not isjunk() then
            dig()
        end
        turn()
    end
end
 
local function fill()
    -- first try junk items
    for slot,state in pairs(items) do
        if state == true then
            if turtle.getItemCount(slot) > 1 then
                turtle.select(slot)
                while not turtle.placeDown() do
                    turtle.attackDown()
                end
                return
            end
        end
    end
    -- then use storage
    for slot = 1,16 do
        if items[slot] ~= true then
            if turtle.getItemCount(slot) > 0 then
                turtle.select(slot)
                if turtle.placeDown() then
                    return
                end
            end
        end
    end
end
 
local function mine()
    local running = true
    while running do
        checksides()
        if not down() then
            print("Probably hit bedrock!")
            running = false
        else
            depth = depth + 1
        end
    end
    print("Heading back up...")
    for count = 1,depth do
        repeat until up()
    end
    fill()
end
 
claim(1)--stone
claim(2)--dirt
claim(3)--cobble/gravel
print("Commencing mining operation!")
mine()

Comments