Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local t = require("libs.tlib")
- local direction
- local tunnelHeight = 3
- local numRepeats = 8
- local tunnelLength
- local continueWork = false
- local tFuel = {
- 'charcoal',
- 'coal',
- }
- tProtectedBlocks = {
- ['minecraft:diamond_ore'] = true,
- ['minecraft:lapis_ore'] = true,
- ['minecraft:redstone_ore'] = true,
- }
- local tkeepItems = {
- ['minecraft:torch'] = true,
- ['minecraft:chest'] = true,
- }
- local tDropTable = {
- -- 'cobblestone',
- 'granite',
- 'andesite',
- 'diorite',
- 'dirt',
- 'gravel',
- }
- local tTurnRightFacing = {
- ['north'] = 'east',
- ['east'] = 'south',
- ['south'] = 'west',
- ['west'] = 'north',
- }
- local tTurnLeftFacing = {
- ['north'] = 'west',
- ['east'] = 'north',
- ['south'] = 'east',
- ['west'] = 'south',
- }
- local function isProtectedBlock(success, data)
- if success and tProtectedBlocks[data.name] then
- if data.name == 'minecraft:diamond_ore' then
- print('I found diamonds!')
- end
- return true
- end
- return false
- end
- local function determineFacing()
- t.forward()
- if not turtle.detect() then
- if not t.existItem('cobblestone') then
- print('add cobblestone')
- while not t.existItem('cobblestone') do
- sleep(1)
- end
- end
- if not (t.selectItem('cobblestone') and turtle.place()) then
- print('ERROR: could not place cobblestone')
- t.back()
- return false
- end
- end
- t.back()
- if not (t.selectItem('torch') and turtle.place()) then
- print('ERROR: could not place torch')
- return false
- end
- success, data = turtle.inspect()
- if not (success and data.state.facing) then
- return false
- else
- local direction = t.tInverseDirection[data.state.facing]
- print('Turtle is facing ' .. direction .. '.')
- return direction
- end
- end
- local function saveDig()
- if not isProtectedBlock(turtle.inspect()) then
- while turtle.dig() do
- --
- end
- end
- end
- local function torchTime(step)
- torchPlacement = {
- [2] = true, [11] = true,
- [20] = true, [29] = true,
- [38] = true,
- }
- return torchPlacement[step]
- end
- local function placeTorch()
- if not t.selectItem('torch') then
- print('out of torches!')
- t.turnLeft()
- return
- end
- turtle.place()
- success, data = turtle.inspect()
- if not t.tInverseDirection[direction] then
- print(direction)
- print(t.tInverseDirection[direction])
- error('ERROR in placeTorch(): no direction value')
- end
- if not success or (data.name == 'minecraft:wall_torch' and data.state.facing ~= t.tInverseDirection[direction]) then
- t.forward()
- if t.selectItem('cobblestone') and (turtle.placeDown() or turtle.place()) then
- t.back()
- if t.selectItem('torch') then
- turtle.place()
- end
- else
- t.turnAround()
- t.forward()
- if t.selectItem('torch') then
- turtle.place()
- end
- t.turnRight()
- return
- end
- elseif isProtectedBlock(turtle.inspect()) then
- t.turnAround()
- if t.selectItem('torch') then
- turtle.place()
- end
- t.turnRight()
- return
- end
- t.turnLeft()
- turtle.select(1)
- end
- local function dig3x3(torch)
- --recursive digging function:
- local function digOutLevel(level, max)
- saveDig()
- if (level < max) and (not isProtectedBlock(turtle.inspectUp())) then
- t.up()
- digOutLevel(level + 1, max)
- t.down()
- else
- t.turnAround()
- end
- saveDig()
- end
- if (not turtle.detectDown()) and t.selectItem('cobblestone') then
- turtle.placeDown()
- end
- t.turnLeft()
- digOutLevel(1, tunnelHeight)
- if torch then
- placeTorch()
- else
- t.turnLeft()
- end
- end
- local function emptySlots()
- local numEmptySlots = 0
- for slot=1, 16 do
- if turtle.getItemCount(slot) == 0 then
- numEmptySlots = numEmptySlots + 1
- end
- end
- return numEmptySlots
- end
- local function cleanUpInventory()
- for slot_n = 16, 2, -1 do
- if turtle.getItemCount(slot_n) > 0 then
- item_source = turtle.getItemDetail(slot_n)
- for slot_m = 1, slot_n-1 do
- if turtle.getItemSpace(slot_m) > 0 then
- item_target = turtle.getItemDetail(slot_m)
- if (item_source and item_target) and (item_source.name == item_target.name) then
- turtle.select(slot_n)
- turtle.transferTo(slot_m)
- if turtle.getItemCount(slot_n) == 0 then
- break
- end
- end
- end
- end
- end
- end
- for slot_n = 16, 2, -1 do
- if turtle.getItemCount(slot_n) > 0 then
- for slot_m = 1, slot_n-1 do
- if turtle.getItemCount(slot_m) == 0 then
- turtle.select(slot_n)
- turtle.transferTo(slot_m)
- break
- end
- end
- end
- end
- end
- local function inventoryManagement()
- cleanUpInventory()
- if emptySlots() <= 2 then
- t.back()
- t.turnAround()
- if t.selectItem('chest') and turtle.place() then
- print('Turtle placed chest.')
- for i, v in ipairs(tDropTable) do
- t.dropAll(v)
- end
- end
- t.dropAllButOneStack('cobblestone')
- cleanUpInventory()
- t.turnAround()
- t.forward()
- end
- turtle.select(1)
- end
- local function stepAround(stepsLeft)
- local level = 1
- while stepsLeft > 0 do
- if isProtectedBlock(turtle.inspectUp()) then
- -- case 1
- t.back()
- stepsLeft = stepsLeft + 1
- if not isProtectedBlock(turtle.inspectUp()) then
- t.up()
- level = level + 1
- end
- elseif isProtectedBlock(turtle.inspect()) then
- -- case 2
- t.up()
- level = level + 1
- if not isProtectedBlock(turtle.inspect()) then
- t.forward()
- stepsLeft = stepsLeft - 1
- end
- elseif isProtectedBlock(turtle.inspectDown()) then
- -- case 3
- t.forward()
- stepsLeft = stepsLeft - 1
- elseif level > 1 then
- -- get down
- t.down()
- level = level - 1
- else
- return stepsLeft
- end
- end
- while level > 1 do
- t.down()
- level = level - 1
- end
- return stepsLeft
- end
- local function tunnel(stepsLeft)
- local torchCounter = 7
- while stepsLeft > 0 do
- if isProtectedBlock(turtle.inspect()) then
- local temp = stepAround(stepsLeft)
- local diff = stepsLeft - temp
- torchCounter = (torchCounter + diff) % 9
- stepsLeft = temp
- else
- t.forward()
- stepsLeft = stepsLeft - 1
- torchCounter = (torchCounter + 1) % 9
- end
- dig3x3(torchCounter == 0)
- inventoryManagement()
- sleep(0)
- end
- end
- local function uTurnRight()
- t.back()
- t.turnRight()
- direction = tTurnRightFacing[direction]
- t.forward()
- tunnel(6)
- t.back()
- t.turnRight()
- direction = tTurnRightFacing[direction]
- t.forward()
- end
- local function uTurnLeft()
- t.back()
- t.turnLeft()
- direction = tTurnLeftFacing[direction]
- t.forward()
- tunnel(6)
- t.back()
- t.turnLeft()
- direction = tTurnLeftFacing[direction]
- t.forward()
- end
- local function unload()
- for slot=1, 16 do
- item = turtle.getItemDetail(slot)
- if item and not tkeepItems[item.name] then
- turtle.select(slot)
- turtle.drop()
- end
- end
- turtle.select(1)
- end
- local function gotoNextTunnel()
- t.forward()
- t.forward()
- t.turnLeft()
- if t.selectItem('chest') and turtle.place() then
- unload()
- end
- t.turnRight()
- t.forward()
- end
- -- **** MAIN ********
- -- SETUP
- local tArgs = { ... }
- if #tArgs == 0 then
- tunnelLength = false
- elseif #tArgs ~= 1 then
- print( 'Usage: [mine] OR [mine <length>]' )
- return
- else
- tunnelLength = tonumber(tArgs[1])
- if not (tunnelLength ~= nil and tunnelLength > 0 and tunnelLength <= 256) then
- print('Argument "' .. tArgs[1] .. '" is not a valid number (between 1 and 256)')
- return
- end
- end
- t.setFuel(tFuel)
- if not t.existItem('torch') then
- print('need torches...')
- while not t.existItem('torch') do
- sleep(1)
- end
- end
- direction = determineFacing()
- if direction then
- t.setDirection(direction)
- direction = tTurnRightFacing[direction]
- else
- error('ERROR: no Facing value')
- end
- print('Going Mining...')
- print()
- -- SETUP END
- if tunnelLength then
- tunnel(tunnelLength)
- else
- for n=1, numRepeats do
- -- first tunnel
- tunnel(39+3)
- -- U-Turn
- if (n % 2 == 0) then
- uTurnLeft()
- else
- uTurnRight()
- end
- -- second tunnel
- tunnel(39)
- -- cross the road
- gotoNextTunnel()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement