Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to check if the desired peripheral is equipped
- local function isPeripheralEquipped(peripheralType)
- local equipped = peripheral.getType("left")
- return equipped == peripheralType
- end
- -- Function to swap peripheral with the one in slot 16
- local function swapPeripheral()
- turtle.select(16)
- if not turtle.equipLeft() then
- print("Failed to swap peripheral.")
- else
- print("Peripheral swapped.")
- end
- end
- -- Function to mine in front if needed
- local function mineIfNeeded()
- if turtle.detect() then
- turtle.dig()
- end
- end
- -- Function to mine above if needed
- local function mineUpIfNeeded()
- if turtle.detectUp() then
- turtle.digUp()
- end
- end
- -- Function to mine below if needed
- local function mineDownIfNeeded()
- if turtle.detectDown() then
- turtle.digDown()
- end
- end
- -- Function to check and mine ores around the turtle without moving
- local function checkAndMineAround()
- mineIfNeeded() -- Front
- turtle.turnRight()
- mineIfNeeded() -- Right
- turtle.turnRight()
- mineIfNeeded() -- Back
- turtle.turnRight()
- mineIfNeeded() -- Left
- turtle.turnLeft() -- Reset to original orientation
- mineUpIfNeeded() -- Up
- mineDownIfNeeded() -- Down
- turtle.turnLeft() -- Reset to original orientation
- turtle.turnLeft() -- Reset to original orientation
- end
- -- Function to move to a specific coordinate
- local function moveTo(x, y, z)
- -- Move in x direction
- for i = 1, math.abs(x) do
- mineIfNeeded()
- if x < 0 then
- turtle.forward()
- else
- turtle.back()
- end
- end
- -- Move in y direction
- for i = 1, math.abs(y) do
- if y > 0 then
- turtle.up()
- else
- mineDownIfNeeded()
- turtle.down()
- end
- end
- -- Move in z direction
- if z ~= 0 then
- local turnFunc = z < 0 and turtle.turnRight or turtle.turnLeft
- turnFunc()
- for i = 1, math.abs(z) do
- mineIfNeeded()
- turtle.forward()
- end
- -- Return to the original facing direction
- if z < 0 then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- end
- -- Function to check and refuel the turtle
- local function refuelIfNeeded()
- local fuelLevel = turtle.getFuelLevel()
- local fuelThreshold = 1000 -- Set your desired fuel threshold
- if fuelLevel < fuelThreshold then
- turtle.select(13) -- Select the Ender Chest with fuel in slot 13
- turtle.place() -- Place the Ender Chest
- -- Assuming fuel is stored in the Ender Chest at a known slot (e.g., slot 12)
- turtle.select(12) -- Select the slot with fuel
- turtle.suck() -- Take some fuel
- turtle.refuel() -- Refuel with the taken fuel
- turtle.select(13) -- Re-select the Ender Chest slot
- turtle.dig() -- Pick up the Ender Chest
- end
- end
- -- Function to empty inventory into Ender Chest
- local function emptyInventoryIfNeeded()
- if turtle.getItemCount(10) > 0 then -- Check if slot 10 has items
- turtle.select(14) -- Select the Ender Chest for items in slot 14
- turtle.place() -- Place the Ender Chest
- for i = 1, 10 do -- Loop through slots 1 to 10 to empty them
- turtle.select(i)
- turtle.drop() -- Drop items into the Ender Chest
- end
- turtle.select(14) -- Re-select the Ender Chest slot
- turtle.dig() -- Pick up the Ender Chest
- end
- end
- -- Main Script
- while true do
- local radius = 8 -- Scan radius set to 5
- -- Equip geoScanner if not already equipped
- if not isPeripheralEquipped("geoScanner") then
- swapPeripheral()
- end
- -- Scan for ores
- local geoScanner = peripheral.find("geoScanner")
- if not geoScanner then
- error("Geo Scanner not found")
- end
- local data, reason = geoScanner.scan(radius)
- if not data then
- print("Scan failed: " .. reason)
- return
- end
- print("Scan successful. Searching for ores...")
- for _, block in ipairs(data) do
- if string.match(block.name, "_ore") then
- local x, y, z = block.x, block.y, block.z
- print("Ore found at: x=" .. x .. ", y=" .. y .. ", z=" .. z)
- -- Equip pickaxe if not already equipped
- if not isPeripheralEquipped("pickaxe") then
- swapPeripheral()
- end
- -- Move to the ore and mine it
- moveTo(x, y, z)
- print("Mining ore: " .. block.name)
- mineIfNeeded()
- -- Check and mine ores around the current position
- checkAndMineAround()
- refuelIfNeeded()
- emptyInventoryIfNeeded()
- break -- Stop after handling the first ore
- end
- end
- os.sleep(5)
- end
Add Comment
Please, Sign In to add comment