Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Island Miner v0.2.0
- -- A turtle program that mines gravel and andesite around an island
- -- This program requires a turtle with a pickaxe
- -- Constants
- local VERSION = "0.2.0"
- local STARTING_POSITION = {x = -279, y = 64, z = -182}
- local MAX_Y_LEVEL = 70
- local REFUEL_THRESHOLD = 50 -- Refuel when fuel level drops below this value
- local TARGET_BLOCKS = {
- ["minecraft:gravel"] = true,
- ["minecraft:andesite"] = true
- }
- -- Debug logging function
- local function log(message)
- print("[DEBUG] " .. message)
- end
- -- Current position and orientation
- local position = {
- x = STARTING_POSITION.x,
- y = STARTING_POSITION.y,
- z = STARTING_POSITION.z
- }
- local facing = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
- -- Function to check if a block is a target block
- local function isTargetBlock()
- local success, data = turtle.inspect()
- if success then
- -- Log the block type we found
- log("Found block: " .. data.name)
- -- Check if it's in our target list
- local isTarget = TARGET_BLOCKS[data.name] == true
- if not isTarget then
- log("Block is not a target block, skipping")
- end
- return isTarget
- end
- return false
- end
- -- Function to check if a block is exposed to air
- local function isExposedToAir()
- -- Check if we can move up first before trying to move up
- if turtle.detectUp() then
- log("Block above is not air, can't check if target is exposed to air")
- return false
- end
- -- Move up to check if the block above is air
- turtle.up()
- position.y = position.y + 1
- -- Look down to check the block below
- turtle.turnLeft()
- turtle.turnLeft()
- facing = (facing + 2) % 4
- local success, data = turtle.inspect()
- local isAir = not success
- if success then
- isAir = data.name == "minecraft:air"
- log("Block above target is: " .. data.name .. ", isAir: " .. tostring(isAir))
- else
- log("No block detected above target, treating as air")
- end
- -- Return to original position and orientation
- turtle.turnLeft()
- turtle.turnLeft()
- facing = (facing + 2) % 4
- turtle.down()
- position.y = position.y - 1
- return isAir
- end
- -- Function to refuel if needed
- local function refuelIfNeeded()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel < REFUEL_THRESHOLD then
- print("Fuel low (" .. fuelLevel .. "), attempting to refuel...")
- -- Try each inventory slot for fuel
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- local before = turtle.getFuelLevel()
- turtle.refuel(1)
- local after = turtle.getFuelLevel()
- print("Refueled with " .. (after - before) .. " fuel units")
- if turtle.getFuelLevel() >= REFUEL_THRESHOLD then
- break
- end
- end
- end
- -- Check if refueling was successful
- if turtle.getFuelLevel() < REFUEL_THRESHOLD then
- print("WARNING: Unable to refuel to threshold. Current fuel: " .. turtle.getFuelLevel())
- end
- end
- end
- -- Movement functions that update position
- local function forward()
- refuelIfNeeded()
- if turtle.forward() then
- if facing == 0 then position.z = position.z - 1 -- North
- elseif facing == 1 then position.x = position.x + 1 -- East
- elseif facing == 2 then position.z = position.z + 1 -- South
- elseif facing == 3 then position.x = position.x - 1 -- West
- end
- return true
- end
- return false
- end
- local function up()
- refuelIfNeeded()
- if position.y < MAX_Y_LEVEL and turtle.up() then
- position.y = position.y + 1
- return true
- end
- return false
- end
- local function down()
- refuelIfNeeded()
- if turtle.down() then
- position.y = position.y - 1
- return true
- end
- return false
- end
- local function turnLeft()
- turtle.turnLeft()
- facing = (facing - 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- facing = (facing + 1) % 4
- end
- -- Function to check and mine target blocks
- local function checkAndMine()
- -- Only check if it's exposed to air if it's a target block
- if isTargetBlock() then
- if isExposedToAir() then
- print("Mining " .. position.x .. "," .. position.y .. "," .. position.z)
- turtle.dig()
- return true
- else
- log("Block not exposed to air, skipping")
- end
- end
- return false
- end
- -- Spiral search pattern to find and mine blocks
- local function spiralSearch(maxRadius)
- local radius = 1
- local direction = 0 -- 0 = north, 1 = east, 2 = south, 3 = west
- local steps = 1
- while radius <= maxRadius do
- for i = 1, 2 do
- for j = 1, steps do
- checkAndMine()
- -- Check blocks in other directions
- turnLeft()
- checkAndMine()
- turnRight()
- turnRight()
- checkAndMine()
- turnLeft()
- -- Move forward, but try not to dig if it's not a target block
- if not forward() then
- -- Check if it's a target block before digging
- if isTargetBlock() then
- log("Target block in path, digging to move forward")
- turtle.dig()
- else
- -- Try to go around the obstacle
- log("Non-target block in path, trying to go around")
- -- Try going up and over
- if position.y < MAX_Y_LEVEL - 1 and up() then
- forward()
- down()
- else
- -- If we can't go over, try going around
- turnLeft()
- if forward() then
- turnRight()
- forward()
- turnRight()
- forward()
- turnLeft()
- else
- turnRight()
- turnRight()
- if forward() then
- turnLeft()
- forward()
- turnLeft()
- forward()
- turnRight()
- else
- -- If we can't go around, just dig through as a last resort
- turnRight()
- log("Unable to navigate around obstacle, digging through")
- turtle.dig()
- if not forward() then
- print("Path completely blocked at " .. position.x .. "," .. position.y .. "," .. position.z)
- end
- end
- end
- end
- end
- end
- end
- -- Change direction
- turnLeft()
- direction = (direction + 1) % 4
- end
- steps = steps + 1
- radius = radius + 1
- end
- end
- -- Main function to search at different y levels
- local function searchArea()
- print("Island Miner v" .. VERSION)
- print("Starting at position: " .. position.x .. "," .. position.y .. "," .. position.z)
- -- Start at the initial position
- for y = position.y, MAX_Y_LEVEL - 1 do
- print("Searching at y level: " .. position.y)
- spiralSearch(20) -- Search in a spiral pattern with max radius of 20 blocks
- -- Move up to the next level if not at max height
- if position.y < MAX_Y_LEVEL - 1 then
- up()
- end
- end
- -- Return to starting position
- print("Mining complete, returning to start position")
- -- Navigate back to starting coordinates
- while position.y > STARTING_POSITION.y do
- down()
- end
- print("Mining operation completed")
- end
- -- Run the program
- searchArea()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement