Skip to content

Instantly share code, notes, and snippets.

@martok
Created April 5, 2013 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martok/5322420 to your computer and use it in GitHub Desktop.
Save martok/5322420 to your computer and use it in GitHub Desktop.
-- Automatic Treefarm
-- * Works with or without Bonemeal
-- * Optimized for Birchwood, but should work with any 1x1 tree that doesn't branch
-- Martok 2013-04-05
local WORK,WORK2,SAPLING,LOG = 13,14,15,16
if (turtle.getItemCount(WORK) ~= 0) or (turtle.getItemCount(WORK2) ~= 0) or (turtle.getItemCount(SAPLING) == 0) or (turtle.getItemCount(LOG) == 0) then
print("Please clear Slots ",WORK,"+",WORK2,", put 1 Sapling in Slot ",SAPLING," and 1 Log in Slot ",LOG,".")
print("Bonemeal may be placed in any Slot not mentioned before.")
return
end
local facing = 0
local wasSuccessful = false
function left()
turtle.turnLeft()
facing = (facing - 1 + 4) % 4
end
function right()
turtle.turnRight()
facing = (facing + 1) % 4
end
function faceTo(target)
local old = facing
while facing ~= target do
right()
end
return old
end
function bonemealForward()
for i=1,16 do
if (turtle.getItemCount(i)>0) and (i~=WORK) and (i~=WORK2) and (i~=SAPLING) and (i~=LOG) then
turtle.select(i)
if turtle.place() then
print("Bonemeal'd")
return
end
end
end
end
function plantTree()
-- assumes we're looking at where the tree should be
local tree = faceTo(0)
turtle.down()
turtle.down()
turtle.down()
turtle.select(WORK)
if turtle.suck() then
if turtle.compareTo(SAPLING) then
turtle.transferTo(WORK2,1)
end
turtle.drop()
end
turtle.up()
turtle.up()
turtle.up()
faceTo(tree)
turtle.select(WORK2)
if turtle.place() then
bonemealForward()
end
end
function offloadLogs()
local tree = faceTo(2)
turtle.down()
turtle.down()
turtle.select(LOG)
turtle.transferTo(WORK, turtle.getItemCount(LOG) - 1)
turtle.select(WORK)
turtle.drop()
turtle.up()
turtle.up()
faceTo(tree)
end
function workOneRound()
-- assumes we're on the block above the saplings
for i=0,3 do
faceTo(i)
if turtle.detect() then
-- that's a tree!
print("Found a Tree!")
turtle.select(WORK)
turtle.dig()
turtle.forward()
turtle.digDown()
-- all the way up
while turtle.detectUp() do
turtle.digUp()
turtle.up()
end
-- back down
while not turtle.detectDown() do
turtle.down()
end
-- back to center
turtle.up()
turtle.back()
turtle.transferTo(LOG,64)
wasSuccessful = true
else
-- no tree. Sapling at least?
turtle.down()
if not turtle.detect() then
print("Replanting!")
plantTree()
wasSuccessful = true
end
turtle.up()
end
os.sleep(1)
end
end
-- initialisierung: wo ist vorn?
print("Setting up local coordinate system (Looking for saplings)")
while not turtle.detectDown() do
turtle.down()
end
local foundFront = false
for i=1,4 do
turtle.select(WORK)
if turtle.suck() then
if turtle.compareTo(SAPLING) then
--found it. put sapling back, we don't need them now
turtle.drop()
foundFront = true
break
end
end
turtle.turnRight()
end
if not foundFront then
print("No chest with Saplings found! Can't do anything.")
return
end
print("Found saplings, let's go.")
facing=0
-- STATE: we're on chest level, facing saplings
turtle.up()
turtle.up()
-- STATE: grass level
for x=1,1000 do
print(":: ",x,"/1000, Fuel: ",turtle.getFuelLevel())
wasSuccessful = false
turtle.up()
turtle.up()
workOneRound()
turtle.down()
turtle.down()
if turtle.getItemSpace(LOG) < 9*4 then
offloadLogs()
end
if not wasSuccessful then
print("Having a li'l snooze...")
os.sleep(30)
else
os.sleep(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment