Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local extargs = false
- local depth = 0
- local xPos,zPos = 0,0
- local xDir,zDir = 0,1
- local tArgs = { ... }
- if #tArgs == 1 then
- local size = tonumber( tArgs[1] )
- elseif #tArgs == 6 then
- local size = tonumber( tArgs[1] )
- extargs = true
- xPos = tonumber( tArgs[2] )
- depth = tonumber( tArgs[3] )
- zPos = tonumber( tArgs[4] )
- xDir = tonumber( tArgs[5] )
- zDir = tonumber( tArgs[6] )
- else
- if fs.exists("lastPos") then
- else
- print( "Usage: excavate <diameter> <posX> <posY> <posZ> <xDir> <zDir>" )
- return
- end
- end
- term.clear()
- term.setCursorPos(1,2)
- -- 123456789 123456789 123456789 123456789
- print(" ============ Excavating... ==========")-- 2
- print(" == ==")-- 3
- print(" == Items held: 0 ==")-- 4
- print(" == Items Unloaded: 0 ==")-- 5
- print(" == Fuel Remaining: 0 ==")-- 6
- print(" == Times Unloaded: 0 ==")-- 7
- print(" == ==")-- 8
- print(" == Current status: ==")-- 9
- print(" == Starting... ==")--10
- print(" == ==")--11
- print(" =====================================")--12
- -- Mine in a quarry pattern until we hit something we can't dig
- if size < 1 then
- print( "Excavate diameter must be positive" )
- return
- end
- local unloads = 0
- local unloaded = 0
- local collected = 0
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- local function update()
- term.setCursorPos(1,4)
- print(" == Items held: "..collected.." ")-- 4
- print(" == Items Unloaded: "..unloaded)-- 5
- print(" == Fuel Remaining: "..turtle.getFuelLevel().." ")-- 6
- print(" == Times Unloaded: "..unloads)-- 7
- end
- local function unload()
- unloads = unloads + 1
- update()
- term.setCursorPos(5, 10)
- --print("=======================================")
- print("Unloading items... ")
- for n=1,16 do
- unloaded = unloaded + turtle.getItemCount(n)
- turtle.select(n)
- turtle.drop()
- end
- collected = 0
- turtle.select(1)
- update()
- end
- local function returnSupplies()
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- update()
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Returning to surface... ")
- goTo( 0,0,0,0,-1 )
- local fuelNeeded = x+y+z + x+y+z + 1
- if not refuel( fuelNeeded ) then
- unload()
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Waiting for fuel... ")
- while not refuel( fuelNeeded ) do
- update()
- sleep(1)
- end
- else
- unload()
- end
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Resuming mining... ")
- goTo( x,y,z,xd,zd )
- end
- local function collect()
- local bFull = true
- local nTotalItems = 0
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- bFull = false
- end
- nTotalItems = nTotalItems + nCount
- end
- if nTotalItems > collected then
- collected = nTotalItems
- update()
- end
- if bFull then
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("No empty slots left. ")
- return false
- end
- return true
- end
- function refuel( ammount )
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = ammount or (xPos + zPos + depth + 1)
- if turtle.getFuelLevel() < needed then
- local fueled = false
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- return true
- end
- end
- end
- end
- turtle.select(1)
- return false
- end
- return true
- end
- local function tryForwards()
- if not refuel() then
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Not enough Fuel! ")
- returnSupplies()
- end
- while not turtle.forward() do
- if turtle.detect() then
- if turtle.dig() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attack() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- update()
- xPos = xPos + xDir
- zPos = zPos + zDir
- local file = fs.open("lastPos", "w")
- file.write(xPos..", "..depth..", "..zPos..", "..xDir..", "..zDir)
- file.close()
- return true
- end
- local function tryDown()
- if not refuel() then
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Not enough Fuel! ")
- returnSupplies()
- end
- while not turtle.down() do
- if turtle.detectDown() then
- if turtle.digDown() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attackDown() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- depth = depth + 1
- update()
- local file = fs.open("lastPos", "w")
- file.write(xPos..", "..depth..", "..zPos..", "..xDir..", "..zDir)
- file.close()
- return true
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- local file = fs.open("lastPos", "w")
- file.write(xPos..", "..depth..", "..zPos..", "..xDir..", "..zDir)
- file.close()
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- local file = fs.open("lastPos", "w")
- file.write(xPos..", "..depth..", "..zPos..", "..xDir..", "..zDir)
- file.close()
- end
- function goTo( x, y, z, xd, zd )
- while depth > y do
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- if xPos > x then
- while xDir ~= -1 do
- turnLeft()
- update()
- end
- while xPos > x do
- if turtle.forward() then
- xPos = xPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- elseif xPos < x then
- while xDir ~= 1 do
- turnLeft()
- update()
- end
- while xPos < x do
- if turtle.forward() then
- xPos = xPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- end
- if zPos > z then
- while zDir ~= -1 do
- turnLeft()
- update()
- end
- while zPos > z do
- if turtle.forward() then
- zPos = zPos - 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- elseif zPos < z then
- while zDir ~= 1 do
- turnLeft()
- update()
- end
- while zPos < z do
- if turtle.forward() then
- zPos = zPos + 1
- elseif turtle.dig() or turtle.attack() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- end
- while depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- update()
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- update()
- end
- local file = fs.open("lastPos", "w")
- file.write(xPos..", "..depth..", "..zPos..", "..xDir..", "..zDir)
- file.close()
- end
- if not refuel() then
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Waiting for fuel... ")
- while not refuel(1024) do
- sleep(1)
- update()
- end
- end
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Excavating... ")
- local reseal = false
- turtle.select(1)
- if turtle.digDown() then
- reseal = true
- end
- local alternate = 0
- local done = false
- while not done do
- for n=1,size do
- for m=1,size-1 do
- if not tryForwards() then
- done = true
- break
- end
- end
- if done then
- break
- end
- if n<size then
- if math.fmod(n + alternate,2) == 0 then
- turnLeft()
- if not tryForwards() then
- done = true
- break
- end
- turnLeft()
- else
- turnRight()
- if not tryForwards() then
- done = true
- break
- end
- turnRight()
- end
- end
- end
- if done then
- break
- end
- if size > 1 then
- if math.fmod(size,2) == 0 then
- turnRight()
- else
- if alternate == 0 then
- turnLeft()
- else
- turnRight()
- end
- alternate = 1 - alternate
- end
- end
- if not tryDown() then
- done = true
- break
- end
- end
- term.setCursorPos(5, 10)
- -- print("=======================================")
- print("Returning to surface... ")
- -- Return to where we started
- goTo( 0,0,0,0,-1 )
- unload()
- goTo( 0,0,0,0,1 )
- -- Seal the hole
- if reseal then
- turtle.placeDown()
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Mined "..(collected + unloaded).." items total.")
Add Comment
Please, Sign In to add comment