Page 1 of 1

[MOD 0.13.x] Tree Sapling Mod v0.1.4

Posted: Tue Mar 22, 2016 8:56 pm
by LukeM
Tree Sapling Mod

Note: I am no longer updating this forum page, please go to the factorio mods page for updated details / versions or to let me know of any bugs, or ideas

Description:
This small mod adds saplings that grow into vanilla trees, recipes for saplings, and a technology to unlock them.

Details:
Factorio Version: 0.13.x
Factorio Versions tested: 0.13.20 (not tested in multiplayer)
Latest Mod Version: 0.1.4

Download:
https://mods.factorio.com/mods/LukeM/TreeSaplings

Credits:
Thanks to Shrooblord for the sapling and fertiliser item textures :D
Thanks to ntm for uploading a version for 0.13 while I wasn't here

Changelog:

Version: 0.1.4
Date: 16. 10. 2016
Changes:
- Updated to 0.13.x (apart from probably the multiplayer parts which I haven't been able to test yet)
- Replaced my bad graphics with the much better ones made by Shrooblord (Thanks!)

Version: 0.1.3
Date: 21. 05. 2016
Changes:
- Changed sapling growth code so that the time to grow is calculated when it is placed, rather than every second until it grows

Version: 0.1.2
Date: 22. 03. 2016
Bugfixes:
- Fixed breaking saplings as before they would not stop growing even after destroyed

Bugs:
None currently known, if you find any, please reply and give me as much information about them as possible

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Wed Mar 23, 2016 3:38 am
by ALittleGreenStone
I'm intrigued, but I do not understand. Do these sapling things become vanilla trees eventually?

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Wed Mar 23, 2016 9:19 am
by seronis
Please SPECIFICALLY list the version of factorio you test with. Its useless in the future if someone is using factorio 0.12.37 and they cant figure out that your mod was only tested with 0.12.22 and there was a change in the script API in the meantime.

That aside what is the performance hit on this? Are you running scripts on every individual sapling that check each tick or is it event based in some manner where updates only occur AS a growth actually needs to happen? Thats the difference between dozens of saplings being the max feasible and thousands of saplings being possible.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Wed Mar 23, 2016 3:32 pm
by LukeM
ALittleGreenStone wrote:I'm intrigued, but I do not understand. Do these sapling things become vanilla trees eventually?
Yes, every second after 1 minute, the saplings have a chance to become trees, the chance increases until 61 minutes when the chance is 100%, but it is very VERY unlikely it will take this long, so it almost always takes less than 5 minutes. (The chance is (age in seconds - 60) / 3600 but this is likely to change)
seronis wrote:Please SPECIFICALLY list the version of factorio you test with. Its useless in the future if someone is using factorio 0.12.37 and they cant figure out that your mod was only tested with 0.12.22 and there was a change in the script API in the meantime.

That aside what is the performance hit on this? Are you running scripts on every individual sapling that check each tick or is it event based in some manner where updates only occur AS a growth actually needs to happen? Thats the difference between dozens of saplings being the max feasible and thousands of saplings being possible.
1. Ok, I will add it to the main post
2. The performance hit is relatively low I believe, when you place a sapling it adds it to a sapling table, and then every 60 ticks, (once a second) it loops through the list and for each sapling, generates a random number (most likely the most performance heavy part) and if it is less than ((age in seconds - 60)/3600) grows it into a random tree.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Wed Mar 23, 2016 6:10 pm
by seronis
Thats not the worst possible efficiency but its still far from optimal. You should consider using an event queue instead where each update you only process the saplings that are going to grow and dont run any computations at all on the ones that wont. To do this you would precompute the time it will take a given tree to grow using whatever random number generation you like. But this lets you do just ONE calculation on each sapling rather than a calculation per second until it grows. Once you know the time that the sapling will grow you put it in a table that is indexed by the timestamp, and the value at that index is itself a table that contains all the saplings that will grow on that particular 'tick'. This way each update of your mods main loop you do something equivalent to:

Code: Select all

function tick()
   local saplings = table[now]
   if saplings ~= nil
      //we have a table of 1 or more saplings,  process them!
      for _,sap in pairs(saplings) do
         grow(sap)
      end
      table[now] = nil //finished processing.  purge this group of saplings
   end
end
Its been a year(ish) since i've touched lua so that might be a little bad syntax. 'now' there is meant to be a timestamp representing the current tick.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Thu Mar 24, 2016 8:08 am
by Injen
This is exactly what I was looking for! Thank you so much!

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Thu Mar 24, 2016 5:34 pm
by LukeM
seronis wrote:Thats not the worst possible efficiency but its still far from optimal. You should consider using an event queue instead where each update you only process the saplings that are going to grow and dont run any computations at all on the ones that wont. To do this you would precompute the time it will take a given tree to grow using whatever random number generation you like. But this lets you do just ONE calculation on each sapling rather than a calculation per second until it grows. Once you know the time that the sapling will grow you put it in a table that is indexed by the timestamp, and the value at that index is itself a table that contains all the saplings that will grow on that particular 'tick'.
That seems like a much better idea :) I have no idea why I didn't think of doing it that way in the first place... Probably because I originally had each tree take an exact number of seconds to grow, but then later thought that that was kind of silly, having all saplings grow in the exact same order that they were planted, so added the random part. I will add something similar to what you suggested and will include it in the next version, ty for the help :)
Injen wrote:This is exactly what I was looking for! Thank you so much!
I'm glad that you like it :)

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Fri Mar 25, 2016 10:40 am
by seronis
Right now my main laptop is out of commision and i'm stuck using an Inspirion without a dedicated gpu and only 2gb ram. So im kinda focused on finding mod alternatives that are highly efficient =-) Good luck with those updates.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Thu Mar 31, 2016 6:49 pm
by Speadge
there is a problem with waitex which has some code in it to look for a -1 and -2 filename for tree entities in the base-override.lua:

Code: Select all

--trees are a special case because there is so many files for them. Therefore they are checked here but only if they are of the type tree
if AllowChange("tree") then
	if AllowChange("tree", data.raw["tree"]["tree-01"].variations[1].leaves) then
		for k,trees in pairs(data.raw["tree"]) do
			if trees.variations then
				for k1, treeVariations in pairs(trees.variations) do
					OverrideSprite(treeVariations.leaves)
					local filename = treeVariations.trunk.filename
					filename = string.gsub(filename, ".png", "") -- not good to have it here... oh well
					filename = string.gsub(filename, "__base__", "__WaiTex__")
				
					AddStripes(treeVariations.trunk, nil, nil, 
					{
						{filename.."-1.png", 3, 1},
						{filename.."-2.png", 2, 1}
					})
				end
			end
		end
	end
end

since u are "just" providing a "trunk.png", its missing a trunk-1 and trunk-2 file

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Tue Apr 05, 2016 12:44 pm
by LukeM
Speadge wrote:there is a problem with waitex which has some code in it to look for a -1 and -2 filename for tree entities in the base-override.lua:

Code: Select all

Some code was here
since u are "just" providing a "trunk.png", its missing a trunk-1 and trunk-2 file
I will try to fix that as soon as possible :), ty for telling me about this problem

Edit: The problem here is that WaiTex, when it finds a tree file location, replaces '__base__' with '__WaiTex__' as the rest of the file directories are the same. This does not work with trees from mods, as it cannot change '__base__' with '__WaiTex__' as it is not in the filename (as it begins with __TreeSaplings__ instead) so tries to look for the HD textures inside the other mod's directory, which causes it to crash, as it does not exist. I will try to see if there is anything that I can do to fix this, but it is mostly caused by WaiTex, not TreeSaplings so it may be difficult. I will also try to contact the maker of WaiTex to see if they can fix the problem, as it should be relatively easy for them to do.

Edit 2: Just noticed that they have already responded to the message you put on their forum page, saying that they have already fixed this :)

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Sun May 22, 2016 9:47 am
by vanatteveldt
Is there a way to have construction bots automatically plant saplings in the place of destroyed trees like with landmines?

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Mon May 23, 2016 5:34 pm
by Shrooblord
Hi!

I made new images for the tree sapling and for the ammonium nitrate. :)

The sapling is a combination of in-game tree and leaves, except I cut some branches off, shortened the trunk and made the branches shorter and gave the leaves a young-and-freshly-new green tint (as young plants in real life would have). The ammonium nitrate sprite is my go at making a heap of sort of see-throughish crystals using your original sprite's colour.

Tested them in-game. They look nice and blend in well (that was the intention in any case :P ).

Hope you like 'em and want to incorporate them.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Thu May 26, 2016 2:40 pm
by vaultdweller
This mod does not seem to work in multiplayer and causes a neverending desync loop. It uses a random number generator call when checking if a sapling should turn into a tree; each player generates a different random number, causing the desync. We had to edit that function to make the saplings become trees after a pre-determined interval to get it to work for us.

Factorio is generally a deterministic game, not a probablistic one. If you want probabilities in the mod, then for multiplayer you'll need some way to seed the RNG with a constant that is shared between all players so that all players will have the same RNG output.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Sat May 28, 2016 7:58 am
by seronis
vaultdweller wrote:This mod does not seem to work in multiplayer and causes a neverending desync loop. It uses a random number generator call when checking if a sapling should turn into a tree; each player generates a different random number, causing the desync. We had to edit that function to make the saplings become trees after a pre-determined interval to get it to work for us.

Factorio is generally a deterministic game, not a probablistic one. If you want probabilities in the mod, then for multiplayer you'll need some way to seed the RNG with a constant that is shared between all players so that all players will have the same RNG output.
False. The games rng is a seeded PRNG meaning that at any time every player will always generate the exact same sequence of numbers. It can be a logic issue causing the desync. But the rng itself is mp safe and is used in lots of mods.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Thu Jun 23, 2016 5:44 pm
by vaultdweller
seronis wrote:
vaultdweller wrote:This mod does not seem to work in multiplayer and causes a neverending desync loop. It uses a random number generator call when checking if a sapling should turn into a tree; each player generates a different random number, causing the desync. We had to edit that function to make the saplings become trees after a pre-determined interval to get it to work for us.

Factorio is generally a deterministic game, not a probablistic one. If you want probabilities in the mod, then for multiplayer you'll need some way to seed the RNG with a constant that is shared between all players so that all players will have the same RNG output.
False. The games rng is a seeded PRNG meaning that at any time every player will always generate the exact same sequence of numbers. It can be a logic issue causing the desync. But the rng itself is mp safe and is used in lots of mods.
Okay then. Well, one way or another, this mod can NOT be used in multiplayer as-is, and removing the random interval fixes it.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Sat Jun 25, 2016 12:47 am
by TOGoS
The large bounding boxes (larger than those of full grown trees!) were bothering me, so I made the following change to entity.lua:

Code: Select all

    collision_box = {{-0.3, -0.0}, {0.3, 0.6}},
    selection_box = {{-0.5, -0.2}, {0.5, 1.0}},
This makes them much smaller and centered on the place where they come out of the ground
(there may be a way to offset the image so that the collision_box can be centered on 0,0, but I am new at this and didn't figure it out)

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Mon Jun 27, 2016 1:17 pm
by TheSAguy
Had anyone else experienced MP Desync?
I don't play any MP, but was hoping to use a little of this code in my mod, but dont want to if it might cause desyncs.
Looking at the code, I don't see why there would be though.

Thanks.

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Wed Jul 06, 2016 1:15 pm
by ntm
I updated this mod to work with 0.13.x, feel free to upload it to mods.factorio.com, or I can do it too if you'd no longer like to mantain this mod.

Changes:
  • Now works for 0.13.x
  • moved sapling recipe from nature tab to terrains (alongside concrete)
  • new icon for saplings

Re: [MOD 0.12.x] Tree Sapling Mod v0.1.2

Posted: Sun Oct 16, 2016 1:34 pm
by LukeM
Sorry for not replying to any comments for the past few months, the last time I checked (just before 0.13) this topic had drifted down to about page 4 or 5, so I didnt think there would be anyone using this mod anymore. I have tried to update it to 0.13, and have uploaded it to the factorio mods page, but am not yet able to test the multiplayer thing, as my friend who I usually play factorio with is not online.
Shrooblord wrote:Hi!

I made new images for the tree sapling and for the ammonium nitrate. :)

The sapling is a combination of in-game tree and leaves, except I cut some branches off, shortened the trunk and made the branches shorter and gave the leaves a young-and-freshly-new green tint (as young plants in real life would have). The ammonium nitrate sprite is my go at making a heap of sort of see-throughish crystals using your original sprite's colour.

Tested them in-game. They look nice and blend in well (that was the intention in any case :P ).

Hope you like 'em and want to incorporate them.
Thanks! Your graphics are much better than my ones, and they do, as you said, fit into the game very well. I have added them to the latest version and credited you in the factorio mods page :D
seronis wrote:
vaultdweller wrote:This mod does not seem to work in multiplayer and causes a neverending desync loop. It uses a random number generator call when checking if a sapling should turn into a tree; each player generates a different random number, causing the desync. We had to edit that function to make the saplings become trees after a pre-determined interval to get it to work for us.
...
False. The games rng is a seeded PRNG meaning that at any time every player will always generate the exact same sequence of numbers. It can be a logic issue causing the desync. But the rng itself is mp safe and is used in lots of mods.
vaultdweller: Thanks for letting me know that there is a problem with the newest version
seronis: Thanks for the info on how the random numbers work :D, I think the version that I was using, that I didn't upload as it was such a small change, shouldn't have that issue, but as I said at the top of this post, I can't test it yet
ntm wrote:I updated this mod to work with 0.13.x, feel free to upload it to mods.factorio.com, or I can do it too if you'd no longer like to mantain this mod.

Changes:
  • Now works for 0.13.x
  • moved sapling recipe from nature tab to terrains (alongside concrete)
  • new icon for saplings
Thanks for uploading a fix, at least for singleplayer :D
I will continue to try to maintain this mod as long as I still play factorio, and notice that something needs maintaining, but thanks for the offer

Re: [MOD 0.13.x] Tree Sapling Mod v0.1.4

Posted: Thu Jan 19, 2017 11:33 pm
by JoaoBernardino
Don't abandon this!! I need trees on my base, for aesthetic and pollution reasons!! This is the only mod that actually plant real trees!

Good work!! :D