Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mining starts from lower right corner of quarry heading forward
- -- Turtle facing rotations
- -- 0 ^
- -- 1 3 |
- -- 2 <---+
- x = 1 -- Turtle position X, Y, Z
- y = 1
- z = 1
- rot = 0 -- 0:+Z, 1:+X, 2:-Z, 3: -X
- ix = 1 -- Increase X
- ty = 1 -- Target Y
- bedrock = false
- resuming = false
- function writeStatus()
- local f = fs.open("_status", "wb")
- f.write(1) -- Orequarry ID
- f.write(x)
- f.write(y)
- f.write(z)
- f.write(rot)
- f.write(ix+1) -- Byte is limited to 0, so -1 would loop over to 255
- f.write(ty)
- f.close()
- end
- function quarry(sx, sy, sz)
- local fuelDelay = 260
- local oldix = ix
- if (sx < 1) or (sy < 1) or (sz < 1) then
- return
- end
- fuel(1000, 10000)
- empty()
- if resuming then
- local f = fs.open("_status", "rb")
- f.read() -- Orequarry ID
- x = f.read()
- y = f.read()
- z = f.read()
- rot = f.read()
- ix = f.read()-1
- ty = f.read()
- f.close()
- print(string.format("Status: %d %d %d %d %d", x, y, z, rot, ix))
- else
- if sx*sy*sz == 1 then return
- elseif sy > 2 then ty = 2
- end
- if sx > 255 then sx = 255 end
- if sy > 255 then sy = 255 end
- if sz > 255 then sz = 255 end
- local f = fs.open("_area", "wb")
- f.write(sx)
- f.write(sy)
- f.write(sz)
- f.close()
- end
- -- Start quarry loop
- sdelay = 0
- while true do
- if sdelay == 0 then sleep(1) end
- sdelay = (sdelay+1) % 10
- oldix = ix
- if y < ty then
- turtle.digDown()
- if turtle.down() then
- y = y+1 writeStatus()
- empty()
- else
- bedrock = true
- ty = y
- end
- elseif rot == 0 then -- Going forward
- if z < sz then
- if dig(sy) then
- z = z+1 writeStatus()
- end
- else -- Reached edge
- if ix > 0 then turnTo(1)
- else turnTo(3)
- end
- end
- elseif rot == 2 then -- Going backwards
- if z > 1 then
- if dig(sy) then
- z = z-1 writeStatus()
- end
- else -- Reached edge
- if ix > 0 then turnTo(1)
- else turnTo(3)
- end
- end
- elseif rot == 1 then -- Going left
- if x < sx then
- if dig(sy) then
- x = x+1 writeStatus()
- end
- else
- ty = math.min(y+3, sy)
- ix = -ix writeStatus()
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- if (math.abs(ty-y)<1.5) or bedrock then break end
- end
- if z < 2 then turnTo(0)
- else turnTo(2)
- end
- elseif rot == 3 then -- Going right
- if x > 1 then
- if dig(sy) then
- x = x-1 writeStatus()
- end
- else
- ty = math.min(y+3, sy)
- ix = -ix writeStatus()
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- if (math.abs(ty-y)<1.5) or bedrock then break end
- end
- if z < 2 then turnTo(0)
- else turnTo(2)
- end
- end
- fuelDelay = fuelDelay-1
- if fuelDelay < 0 then
- fuelDelay = 260
- if turtle.getFuelLevel() < 600 then
- fuel(10000, 10000)
- end
- end
- end
- fs.delete("_status")
- fs.delete("_area")
- -- Return to surface
- -- Come up half way
- while y > 1+sy/2 do
- y = y-1
- turtle.digUp() turtle.up()
- end
- -- Come to X = 1
- -- Turn to left
- turnTo(3, true) -- Turn but don't save
- while x > 1 do
- x = x-1
- turtle.dig() turtle.forward()
- end
- -- Come to Z = 1
- -- Turn to backwards
- rot = 2
- turtle.turnRight()
- while z > 1 do
- z = z-1
- turtle.dig() turtle.forward()
- end
- -- Come up all the way
- while y > 1 do
- y = y-1
- turtle.digUp() turtle.up()
- end
- -- Finally empty items to chest
- doEmpty()
- print("Mining trip finished!")
- end
- function dig(sy)
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- if (y < sy) and (not bedrock) and turtle.detectDown() then
- if not turtle.digDown() then
- bedrock = true
- end
- end
- while turtle.detect() do
- if not turtle.dig() then
- repeat
- empty()
- until not turtle.suck()
- break
- end
- empty()
- end
- return turtle.forward()
- end
- function doEmpty()
- print("Emptying inventory...")
- while turtle.detectUp() do
- if not turtle.digUp() then
- break
- end
- end
- turtle.select(16) -- Select ender chest for placing
- if not turtle.placeUp() then
- print("Error placing ender chest")
- return
- end
- for i=1,14 do
- turtle.select(i)
- turtle.dropUp()
- end
- turtle.select(16)
- turtle.digUp()
- sleep(1)
- print("Emptying finished")
- end
- function empty()
- if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
- doEmpty()
- end
- end
- function fuel(minfuel, maxfuel)
- empty() -- Make sure slot 14 is empty
- write("Checking fuel...")
- if turtle.getFuelLevel() > minfuel then
- print("Fuel tank already full")
- return
- end
- while turtle.detectUp() do
- if not turtle.digUp() then break end
- end
- local chest = false
- turtle.select(14)
- while turtle.getFuelLevel() < maxfuel do
- turtle.refuel()
- if not chest then
- turtle.select(15)
- if not turtle.placeUp() then
- print("Error placing ender chest")
- break
- end
- chest = true
- turtle.select(14)
- end
- turtle.suckUp()
- print("Fuel "..turtle.getFuelLevel())
- end
- if chest then
- turtle.select(15)
- turtle.digUp()
- end
- print("Fuel tank filled")
- end
- function turnTo(newRot, nosave)
- function capRot(r)
- if r < -0.5 then r = r+4
- elseif r > 3.5 then r = r-4
- end
- return r
- end
- if rot ~= newRot then
- if capRot(rot-1) == newRot then
- turtle.turnRight()
- elseif capRot(rot+1) == newRot then
- turtle.turnLeft()
- else
- turtle.turnRight() turtle.turnRight()
- end
- rot = newRot
- if nosave == nil then writeStatus() end
- end
- end
- -- Main program begins
- param = {...}
- if #param == 1 then
- fs.delete("_status")
- fs.delete("_area")
- end
- if fs.exists("_status") and fs.exists("_area") then
- local f = fs.open("_status", "rb")
- m = f.read()
- f.close()
- if m == 1 then
- resuming = true
- local f = fs.open("_area", "rb")
- local sx = f.read()
- local sy = f.read()
- local sz = f.read()
- f.close()
- print(string.format("Resuming quarry (%d x %d x %d)", sx, sy, sz))
- quarry(sx, sy, sz)
- fs.delete("_status")
- else
- print("Resume is already in use for other program")
- end
- else
- if turtle.getItemCount(16) == 0 then
- print("Missing enderchest (slot 16) for emptying")
- print("Continue anyway? [y/n] ")
- if read() ~= "y" then return end
- end
- if turtle.getItemCount(15) == 0 then
- print("Missing enderchest (slot 15) for fuel")
- print("Continue anyway? [y/n] ")
- if read() ~= "y" then return end
- end
- if #param == 3 then
- quarry(0+param[1], 0+param[2], 0+param[3])
- elseif #param == 2 then
- quarry(0+param[1], 255, 0+param[2])
- else
- print("Usage: quarry [width] ([height] optional) [depth]")
- print("Example: quarry 30 99 30")
- print(" or: quarry 32 64")
- print("Meeting bedrock will stop the turtle.")
- print("Turtle will advance forward and left")
- end
- fs.delete("_status")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement