Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local logFilePath = "mine.log"
- -- Function to log messages to a file
- function log(message)
- local file = fs.open(logFilePath, "a")
- file.writeLine(message)
- file.close()
- end
- -- Direction vectors: north (-Z), east (+X), south (+Z), west (-X)
- local dirs = {
- [0] = {x = 0, z = -1},
- [1] = {x = 1, z = 0},
- [2] = {x = 1, z = 0},
- [3] = {x = 0, z = 1},
- [4] = {x = -1, z = 0},
- }
- -- Initial state
- local pos = {x = 0, y = 0, z = 0}
- local heading = 0 -- 0=north, 1=east, 2=south, 3=west
- -- Helpers
- function turnLeft()
- turtle.turnLeft()
- heading = (heading - 1) % 4
- end
- function turnRight()
- turtle.turnRight()
- heading = (heading + 1) % 4
- end
- function moveForward()
- while not turtle.forward() do
- turtle.attack()
- digForward()
- sleep(0.5)
- end
- pos.x = pos.x + dirs[heading].x
- pos.z = pos.z + dirs[heading].z
- end
- function moveBackward()
- turtle.back()
- pos.x = pos.x - dirs[heading].x
- pos.z = pos.z - dirs[heading].z
- end
- function moveUp()
- while not turtle.up() do
- turtle.attackUp()
- digUp()
- sleep(0.5)
- end
- pos.y = pos.y + 1
- end
- function moveDown()
- while not turtle.down() do
- turtle.attackDown()
- digDown()
- sleep(0.5)
- end
- pos.y = pos.y - 1
- end
- function digForward()
- while turtle.detect() do
- turtle.dig()
- sleep(0.3)
- end
- end
- function digUp()
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.3)
- end
- end
- function digDown()
- while turtle.detectDown() do
- turtle.digDown()
- sleep(0.3)
- end
- end
- function isInventoryFull()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then return false end
- end
- return true
- end
- function refuelIfNeeded()
- for i = 1, 16 do
- turtle.select(i)
- if turtle.refuel(0) then
- turtle.refuel()
- log("Refueled from slot " .. i)
- end
- end
- turtle.select(1)
- end
- function dig()
- digForward()
- moveForward()
- digUp()
- digDown()
- if isInventoryFull() then
- placeChestAndUnload()
- end
- log("Dug block at position: (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
- end
- function placeChestAndUnload()
- turtle.select(1)
- if turtle.placeDown() then
- log("Placed chest below at position: (" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")")
- for i = 2, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.select(1)
- else
- log("Error: Unable to place chest!")
- print("Error: Unable to place chest!")
- error("Stopping script due to inability to place chest.")
- end
- end
- function digTunnel(x)
- for i = 1, x do
- dig()
- end
- refuelIfNeeded()
- end
- function goTo(x, y, z)
- while pos.y < y do moveUp() end
- while pos.y > y do moveDown() end
- local dx = x - pos.x
- if dx ~= 0 then
- faceDirection(dx > 0 and 1 or 3)
- for i = 1, math.abs(dx) do moveForward() end
- end
- local dz = z - pos.z
- if dz ~= 0 then
- faceDirection(dz > 0 and 2 or 0)
- for i = 1, math.abs(dz) do moveForward() end
- end
- end
- function faceDirection(dir)
- while heading ~= dir do
- turnRight()
- end
- end
- local args = { ... }
- function main()
- if #args < 3 then
- print("Usage: mine <tunnelLength> <numTunnels> <spacing>")
- return
- end
- local tunnelLength = tonumber(args[1])
- local numTunnels = tonumber(args[2])
- local spacing = tonumber(args[3])
- if not tunnelLength or not numTunnels or not spacing then
- print("Invalid input.")
- return
- end
- log("Starting mining operation with tunnel length: " .. tunnelLength .. ", number of tunnels: " .. numTunnels .. ", spacing: " .. spacing)
- -- digTunnel(3)
- for _ = 1, math.floor(numTunnels / 2) do
- digTunnel(1 + spacing)
- turnLeft()
- digTunnel(tunnelLength)
- turnRight()
- digTunnel(1 + spacing)
- turnRight()
- digTunnel(tunnelLength)
- turnLeft()
- end
- log("Mining complete.")
- print("Mining complete.")
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement