Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = { ... }
- if #args ~= 3 then
- print("Usage: <scriptname> <width> <height> <length>")
- return
- end
- local maxWidth = args[1]
- local maxHeight = args[2]
- local maxLength = args[3]
- local height = 0
- local unloaded = 0
- local collected = 0
- local xPos,zPos = 0,0
- local xDir,zDir = 0,1
- local goTo -- Filled in further down
- local refuel -- Filled in further down
- shell.run("pastebin get BgrVUJyh kapi")
- os.loadAPI("kapi")
- local function unload( _bKeepOneFuelStack )
- print( "Unloading items..." )
- for n=1,16 do
- local nCount = turtle.getItemCount(n)
- if nCount > 0 then
- turtle.select(n)
- local bDrop = true
- if _bKeepOneFuelStack and turtle.refuel(0) then
- bDrop = false
- _bKeepOneFuelStack = false
- end
- if bDrop then
- turtle.drop()
- unloaded = unloaded + nCount
- end
- end
- end
- collected = 0
- turtle.select(1)
- end
- local function returnSupplies()
- local x,y,z,xd,zd = xPos,height,zPos,xDir,zDir
- print( "Returning to surface..." )
- goTo( 0,0,0,0,-1 )
- local fuelNeeded = 2*(x+y+z) + 1
- if not refuel( fuelNeeded ) then
- unload( true )
- print( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- os.pullEvent( "turtle_inventory" )
- end
- else
- unload( true )
- end
- 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
- if math.fmod(collected + unloaded, 50) == 0 then
- kapi.notify( "Mined "..(collected + unloaded).." items." )
- end
- end
- if bFull then
- kapi.notify("No empty slots left.")
- returnSupplies()
- kapi.notify("returning supplies.")
- return false
- end
- return true
- end
- function refuel( ammount )
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- local needed = ammount or (maxWidth - xPos + maxLength - zPos + maxHeight - height + 2)
- kapi.notify("fuel: ".. turtle.getFuelLevel())
- if turtle.getFuelLevel() < needed then
- kapi.notify("refueling: "..needed)
- 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
- 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
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- local function tryDown()
- if not refuel() then
- kapi.notify( "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
- height = height - 1
- if math.fmod( height, 10 ) == 0 then
- print( "Descended "..height.." metres." )
- end
- return true
- end
- local function tryUp()
- if not refuel() then
- kapi.notify( "Not enough Fuel" )
- returnSupplies()
- end
- while not turtle.up() do
- if turtle.detectUp() then
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- else
- return false
- end
- elseif turtle.attackUp() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- height = height + 1
- if math.fmod( height, 10 ) == 0 then
- print( "Ascended "..height.." metres." )
- end
- return true
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- function goTo( x, y, z, xd, zd )
- while height - y ~= 0 do
- if height < y then
- if turtle.up() then
- height = height + 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- end
- elseif height > y then
- if turtle.down() then
- height = height - 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- end
- if xPos > x then
- while xDir ~= -1 do
- turnLeft()
- 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
- end
- elseif xPos < x then
- while xDir ~= 1 do
- turnLeft()
- 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
- end
- end
- if zPos > z then
- while zDir ~= -1 do
- turnLeft()
- 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
- end
- elseif zPos < z then
- while zDir ~= 1 do
- turnLeft()
- 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
- end
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- if not refuel() then
- kapi.notify( "Out of Fuel" )
- return
- end
- print( "Tunneling..." )
- local Yalternate = false
- local Xalternate = false
- local W,H = 0,0
- for l=1, maxLength do
- kapi.notify("distance: "..l .."/"..maxLength)
- kapi.notify("Around ".. (maxWidth*maxHeight*maxLength) - (maxWidth*zPos*maxHeight).. " blocks left to dig")
- for w=0, maxWidth-1 do
- for h=0, maxHeight-1 do
- if Yalternate then
- H = maxHeight-1-h
- else
- H = h
- end
- if Xalternate then
- W = maxWidth-1-w
- else
- W = w
- end
- goTo(W,H,l,xDir,zDir)
- end
- Yalternate = not Yalternate
- refuel()
- end
- Xalternate = not Xalternate
- end
- print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment