Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h,d,oy= ...
- -- n -> 0, w -> 1, s -> 2, e -> 3
- local dir = 0
- local x,y,z = 0, 0, 0
- local function oppositeDir(d)
- if d == 0 then return 2
- elseif d == 1 then return 3
- elseif d == 2 then return 0
- elseif d == 3 then return 1
- else return d end
- end
- local function tryDig()
- while turtle.detect() do
- if not turtle.dig() then return false end
- end
- return true
- end
- local function tryDigDown()
- while turtle.detectDown() do
- if not turtle.digDown() then return false end
- end
- return true
- end
- local function tryDigUp()
- while turtle.detectUp() do
- if not turtle.digUp() then return false end
- end
- return true
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return
- end
- local function tryRefuel()
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(n) then
- turtle.select(n)
- return true
- end
- end
- end
- turtle.select(1)
- return false
- end
- if not tryRefuel() then
- print("Out of fuel. Please add more fuel!")
- while not tryRefuel() do
- os.pullEvent("turtle_inventory")
- end
- print("Resuming...")
- end
- end
- local function move()
- refuel()
- while not turtle.forward() do
- if turtle.detect() then
- if not tryDig() then return false end
- elseif turtle.attack() then
- print("Something in the way. Attacking...")
- else sleep(0.5) end
- end
- if dir == 0 then z = z + 1
- elseif dir == 1 then x = x - 1
- elseif dir == 2 then z = z - 1
- elseif dir == 3 then x = x + 1
- else return false end
- return true
- end
- local function moveUp()
- refuel()
- while not turtle.up() do
- if turtle.detectUp() then
- if not tryDigUp() then return false end
- elseif turtle.attackUp() then
- print("Something in the way. Attacking...")
- else sleep(0.5) end
- end
- y = y + 1
- return true
- end
- local function moveDown()
- refuel()
- while not turtle.down() do
- if turtle.detectDown() then
- if not tryDigDown() then return false end
- elseif turtle.attackDown() then
- print("Something in the way. Attacking...")
- else sleep(0.5) end
- end
- y = y - 1
- return true
- end
- local function turn(d)
- if dir == d then return end
- local dx = dir - d
- while dx ~= 0 do
- if dx < 0 then
- if dx == -3 then
- turtle.turnRight()
- dx = 0
- else
- turtle.turnLeft()
- dx = dx + 1
- end
- else
- if dx == 3 then
- turtle.turnLeft()
- dx = 0
- else
- turtle.turnRight()
- dx = dx - 1
- end
- end
- end
- dir = d
- end
- local function navigateTo(ax, ay, az)
- while y ~= ay do
- if y > ay then
- moveDown()
- else
- moveUp()
- end
- end
- while x ~= ax do
- if x > ax then
- turn(1)
- move()
- else
- turn(3)
- move()
- end
- end
- while z ~= az do
- if z > az then
- turn(2)
- move()
- else
- turn(0)
- move()
- end
- end
- end
- local function emptyInv()
- local lx, ly, lz, ld = x, y, z, dir
- local hw = math.floor(w/2)
- print("x: %d, y:%d, z:%d", x, y, z)
- navigateTo(hw, 0, -1)
- for n=2,16 do
- turtle.select(n)
- turtle.dropDown()
- end
- turtle.select(1)
- turn(0)
- move()
- navigateTo(lx, ly, lz)
- turn(ld)
- end
- local function invHasSpace()
- return turtle.getItemCount(16) == 0
- end
- if w == nil or h == nil or d == nil then
- print("Usage: quarry <width> <height> <depth>")
- return
- end
- if oy == nil then oy = 0 end
- w = tonumber(w)
- h = tonumber(h)
- d = tonumber(d)
- oy = tonumber(oy)
- assert(w ~= nil, "Width must be a number")
- assert(h ~= nil, "Height must be a number")
- assert(d ~= nil, "Depth must be a number")
- assert(oy ~= nil, "Y-offset must be a number")
- if w <= 0 or h <= 0 or d <= 0 then
- print("All dimension values must be positive!")
- return
- end
- w = math.floor(w)
- h = math.floor(h)
- d = math.floor(d)
- oy = math.floor(oy)
- print(string.format("Starting to mine in %dx%dx%d area. With Y-offset: %d", w, h, d, oy))
- local hw = math.floor(w/2)
- turn(1)
- while hw > 0 do
- tryDig()
- turtle.forward()
- hw = hw - 1
- end
- turn(0)
- local n = true
- local wdir = 1
- local hdir = 1
- local wi,hi,di = 0, 1, 0
- local wk = w
- local hk = h
- while di ~= d do
- while wi ~= wk do
- while hi ~= hk do
- if not invHasSpace() then
- emptyInv()
- end
- tryDig()
- move()
- hi = hi + hdir
- end
- if math.fmod(wi + wdir, w) ~= 0 then
- if n then
- turn(3)
- else
- turn(1)
- end
- tryDig()
- move()
- end
- if hk == h then
- hk = 1
- turn(2)
- else
- hk = h
- turn(0)
- end
- hdir = -hdir
- wi = wi + wdir
- end
- if wk == w then
- wk = 0
- else
- wk = w
- end
- wdir = -wdir
- di = di + 1
- n = not n
- if di ~= d then
- tryDigDown()
- moveDown()
- end
- end
- print("Task finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement