Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Mining Script
- -- Ensure your turtle has a pickaxe and enough fuel.
- -- Variables
- local targetOre
- local requiredAmount = 10 -- Amount of ore to mine
- local homePosition = {x = 0, y = 0, z = 0} -- Starting coordinates
- local chunkBoundary = {xMin = 0, xMax = 15, zMin = 0, zMax = 15} -- Current chunk boundaries
- local minedAmount = 0
- local position = {x = 0, y = 0, z = 0} -- Turtle's current position
- local direction = 0 -- 0: forward, 1: right, 2: back, 3: left
- local spiralStep = 1
- local spiralDirection = 0
- -- Helper Functions
- local function turnRight()
- turtle.turnRight()
- direction = (direction + 1) % 4
- end
- local function turnLeft()
- turtle.turnLeft()
- direction = (direction - 1) % 4
- end
- local function faceDirection(targetDirection)
- while direction ~= targetDirection do
- turnRight()
- end
- end
- local function moveForward()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- return false
- end
- end
- if direction == 0 then position.z = position.z + 1
- elseif direction == 1 then position.x = position.x + 1
- elseif direction == 2 then position.z = position.z - 1
- elseif direction == 3 then position.x = position.x - 1 end
- return true
- end
- local function moveUp()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- return false
- end
- end
- position.y = position.y + 1
- return true
- end
- local function moveDown()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- return false
- end
- end
- position.y = position.y - 1
- return true
- end
- local function detectOre()
- local success, data = turtle.inspect()
- if success then
- if data.name == targetOre then
- return true
- end
- end
- return false
- end
- local function mineOre()
- if turtle.dig() then
- minedAmount = minedAmount + 1
- end
- end
- local function isWithinChunkBounds()
- return position.x >= chunkBoundary.xMin and position.x <= chunkBoundary.xMax and
- position.z >= chunkBoundary.zMin and position.z <= chunkBoundary.zMax
- end
- local function returnHome()
- -- Move back to the original Y position
- while position.y > homePosition.y do
- moveDown()
- end
- while position.y < homePosition.y do
- moveUp()
- end
- -- Move back to the original Z position
- if position.z > homePosition.z then
- faceDirection(2) -- Face back
- while position.z > homePosition.z do
- moveForward()
- end
- elseif position.z < homePosition.z then
- faceDirection(0) -- Face forward
- while position.z < homePosition.z do
- moveForward()
- end
- end
- -- Move back to the original X position
- if position.x > homePosition.x then
- faceDirection(3) -- Face left
- while position.x > homePosition.x do
- moveForward()
- end
- elseif position.x < homePosition.x then
- faceDirection(1) -- Face right
- while position.x < homePosition.x do
- moveForward()
- end
- end
- end
- local function depositOre()
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1) -- Reset to slot 1
- end
- local function spiralMove()
- if spiralDirection % 2 == 0 then
- for _ = 1, spiralStep do
- if not moveForward() then
- return false
- end
- end
- else
- for _ = 1, spiralStep do
- if not moveForward() then
- return false
- end
- end
- spiralStep = spiralStep + 1
- end
- turnRight()
- spiralDirection = (spiralDirection + 1) % 4
- return true
- end
- -- Setup
- print("Enter the ore to mine (e.g., minecraft:iron_ore):")
- targetOre = read()
- print("Enter the turtle's starting X coordinate:")
- homePosition.x = tonumber(read())
- print("Enter the turtle's starting Y coordinate:")
- homePosition.y = tonumber(read())
- print("Enter the turtle's starting Z coordinate:")
- homePosition.z = tonumber(read())
- -- Calculate chunk boundaries based on home position
- chunkBoundary.xMin = homePosition.x - (homePosition.x % 16)
- chunkBoundary.xMax = chunkBoundary.xMin + 15
- chunkBoundary.zMin = homePosition.z - (homePosition.z % 16)
- chunkBoundary.zMax = chunkBoundary.zMin + 15
- position.x, position.y, position.z = homePosition.x, homePosition.y, homePosition.z
- -- Main Loop
- while minedAmount < requiredAmount do
- if not isWithinChunkBounds() then
- print("Out of chunk bounds, returning home.")
- break
- end
- if detectOre() then
- mineOre()
- else
- if not spiralMove() then
- print("Obstacle encountered, returning home.")
- break
- end
- end
- end
- -- Return and Deposit
- returnHome()
- depositOre()
- print("Mining complete! Ores deposited.")
Advertisement
Add Comment
Please, Sign In to add comment