heresjohnny32 icon

simple mineing

heresjohnny32 | PRO | 07/17/26 02:07:45 AM UTC (Edited) | 1 ⭐ | 31778 👁️ | Never ⏰ | []
Lua |

5.29 KB

|

Gaming

|

1 👍

/

0 👎

-- Permanent Home Quarry Script
 
-- Track coordinates relative to start (0,0,0 is the starting point before the 4-block offset)
local x, y, z = 0, 0, 0
local dir = 0 -- 0=forward (+Z), 1=right (+X), 2=back (-Z), 3=left (-X)
local targetX, targetY, targetZ = 0, 0, 0
 
-- Prompt for dimensions
print("Enter Width (X - Right):")
targetX = tonumber(read()) or 32
print("Enter Length (Z - Forward):")
targetZ = tonumber(read()) or 32
print("Enter Height (Y - Down):")
targetY = tonumber(read()) or 32
 
-- Fuel and inventory config
local FUEL_SLOT = 1
local MIN_FUEL = 200
 
-- Navigation helpers
local function turnRight()
    turtle.turnRight()
    dir = (dir + 1) % 4
end
 
local function turnLeft()
    turtle.turnLeft()
    dir = (dir - 1) % 4
end
 
local function face(targetDir)
    while dir ~= targetDir do
        turnRight()
    end
end
 
local function checkFuel()
    if turtle.getFuelLevel() == "unlimited" then return true end
    
    -- Try auto-refueling from inventory slot 1
    if turtle.getFuelLevel() < MIN_FUEL then
        local currentSlot = turtle.getSelectedSlot()
        turtle.select(FUEL_SLOT)
        turtle.refuel(16)
        turtle.select(currentSlot)
    end
    
    -- Return total distance back to absolute (0,0,0) home estimate
    local distHome = math.abs(x) + math.abs(y) + math.abs(z)
    return turtle.getFuelLevel() > (distHome + 50)
end
 
local function isInventoryFull()
    for i = 1, 16 do
        if turtle.getItemCount(i) == 0 then return false end
    end
    return true
end
 
-- Clear block ahead to prevent getting stuck
local function digForward()
    while turtle.detect() do
        turtle.dig()
        os.sleep(0.5)
    end
end
 
-- Move to absolute coordinate system safely
local function goTo(targetX, targetY, targetZ, targetDir)
    -- 1. Always clear height (Y) first to avoid obstacles
    while y < targetY do
        while turtle.detectUp() do turtle.digUp() end
        if turtle.up() then y = y + 1 end
    end
    while y > targetY do
        while turtle.detectDown() do turtle.digDown() end
        if turtle.down() then y = y - 1 end
    end
 
    -- 2. Move along X axis
    if x < targetX then
        face(1)
        while x < targetX do digForward() if turtle.forward() then x = x + 1 end end
    elseif x > targetX then
        face(3)
        while x > targetX do digForward() if turtle.forward() then x = x - 1 end end
    end
 
    -- 3. Move along Z axis
    if z < targetZ then
        face(0)
        while z < targetZ do digForward() if turtle.forward() then z = z + 1 end end
    elseif z > targetZ then
        face(2)
        while z > targetZ do digForward() if turtle.forward() then z = z - 1 end end
    end
 
    -- 4. Set final direction
    face(targetDir)
end
 
-- Return to absolute (0,0,0), dump, and return to exact coordinate
local function returnHomeAndResume()
    print("Returning home...")
    -- Remember current mining state
    local savedX, savedY, savedZ, savedDir = x, y, z, dir
    
    -- Go to absolute 0,0,0 (Home) and face original forward (0)
    -- This lands right back in front of your chests
    goTo(0, 0, 0, 0)
    
    -- Empty inventory into rear chest (turtle faces 0, chest is behind at 2)
    face(2)
    for i = 2, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.select(1)
    
    -- Pull fuel from bottom chest if needed
    if turtle.getFuelLevel() < MIN_FUEL then
        turtle.suckDown()
        turtle.refuel()
    end
    
    print("Resuming mining...")
    -- Fly directly back to the exact saved mining coordinates
    goTo(savedX, savedY, savedZ, savedDir)
end
 
local function moveForward()
    if not checkFuel() or isInventoryFull() then
        returnHomeAndResume()
    end
    
    digForward()
    
    if turtle.forward() then
        if dir == 0 then z = z + 1
        elseif dir == 1 then x = x + 1
        elseif dir == 2 then z = z - 1
        elseif dir == 3 then x = x - 1
        end
        return true
    end
    return false
end
 
-- Main Program Execution
 
-- 1. Initial 4 block home offset (Moves to absolute Z = 4)
print("Moving 4 blocks out...")
while z < 4 do
    moveForward()
end
 
-- Save the starting Z boundary for the mining area
local startZ = 4
 
-- 2. Mining Logic loop
for layer = 1, targetY do
    for row = 1, targetX do
        for col = 1, targetZ - 1 do
            moveForward()
        end
        
        -- Turn to next row logic
        if row < targetX then
            if row % 2 == 1 then
                if layer % 2 == 1 then turnRight() moveForward() turnRight()
                else turnLeft() moveForward() turnLeft() end
            else
                if layer % 2 == 1 then turnLeft() moveForward() turnLeft()
                else turnRight() moveForward() turnRight() end
            end
        end
    end
    
    -- Move down to next layer if not finished
    if layer < targetY then
        while turtle.detectDown() do turtle.digDown() end
        turtle.down()
        y = y - 1
        -- Turn around to reverse pattern for next layer efficiency
        turnRight()
        turnRight()
    end
end
 
-- Return Home permanently when complete
print("Mining complete! Returning home.")
goTo(0, 0, 0, 0)
print("Done!")
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎