Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local targs = { ... }
- local size = tonumber ( targs[1] )
- local depth = 0
- if #targs == 2 then
- depth = tonumber(tArgs[2])
- else
- depth = tonumber(tArgs[3]) - tonumber(tArgs[2]))
- end
- 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()
- print( "unloading items..." )
- for n=1, 16 do
- turtle.select(n)
- if (turtle.drop()) == false then
- return false
- end
- end
- turtle.select(n)
- return true
- 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 )
- -- Attempts to unloads
- local notStuck == false
- repeat
- local notStuck = unload()
- if notStuck == false then
- sleep(5)
- end
- until notStuck
- local fuelNeeded = x+y+z + x+y+z + 5
- if not refuel( fuelNeeded ) then
- print( "Waiting for fuel" )
- while not refuel( fuelNeeded ) do
- sleep(5)
- end
- end
- print( "Resuming mining..." )
- goTo( x,y,z,xd,zd )
- end
- local function collect()
- local bFull = true
- for n=1, 16 do
- local nCount = turtle.getItemCount(n)
- if nCount == 0 then
- bFull = false
- 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
- if turtle.refuel() then
- return true
- else
- return false
- end
- end
- return true
- end
- local function dig()
- if turtle.detectUp() then
- if turtle.digUp() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep(1)
- end
- end
- if turtle.detectDown() then
- if turtle.digDown() then
- if not collect() then
- returnSupplies()
- end
- else
- sleep(1)
- end
- 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( 1 )
- end
- end
- xPos = xPos + xDir
- zPos = zPos + zDir
- return true
- end
- local function tryDown()
- if not refuel() then
- 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( 1 )
- end
- end
- depth = depth + 1
- 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
- -- Here is the start of the program really
- if not refuel() then
- print( "Out of Fuel" )
- return
- end
- print( "Excavating..." )
- local alternate = 0
- local done = false
- if depth > 0 then
- for n=0, depth do
- tryDown()
- end
- end
- while not done do
- for n=1,size do
- for m=1,size-1 do
- dig() -- dig top and bottom
- 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()
- dig()
- if not tryForwards() then
- done = true
- break
- end
- dig()
- turnLeft()
- else
- turnRight()
- dig()
- if not tryForwards() then
- done = true
- break
- end
- dig()
- 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
- dig()
- tryDown()
- tryDown() -- we go down twice
- if not tryDown() then
- done = true
- break
- end
- end
- print( "Returning to surface..." )
- -- Return to where we started
- goTo( 0,0,0,0,-1 )
- unload()
- goTo( 0,0,0,0,1 )
Advertisement
Add Comment
Please, Sign In to add comment