Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = { ... }
- local length = 0
- local width = 0
- local height = 1
- local dumpInventory = true
- local posX = -1 -- Relative distance from the starting corner. We start one block outside of the area.
- local posZ = 0
- local posY = 0
- local orientation = 0 -- 0: starting orientation; 1: right; 2: back; 3: left
- local doneRows = 0 -- Already cleared area on the current layer
- local doneLayers = 0 -- Already cleared layers
- local smallerDimension = 0
- local largerDimension = 0
- local tunnelingDirection = 0 -- Direction that the tunnels will be dug (axis)
- local nextTunnelDirection = 0 -- The direction of the next tunnel (alternates with +-2 when going back and forth)
- local tunnelingOrder = 0 -- In which direction are we creating the new tunnels (= the direction perpendicular to the tunnels)
- local distanceTraveled = 0 -- Total distance moved
- local numBlocks = 0 -- Total number of blocks mined
- local numAttacks = 0 -- Total number of times that we attacked mobs
- local dumpingInventory = false -- Used as a state to check if we are already on a inventory dump trip
- local numFilters = 0 -- Number of filter slots
- local invNumBlocks = {} -- Number of blocks in our inventory, grouped by filter blocks
- local invNumBlocksTotal = 0 -- Total number of blocks in our inventory
- local invNumEmptySlots = 0 -- Number of empty slots in our inventory
- local dropsSelf = {} -- Does the block drop itself or something else
- if #args < 1 then
- print("Usage: progname <length> [width] [height] [dumpInventory (true|false)]")
- return
- end
- length = tonumber( args[1] )
- if #args >= 2 then
- width = tonumber( args[2] )
- else
- width = length
- end
- if #args >= 3 then
- height = tonumber( args[3] )
- end
- if #args == 4 then
- dumpInventory = args[4]
- end
- if length < 1 then
- print("Error: Length must be at least 1")
- return
- end
- if width < 1 then
- print("Error: Width must be at least 1")
- return
- end
- if height < 1 then
- print("Error: Height must be at least 1")
- return
- end
- -- Initialize the inventory related tables
- function initInventory()
- local num = 0
- invNumEmptySlots = 16
- for i = 1, 16 do
- num = turtle.getItemCount(i)
- if num > 0 then
- numFilters = numFilters + 1
- invNumBlocks[numFilters] = num
- invNumBlocksTotal = invNumBlocksTotal + num
- invNumEmptySlots = invNumEmptySlots - 1
- dropsSelf[i] = "unknown"
- end
- end
- end
- -- FIXME: We should use a better, runtime fuel check
- --if fl < (length * 2) then
- -- print("Error: Not enough fuel to complete the action (need at least " .. (length * 2) .. ")")
- -- return
- --end
- -- Dump the contents of the inventory into a chest
- function dumpInventory()
- -- If there is a block behind the starting position, assume it is a chest
- -- and dump the inventory to it.
- if turtle.detect() then
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.drop() == false then
- return false
- end
- end
- end
- end
- return true
- end
- -- Return to the starting position to dump the inventory and then return to the spot we were at
- function returnToDumpInventory()
- local resumeX = posX
- local resumeZ = posZ
- local resumeY = posY
- local resumeOrientation = orientation
- dumpingInventory = true
- moveToY(0)
- moveToZ(0)
- moveToX(-1)
- dumpInventory()
- moveToX(resumeX)
- moveToZ(resumeZ)
- moveToY(resumeY)
- reOrient(resumeOrientation)
- dumpingInventory = false
- return true
- end
- -- Get the number of empty slots in inventory
- function getInvNumEmptySlots()
- local slots = 0
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- slots = slots + 1
- end
- end
- return slots
- end
- -- Check if the block to-be-dug fits in inventory
- function fitsInInventory(side)
- if side == nil then
- func = turtle.compare
- elseif side == "up" then
- func = turtle.compareUp
- elseif side == "down" then
- func = turtle.compareDown
- else
- func = turtle.compare
- end
- for i = 1, 16 do
- turtle.select(i)
- if func() == true then
- if turtle.getItemCount(i) < 64 then
- return true
- end
- end
- end
- return false
- end
- -- Check how many of the blocks to-be-dug can fit in our inventory
- function numFitsInInventory(side)
- if side == nil then
- func = turtle.compare
- elseif side == "up" then
- func = turtle.compareUp
- elseif side == "down" then
- func = turtle.compareDown
- else
- func = turtle.compare
- end
- local num = 0
- for i = 1, 16 do
- turtle.select(i)
- -- FIXME: We should maintain mappings from blocks that break into something else, such as stone -> cobble
- if func() == true or turtle.getItemCount(i) == 0 then
- num = num + (64 - turtle.getItemCount(i))
- end
- end
- return num
- end
- -- Dig the block in front of the turtle until no block is detected anymore
- function digUntilClear(face)
- if face == nil then
- face = "front"
- end
- -- We need to check that we are not already on a inventory dumping trip
- -- (This function can be called while moving to/from the chest, if there are new
- -- blocks in our way)
- if dumpingInventory == false and numFitsInInventory(face) == 0 then
- returnToDumpInventory()
- end
- if face == "up" then
- -- If there is a block above, dig it. Loop in case of falling sand/gravel.
- while turtle.detectUp() == true do
- if turtle.digUp() == true then
- numBlocks = numBlocks + 1
- else
- print("digUntilClear(\"up\"): Could not dig the block above! Aborting...")
- return false
- end
- sleep(0.5)
- end
- elseif face == "down" then
- -- If there is a block below, dig it.
- while turtle.detectDown() == true do
- if turtle.digDown() == true then
- numBlocks = numBlocks + 1
- else
- print("digUntilClear(\"down\"): Could not dig the block below! Aborting...")
- return false
- end
- sleep(0.5)
- end
- else -- front
- -- If there is a block in front, dig it. Loop in case of falling sand/gravel.
- while turtle.detect() == true do
- if turtle.dig() == true then
- numBlocks = numBlocks + 1
- else
- print("digUntilClear(): Could not dig the block in front! Aborting...")
- return false
- end
- sleep(0.5)
- end
- end
- return true
- end
- -- Attack as long as the attack succeeds (= mobs in front)
- function attackUntilClear()
- local tmp1 = 0
- -- Attack if there are mobs in front of the turtle
- while turtle.attack() == true do
- numAttacks = numAttacks + 1
- -- Failsafe limit
- tmp1 = tmp1 + 1
- if tmp1 > 100 then
- print("attackUntilClear(): Hit the failsafe limit (100) of attacks!")
- return false
- end
- end
- return true
- end
- -- Attack as long as the attack succeeds (= mobs above)
- function attackUpUntilClear()
- local tmp1 = 0
- -- Attack if there are mobs in front of the turtle
- while turtle.attackUp() == true do
- numAttacks = numAttacks + 1
- -- Failsafe limit
- tmp1 = tmp1 + 1
- if tmp1 > 100 then
- print("attackUpUntilClear(): Hit the failsafe limit (100) of attacks!")
- return false
- end
- end
- return true
- end
- -- Attack as long as the attack succeeds (= mobs above)
- function attackDownUntilClear()
- local tmp1 = 0
- -- Attack if there are mobs in front of the turtle
- while turtle.attackDown() == true do
- numAttacks = numAttacks + 1
- -- Failsafe limit
- tmp1 = tmp1 + 1
- if tmp1 > 100 then
- print("attackDownUntilClear(): Hit the failsafe limit (100) of attacks!")
- return false
- end
- end
- return true
- end
- -- Move forward dist blocks
- function moveForward(dist)
- local tmp2 = 0
- if dist <= 0 then
- return true
- end
- for i = 1, dist do
- tmp2 = 0
- -- Attack while we can't move forward (because someONE is blocking us)
- while turtle.forward() == false do
- if attackUntilClear() == false then
- print("moveForward(dist = " .. dist .. "): attackUntilClear() returned false")
- return false
- end
- -- Failsafe limit
- tmp2 = tmp2 + 1
- if tmp2 > 100 then
- print("moveForward(dist = " .. dist .. "): Hit the failsafe limit (100) of trying to move")
- return false
- end
- end
- if orientation == 0 then
- posX = posX + 1
- elseif orientation == 1 then
- posZ = posZ + 1
- elseif orientation == 2 then
- posX = posX - 1
- elseif orientation == 3 then
- posZ = posZ - 1
- else
- print("moveForward(dist = " .. dist .. "): Invalid orientation!")
- return false
- end
- distanceTraveled = distanceTraveled + 1
- end
- return true
- end
- function moveUp(dist)
- local tmp2 = 0
- if dist <= 0 then
- return true
- end
- for i = 1, dist do
- tmp2 = 0
- -- Attack while we can't move forward (because someONE is blocking us)
- while turtle.up() == false do
- if attackUpUntilClear() == false then
- print("moveUp(dist = " .. dist .. "): attackUpUntilClear() returned false")
- return false
- end
- -- Failsafe limit
- tmp2 = tmp2 + 1
- if tmp2 > 100 then
- print("moveUp(dist = " .. dist .. "): Hit the failsafe limit (100) of trying to move")
- return false
- end
- end
- posY = posY - 1
- distanceTraveled = distanceTraveled + 1
- end
- return true
- end
- function moveDown(dist)
- local tmp2 = 0
- if dist <= 0 then
- return true
- end
- for i = 1, dist do
- tmp2 = 0
- -- Attack while we can't move forward (because someONE is blocking us)
- while turtle.down() == false do
- if attackDownUntilClear() == false then
- print("moveDown(dist = " .. dist .. "): attackDownUntilClear() returned false")
- return false
- end
- -- Failsafe limit
- tmp2 = tmp2 + 1
- if tmp2 > 100 then
- print("moveDown(dist = " .. dist .. "): Hit the failsafe limit (100) of trying to move")
- return false
- end
- end
- posY = posY + 1
- distanceTraveled = distanceTraveled + 1
- end
- return true
- end
- function turnRight()
- turtle.turnRight()
- if orientation == 3 then
- orientation = 0
- else
- orientation = orientation + 1
- end
- end
- function turnLeft()
- turtle.turnLeft()
- if orientation == 0 then
- orientation = 3
- else
- orientation = orientation - 1
- end
- end
- function reOrient(dir)
- if dir == orientation then
- return true
- end
- if dir > orientation then
- if (dir - orientation) <= 2 then
- while orientation ~= dir do
- turnRight()
- end
- else
- while orientation ~= dir do
- turnLeft()
- end
- end
- else -- orientation >= dir
- if (orientation - dir) <= 2 then
- while orientation ~= dir do
- turnLeft()
- end
- else
- while orientation ~= dir do
- turnRight()
- end
- end
- end
- return true
- end
- function digLeftAndRight(first, turn)
- if turn == nil then
- turn = "forward"
- end
- if first == nil then
- first = "left"
- end
- -- Dig to the left first
- if first == "left" then
- -- Dig the blocks that are to the left and to the right
- turnLeft()
- if digUntilClear() == false then
- print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
- return false
- end
- turnRight()
- turnRight()
- if digUntilClear() == false then
- print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
- return false
- end
- -- Which way do we want to be left facing
- if turn == "forward" or turn == nil then
- turnLeft()
- elseif turn == "backward" then
- turnRight()
- -- else: stay facing right
- end
- elseif first == "right" then -- right first
- -- Dig the blocks that are to the left and to the right
- turnRight()
- if digUntilClear() == false then
- print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (right)")
- return false
- end
- turnLeft()
- turnLeft()
- if digUntilClear() == false then
- print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): digUntilClear() (left)")
- return false
- end
- -- Which way do we want to be left facing
- if turn == "forward" then
- turnRight()
- elseif turn == "backward" then
- turnLeft()
- -- else: stay facing left
- end
- else
- print("digLeftAndRight(first = " .. first .. ", turn = " .. turn .. "): Invalid 'first' value")
- return false
- end
- return true
- end
- function digAndMoveUp(dist)
- if dist <= 0 then return true end
- for i = 1, dist do
- if digUntilClear("up") == false then
- print("digAndMoveUp(dist = " .. dist .. "): digUntilClear(\"up\") (i = " .. i .. ")")
- return false
- end
- if moveUp(1) == false then
- print("digAndMoveUp(dist = " .. dist .. "): moveUp(1) (i = " .. i .. ")")
- return false
- end
- end
- return true
- end
- function digAndMoveDown(dist)
- if dist <= 0 then return true end
- for i = 1, dist do
- if digUntilClear("down") == false then
- print("digAndMoveDown(dist = " .. dist .. "): digUntilClear(\"down\") (i = " .. i .. ")")
- return false
- end
- if moveDown(1) == false then
- print("digAndMoveDown(dist = " .. dist .. "): moveDown(1) (i = " .. i .. ")")
- return false
- end
- end
- return true
- end
- -- Dig and move forward in a straight line (1 high)
- function digAndMoveForward1W(len, ud)
- if len <= 0 then return true end
- if ud == nil then ud = "none" end
- for i = 1, len do
- if digUntilClear() == false then
- print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\"): digUntilClear() (i = " .. i .. ")")
- return false
- end
- if moveForward(1) == false then
- print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): moveForward(1) (i = " .. i .. ")")
- return false
- end
- if ud == "up" then
- if digUntilClear("up") == false then
- print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): digUntilClear(\"up\") (i = " .. i .. ")")
- return false
- end
- elseif ud == "down" then
- if digUntilClear("down") == false then
- print("digAndMoveForward1W(len = " .. len .. ", ud = \"" .. ud .. "\): digUntilClear(\"down\") (i = " .. i .. ")")
- return false
- end
- end
- end
- return true
- end
- -- Dig a 2 wide, 1 high tunnel (dir determines if we dig to the left or to the right)
- function digAndMoveForward2Wx1H(len, dir, turn)
- if len <= 0 then return true end
- if dir == nil then dir = "right" end
- if turn == nil then turn = "forward" end
- for i = 1, len do
- -- Dig the block directly in front
- if digUntilClear() == false then
- print("digAndMoveForward2Wx1H(): digUntilClear() (front, i = " .. i .. ")")
- return false
- end
- -- Move to the digged spot
- if moveForward(1) == false then
- print("digAndMoveForward2Wx1H(): moveForward(1) (i = " .. i .. ")")
- return false
- end
- if dir == "right" then
- -- Dig the block that is to the right
- turnRight()
- if digUntilClear() == false then
- print("digAndMoveForward2Wx1H(): digUntilClear() (right, i = " .. i .. ")")
- return false
- end
- if i < len or turn == "forward" then
- -- Reorient to the forward direction
- turnLeft()
- elseif turn == "backward" then
- -- Turn around (= backwards)
- turnRight()
- -- else: leave facing right
- end
- else
- -- Dig the block that is to the left
- turnLeft()
- if digUntilClear() == false then
- print("digAndMoveForward2Wx1H(): digUntilClear() (left, i = " .. i .. ")")
- return false
- end
- if i < len or turn == "forward" then
- -- Reorient to the forward direction
- turnRight()
- elseif turn == "backward" then
- -- Turn around (= backwards)
- turnLeft()
- -- else: leave facing left
- end
- end
- end
- return true
- end
- -- Dig a 3 wide, 1 high tunnel
- -- len: Length of the tunnel we want to dig. We start outside of it.
- -- first: Do we want to dig to the left or to the right first (parameter to digLeftAndRight() )
- -- turn: Which way do we want to face after the digLeftAndRight() finishes.
- function digAndMoveForward3Wx1H(len, first, turn)
- if len <= 0 then
- return true
- end
- for i = 1, len do
- -- Dig the block in front
- if digUntilClear() == false then
- print("digAndMoveForward3Wx1H(): digUntilClear() (front, i = " .. i .. ")")
- return false
- end
- -- Move to the digged spot
- if moveForward(1) == false then
- print("digAndMoveForward3Wx1H(): moveForward(1) (i = " .. i .. ")")
- return false
- end
- -- Dig the blocks that are to the left and to the right
- if i < len then
- -- Reorient to the forward direction
- if digLeftAndRight(first, "forward") == false then
- print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ") (i = " .. i .. ")")
- return false
- end
- else
- -- Last block, reorient to the requested direction
- if digLeftAndRight(first, turn) == false then
- print("digAndMoveForward3Wx1H(): digLeftAndRight(first = " .. first .. ", turn = " .. turn .. ") (last)")
- return false
- end
- end
- end
- return true
- end
- -- Move to the requested X coordinate
- function moveToX(X)
- if X < posX then
- reOrient(2)
- if digAndMoveForward1W(posX - X, "none") == false then
- print("moveToX(): digAndMoveForward1W(posX - X = " .. (posX - X) .. ", \"none\")")
- return false
- end
- elseif posX < X then
- reOrient(0)
- if digAndMoveForward1W(X - posX, "none") == false then
- print("moveToX(): digAndMoveForward1W(X - posX = " .. (X - posX) .. ", \"none\")")
- return false
- end
- end
- return true
- end
- -- Move to the requested Z coordinate
- function moveToZ(Z)
- if Z < posZ then
- reOrient(3)
- if digAndMoveForward1W(posZ - Z, "none") == false then
- print("moveToZ(): digAndMoveForward1W(posZ - Z = " .. (posZ - Z) .. ", \"none\")")
- return false
- end
- elseif posZ < Z then
- reOrient(1)
- if digAndMoveForward1W(Z - posZ, "none") == false then
- print("moveToZ(): digAndMoveForward1W(Z - posZ = " .. (Z - posZ) .. ", \"none\")")
- return false
- end
- end
- return true
- end
- -- Move to the requested Y coordinate
- function moveToY(Y)
- if Y < posY then
- if digAndMoveUp(posY - Y) == false then
- print("moveToY(): digAndMoveUp(posY - Y = " .. (posY - Y) .. ")")
- return false
- end
- elseif posY < Y then
- if digAndMoveDown(Y - posY) == false then
- print("moveToY(): digAndMoveDown(Y - posY = " .. (Y - posY) .. ")")
- return false
- end
- end
- return true
- end
- -- Get the tunnel axis direction
- -- The tunnels will be dug along the longer dimension,
- -- so that we can avoid unnecessary zig-zag motion.
- -- This only needs to be called once
- function getTunnelingDirection()
- if width > length then
- return 1
- end
- return 0
- end
- -- Get the next tunnel's digging direction
- -- This should be called when moving to position for the next tunnel
- function getNextTunnelDirection()
- -- Tunnels go along the X-axis: diggingDirection is 0 or 2
- if tunnelingDirection == 0 then
- -- We are closer to the starting corner than the back corner in X-direction
- if posX <= (length - 1 - posX) then
- return 0
- else
- return 2
- end
- else -- Tunnels go along the Z-axis: diggingDirection is 1 or 3
- -- We are closer to the starting corner than the back corner in Z-direction
- if posZ <= (width - 1 - posZ) then
- return 1
- else
- return 3
- end
- end
- return false
- end
- -- Get tunneling order (starting from the front or the back end of the area?)
- -- This should be called when starting a new layer (Y-coordinate changes)
- function getTunnelingOrder()
- -- Tunnels go along the X-axis: diggingDirection is 0 or 2
- if tunnelingDirection == 0 then
- -- We are closer to the starting corner than the back corner in X-direction
- if posZ <= (width - 1 - posZ) then
- return 1
- else
- return 3
- end
- else -- Tunnels go along the Z-axis: diggingDirection is 1 or 3
- -- We are closer to the starting corner than the back corner in Z-direction
- if posX <= (length - 1 - posX) then
- return 0
- else
- return 2
- end
- end
- return false
- end
- -- Move to the starting position for the next tunnel (so that the tunnel
- -- will align with the already dug area)
- function moveToTunnelStartPos()
- local Z = 0
- local X = 0
- -- Tunnels along the X-axis
- if tunnelingDirection == 0 then
- -- At least 3 blocks still to go
- if width >= (doneRows + 3) then
- -- Tunnels go from the starting corner to the right
- if tunnelingOrder == 1 then
- Z = doneRows + 1 -- the middle of the next 3-wide tunnel
- else -- Tunnels go from the right towards the starting corner
- Z = width - doneRows - 2 -- the middle of the next 3-wide tunnel
- end
- else -- 1 or 2 blocks to go
- -- Tunnels go from the start corner to the right
- if tunnelingOrder == 1 then
- Z = doneRows -- one block into the undug part
- else -- Tunnels go from the right towards the starting corner
- Z = width - doneRows - 1 -- one block into the undug part
- end
- end
- moveToZ(Z)
- -- 2 or 3 blocks to go, dig the block next to the starting position
- if width > (doneRows + 1) then
- reOrient(tunnelingOrder)
- digUntilClear()
- end
- else -- Tunnels along the Z-axis
- -- At least 3 blocks still to go
- if length >= (doneRows + 3) then
- -- Tunnels go from the starting corner towards the back
- if tunnelingOrder == 0 then
- X = doneRows + 1 -- the middle of the next 3-wide tunnel
- else -- Tunnels go from the back towards the starting corner
- X = length - doneRows - 2 -- the middle of the next 3-wide tunnel
- end
- else -- 1 or 2 blocks to go
- -- Tunnels go from the starting corner towards the back
- if tunnelingOrder == 0 then
- X = doneRows -- one block into the undug part
- else -- Tunnels go from the back towards the starting corner
- X = length - doneRows - 1 -- one block into the undug part
- end
- end
- moveToX(X)
- -- 2 or 3 blocks to go, dig the block next to the starting position
- if length > (doneRows + 1) then
- reOrient(tunnelingOrder)
- digUntilClear()
- end
- end
- return true
- end
- function digTunnel(len)
- if (smallerDimension - doneRows) >= 3 then
- --digAndMoveForward3Wx1H(len, first, turn)
- -- Optimize the turns based on the tunneling order and digging direction
- if nextTunnelDirection == 0 then
- if tunnelingOrder == 1 then
- -- +X, +Z
- digAndMoveForward3Wx1H(len, "left", "none")
- else
- -- +X, -Z
- digAndMoveForward3Wx1H(len, "right", "none")
- end
- elseif nextTunnelDirection == 2 then
- if tunnelingOrder == 1 then
- -- -X, +Z
- digAndMoveForward3Wx1H(len, "right", "none")
- else
- -- -X, -Z
- digAndMoveForward3Wx1H(len, "left", "none")
- end
- elseif nextTunnelDirection == 1 then
- if tunnelingOrder == 0 then
- -- +Z, +X
- digAndMoveForward3Wx1H(len, "right", "none")
- else
- -- +Z, -X
- digAndMoveForward3Wx1H(len, "left", "none")
- end
- elseif nextTunnelDirection == 3 then
- if tunnelingOrder == 0 then
- -- -Z, +X
- digAndMoveForward3Wx1H(len, "left", "none")
- else
- -- -Z, -X
- digAndMoveForward3Wx1H(len, "right", "none")
- end
- end
- doneRows = doneRows + 3
- elseif (smallerDimension - doneRows) == 2 then
- --digAndMoveForward2Wx1H(len, dir, turn)
- -- Optimize the turns based on the tunneling order and digging direction
- if nextTunnelDirection == 0 then
- if tunnelingOrder == 1 then
- -- +X, +Z
- digAndMoveForward2Wx1H(len, "right", "none")
- else
- -- +X, -Z
- digAndMoveForward2Wx1H(len, "left", "none")
- end
- elseif nextTunnelDirection == 2 then
- if tunnelingOrder == 1 then
- -- -X, +Z
- digAndMoveForward2Wx1H(len, "left", "none")
- else
- -- -X, -Z
- digAndMoveForward2Wx1H(len, "right", "none")
- end
- elseif nextTunnelDirection == 1 then
- if tunnelingOrder == 0 then
- -- +Z, +X
- digAndMoveForward2Wx1H(len, "left", "none")
- else
- -- +Z, -X
- digAndMoveForward2Wx1H(len, "right", "none")
- end
- elseif nextTunnelDirection == 3 then
- if tunnelingOrder == 0 then
- -- -Z, +X
- digAndMoveForward2Wx1H(len, "right", "none")
- else
- -- -Z, -X
- digAndMoveForward2Wx1H(len, "left", "none")
- end
- end
- doneRows = doneRows + 2
- elseif (smallerDimension - doneRows) == 1 then
- --digAndMoveForward1W(len, ud)
- digAndMoveForward1W(len, "none")
- doneRows = doneRows + 1
- else
- print("digTunnel(" .. length .. "): length less than 1")
- end
- return true
- end
- initInventory()
- print("numFilters: " ..numFilters)
- print("invNumBlocksTotal: " .. invNumBlocksTotal)
- print("invNumEmptySlots: " .. invNumEmptySlots)
- print("invNumBlocks: " .. invNumBlocks)
- print("dropsSelf: " .. dropsSelf)
- return
- -- Select the first slot so that the items get filled to the inventory starting from the first slot
- turtle.select(1)
- -- Corner case check: only dig the one block if the area is 1x1.
- if length == 1 and width == 1 then
- digUntilClear()
- doneRows = 1
- else -- normal, larger than 1x1 areas: move to the starting corner 0, 0
- moveToX(0)
- end
- if length >= width then
- smallerDimension = width
- largerDimension = length
- else
- smallerDimension = length
- largerDimension = width
- end
- tunnelingDirection = getTunnelingDirection()
- tunnelingOrder = getTunnelingOrder()
- while doneLayers < height do
- while (smallerDimension - doneRows) > 0 do
- nextTunnelDirection = getNextTunnelDirection()
- print("posX: " .. posX)
- print("posZ: " .. posZ)
- print("posY: " .. posY)
- print("tunnelingDirection: " .. tunnelingDirection)
- print("tunnelingOrder: " .. tunnelingOrder)
- print("nextTunnelDirection: " .. nextTunnelDirection)
- moveToTunnelStartPos()
- reOrient(nextTunnelDirection)
- digTunnel(largerDimension - 1)
- end
- doneLayers = doneLayers + 1
- if doneLayers >= height then
- break
- end
- digAndMoveDown(1)
- -- Dig the one block on the "back" side (on the corner of the new layer) that would be left undug
- -- by the moveToTunnelStartPos(), but only if the last tunnel was not a 1-wide one.
- -- if: We are facing sideways, which means the last tunnel was a 2 or 3 wide one.
- if orientation == tunnelingOrder then
- digUntilClear()
- end
- doneRows = 0
- --tunnelingOrder = getTunnelingOrder()
- tunnelingOrder = (tunnelingOrder + 2) % 4
- end
- if length > 1 or width > 1 then
- -- Return to the starting level
- if moveToY(0) == false then
- print("Error while returning to the starting corner (Y)")
- return false
- end
- -- Return to the starting corner
- if orientation == 0 or orientation == 3 then
- if moveToZ(0) == false then
- print("Error while returning to the starting corner (Z)")
- return false
- end
- if moveToX(0) == false then
- print("Error while returning to the starting corner (X)")
- return false
- end
- else
- if moveToX(0) == false then
- print("Error while returning to the starting corner (X)")
- return false
- end
- if moveToZ(0) == false then
- print("Error while returning to the starting corner (Z)")
- return false
- end
- end
- end
- if posX == 0 then
- reOrient(2)
- if digAndMoveForward1W(1, "none") == false then
- print("digAndMoveForward1W(1, \"none\"): Error while trying to move to the starting corner of the area")
- return false
- end
- elseif posX ~= -1 then
- print("Error: Wrong position while trying to move to the starting spot")
- return false
- end
- if dumpInventory == "true" then
- -- reOrient(2)
- dumpInventory()
- end
- -- And finally reorient
- reOrient(0)
- -- And print a summary
- print("Done.")
- print("Mined " .. numBlocks .. " blocks.")
- print("Moved a total of " .. distanceTraveled .. " blocks.")
- print("Attacked mobs " .. numAttacks .. " times.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement