This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GalaxyMaster's profile picture

Automatic Tree Farm

Started by GalaxyMaster, 16 May 2013 - 10:57 AM
GalaxyMaster #1
Posted 16 May 2013 - 12:57 PM
Hello ComputerCrafters,

I just made a program which I call: Automatic Tree Farm.
This is also my first ComputerCraft program.

Note: It currently only works on birch, jungle and spruce saplings. As oak saplings have a chance of growing into a giant tree, it won't work.
Note: The bug where the turtle will do weird things when it runs out of saplings, bonemeal or fuel is now solved. However, you do need to place the exact same saplings, bonemeal and fuel (coal and charcoal are different items) in slots 14, 15 and 16. You only need 1 template item per slot. If you place the template items in the wrong order, or you don't even place them, the turtle will show a fake message saying it has run out of saplings, bonemeal or fuel.

If you didn't already guess, this is a fully automatic tree farm program. You need to give it saplings, bonemeal and some kind of fuel (coal, wood, lava, etc). The saplings go into the first slot, the bonemeal into the second slot and the fuel into the third slot. You now also need to place template items in slots 14, 15 and 16. The items must be exactly the same as the items the turtle is using. This is needed for the failsafe function. You only need 1 template item per slot. If you place the template items in the wrong order, or you don't even place them, the turtle will show a fake message saying it has run out of saplings, bonemeal or fuel. It currently only supports 1 stack of bonemeal, unfortunately.

How to install:
SpoilerIf you have the http api installed:

Make a mining turtle and enter this:

pastebin get 7Nxdz8Le programname

You can replace programname with whatever you want the name of the program to be.

If you don't have the http api installed:

Make a mining turtle and make an empty program (name doesn't matter). Then go to your minecraft directory and then to saves, then your world, then computer and then the search the folders there for the program you just made. Open that file in notepad (or textedit for Mac users) and paste the code from here: http://pastebin.com/7Nxdz8Le

Version history:
Spoiler1.2 - added a failsafe. You now do need to place template items in slots 14, 15 and 16. More info in the notes section.
1.1.1 again some minor coding changes thanks to UNOBTANIUM
1.1 - some minor coding changes thanks to UNOBTANIUM
1.0 - initial release

Screenshots:
Spoiler




Code:
Spoiler


function failsafe()
  turtle.select(1)
  if not turtle.compareTo(14) then
	print("No more Saplings")
	print("Press any button to shutdown computer...")
	os.pullEvent("key")
	stop()
  end
  turtle.select(2)
  if not turtle.compareTo(15) then
	print("No more Bone Meal")
	print("Press any button to shutdown computer...")
	os.pullEvent("key")
	stop()
  end
  turtle.select(3)
  if not turtle.compareTo(16) then
	print("No more fuel")
	print("Press any button to shutdown computer...")
	os.pullEvent("key")
	stop()
  end
end
function stop()
  os.shutdown()
end
function chop() --Creates the function chop() for the wood chopping algorithm.
  turtle.dig()
  turtle.digUp()
  turtle.up()
end
Bonemealcount = 20 --Sets the number of times it will place bonemeal on the sapling. I don't recommend changing this as this ensures the trees will grow and don't worry, it won't use bonemeal when the tree is already grown.

while true do
  failsafe()
  while turtle.getFuelLevel() <= 20 do --Checks if a refuel is needed and if it is needed it will refuel.
	turtle.select(3)
	turtle.refuel(1)
  end
  turtle.select(1) --Selects and places sapling.
  turtle.place()
  turtle.select(2) --Selects bonemeal.
  for i = 1, Bonemealcount do --Places bonemeal on sapling.
	turtle.place()
  end
  while turtle.detect() do --Wood chopping algorithm.
	chop()
  end
  while not turtle.detectDown() do --Goes down until it reaches the ground.
	turtle.down()
  end
end

Be sure to leave feedback and report any bugs you encounter.
If you have questions feel free to ask them.

Also, if you have an idea on how it can support multiple (3 preferred) stacks of bonemeal, tell me.
unobtanium #2
Posted 16 May 2013 - 01:24 PM
Sweet :D/>
Great if you are early in the game and having the need of wood.

SpoilerA minor change you can make with your while loop at line 12. You can go and use a for loop instead, because you allready know when to stop the loop. Just use while loops if you dont know how long it will take.


for i=1,Bonemealcount do
turtle.place()
end

Then you also can go and delete line 29.
The turtle.detect in line 19 doesnt get checked. Same at line 25 with turtle.detectDown() Get rid of it :D/>
If you are compareing boolean values, you dont have to write down == true or ==false
Instead you can make it like this:

while turtle.detect() == true do
-- can be done with
while turtle.detect() do
while turtle.detect() == false do
-- can be done with
while not turtle.detect() do
--the same for if-statements
if turtle.detect() then
if not turtle.detect() then

If you are using code multiple times you can create a function for it. Then you just have to write down the function name instead of all the commands over and over again. I know you can copy paste, but it makes it more visual and clear.
I would add a function like

function dig()
turtle.dig()
turtle.digUp()
end

One last thing :D/>
If you are refueling, especially if you want high fuel levels it would be better using:

  while turtle.getFuelLevel() <= 20 do
    turtle.select(3)
    turtle.refuel(1)
  end
You created the variables fuel just at the beginning of the program, which means that the turtle will allways refuel on every loop. The variable doesnt get changed at any point. Just use the accual command in the statement.

I hope that was helpful. It is mostly minor stuff and "better programming" to make a program more error proof.
GalaxyMaster #3
Posted 16 May 2013 - 02:00 PM
Sweet :D/>
Great if you are early in the game and having the need of wood.

SpoilerA minor change you can make with your while loop at line 12. You can go and use a for loop instead, because you allready know when to stop the loop. Just use while loops if you dont know how long it will take.


for i=1,Bonemealcount do
turtle.place()
end

Then you also can go and delete line 29.
The turtle.detect in line 19 doesnt get checked. Same at line 25 with turtle.detectDown() Get rid of it :D/>
If you are compareing boolean values, you dont have to write down == true or ==false
Instead you can make it like this:

while turtle.detect() == true do
-- can be done with
while turtle.detect() do
while turtle.detect() == false do
-- can be done with
while not turtle.detect() do
--the same for if-statements
if turtle.detect() then
if not turtle.detect() then

If you are using code multiple times you can create a function for it. Then you just have to write down the function name instead of all the commands over and over again. I know you can copy paste, but it makes it more visual and clear.
I would add a function like

function dig()
turtle.dig()
turtle.digUp()
end

One last thing :D/>
If you are refueling, especially if you want high fuel levels it would be better using:

  while turtle.getFuelLevel() <= 20 do
	turtle.select(3)
	turtle.refuel(1)
  end
You created the variables fuel just at the beginning of the program, which means that the turtle will allways refuel on every loop. The variable doesnt get changed at any point. Just use the accual command in the statement.

I hope that was helpful. It is mostly minor stuff and "better programming" to make a program more error proof.
Thank you for taking the time to reply, vote and giving me tips on how to make my code cleaner. I will take a look at your suggestions and implement them if I feel that it is needed.
GalaxyMaster #4
Posted 16 May 2013 - 02:01 PM
Sweet :D/>
Great if you are early in the game and having the need of wood.

SpoilerA minor change you can make with your while loop at line 12. You can go and use a for loop instead, because you allready know when to stop the loop. Just use while loops if you dont know how long it will take.


for i=1,Bonemealcount do
turtle.place()
end

Then you also can go and delete line 29.
The turtle.detect in line 19 doesnt get checked. Same at line 25 with turtle.detectDown() Get rid of it :D/>
If you are compareing boolean values, you dont have to write down == true or ==false
Instead you can make it like this:

while turtle.detect() == true do
-- can be done with
while turtle.detect() do
while turtle.detect() == false do
-- can be done with
while not turtle.detect() do
--the same for if-statements
if turtle.detect() then
if not turtle.detect() then

If you are using code multiple times you can create a function for it. Then you just have to write down the function name instead of all the commands over and over again. I know you can copy paste, but it makes it more visual and clear.
I would add a function like

function dig()
turtle.dig()
turtle.digUp()
end

One last thing :D/>
If you are refueling, especially if you want high fuel levels it would be better using:

  while turtle.getFuelLevel() <= 20 do
	turtle.select(3)
	turtle.refuel(1)
  end
You created the variables fuel just at the beginning of the program, which means that the turtle will allways refuel on every loop. The variable doesnt get changed at any point. Just use the accual command in the statement.

I hope that was helpful. It is mostly minor stuff and "better programming" to make a program more error proof.
Oh, and i forgot to ask, in your first suggestion with the for loop, I don't understand the part with i=1. Can you explain that?
EDIT: I did my research and I saw that 'i' is a variable you can always use and don't have to create, and that for loops use a variable with the start value, and then it starts counting until it reaches the finish value and it counts in the steps you specify after that and if you don't specify the step then it will be 1.
unobtanium #5
Posted 16 May 2013 - 02:17 PM
Sure
Spoiler

for i=a, b do
  d
end

i - The running number. It counts like your Bonemealcount did before.
a - This is your start number. Most of the time you want to start at 1.
b - This is your stop variable. Visually it would be like: if a > b then stop.
d - What should happen x times

Expamle: This would let the turtle move 7 blocks forward.

for i=1, 7 do
  turtle.forward()
end

You also can set the d variable:

for i=a, b,d do
  c
end

d - this number is added every loop to i
Examples: All let the turtle move forward 7 blocks as well:

for i=1,7,1 do
  turtle.forward()
end

for i=7, 1, -1 do
  turtle.forward()
end

for i=14, 1, -2 do
  turtle.forward()
end
GalaxyMaster #6
Posted 17 May 2013 - 10:32 AM
UPDATE: 1.1 - some minor coding changes thanks to UNOBTANIUM
unobtanium #7
Posted 17 May 2013 - 10:49 AM
No problem. I help where i can. Some folks here helped me as well, to become what i am at the moment ;D

Btw, one last thing :P/>

SpoilerYou didnt changed the code in the original post. Maybe get rid of it, because you allready have a pastebin link in the installation part.

If you are using the for-loop, and you just want to add just one (the c variable), you dont have to write down the "1". Just a minor thingy again…
At line 19 the chop(): I dont know why you put it there, because the while loop will do this as well.
GalaxyMaster #8
Posted 17 May 2013 - 12:22 PM
No problem. I help where i can. Some folks here helped me as well, to become what i am at the moment ;D

Btw, one last thing :P/>

SpoilerYou didnt changed the code in the original post. Maybe get rid of it, because you allready have a pastebin link in the installation part.

If you are using the for-loop, and you just want to add just one (the c variable), you dont have to write down the "1". Just a minor thingy again…
At line 19 the chop(): I dont know why you put it there, because the while loop will do this as well.
Thanks!
GalaxyMaster #9
Posted 17 May 2013 - 12:29 PM
UPDATE: 1.1.1 again some minor coding changes thanks to UNOBTANIUM
GalaxyMaster #10
Posted 18 May 2013 - 04:39 AM
UPDATE: 1.2 - added a failsafe. You now do need to place template items in slots 14, 15 and 16. More info in the notes section.
tristan1301 #11
Posted 13 September 2013 - 03:07 AM
One idea: Have it output slots 4-13 to a nearby chest. That would keep it from filling up with logs.