Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = { ... }
- local fl = turtle.getFuelLevel()
- local dumpinventory = false
- if #args < 1 then
- print("Usage: digger3Wx1H <length> [dump inventory when done (true|false)]")
- return
- end
- local length = tonumber( args[1] )
- if #args == 2 then
- dumpinventory = args[2]
- end
- if length < 1 then
- print("Error: Length must be at least 1")
- return
- end
- if fl < ((length - 1) * 2) then
- print("Error: Not enough fuel to complete the action (need at least " .. ((length - 1) * 2) .. ")")
- return
- end
- local posX = 0
- local distanceTraveled = 0
- local numBlocks = 0
- local numAttacks = 0
- -- Dig the block in front of the turtle until no block is detected anymore
- function digUntilClear()
- -- 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("Error: Could not dig the block! Aborting...")
- return false
- end
- sleep(0.5)
- 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("Error: Hit the failsafe limit (100) of attacks!")
- return false
- end
- end
- return true
- end
- -- Move forward one block
- function moveForward()
- local tmp2 = 0
- -- Attack while we can't move forward (because someONE is blocking us)
- while turtle.forward() == false do
- if attackUntilClear() == false then
- return false
- end
- -- Failsafe limit
- tmp2 = tmp2 + 1
- if tmp2 > 100 then
- print("Error: Hit the failsafe limit (100) of trying to move")
- return false
- end
- end
- return true
- end
- -- Dig a 3 wide area and move forward
- function digAndMoveForward3W(fw)
- -- Dig the block in front
- if digUntilClear() == false then
- return false
- end
- -- Move to the digged spot
- if moveForward() == false then
- return false
- end
- -- Dig the blocks that are to the left and to the right
- turtle.turnLeft()
- if digUntilClear() == false then
- return false
- end
- turtle.turnRight()
- turtle.turnRight()
- if digUntilClear() == false then
- return false
- end
- if fw == true then
- -- Turn back to the forward direction
- turtle.turnLeft()
- else
- -- Turn around (= backwards)
- turtle.turnRight()
- end
- return true
- end
- -- (Dig and) move forward in a straight line (used for the return trip)
- function digAndMoveForward1W()
- if digUntilClear() == false then
- return false
- end
- if moveForward() == false then
- return false
- end
- return true
- end
- -- Select the first slot so that the items get filled to the inventory starting from the first slot
- turtle.select(1)
- -- Dig and move forward for every block except the last one
- for i = 1, (length - 1) do
- if digAndMoveForward3W(true) == false then
- break
- end
- posX = posX + 1
- distanceTraveled = distanceTraveled + 1
- end
- -- After the last row of blocks, we orient backwards for the return trip
- if digAndMoveForward3W(false) == true then
- posX = posX + 1
- distanceTraveled = distanceTraveled + 1
- end
- -- Return back to the starting position
- for i = 1, posX do
- if digAndMoveForward1W() == false then
- print("Error: Got stuck at distance " .. distanceTraveled)
- print(" blocks from start position while returning.")
- return
- end
- posX = posX - 1
- distanceTraveled = distanceTraveled + 1
- end
- if dumpinventory == "true" then
- -- 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
- turtle.select(i)
- turtle.drop()
- -- if turtle.drop() == false then
- -- break
- -- end
- end
- end
- end
- -- And finally reorient
- turtle.turnRight()
- turtle.turnRight()
- -- 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