Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 11ifxreH quarry
- -- These variables are continually updated
- local xx, yy, zz = 0, 0, 0
- local xDir, zDir = 0, 1
- local blocksMined = 0
- -- These constants will never change once set
- local debugMode
- local enderChest
- local fuelLimit = turtle.getFuelLimit()
- local fuelValue
- local logInterval
- local quarryLeft
- local quarryUpwd
- local width, length
- local args = {...}
- local nArgs = #args
- local optns = 0
- -- Parsing program options
- if string.sub(args[1], 1, 1) == "-" then
- optns = 1
- local ops = string.lower(string.sub(args[1], 2))
- debugMode = string.match(ops, "d")
- enderChest = string.match(ops, "e")
- quarryLeft = string.match(ops, "l")
- quarryUpwd = string.match(ops, "u")
- end
- if debugMode then logInterval = 1
- else logInterval = 100 end
- -- Parsing program arguments
- width = tonumber(args[optns+1])
- if (nArgs - optns == 1) then
- length = width
- elseif (nArgs - optns == 2) then
- length = tonumber(args[optns+2])
- end
- -- Log message to the console, but only if debugMode is enabled
- function dLog(message) if debugMode then print(message) end end
- -- Increment and periodically log # of blocks mined
- function iBlock()
- dLog("iBlock() called")
- blocksMined = blocksMined + 1
- if math.fmod(blocksMined, logInterval) == 0 then
- print(blocksMined.." Blocks mined")
- end
- end
- -- Checks if there is any space left in the turtle's inventory
- function spaceLeft()
- dLog("spaceLeft() called")
- if enderChest then return turtle.getItemCount(15)==0
- else return turtle.getItemCount(16)==0 end
- end
- -- Checks if the turtle can safely return home after one more move
- function isFueled(over)
- local over = over or 0
- dLog("isFueled("..over..") called")
- local dist = math.abs(xx) + math.abs(yy) + math.abs(zz)
- return ( turtle.getFuelLevel() >= ( dist + over ) )
- end
- -- Turn left
- function left()
- dLog("left() called")
- local swap = xDir
- turtle.turnLeft()
- xDir = zDir
- zDir = -swap
- end
- -- Turn right
- function right()
- dLog("right() called")
- local swap = xDir
- turtle.turnRight()
- xDir = -zDir
- zDir = swap
- end
- function iLeft() if quarryLeft then right() else left() end end
- function iRight() if quarryLeft then left() else right() end end
- function oneEighty() left() left() end
- -- Move forward if possible and confirm success/failure
- function forward(dist)
- local dist = dist or 1
- local rev = (dist < 0)
- dLog("forward("..dist..") called")
- if rev then oneEighty() end
- for iter = 1, dist do
- while not turtle.forward() do
- if turtle.dig() then
- iBlock()
- elseif turtle.detect() then
- return false
- elseif turtle.attack() then
- turtle.suck()
- else return false end
- end
- xx = xx + xDir
- zz = zz + zDir
- end
- if rev then oneEighty() end
- return true
- end
- -- Move up if possible and confirm success/failure
- function up(dist)
- local dist = dist or 1
- local rev = (dist < 0)
- dLog("up("..dist..") called")
- if rev then return down(-dist) end
- for iter = 1, dist do
- while not turtle.up() do
- if turtle.digUp() then
- iBlock()
- elseif turtle.detectUp() then
- return false
- elseif turtle.attackUp() then
- turtle.suckUp()
- else return false end
- end
- yy = yy - 1
- end
- return true
- end
- -- Move down if possible and confirm success/failure
- function down(dist)
- local dist = dist or 1
- local rev = (dist < 0)
- dLog("down("..dist..") called")
- if rev then return up(-dist) end
- for iter = 1, dist do
- while not turtle.down() do
- if turtle.digDown() then
- iBlock()
- elseif turtle.detectDown() then
- return false
- elseif turtle.attackDown() then
- turtle.suckDown()
- else return false end
- end
- yy = yy + 1
- end
- return true
- end
- -- Goes to a position passed as coordinates and direction
- function goTo(xX, yY, zZ, xXDir, zZDir)
- dLog("goTo("..xX..", "..yY..", "..zZ..", "..xXDir..", "..zZDir..") called")
- local dist = math.abs(xX) + math.abs(yY) + math.abs(zZ)
- refuel((2 * dist) + 2)
- if (yY < yy) then up(2)
- elseif (yY > yy) then down(2) end
- if (xX > xx) then
- while (xDir ~= 1) do
- left()
- end
- elseif (xX < xx) then
- while (xDir ~= -1) do
- right()
- end
- end
- forward(math.abs(xX - xx))
- if (zZ > zz) then
- while (zDir ~= 1) do
- right()
- end
- elseif (zZ < zz) then
- while (zDir ~= -1) do
- left()
- end
- end
- forward(math.abs(zZ - zz))
- if (yY < yy) then up(yy - yY)
- elseif (yY > yy) then down(yY - yy) end
- while not ((xXDir == xDir) and (zZDir == zDir)) do
- right()
- end
- end
- -- Dig one block forward, as well as above and below
- function mineFwd()
- dLog("mineFwd() called")
- refuel()
- if not spaceLeft() then dropLoot() end
- while (not forward()) do
- if turtle.dig() then
- iBlock()
- elseif turtle.detect() then
- return false
- end
- if not spaceLeft() then dropLoot() end
- turtle.suck()
- end
- while turtle.digUp() do
- iBlock()
- sleep(0.05)
- end
- if not spaceLeft() then dropLoot() end
- if turtle.digDown() then iBlock() end
- turtle.suckUp()
- turtle.suckDown()
- return true
- end
- -- Refuels the turtle if necessary
- function refuel(over)
- local over = over or 5
- dLog("refuel("..over..") called")
- if not (fuelLimit == 0) then
- while not isFueled(over) do
- if enderChest then
- turtle.select(1)
- turtle.placeUp()
- if (fuelValue == nil) then
- fuel = turtle.getFuelLevel()
- turtle.suckUp(1)
- turtle.refuel(1)
- fuelValue = turtle.getFuelLevel() - fuel
- end
- local num = 64
- if ((fuelLimit / fuelValue) < num) then
- num = math.floor(fuelLimit / fuelValue)
- end
- turtle.suckUp(num)
- turtle.refuel(num)
- turtle.digUp()
- elseif turtle.refuel(0) then
- turtle.select(1)
- turtle.refuel(1)
- else
- local pos = {xx, yy, zz, xDir, zDir}
- goTo(0, 0, 0, 0, -1)
- dropLoot()
- print("Waiting for Fuel...")
- while not turtle.refuel(0) do
- if (turtle.getItemCount() ~= 0) then turtle.drop() end
- os.pullEvent("turtle_inventory")
- end
- print("Resuming mining")
- goTo(unpack(pos))
- end
- end
- end
- end
- -- Drops all loot in the output chest. If done is true the turtle will return to the position it was started from
- function dropLoot(done)
- local done = done or false
- dLog("dropLoot("..tostring(done)..") called")
- local pos = {0, 0, 0, 0, 1}
- if not done then pos = {xx, yy, zz, xDir, zDir} end
- if enderChest then
- turtle.select(16)
- turtle.placeUp()
- for slot = 2, 15 do
- if (turtle.getItemCount(slot) > 0) then
- turtle.select(slot)
- while not turtle.dropUp() do
- sleep(10)
- end
- end
- end
- turtle.select(16)
- turtle.digUp()
- else
- goTo(0, 0, 0, 0, -1)
- for slot = 2, 16 do
- if (turtle.getItemCount(slot) > 0) then
- turtle.select(slot)
- while not turtle.drop() do
- sleep(10)
- end
- end
- end
- end
- turtle.select(1)
- goTo(unpack(pos))
- end
- -- Digs a width * length sized hole and returns true when done
- function quarry(width, length)
- local alternate = 0
- local square = width == length
- local swapSides = width > length
- local iWidth, iLength = width, length
- if swapSides then
- iWidth, iLength = length, width
- iRight()
- quarryLeft = not quarryLeft
- end
- turtle.digUp()
- turtle.digDown()
- while true do
- for ii = 1, iWidth do
- for jj = 1, iLength-1 do
- if (not mineFwd()) then return true end
- end
- if ii < iWidth then
- if math.fmod(ii + alternate, 2) == 0 then
- iLeft()
- if (not mineFwd()) then return true end
- iLeft()
- else
- iRight()
- if (not mineFwd()) then return true end
- iRight()
- end
- end
- end
- if (iLength > 1) then oneEighty() end
- for n = 1, 3 do
- refuel()
- if quarryUpwd then
- if (not up()) and (n < 3) then return true end
- turtle.digUp()
- else
- if (not down()) and (n < 3) then return true end
- turtle.digDown()
- end
- end
- if math.fmod(iWidth, 2) == 0 then
- alternate = 1 - alternate
- end
- end
- end
- dropLoot(quarry(width, length))
Advertisement
Add Comment
Please, Sign In to add comment