Skip to content

Instantly share code, notes, and snippets.

@mordaha
Last active August 29, 2015 14:24
Show Gist options
  • Save mordaha/4450480468ab6021cb85 to your computer and use it in GitHub Desktop.
Save mordaha/4450480468ab6021cb85 to your computer and use it in GitHub Desktop.
turtles
--
-- Usage: mt2 length [width=3] [needEmptyStash=true]
--
local function refuel()
local fuelLevel = turtle.getFuelLevel()
if fuelLevel == "unlimited" or fuelLevel > 0 then
return
end
local function tryRefuel()
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
os.pullEvent( "turtle_inventory" )
end
print( "Resuming Tunnel." )
end
end
local function moveForward()
refuel()
-- mooooooveeee soooooo4aaaaraaa
while not turtle.forward() do
turtle.dig()
turtle.attack()
sleep(1)
end
end
local function moveUp()
refuel()
-- mooooooveeee soooooo4aaaaraaa
while not turtle.up() do
turtle.digUp()
turtle.attackUp()
sleep(1)
end
end
local function moveDown()
refuel()
-- mooooooveeee soooooo4aaaaraaa
while not turtle.down() do
turtle.digDown()
turtle.attackDown()
sleep(1)
end
end
-- return true if item count in slot 2 == 64
local function refill2()
local cobbleCount = turtle.getItemCount(2)
local refillCount = 64 - cobbleCount
if (cobbleCount == 0) then
print("Out of tunnel buildblock: place buildblock in slot 2 to continue")
repeat
sleep(1)
until (turtle.getItemCount(2) > 0)
else
for n = 3,16 do
turtle.select(2)
if (turtle.compareTo(n) == true) then
turtle.select(n)
if (refillCount > turtle.getItemCount(n)) then
turtle.transferTo(2, turtle.getItemCount(n))
refillCount = 64 - turtle.getItemCount(2)
else
turtle.transferTo(2, refillCount)
break
end
end
end
end
return turtle.getItemCount(2) == 64
end
local function place()
if not turtle.detect() then
refill2()
turtle.select(2)
turtle.place()
return true
end
return false
end
local function placeUp()
if not turtle.detectUp() then
refill2()
turtle.select(2)
turtle.placeUp()
return true
end
return false
end
local function placeDown()
if not turtle.detectDown() then
refill2()
turtle.select(2)
turtle.placeDown()
return true
end
return false
end
function emptyStash()
for slot=3,16 do
inSlot = turtle.getItemDetail(slot)
if inSlot then
if (inSlot.name == "minecraft:cobblestone") or (inSlot.name == "minecraft:dirt") then
turtle.select(slot)
turtle.drop()
end
end
end
end
local function dig()
while turtle.detect() do
turtle.dig()
end
end
local function digUp()
while turtle.detectUp() do
turtle.digUp()
end
end
-- 1 floor step
local function step1()
dig()
moveForward()
placeDown()
end
-- 2 floor step
local function step2()
dig()
moveForward()
placeUp()
end
local function placeTorch()
turtle.select(1)
if turtle.getItemCount() < 1 then
print("No torches in slot 1, place torches in slot 1")
return false
end
turtle.turnRight()
turtle.turnRight()
turtle.place()
turtle.turnLeft()
turtle.turnLeft()
return true
end
-- MAIN
local args = { ... }
local length = 0
local width = 3
local depth = 0
local needEmptyStash = true
if #args < 1 then
print( "Usage: tunnel <length> [width, default 3] [no emptyStash, default false]" )
return
else
length = tonumber( args[1] )
if length < 1 then
print( "Tunnel length must be > 0" )
return
end
end
if #args > 1 then
width = tonumber( args[2] )
if width < 1 then
print( "Tunnel width must be > 0" )
return
end
end
if #args > 2 then
needEmptyStash = false
end
print( "Tunnelling: length="..length.." width="..width.." height=2" )
for i=1,length do
step1()
turtle.turnRight()
for j=2,width do
step1()
if (i % 7 == 0) and (j % 7 == 0) then
placeTorch()
end
end
place()
digUp()
moveUp()
place()
placeUp()
turtle.turnRight()
turtle.turnRight()
for j=2,width do
step2()
end
place()
moveDown()
place()
turtle.turnRight()
if (i == 2) or (i % 7 == 0) then
placeTorch()
end
depth = depth + 1
if needEmptyStash and (i % 3 == 0) and refill2() then
emptyStash()
end
end
print( "Returning to start..." )
-- Return to where we started
turtle.turnLeft()
turtle.turnLeft()
while depth > 0 do
moveForward()
depth = depth - 1
end
turtle.turnRight()
turtle.turnRight()
print( "Tunnel complete." )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment