Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
- refuel()
- print( "Unloading items..." )
- for n=1,16 do
- turtle.select(n)
- turtle.drop()
- end
- end
- local function returnLoot()
- local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
- print( "Returning to surface..." )
- goTo(0,0,0,0,-1)
- unload()
- local roundTripFuel = (x + y + z) * 2
- if refuel(roundTripFuel) then
- print( "Resuming mining..." )
- goTo(x,y,z,xd,zd)
- else
- error( "Insufficient fuel to make it back")
- end
- end
- local function isFull()
- for n=1,16 do
- if turtle.getItemCount(n) == 0 then
- --Empty slots
- return false;
- end
- end
- print( "No empty slots left." )
- return true;
- end
- function refuel( amount )
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- if amount == nil or fuelLevel < amount then
- for n=1,16 do
- turtle.select(n)
- turtle.refuel()
- if amount ~= nil and turtle.getFuelLevel() >= amount then
- return true
- end
- end
- if amount ~= nil then
- -- Not enough fuel
- print("Insufficient fuel")
- return false
- else
- return true
- end
- else
- return true
- end
- end
- movements = {}
- movements["forward"] = {}
- movements["forward"]["detect"] = turtle.detect
- movements["forward"]["dig"] = turtle.dig
- movements["forward"]["move"] = turtle.forward
- movements["forward"]["attack"] = turtle.attack
- movements["up"] = {}
- movements["up"]["detect"] = turtle.detectUp
- movements["up"]["dig"] = turtle.digUp
- movements["up"]["move"] = turtle.up
- movements["up"]["attack"] = turtle.attackUp
- movements["down"] = {}
- movements["down"]["detect"] = turtle.detectDown
- movements["down"]["dig"] = turtle.digDown
- movements["down"]["move"] = turtle.down
- movements["down"]["attack"] = turtle.attackDown
- local function move(direction)
- --print("Attempting to move with " .. direction)
- while not movements[direction].move() do
- if movements[direction].detect() then
- if movements[direction].dig() then
- if isFull() then
- returnLoot()
- end
- else
- error("Failed to dig block")
- end
- elseif movements[direction].attack() then
- -- Do nothing, next loop after attacking
- else
- sleep( 0.5 )
- end
- end
- if direction == "forward" then
- xPos = xPos + xDir
- zPos = zPos + zDir
- elseif direction == "up" then
- depth = depth - 1
- elseif direction == "down" then
- depth = depth + 1
- end
- end
- local function turnLeft()
- turtle.turnLeft()
- xDir, zDir = -zDir, xDir
- end
- local function turnRight()
- turtle.turnRight()
- xDir, zDir = zDir, -xDir
- end
- function alignXDir( xGoalDir )
- if xGoalDir == xDir then
- return
- elseif xGoalDir == zDir then
- turnRight()
- elseif xGoalDir == -zDir then
- turnLeft()
- else
- turnRight()
- turnRight()
- end
- if xGoalDir ~= xDir then
- error("Failed to fix direction")
- end
- end
- function alignZDir( zGoalDir )
- if zGoalDir == zDir then
- return
- elseif zGoalDir == xDir then
- turnLeft()
- elseif zGoalDir == -xDir then
- turnRight()
- else
- turnRight()
- turnRight()
- end
- if zGoalDir ~= zDir then
- error("Failed to fix direction")
- end
- end
- function goTo( x, y, z, xd, zd, atEachSqurare)
- --print( "GoTo: " .. x .. "," .. y .. "," .. z .. " From: " .. xPos .. "," .. depth .. "," .. zPos)
- local targetDistanceToHome = x + y + z
- if not refuel(targetDistanceToHome) then
- print("We can't go there and make it home.")
- local distanceHome = xPos + zPos + depth
- if not refuel(distanceHome) then
- error("Not enough fuel to get home!")
- else
- goTo( 0,0,0,0,-1 )
- unload()
- error("Didn't have enough fuel to finish")
- end
- end
- while depth ~= y do
- -- print("Depth = " .. depth .. " | y = " .. y)
- if depth > y then
- move("up")
- elseif depth < y then
- move("down")
- end
- if atEachSqurare then
- atEachSqurare()
- end
- end
- local xGoalDir = (x - xPos) / math.abs(x - xPos)
- while xPos ~= x do
- alignXDir(xGoalDir)
- move("forward")
- if atEachSqurare then
- atEachSqurare()
- end
- end
- local zGoalDir = (z - zPos) / math.abs(z - zPos)
- while zPos ~= z do
- alignZDir(zGoalDir)
- move("forward")
- if atEachSqurare then
- atEachSqurare()
- end
- end
- --Fix alignment
- if xd ~= -1 then
- alignXDir(xd)
- end
- if zd ~= -1 then
- alignZDir(zd)
- end
- end
- function mineAboveAndBelow ()
- turtle.digUp()
- turtle.digDown()
- end
- function mineLevel(maxX, maxZ, yDepth, upAndDown)
- local nextColumnDirection = turnRight
- local initalZ = zPos
- local targetZ = maxZ
- local incrememnt = 1
- if zPos == maxZ then
- targetZ = 0
- incrememnt = -1
- end
- local atEachSqurare = nil
- if upAndDown then
- atEachSqurare = mineAboveAndBelow
- end
- for z=initalZ, targetZ, incrememnt do
- --Move to start of the column
- goTo(xPos,yDepth,z,-1,-1, atEachSqurare)
- --Sweep the column
- if xPos == 0 then
- goTo(maxX,depth,zPos,-1,-1, atEachSqurare)
- else
- goTo(0,depth,zPos,-1,-1, atEachSqurare)
- end
- end
- end
- -----------------------------------------------------------------------------
- --Main Program
- -----------------------------------------------------------------------------
- local tArgs = { ... }
- if #tArgs ~= 3 then
- print( "My Version Usage: excavate xLength zLength depth" )
- return
- end
- local xLength = tonumber( tArgs[1] ) - 1
- local zLength = tonumber( tArgs[2] ) - 1
- local yLength = tonumber( tArgs[3] ) - 1
- if xLength < 0 or zLength < 0 or yLength < 0 then
- print( "All arguments must be positive" )
- return
- end
- print( "Quarrying..." )
- if yLength >= 2 then
- for y=1, (yLength-1), 3 do
- print("Mining level " .. y)
- mineLevel(xLength, zLength, y, true)
- end
- end
- print( "Done updown levels. Depth " .. depth .. "/" .. yLength)
- if depth < (yLength-1) then
- for y=depth+2, yLength do
- print("Finishing level " .. y )
- mineLevel(xLength, zLength, y)
- end
- end
- print( "Returning to entance..." )
- -- Return to where we started
- goTo( 0,0,0,0,-1 )
- unload()
- goTo( 0,0,0,0,1)
- print("Done")
Add Comment
Please, Sign In to add comment