Turtle Script, would this work?

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

Soulsbane96

New Member
Jul 29, 2019
30
0
0
I've never made a turtle script (or used a turtle for anything except "craft") but I know they're a great resource/way to do things so I wanted to set one up to do a very simple tree farm.

Since I've never programmed I went on the tutorial site/wiki and read up a bit (nothing in depth, I'm at school so dont have the time) and from what I learned I went to the Turtle Lumberjack and Advanced Lumberjack script pages.
I took some code and pieced it together with others taht I wrote from viewing and absorbing information and came up with this and I was wondering if it would work
Code:
print ("Saplings go into slot 1, Bonemeal into slot 2, and Logs into slot 3.")
print ("How many trees to create and harvest?")
local x = io.read()
local sapling, bonemeal, log = 1, 2, 3
 
for i = 0, x do
    turtle.back()
    turtle.select(sapling)
    turtle.place
    turtle.select(bonemeal)
    turtle.place
    turtle.select(log)
    if turtle.compare() then
        turtle.dig()
        turtle.forward()
            while turtle.detectUp do
                turtle.digUp()
                turtle.up()
            end
        while not turtle.detectDown() do
            turtle.down()
        end
    end
end
TL;DR: Will that code work for a very basic tree farm (place sapling, bonemeal it, break tree, repeat)?
Any help appreciated.
 

solidity

Active Member
Jul 29, 2019
53
0
26
Looks good, you're missing a few brackets after functions though. Apart from that, it should do what you want it to do, as long as you use only straight trees or prevent them from branching out. You might also want to add some sleep-time to the loop to give the leaves time to decay and collect some saplings.
 

Soulsbane96

New Member
Jul 29, 2019
30
0
0
Looks good, you're missing a few brackets after functions though. Apart from that, it should do what you want it to do, as long as you use only straight trees or prevent them from branching out. You might also want to add some sleep-time to the loop to give the leaves time to decay and collect some saplings.

I put () after the turtle.place commands, was that the brackets you were referring to?
And do you think a sleep(30) will be long enough for leaf decay? I'll have to put in a turtle.suck() (does it have to move around while sucking?) or something so it'll pick up saplings.
 

Abdiel

New Member
Jul 29, 2019
1,062
0
0
It should work with any trees that only generate one column of wood blocks (i.e. not with oak). With birch/spruce you don't even need the delay between cycles, as leaves don't block tree growth.

If you have turtle.needsFuel on, remember to refuel the turtle before starting or add a turtle.refuel() somewhere.
 

Soulsbane96

New Member
Jul 29, 2019
30
0
0
Have added to the code and now I get the message
bios:338: [string "treefarm.txt"]:33: '<eof> expected
when I try and run int. I know its an error in line 33 but I'm not entirely sure what the problem is

code:
Code:
print ("Saplings go into slot 1, Bonemeal into slot 2, Logs into slot 3, and
 
Cobblestone into slot 4.")
print ("How many trees to create and harvest?")
local x = io.read()
local sapling, bonemeal, log, cobblestone = 1, 2, 3, 4
 
for i = 0, x do
    turtle.back()
    turtle.select(sapling)
    turtle.place()
    turtle.select(bonemeal)
    turtle.place()
    turtle.select(log)
    if turtle.compare() then
        turtle.dig()
        turtle.forward()
            while turtle.detectUp do
                while not turtle.detectUp(cobblestone) do
                    turtle.digUp()
                    turtle.up()
                end
            end
        while not turtle.detectDown() do
            turtle.down()
        end
    end
    sleep(30)
    itemcount = turtle.getItemCount(16)
        for i=5,16 do
            turtle.select(i)
            turtle.drop()
        end
    end
end

Any help?
 

Abdiel

New Member
Jul 29, 2019
1,062
0
0
You have an extra "end" at the end. The "end" at the penultimate line pairs with the main for loop.
 

Math-Man

New Member
Jul 29, 2019
11
0
0
Eof means you got an end that doesnt end anything
Try removing an endfrom bottom
Ninja'd
 

Soulsbane96

New Member
Jul 29, 2019
30
0
0
You have an extra "end" at the end. The "end" at the penultimate line pairs with the main for loop.
Thank you
Think I could trouble you for some information?
I keep trying to make it CompareUp to check for cobblestone but if I put it in different places it either a. only breaks the bottom, b. breaks the log, moves forward, breaks up, moves up then gets stuck moving up and down

And since I'm a noob I have no real idea what I'm doing. Do you have suggestions on how I'd put in a CompareUp for cobblestone so it doesnt just break through my ceiling and keep going up?
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Have added to the code and now I get the message
bios:338: [string "treefarm.txt"]:33: '<eof> expected
when I try and run int. I know its an error in line 33 but I'm not entirely sure what the problem is

code:
Code:
print ("Saplings go into slot 1, Bonemeal into slot 2, Logs into slot 3, and
 
Cobblestone into slot 4.")
print ("How many trees to create and harvest?")
local x = io.read()
local sapling, bonemeal, log, cobblestone = 1, 2, 3, 4
 
for i = 0, x do
    turtle.back()
    turtle.select(sapling)
    turtle.place()
    turtle.select(bonemeal)
    turtle.place()
    turtle.select(log)
    if turtle.compare() then
        turtle.dig()
        turtle.forward()
        turtle.select(cobblestone)
        repeat
          turtle.digUp()
          turtle.up()
        until turtle.compareUp(cobblestone) == true
    end
        while not turtle.detectDown() do
            turtle.down()
        end
    end
    sleep(30)
    itemcount = turtle.getItemCount(16)
        for i=5,16 do
            turtle.select(i)
            turtle.drop()
        end
    end

Any help?

EDIT this code for sure works to detect and compare anything in the 4th slot.
 
  • Like
Reactions: Soulsbane96

Abdiel

New Member
Jul 29, 2019
1,062
0
0
Two pointers:

- Make sure the roof is actually cobblestone. If it's stone it will not compare to true with cobblestone!
- Wouldn't it be easier to compare with wood? Try doing something like "as long as there is wood above me, chop it down".
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Also, a while loop instead of a repeat until can prevent some problems.

I would also make it so that it counts how many times it goes up, and do as many turtle.down as the count ended at, it lets you have the turtle do its business with a single block of dirt hanging above water or other systems to collect the saplings.
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Also, a while loop instead of a repeat until can prevent some problems.

I would also make it so that it counts how many times it goes up, and do as many turtle.down as the count ended at, it lets you have the turtle do its business with a single block of dirt hanging above water or other systems to collect the saplings.
How would a while loop better fit this action? Basically I wrote the loop based on what he needed not what I think would be better.

Two pointers:

- Make sure the roof is actually cobblestone. If it's stone it will not compare to true with cobblestone!
- Wouldn't it be easier to compare with wood? Try doing something like "as long as there is wood above me, chop it down".

It uses anything that is in the 4th slot to compare it could be a macerator as long as its in the 4th slot.
The cobblestone variable was declared at the top for number 4.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
How would a while loop better fit this action? Basically I wrote the loop based on what he needed not what I think would be better.



It uses anything that is in the 4th slot to compare it could be a macerator as long as its in the 4th slot.
The cobblestone variable was declared at the top for number 4.
Basically, a while loop wouldn't have to be nested in a if statement, but whatever woks for you is good anyway, it's just a different approach.
Let's just say that I had bad experience with people overusing the repeat until (or similar in other languages), that would create more problems than what was needed, due to the fact that it will always to the code inside at least once, no matter what.


As for the cobble, he is right though, why don't you do a
until not turtle.compareUp(log)

Oh, and while I am at it, why are you comparing a boolean to true?
It is redundant, as 'true == true' will send back 'true' anyway, but you might have a reason that I didn't think off, so I am interested to know now.
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Basically, a while loop wouldn't have to be nested in a if statement, but whatever woks for you is good anyway, it's just a different approach.
Let's just say that I had bad experience with people overusing the repeat until (or similar in other languages), that would create more problems than what was needed, due to the fact that it will always to the code inside at least once, no matter what.


As for the cobble, he is right though, why don't you do a
until not turtle.compareUp(log)

Oh, and while I am at it, why are you comparing a boolean to true?
It is redundant, as 'true == true' will send back 'true' anyway, but you might have a reason that I didn't think off, so I am interested to know now.
All i wrote was the loop and shoe-horned it in, the if statement was already there.

The reason I wrote a loop to cycle through until it finds what ever block you have in the 4th slot was to make a cut straight up the tree so that you can you can replant a spruce or a birch tree and it will constantly grow to full height since there are no blocks in the way. In all my tests it worked until it ran into the marked block every time without fail.

Just a habit. I've taken classes for java and VB in both you type true or false "That I know of I'm not a master be any means." also I like to make for sure what I'm seeing is what is happening.
 

slay_mithos

New Member
Jul 29, 2019
1,288
0
0
Then, you should use a "not detectUp()" condition, it would stop as soon as there are no blocks above, and leaves decay isn't fast enough to outrun a turtle, at least from my tests.

Don't take my posts the wrong way, your method indeed works as intended, I am just trying to give a few pointers from all the mistakes I made when trying to make a decent logger and miner programs.
I am nowhere as good as many people, so I tend to do many mistakes, and my speech should not be taken too religiously anyway, as I can indeed be completely wrong.
 

Quesenek

New Member
Jul 29, 2019
396
0
0
Then, you should use a "not detectUp()" condition, it would stop as soon as there are no blocks above, and leaves decay isn't fast enough to outrun a turtle, at least from my tests.

Don't take my posts the wrong way, your method indeed works as intended, I am just trying to give a few pointers from all the mistakes I made when trying to make a decent logger and miner programs.
I am nowhere as good as many people, so I tend to do many mistakes, and my speech should not be taken too religiously anyway, as I can indeed be completely wrong.
Don't worry about it, I know it was only intended for help. That's what I love about programming, no matter what your approach is to programming there are tons of different ways you can achieve pretty much the same result and they are all correct.

Also don't take my posts the wrong way I like to explain things and a lot of the times it comes out very blunt lol.