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

lua coding and a tree farm

Started by iceman11a, 24 April 2014 - 11:35 AM
iceman11a #1
Posted 24 April 2014 - 01:35 PM
I started playing with CC about 3 weeks ago. I been trying to come up with an lua code that will plant and farm trees. I have been able to setup some thing to plant trees. I haven't been able to come up with some thing that will grow them and then get the wood from them and put them in a chest.

Does some one know how to do this (Very Basic) sample on how to do this.

I all so had an idea for an update. for CC and I wanted to see if I could get it added, I can't post in the forum for this.

My idea was that coping files and putting it together just sucks. Losing your code when you exit the game or deleting it. My idea what to add some thing like this.

On the players PC. a new folder. In the folder there are text files that have lua code all ready make. The text files can have any name they want to use. then right click on a turtle or a computer and type in

load openFileName SaveFileName

Sample. load start1 start1

1) load – The command used to load the lua file in
2) openFileName – This is the name of the text file to load in.
3) saveFileName – this is the name of the file to save your lua code in from the file you loaded in

then do a dir and you will see a new file created that contains the lua code from the file you loaded.
Then you can run it, edit the file. or even delete it. Don't worry you can load it in at any time.
CometWolf #2
Posted 24 April 2014 - 04:15 PM
A tree farm should be fairly basic, something like move in a set pattern, and using turtle.compare to check for logs. If a log is found, dig it, move forward, and countinue digging up while there are still logs above. Maybe throw in some spin scans going upwards to look for branches. Once there are no more branches, select a slot with leaves in it, and just go crazy scanning and digging for leaves. Then return to the ground and use turtle.suck in a 4x4 area from where the tree trunk was, then select whatever slot you use for saplings, and replant. Should be it really, if you want some actual code, id suggest looking around in the programs section. Plenty of tree farm programs there.

AS for your suggestion… wat? You realize you can easily upload and download codes directly from pastebin.com, right? Aswell as any code stored on your computer is stored as pure text file in your minecraft save folder. You can even edit them externally while you're playing the game.
Lyqyd #3
Posted 24 April 2014 - 05:49 PM
Good suggestions above, except:

1. Sideways logs and vertical logs compare as false.
2. Sheared leaves and grown leaves compare as false.
iceman11a #4
Posted 24 April 2014 - 07:45 PM
Sorry, you 2 have me all confused. I did find some on the wiki, and they didn't work. I tried editing them, I didn't know what I was doing.

Yes. It is very basic. The problem is figuring it out. How to code it. That's where the problem is. The other problem is that they need to be refueled. I did setup an function for this. It works. Like I said. The problem is getting the basic code to work the way you want it to work.

going up and down and left and right is not the problem. It's when to do these commands. One too many turnRight() and it could go to china. I need some thing that can plant 8 trees in 4 rows. and then watch them and maintain them. When a tree is there. It gets dug up. The other problem is how to and when to get the turlte to empty the inventory out and get more fuel.

so I would need the turtle. to come up to 2 chests. One for fuel and the other to drop the inventory off. This is where I get so confused at.
Bomb Bloke #5
Posted 25 April 2014 - 02:45 AM
We can't comment ony where you're going wrong without seeing the code involved. Maybe dump your current progress on Pastebin and provide a link to it here.

Regarding file editing, if a given computer/turtle has had files saved onto its drive, then if you look in your MineCraft save folder you'll find a "computer" folder. There should be a folder in that named after whatever the computer/turtle's ID number is (just type "id" into your turtle's command prompt to get that). You'll find the system's files stored there, and you can edit/add/remove them as you please.

If it helps, assuming you're using basic "for" loops to get the turtle up and down the rows, a simple odd/even check on your counter variable will tell you which way to turn at the end of each one. A loose example:

for y=1,16 do
  for x=1,16 do
    (move forward, chop trees, whatever)
  end

  if bit.band(y,1) == 1 then
    (turn left)
  else
    (turn right)
  end
end

Long story short, "bit.band(y,1)" will be 1 if y is odd, or 0 if y is even.

My preferred method of harvesting involves having a turtle wander through a tesselated pattern of trees, hovering just above the saplings it plants:

0S0000S0
000S0000
S0000S00
00S0000S
0000S000
0S0000S0
000S0000
S0000S00

I took the tactic of not coding the turtle with information as to where the saplings are - all it knows is that it needs to wander up and down the rows, turning left or right at the end of them. It attempts to plant saplings in every square it passes over (generally failing, as only the squares where saplings are desired contain dirt - the rest initially comprise of pits with torches, later replaced with glowstone), while chopping through everything it encounters.

If it detects a block underneath itself, and a block above itself, then it knows it has entered a grown tree. It then digs upwards until it encounters air, spinning and digging around itself as it goes (effectively "coring" the tree). This leaves lots of spare leaves / logs around the place, but due to the tesselation (and the fact that oak trees are quite happy to grow through other oak trees), that's not a problem and nearly everything gets harvested eventually.

It need only use "compare" in order to tell the difference between saplings and logs underneath it. Anything else it encounters in the planting grid it automatically assumes is fair game to cut through.

This pattern also happens to provide enough new saplings that the turtle will never run out, though production will slow down considerably if you're using a tree species that refuses to grow over other existing trees. Works extremely well with MFR rubber trees, especially when you consider that the rubber can be used in torch production…

The script itself is a tad complex and will likely confuse you more than it helps you, so I won't link to it here (as in, complex just to use, let alone "read"). My main goal in typing it was to get some wood chopped (… while continuing despite anything up to and including the turtle being destroyed) as opposed to having something to distribute, so it ended up fairly inscrutable.
iceman11a #6
Posted 25 April 2014 - 03:32 PM
Thanks for the info. How ever I can't find it. I mean what you said about the computers id on the hard drive. In the .minecraft folder I have. I have 2 folders.

1) Joe's Test World
2) NEI

That's it. I do this on a server. I don't see an folder for computer.

And for the lua code. At that time I didn't have a lua code. I just started putting one together, as soon as I can find that file. I'll post the code that I have. I have been able to get some of it to work, Just not all of it,
CometWolf #7
Posted 25 April 2014 - 04:38 PM
It's something like .minecraft/worlds/worldname/computer/id
Lyqyd #8
Posted 25 April 2014 - 05:35 PM
If you're playing on a server, the files are stored on the server, not locally.
iceman11a #9
Posted 25 April 2014 - 08:15 PM
Ok, Yes I fount them. I tried to edit one. and it's all in one line. There's no ext that's use for the file. So the OS doesn't see it has a text file.
Do the coders have plans on changing this so that they will be text files. I wanted to be able to edit these files. and it would make them easy for me to post them here all so,
Lyqyd #10
Posted 25 April 2014 - 08:31 PM
They don't need file extensions. They are regular plain text files. You probably have an OS mismatch between the server and your computer. I'd suspect that your computer is running Windows and the other one isn't, so the difference in line endings is causing the file to appear that way on your machine. A better text editor should be able to handle the line endings correctly.
Agoldfish #11
Posted 25 April 2014 - 08:40 PM
I would recommend Sublime Text with the ComputerCraft Package.
iceman11a #12
Posted 25 April 2014 - 09:08 PM
Thank you very much The text editor is working.

here's my code.

shell.run("clear")
local x = 4
local sapling, log, fuel = 1,2,16
local mf = 4
local mb = 1
function refuel()
turtle.select(fuel)
turtle.refuel(5)
end
function cutit()
  turtle.dig()
  turtle.forward()
    while turtle.detectUp() do
    turtle.digUp()
    turtle.dig()
    turtle.up() 
    end
  while not turtle.detectDown() do
  turtle.down()
  end
 
  --turtle.back()
  --turtle.turnRight()
end
function mforward()
  for i = 1, mf do
  turtle.forward()
  end
end
function checkSapling()
turtle.turnRight()
  turtle.select(log)
  if turtle.compare(log) == true then
  cutit()
  turtle.back()
  turtle.select(sapling)
  turtle.place(sapling)
  turtle.turnLeft()
  else
  turtle.select(sapling)
  turtle.dig()
  turtle.place(sapling)
  turtle.turnLeft()   
  end
end
function nextRow()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
end
function Home()
mforward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
end
--Refuel if needed
if turtle.getFuelLevel() < 20 then
refuel()
end
--start main program
print("Program running")
for i = 1, x do
mforward()
checkSapling()
end
nextRow()
for i = 1, x do
mforward()
checkSapling()
end
Home()

The problem I have is on line 43
if turtle.compare(log) == true then

I need to be able to add another to this after this 1st, and it doesn't seem to work, If I don't add the turtle.select(sapling) I can't select one and then the other

I need it some thing like this

if turtle.compare(log) == true and not turtle.compare(sapling) then

This only works. to compare the log, not the sapling. What happens is that the turtle replaces the sapling it just layed down. (When I run the code again. I wanted to be able to add a loop to this so it doesn't end.

I have to send a turtle.select(sapling) in order for the code to work. and I only can use one at a time.
Bomb Bloke #13
Posted 26 April 2014 - 01:22 AM
I see that as line 33…

turtle.compare() doesn't take a parameter. It doesn't matter what you put in the brackets, it'll be ignored: Instead, it simply checks whatever is in the turtle's currently selected inventory slot against whatever block is in front of the turtle.

That said, if you really wanted to do something like "turtle.compare(log)", you could implement a custom function that switches slots and does the compare in one go. It'd look something like this:

location function compare(slotID)
  turtle.select(slotID)
  return turtle.compare()
end

You'd then be able to do these checks in a single line:

if compare(log) and not compare(sapling) then

(Putting aside for now that that's pointless - if it IS a log then you already know it's NOT a sapling…)

Also, be aware that comparing against saplings is unreliable. Saplings change state at some random point before they turn into trees - to the player they look the same, but to a turtle they look "different". Avoid it if you can.

Again, don't forget that turtles can fly! Things get a bit simpler if you have it hover one block above the ground.

One more point for now: "if" statements already check to see whether the condition you pass them is true or not. Thus, "if turtle.compare() == true then" and "if turtle.compare() then" are the same.
iceman11a #14
Posted 27 April 2014 - 05:52 AM
Sorry I didn't know you replied. This forum is not sending me messages letting me know that some one answered.

any way. Well that code of for CC 1.63. so the options do work. like turtle.select(log) or turtle.compare(sapling). I think all this was added in CC version 1.6x.

And way. I need a code that will put 4 trees down in a row and add 4 more trees to the 2nd row. and them maintain it in a loop. I been looking and havn't fount any thing yet.
The problem I have is under standing how to turn and when and where to go next.
Bomb Bloke #15
Posted 27 April 2014 - 08:44 AM
any way. Well that code of for CC 1.63. so the options do work. like turtle.select(log) or turtle.compare(sapling). I think all this was added in CC version 1.6x.

Sorry, but you've gotten a bit confused there - let's go over this again, with a few more details:

In Lua, when you define a function, you can set it up to accept a given number of parameters (values you can pass to it that it'll use to do its job). However, when people use that function, Lua doesn't actually "care" how many parameters they supply to it.

If they supply less then the function creator considered, then Lua assumes that all the missing parameters are "nil". Depending on how the function was coded, this may work, or the function may error out when it tries to use those values.

If they supply more parameters then the function creator considered, then Lua simply ignores them. They get discarded - no errors appear, they just go out the window and the script carries on.

Note "considered", as opposed to "wanted". It is possible for a function author to make his function throw informative errors if the wrong amount of parameters are passed to it, but Lua won't do it for him, and you shouldn't expect it.

So, with this in mind:

turtle.select() expects one parameter, a number. Fail to provide it and it will error out (because you can't select slot "nil").

turtle.compare() expects no parameters. Provide one, it ignores it - it will always compare the block in the currently selected inventory slot to the block in front of it. You can pass it a string containing the full lyrics to bohemian rhapsody for all it cares, the end result is the same.

ComputerCraft 1.6 and later change nothing out of the above. The lesson to take away is that sometimes there's only so much you'll learn from trial and error; I recommend taking a read through the turtle API documentation if you've not come across it thus far.

And way. I need a code that will put 4 trees down in a row and add 4 more trees to the 2nd row. and them maintain it in a loop. I been looking and havn't fount any thing yet.
The problem I have is under standing how to turn and when and where to go next.

Well, at the moment you've got this:

for i = 1, x do
mforward()
checkSapling()
end
nextRow()
for i = 1, x do
mforward()
checkSapling()
end
Home()

At a glance, best I can make out you've already rigged things so that at the end of this process the turtle will be back where it was at the start of it.

So, all you've gotta do is loop the entire segment:

while true do                   -- Start a loop that repeats indefinitely.
	for i = 1, x do         -- Repeat four times.
		mforward()      -- Move forward four times.
		checkSapling()  -- Handle tree stuff.
	end
	nextRow()               -- Move to the beginning of the next row.
	for i = 1, x do
		mforward()
		checkSapling()
	end
	Home()                  -- Move back to the start position.
	
	-- Maybe throw in code at this point to drop off / refuel.
end
iceman11a #16
Posted 27 April 2014 - 02:36 PM
Hi! BB, thanks for the idea. and the link for the API code for turtles. That list should give me all the commands for turtles.

Look at my new code.

As you can see I changed that function checkSapling(), This does work, The problems I will have is other animals and players getting in front of the turtle. This puts the turtle in a spot and stop. it The only thing I can do is put a fence a round it and put sighs up. lol.

The problem is that the alinement is off. This won't be to big of a problem. I just wanted every thing in a row. It does the 1st row fine. It's the 2nd row that doesn't match up.

My question to you is. If some thing should get in it's way. No matter what, It will stop the turtle. Is there an option ( I was looking for a command or block for this) to send it Home() or an option that if some thing happens to the turlte. that it will go back home to it's starting position.

Like I said, that if a player or some thing should stop the turtle. The Turtle will stop. How ever the program keeps running. even if the turtle is not doing any thing. This is what my testing shows me. It does happen. So my idea is to get another block added that can go under the turtle. So that the turlte is on top of it. Then link the turlte. and the block together. If some thing happens that the turtle just stops. A function can tell it to go home.

As for your code. The while true do is some thing I wanted to add. I can't do that until I get me code working right. How ever I need a way to pass parms from and to functions. Is this possible. I was told it was. The idea is I wanted to do some thing like this.

function mforward(x)

for i = 1, x doe

end

end

mforward(1)

I guess you get the idea and thanks again for your help.







-- advance lumber jack
shell.run("clear")
local x = 4
local sapling, log, fuel = 1,2,16
local mf = 4
local mb = 1
function refuel()
turtle.select(fuel)
turtle.refuel(5)
end
function cutit()
  turtle.dig()
  turtle.forward()
    while turtle.detectUp() do
    turtle.digUp()
    turtle.dig()
    turtle.up() 
    end
  while not turtle.detectDown() do
  turtle.down()
  end
 
  --turtle.back()
  --turtle.turnRight()
end
function mforward()
  for i = 1, mf do
  turtle.forward()
  end
end
function checkSapling()
turtle.turnRight()
  turtle.select(log)
  if turtle.compare(log) == true then
  cutit()
  turtle.back()
  turtle.select(sapling)
    if not turtle.compare() then
    turtle.dig()   
    turtle.place(sapling)
    end
  turtle.turnLeft()
  else
  turtle.select(sapling)
    if not turtle.compare() then
    turtle.dig()
    turtle.place(sapling)
    end
  turtle.turnLeft()   
  end
end
function unLoad()
end

function nextRow()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnLeft()
end
function Home()
mforward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.forward()
turtle.forward()
end
--Refuel if needed
if turtle.getFuelLevel() < 20 then
refuel()
end
--start main program
print("Program running")
for i = 1, x do
mforward()
checkSapling()
end
nextRow()
for i = 1, x do
mforward()
checkSapling()
end
Home()
upLoad()

Bomb Bloke #17
Posted 27 April 2014 - 03:36 PM
I ended up spotting this thread before your new post here, and ended up mostly answering your question there…

Anyway, yes, that's more or less how you define a function that takes a parameter. Here's another example:

local function goForward(moveAmount)
	for i=1,moveAmount do
		while not turtle.forward() do
			turtle.attack()
			turtle.dig()
		end
	end
end

If you then wanted to make the turtle move ahead four blocks, you could run:

goForward(4)

… and assuming it doesn't run out of fuel, four blocks it'll be.

Note that even if it DOES run out of fuel, the script won't continue past the "goForward" function call. The turtle will just sit there attempting to attack and dig over and over again. More checks would be required to have it figure out that it's out of juice.
iceman11a #18
Posted 28 April 2014 - 03:44 AM
I think that's why I never replied. I never got a message on this. Any way this doesn't work, For me any way.

function checkit(num)

for i =1, num do – errors here saying "for" needs a number
– do some thing
end

end
Bomb Bloke #19
Posted 28 April 2014 - 04:03 AM
I can't really comment further without seeing the rest of the code - specifically, the stuff after this function definition, where you tried to use it.
iceman11a #20
Posted 28 April 2014 - 12:52 PM
Look at post #16. Look at mforward()

I added that option to that function and it didn't work. The code is there. Check post #16
CometWolf #21
Posted 28 April 2014 - 01:29 PM
You're getting that error in moveForward, even though mf is defined as the number 4? Or is that the old implementation? We'd obviously have to see the one where the error happens.
Edited on 28 April 2014 - 11:34 AM
iceman11a #22
Posted 28 April 2014 - 04:18 PM
Ok, do this

Here's the old one

function mforward()
for i = 1, mf do
turtle.forward()
end
end


now change it like this

function mforward(num)
for i = 1, num do
turtle.forward()
end
end


I get the error of "for" requires a number.
Bomb Bloke #23
Posted 28 April 2014 - 04:24 PM
Stick this in a script and run it:

local function count(amount)
        for i=1,amount do
                print(i)
        end
end

count()

Now stick this in a script and run it:

local function count(amount)
        for i=1,amount do
                print(i)
        end
end

count(4)

Make sense yet?
iceman11a #24
Posted 28 April 2014 - 04:41 PM
Oh, did I miss the local earlier. Sorry I don't remember that. Well with the problems I been having with the minecraft server, no wonder I can't keep track.

Thanks