Skip to content

Instantly share code, notes, and snippets.

@RX14
Last active January 4, 2016 00:58
Show Gist options
  • Save RX14/8544921 to your computer and use it in GitHub Desktop.
Save RX14/8544921 to your computer and use it in GitHub Desktop.
Turtle Tunnel program V4
-- Arguements and Arg Checking
local tArgs = { ... }
if #tArgs ~= 1 then
print( "Usage: tunnel <length>" )
return
end
-- Mine in a quarry pattern until we hit something we can't dig
local length = tonumber( tArgs[1] )
if length < 1 then
print( "Tunnel length must be positive" )
return
end
local depth = 0
local collected = 0
local function collect()
collected = collected + 1
if math.fmod(collected, 25) == 0 then
print( "Mined "..collected.." blocks." )
end
end
local function tryDig()
while turtle.detect() do
if turtle.dig() then
collect()
sleep(0.5)
else
return false
end
end
return true
end
local function tryDigUp()
while turtle.detectUp() do
if turtle.digUp() then
collect()
sleep(0.5)
else
return false
end
end
return true
end
local function tryDigDown()
while turtle.detectDown() do
if turtle.digDown() then
collect()
sleep(0.5)
else
return false
end
end
return true
end
local function refuel()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel == "unlimited" or fuelLevel > 0 then
return
end
local function tryRefuel()
print( "Refeueling" )
for n=1,16 do
if turtle.getItemCount(n) > 0 then
turtle.select(n)
if turtle.refuel(1) then
turtle.select(1)
return true
end
end
end
turtle.select(1)
return false
end
if not tryRefuel() then
print( "Add more fuel to continue." )
while not tryRefuel() do
sleep(1)
end
print( "Resuming Tunnel." )
end
end
-- Functions for dropping off items into a chest from slot 1.
local function depositChest()
print( "Depositing Chest" )
turtle.turnRight()
turtle.turnRight()
turtle.select(2)
turtle.place()
sleep(0.5)
for slot=3,16 do
turtle.select(slot)
turtle.drop(turtle.getItemCount(slot))
sleep(0.5)
end
turtle.select(1)
end
local function previousState()
print( "previous State" )
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
end
local function tryUp()
refuel()
while not turtle.up() do
if turtle.detectUp() then
if not tryDigUp() then
return false
end
elseif turtle.attackUp() then
collect()
else
sleep( 0.5 )
end
end
return true
end
local function tryDown()
refuel()
while not turtle.down() do
if turtle.detectDown() then
if not tryDigDown() then
return false
end
elseif turtle.attackDown() then
collect()
else
sleep( 0.5 )
end
end
return true
end
local function checkSpace()
local fullslots = 0
for slot=3,16 do
if turtle.getItemCount(slot) > 0 then
fullslots = fullslots + 1
end
end
if fullslots > 13 then
depositChest()
previousState()
return true
else
return true
end
end
local function tryForward()
refuel()
while not turtle.forward() do
if turtle.detect() then
if not tryDig() then
return false
end
elseif turtle.attack() then
collect()
else
sleep( 0.5 )
end
end
return true
end
print( "Tunnelling..." )
for n=1,length do
if not checkSpace() then break end
tryDigUp()
turtle.turnLeft()
tryDig()
turtle.up()
tryDig()
tryDigUp()
turtle.up()
tryDig()
turtle.turnRight()
turtle.turnRight()
tryDig()
turtle.down()
tryDig()
turtle.down()
tryDig()
turtle.turnLeft()
if n<length then
tryDig()
if not tryForward() then
print( "Aborting Tunnel." )
break
end
else
print( "Tunnel complete." )
end
end
--[[
print( "Returning to start..." )
-- Return to where we started
turtle.turnLeft()
turtle.turnLeft()
while depth > 0 do
if turtle.forward() then
depth = depth - 1
else
turtle.dig()
end
end
turtle.turnRight()
turtle.turnRight()
]]
print( "Tunnel complete." )
print( "Mined "..collected.." items total." )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment