Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- simple program for mining turtles to mine a 3x3 tunnel (as opposed to the default
- -- tunnel app, which mines a 3x2)
- -- adapted from user FutileFreedom's 3x3 "tunneler" program, posted on the ComputerCraft
- -- forums: http://www.computercraft.info/forums2/index.php?/topic/3044-turtle-cc-14-3x3-tunnel/
- -- ---------------- config options
- -- specify the slot for chests to place for unloading items when the turtles
- -- inventory gets full. Set to 0 to disable this feature altogether.
- local CHEST_SLOT = 16
- -- specify the slot for torches. Set to 0 to disable altogether.
- local TORCH_SLOT = 15
- -- if TORCH_SLOT is non-zero, then torches will be placed on the right wall of
- -- the excavated tunnel every TORCH_STEP blocks
- local TORCH_STEP = 6
- -- if you're using an ender chest, set this to true
- -- otherwise, provide a stack of chests in slot CHEST_SLOT and the turtle will
- -- leave a trail of chests behind
- local RECOLLECT_CHEST = false
- -- how low our fuel level gets before we try to refuel off of anything in
- -- our inv slots (except for CHEST_SLOT)
- local REFUEL_LEVEL = 100
- -- how long to wait between actions
- local SLEEP_INT = 0.5
- -- sometimes gravel that falls while the turtle is mining a top block gets missed
- -- set this to true to snag that sneaky gravel, with the tradeoff that it adds a few
- -- seconds to each layer/iteration
- local GET_SNEAKY_GRAVEL = true
- -- -------------- initialization
- local tArgs = { ... }
- if #tArgs ~= 1 then
- print( "Usage: tunnel3 <length>" )
- return
- end
- local length = tonumber( tArgs[1] )
- if length < 1 then
- print( "Tunnel length must be positive" )
- return
- end
- -- -------------- digging
- local function oneeighty()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- local collected = 0
- local function collect()
- collected = collected + 1
- if math.fmod(collected, 25) == 0 then
- print( "Mined "..collected.." blocks." )
- end
- if math.fmod(collected, 300) == 0 then
- print( "Fuel level="..turtle.getFuelLevel())
- end
- end
- local function tryDig()
- while turtle.dig() do
- collect()
- sleep(SLEEP_INT)
- if not turtle.detect() then
- return true
- end
- end
- return not turtle.detect()
- end
- local function tryDigUp()
- while turtle.digUp() do
- collect()
- sleep(SLEEP_INT)
- if not turtle.detectUp() then
- return true
- end
- end
- return not turtle.detectUp()
- end
- -- --------------- fuel management
- local function needToRefuel()
- return turtle.getFuelLevel() < REFUEL_LEVEL
- end
- local function doRefuel(amount, once)
- amount = amount or 10
- once = once or false
- for slot=1,16 do
- if slot ~= CHEST_SLOT then
- turtle.select(slot)
- if turtle.refuel(amount) and (turtle.getFuelLevel() > (REFUEL_LEVEL * 2) or once) then
- break
- end
- end
- end
- print("Refueling complete; new fuel level="..turtle.getFuelLevel())
- turtle.select(1)
- end
- -- -------------- inventory management
- local function hasSpace()
- local space = 0
- for slot=1,16 do
- if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
- space = space + turtle.getItemSpace(slot)
- end
- end
- if space < 32 then
- return false
- end
- return true
- end
- local function hasEmptySlots()
- local emptySlots = 0
- for slot=1,16 do
- if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
- if turtle.getItemCount(slot) == 0 then
- emptySlots = emptySlots + turtle.getItemSpace(slot)
- end
- end
- end
- if emptySlots < 2 then
- return false
- end
- return true
- end
- local function unloadToChest(isComplete)
- isComplete = isComplete or false
- oneeighty()
- turtle.select(CHEST_SLOT)
- if not turtle.place() then
- print("Couldn't place chest! Not unloading current inventory..")
- return false
- end
- sleep(SLEEP_INT)
- for slot=1,16 do
- if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
- turtle.select(slot)
- if not turtle.drop(turtle.getItemCount(slot)) then
- print("Couldn't drop off inventory - invalid target or target is full")
- return false
- end
- end
- sleep(SLEEP_INT)
- end
- if RECOLLECT_CHEST and not isComplete then
- turtle.select(CHEST_SLOT)
- turtle.dig()
- end
- turtle.select(1)
- oneeighty()
- return true
- end
- local function checkUnloadToChest()
- if CHEST_SLOT > 0 and (not hasSpace() or not hasEmptySlots()) then
- print("Inv space low - dumping to chest...")
- -- do a small refuel with anything we happen to have before we dump it
- doRefuel(5, true)
- unloadToChest()
- if not hasSpace() or not hasEmptySlots() then
- -- see if we weren't able to unload enough material to the chest
- print("Doh - still no inventory space; aborting! (Is the chest full?)")
- return false
- end
- end
- return true
- end
- -- ----------------- torches
- local function checkPlaceTorch()
- -- if so configured, place a torch
- if TORCH_SLOT > 0 and steps % TORCH_STEP == 0 then
- turtle.select(TORCH_SLOT)
- if not turtle.place() then
- -- couldnt place it on the right; try on the left
- oneeighty()
- if not turtle.place() then
- print('Weird: couldnt place torch')
- end
- oneeighty()
- end
- if turtle.getItemCount(TORCH_SLOT) == 0 then
- print('Out of torches! Disabling torch placement')
- TORCH_SLOT = 0
- end
- end
- end
- -- ----------------- Main Program for digging the tunnel
- local function tunnelize(tunnelLength)
- --[[
- -- Mine out a 3x3 tunnel for tunnelLength blocks:
- -- 1 2 3
- -- A x x x
- -- B x x x
- -- C x S x
- -- ...assuming that the turtle starts in position S
- --]]
- print("Tunneling for "..tunnelLength.." blocks")
- steps = 0
- for n=0,length do
- turtle.select(1)
- if not checkUnloadToChest() then
- -- we were supposed to unload to a chest but
- -- couldnt for some reason ..so abort
- break
- end
- if needToRefuel() then
- print("Fuel low ("..turtle.getFuelLevel().."<"..REFUEL_LEVEL.."); refueling..")
- doRefuel()
- end
- -- C1
- turtle.turnLeft()
- tryDig()
- -- B2
- tryDigUp()
- turtle.up()
- -- B1
- tryDig()
- -- A2
- tryDigUp()
- turtle.up()
- -- A1
- tryDig()
- oneeighty()
- -- A3
- tryDig()
- while not turtle.down() do
- sleep(SLEEP_INT)
- turtle.digDown()
- end
- -- B3
- tryDig()
- while not turtle.down() do
- sleep(SLEEP_INT)
- turtle.digDown()
- end
- -- C3
- tryDig()
- checkPlaceTorch()
- turtle.turnLeft()
- if GET_SNEAKY_GRAVEL then
- -- check for gravel that we might have missed back at C1
- turtle.turnLeft()
- if turtle.detect() then
- tryDig()
- end
- -- start over
- turtle.turnRight()
- end
- -- done?
- if n == tunnelLength then
- -- call unloadToChest with isComplete=True so we leave the chest placed in the world
- if CHEST_SLOT > 0 then
- unloadToChest(true)
- end
- print("Tunnel complete")
- print("Blocks mined="..collected)
- print("Fuel level="..turtle.getFuelLevel())
- return
- end
- -- start the next layer
- tryDig()
- if not turtle.forward() then
- print("Aborting Tunnel!")
- break
- end
- steps = steps + 1
- end
- end
- tunnelize(length)
Add Comment
Please, Sign In to add comment