Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- grid_mine.lua
- -- Mines a grid of parallel tunnels at y=0 until out of fuel.
- -- Configure the tunnel length and spacing below:
- local tunnelLength = 16 -- how long each tunnel runs before shifting over
- local spacing = 4 -- how many blocks between adjacent tunnels
- --------------------------------------------------------------------------------
- -- Helper: try to refuel if out of fuel; returns true if there is fuel available
- local function refuelIfNeeded()
- if turtle.getFuelLevel() > 0 then
- return true
- end
- -- try each slot for 1 item
- for slot = 1,16 do
- turtle.select(slot)
- if turtle.refuel(1) then
- print(("Refueled from slot %d → fuel=%d"):format(slot, turtle.getFuelLevel()))
- turtle.select(1)
- return true
- end
- end
- -- no fuel found
- print("❌ Out of fuel, stopping.")
- return false
- end
- -- ensure we start with slot 1 selected
- turtle.select(1)
- --------------------------------------------------------------------------------
- -- 1) Get starting GPS position
- local x,y,z = gps.locate()
- -- round to integer
- x = math.floor(x + 0.5)
- y = math.floor(y + 0.5)
- z = math.floor(z + 0.5)
- print(("Starting at GPS x=%d, y=%d, z=%d"):format(x,y,z))
- --------------------------------------------------------------------------------
- -- 2) Move down (digging) to y=0 if above it
- if y > 0 then
- print("Moving down to y=0 level...")
- while y > 0 do
- if not refuelIfNeeded() then return end
- if not turtle.down() then
- turtle.digDown()
- if not turtle.down() then
- error("Blocked below, cannot reach y=0")
- end
- end
- y = y - 1
- print(" now at y="..y)
- end
- elseif y < 0 then
- print("⚠️ Already below y=0 (y="..y.."), unexpected.")
- end
- --------------------------------------------------------------------------------
- -- 3) Mine an endless grid of tunnels until fuel runs out
- local direction = 1 -- 1 = moving “forward” along +X, -1 = moving backward
- while true do
- -- each tunnel segment
- for i = 1, tunnelLength do
- if not refuelIfNeeded() then return end
- if turtle.detect() then turtle.dig() end
- turtle.forward()
- end
- -- shift over by `spacing` blocks to start the next tunnel
- if direction == 1 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- for i = 1, spacing do
- if not refuelIfNeeded() then return end
- if turtle.detect() then turtle.dig() end
- turtle.forward()
- end
- if direction == 1 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- direction = -direction
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement