Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- -- Init variables
- local size = args[1] or 5
- local maxSteps = size * size
- local step = 1
- local dir = true -- True for away, false for towards starting position
- local bedrockDetected = false
- local pos = {
- x = 1,
- y = 1,
- z = 0,
- f = 0
- }
- local lastData = {
- x = 0,
- y = 0,
- z = 0,
- f = 0,
- step = 0,
- dir = false
- }
- -- States:
- -- 0 - Mining
- -- 1 - PutToChest
- local state = 0
- --Functions
- local function tryToRefuel( fuelNeeded )
- for i = 1, 16 do
- turtle.select(i)
- while turtle.refuel(0) and turtle.getFuelLevel() < fuelNeeded * 1.5 do
- turtle.refuel(1)
- end
- end
- turtle.select(1)
- end
- local function checkFuel()
- local fuelNeeded = math.abs(pos.z) * 2 + size * 4
- if turtle.getFuelLevel() < fuelNeeded then
- tryToRefuel(fuelNeeded)
- end
- end
- local function moveForward()
- checkFuel()
- if not bedrockDetected then
- local b, data = turtle.inspectDown()
- if b then
- if data.name == "Bedrock" then bedrockDetected = true end
- end
- end
- while turtle.detect() do turtle.dig() end
- turtle.forward()
- if pos.f == 0 then pos.x = pos.x + 1 end
- if pos.f == 2 then pos.x = pos.x - 1 end
- if pos.f == 1 then pos.y = pos.y + 1 end
- if pos.f == 3 then pos.y = pos.y - 1 end
- step = step + 1
- end
- local function moveUp()
- while turtle.detectUp() do turtle.digUp() end
- turtle.up()
- pos.z = pos.z + 1
- end
- local function moveDown()
- while turtle.detectDown() do turtle.digDown() end
- turtle.down()
- pos.z = pos.z - 1
- end
- local function turnLeft()
- turtle.turnLeft()
- pos.f = (pos.f - 1) % 4
- end
- local function turnRight()
- turtle.turnRight()
- pos.f = (pos.f + 1) % 4
- end
- local function checkInventory()
- local a = 0
- for i = 1, 16 do
- turtle.select(i)
- if turtle.getItemCount() > 0 then a = a + 1 end
- end
- if a == 16 then
- state = 1
- lastData.x = pos.x
- lastData.y = pos.y
- lastData.z = pos.z
- lastData.f = pos.f
- lastData.step = step
- lastData.dir = dir
- end
- end
- local function mine()
- if step == maxSteps then
- checkInventory()
- if bedrockDetected then
- state = 1
- end
- turnRight()
- turnRight()
- moveDown()
- step = 1
- dir = not dir
- end
- if dir then
- if pos.x == size and pos.f == 0 then
- turnRight()
- moveForward()
- turnRight()
- elseif pos.x == 1 and pos.f == 2 then
- turnLeft()
- moveForward()
- turnLeft()
- else
- moveForward()
- end
- else
- if pos.x == size and pos.f == 0 then
- turnLeft()
- moveForward()
- turnLeft()
- elseif pos.x == 1 and pos.f == 2 then
- turnRight()
- moveForward()
- turnRight()
- else
- moveForward()
- end
- end
- end
- local function goTo(x, y, z, f)
- if pos.z < z then
- while pos.z ~= z do moveUp() end
- elseif pos.z > z then
- while pos.z ~= z do moveDown() end
- end
- if pos.y < y then
- while pos.f ~= 1 do turnRight() end
- elseif pos.y > y then
- while pos.f ~= 3 do turnRight() end
- end
- while pos.y ~= y do moveForward() end
- if pos.x < x then
- while pos.f ~= 0 do turnRight() end
- elseif pos.x > x then
- while pos.f ~= 2 do turnRight() end
- end
- while pos.x ~= x do moveForward() end
- while pos.f ~= f do turnRight() end
- end
- local function dropAll()
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- -- Main Code
- tryToRefuel(10)
- print("Quarry size: "..size)
- -- Main Loop
- while true do
- if state == 0 then
- mine()
- elseif state == 1 then
- goTo(1, 1, 0, 2)
- if not bedrockDetected then
- tryToRefuel(math.abs(lastData.z) * 2 + size * 4)
- end
- dropAll()
- if bedrockDetected then break end
- goTo(lastData.x, lastData.y, lastData.z, lastData.f)
- step = lastData.step
- dir = lastData.dir
- state = 0
- end
- end
- print("Quarry completed!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement