Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- if #tArgs ~= 2 then
- print( "Width and Length needed." )
- return
- end
- -- length of area
- local xSize = tonumber( tArgs[1] )
- -- Width of area
- local zSize = tonumber( tArgs[2] )
- if xSize < 1 then
- print( "Width can not be negative" )
- return
- end
- if zSize < 1 then
- print( "Length can not be negative" )
- return
- end
- local depth = 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
- 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,depth,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
- sleep(1)
- 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
- print( "Mined "..(collected + unloaded).." items." )
- end
- end
- if bFull then
- 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
- -- The aim of this function is that the turtle detects a block above it, and digs the block if there is one. if there is no block it should go upwards to detect if there was just a hole and there are blocks above the hole.
- -- This should be done for two blocks over the last one that was dug. if there are no further blocks, the turtle should go to the position it was before it dug up the first time.
- local function upwards()
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- local depth = 0
- if not refuel() then
- print( "Not enough Fuel" )
- returnSupplies()
- end
- while not turtle.up() do
- if turtle.detectUp() then
- if turtle.digUp() then
- depth = depth -1
- if not collect() then
- returnSupplies()
- end
- -- else
- -- return false
- end
- elseif turtle.attackUp() then
- if not collect() then
- returnSupplies()
- end
- else turtle.up()
- if turtle.detectUp() then
- if turtle.digUp() then
- depth = depth -1
- if not collect() then
- returnSupplies()
- end
- -- else
- -- return false
- end
- elseif turtle.attackUp() then
- if not collect() then
- returnSupplies()
- end
- else turtle.up()
- if turtle.detectUp() then
- if turtle.digUp() then
- depth = depth -1
- if not collect() then
- returnSupplies()
- end
- -- else
- -- return false
- end
- else goTo( x,0,z,xd,zd )
- end
- end
- end
- end
- goTo( x,0,z,xd,zd )
- return true
- end
- -- The aim of this function is that the turtle goes one forward, digs, then uses the upward() function, goes one block forward, digs, etc...
- 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
- turtle.forward()
- upwards()
- end
- elseif turtle.attack() then
- upwards()
- if not collect() then
- returnSupplies()
- end
- else
- sleep( 0.5 )
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- 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 depth > y do
- if turtle.up() then
- depth = depth - 1
- elseif turtle.digUp() or turtle.attackUp() then
- collect()
- else
- sleep( 0.5 )
- 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 depth < y do
- if turtle.down() then
- depth = depth + 1
- elseif turtle.digDown() or turtle.attackDown() then
- collect()
- else
- sleep( 0.5 )
- end
- end
- while zDir ~= zd or xDir ~= xd do
- turnLeft()
- end
- end
- if not refuel() then
- print( "Out of Fuel" )
- return
- end
- print( "Beggining to level the given area." )
- -- local reseal = false
- -- turtle.select(1)
- -- if turtle.digDown() then
- -- reseal = true
- -- end
- -- this function is slightly (and wrongly) modified from the original excavate program. The turtle shuold dig go one block forward from the start, detect if there are blocks above (the described upwards() function) etc,
- -- then come down, go one step further, etc. The are can be chosen in length and width, and when there are no blocks in front of the turtle anymore, it stays in this area.
- --when there are blocks in front of the turtle it digs, goes one forward and one up. this repeats until there is no block in front of the turtle.
- local alternate = 0
- local done = false
- while not done do
- for n=1,xSize do
- for m=1,zSize-1 do
- if not tryForwards() then
- done = true
- break
- end
- end
- if done then
- break
- end
- if n<xSize 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 xSize > 1 then
- if math.fmod(xSize,2) == 0 then
- turnRight()
- else
- if alternate == 0 then
- turnLeft()
- else
- turnRight()
- end
- alternate = 1 - alternate
- end
- end
- end
- print( "Returning to surface..." )
- -- Return to where we started
- goTo( 0,0,0,0,-1 )
- unload( false )
- goTo( 0,0,0,0,1 )
- -- Seal the hole
- -- if reseal then
- -- turtle.placeDown()
- -- end
- print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement