local tArgs = { ... } if #tArgs < 1 or #tArgs > 3 then print("Usage: excavate [ []]") print("Put empty bucket in first slot, lava bucket in second.") return end local length = tonumber(tArgs[1]) local width = length local depth = 256 if #tArgs > 1 then depth = tonumber(tArgs[2]) end if #tArgs > 2 then width = tonumber(tArgs[3]) end if length < 1 or width < 1 or depth < 1 then print("Excavate parameters must be positive") return end -- Mine in a quarry pattern until we hit something we can't dig local collected = 0 local xPos,yPos,zPos = 0,0,0 local xDir,zDir = 0,1 local state = { DONE=0, FULL=1, EXCAVATING=2, ERROR=3 } local function collect() collected = collected + 1 if math.fmod(collected, 100) == 0 then print("Mined " .. collected .. " blocks.") end for n = 3, 16 do if turtle.getItemCount(n) == 0 then return true end end print("No empty slots left.") return false end local function tryForwards() if not turtle.detect() then turtle.select(1) if turtle.place() then if turtle.compareTo(2) then turtle.refuel() else turtle.place() end end else while turtle.dig() do if not collect() then return state.FULL end -- wait for sand/gravel sleep(yPos == 0 and 1 or 0.25) end end local tries = 0 while not turtle.forward() and tries < 10 do sleep(5) tries = tries + 1 end if tries >= 10 then return state.ERROR end xPos = xPos + xDir zPos = zPos + zDir return state.EXCAVATING end local function tryDown() if not turtle.detectDown() then turtle.select(1) if turtle.placeDown() then if turtle.compareTo(2) then turtle.refuel() else turtle.placeDown() end end else while turtle.digDown() do if not collect() then return state.FULL end -- wait for sand/gravel -- sleep(0.5) end end local tries = 0 while not turtle.down() and tries < 10 do sleep(5) tries = tries + 1 end if tries >= 10 then return state.ERROR end yPos = yPos + 1 if math.fmod(yPos, 10) == 0 then print("Descended " .. yPos .. " metres.") end return state.EXCAVATING end local function tryUp() if not turtle.detectUp() then turtle.select(1) if turtle.placeUp() then if turtle.compareTo(2) then turtle.refuel() else turtle.placeUp() end end else while turtle.digUp() do if not collect() then return state.FULL end -- wait for sand/gravel sleep(1) end end local tries = 0 while not turtle.up() and tries < 10 do sleep(5) tries = tries + 1 end if tries >= 10 then return state.ERROR end yPos = yPos - 1 return state.EXCAVATING end local function turnLeft() turtle.turnLeft() xDir, zDir = -zDir, xDir end local function turnRight() turtle.turnRight() xDir, zDir = zDir, -xDir end local function moveTo(_x, _y, _z, _xD, _zD) -- if going towards 0,0,0 fix in y, x, z order -- else fix in z, x, y order while _y < yPos do tryUp() end while _x < xPos do while xDir ~= -1 do turnRight() end tryForwards() end while _z < zPos do while zDir ~= -1 do turnRight() end tryForwards() end while _z > zPos do while zDir ~= 1 do turnRight() end tryForwards() end while _x > xPos do while xDir ~= 1 do turnRight() end tryForwards() end while _y > yPos do tryDown() end while xDir ~= _xD or zDir ~= _zD do turnRight() end end local function dropAll() print("Turtle full, dropping things") local xB, yB, zB, xDB, zDB = xPos, yPos, zPos, xDir, zDir moveTo(0, 0, 0, 0, 1) print("Dropping everything") for i=3,16 do turtle.select(i) turtle.dropUp() end moveTo(xB, yB, zB, xDB, zDB) end -- get going... print("Excavating a " .. length .. "x" .. width .. " hole, " .. depth .. " deep.") length = length - 1 width = width - 1 depth = depth - 1 local xInc = true while not done do -- print("x" .. xPos .. " y" .. yPos .. " z" .. zPos .. " xd" .. xDir .. " zd" .. zDir) local down = false if zPos == length then if xInc and xPos == width and xDir == 0 then down = true elseif not xInc and xPos == 0 and xDir == 0 then down = true elseif xInc and (zDir == 1 or xDir == 1) then turnRight() elseif not xInc and (zDir == 1 or xDir == -1) then turnLeft() end elseif zPos == 0 then if xInc and xPos == width and xDir == 0 then down = true elseif not xInc and xPos == 0 and xDir == 0 then down = true elseif xInc and (zDir == -1 or xDir == 1) then turnLeft() elseif not xInc and (zDir == -1 or xDir == -1) then turnRight() end end local r if down then if yPos == depth then print("Excavation done.") break end turnLeft() turnLeft() r = tryDown() xInc = not xInc else r = tryForwards() end if r == state.ERROR then print("Something in the way") break elseif r == state.FULL then dropAll() end end -- done moveTo(0, 0, 0, 0, 1) dropAll() print("Mined " .. collected .. " blocks total.")