Tree Saplings (Revisited+RevivePatch)


A small mod that adds craftable saplings that grow into vanilla trees, and a way to farm them. This includes a patch to make the mod work in 1.1 when bots, scripts or blueprints create planters/saplings.

Content
2 years ago
1.1
5.50K
Environment

b Control script is wasting grow bags

2 years ago
(updated 1 year, 11 months ago)

(Edit 2: I've gone ahead and released a new version of this mod, with a rewrite of the code that fixes these bugs, improves UPS, and adds several new features: https://mods.factorio.com/mod/TreeSaplings-Redux)

I've noticed that when the crafting process hits 100%, in addition to the sapling grow bag that it consumed for that craft, it also removes 1 additional grow bag and destroys it.

Additionally, if you manually remove any extra grow bags that got inserted, it cancels the current craft.

Oh, and when a second grow bag is inserted into the planter, crafting progress is automatically jumped to 50%.

It looks like the code is manually intervening with the crafting process a lot and I'm not sure why. Is this a hold over from when the original mod was created? You should be good to just let the planter (assembler) do its thing, and only monitoring and intervening for the following situations:

  • Monitoring crafting progress for determining when to create the tree and when to remove it
  • Removing any tree when the planter is mined or destroyed
  • Canceling the crafting process (without a refund) when the tree you create dies or is mined

(EDIT: I think I understand everything better now. Please see my 3rd post for recommendations on fixing the bugs.)

2 years ago
(updated 2 years ago)

(EDIT: Most of this post is obsolete. Please see my next post for updated understanding of the code and recommendations.)


Ok, I've just been playing with the code and I have the following recommendations:

1) Comment out line 209:

item.entity.crafting_progress = 0.0

This is what is causing the extra grow bags to be wasted/destroyed when the crafting finishes and canceling the current craft (wasting that grow bag as well) when you manually remove extras.

2) Change line 141 to:

if item.entity.crafting_progress >= 0.5 then

And subsequently change line 148 to:

if item.entity.crafting_progress < 0.5 then

This changes the presence of the tree in the planter from grow bag count to craft progress, and thus solves another bug where a planter with only 1 grow bag (the one it's currently consuming) never has a tree show.

3) Comment out line 145:

item.entity.crafting_progress = 0.5

This is unnecessary with the changes from #2, and under the old code essentially made it so a planter that gets an extra grow bag would suddenly jump ahead (or back!) to 50% progress.


A few final notes:

I recommend re-evaluating the code on lines 31-32 and 149-153, and under what circumstances they are supposed to trigger. In my limited testing, I was unable to get them to trigger, but I suspect these may need fixing/eliminating as well due to them attempting to manipulate the craft progress, input ingredient counts, and even the output result count.

I also found an unused function in the code starting at line 70 (allow_productivity_if_needed). This can probably be removed unless it was something you were intending on using.

Finally, if you ever get your hands on a decent "young", true sapling sized sprite, it might be good to have that show at least from 25-50% progress, so it's not just an empty planter until 50% and then boom, a big ol' tree. :)

2 years ago
(updated 2 years ago)

Alright, so after a lot more playing in the code, I think I better understand what the mod is trying to do. Please disregard most of the post above.

I now understand why the code was trying to intervene with the crafting process. It was attempting to simulate a random grow time. An issue with the way the code was handling this, though, is that assemblers (the planters) consume their needed ingredients from the input slot when it starts a craft, so any attempt to entity.remove_item is going to affect whatever is standing by in the input for the next craft cycle. Couple this with the fact that setting craft progress manually to 0.0 would delete the consumed item, and now you're consuming 2 grow bags per grow cycle in the planter.

There was also another bug related to the actual saplings growing that was preventing saplings from ever maturing.


I have these new recommendations instead after my latest bout though the code:

1) Comment out or remove line 31. By resetting the crafting progress to 0.0, it resets the craft, deleting the item it was consuming without giving an output. There's no need to then also manually remove an item from the input as this will actually remove the next item queued up, not the one that was being used on the current craft.

2) Change line 141 to:

if item.entity.crafting_progress > 0.0 then

And subsequently change line 148 to:

if item.entity.crafting_progress == 0.0 then

This changes the presence of the tree in the planter from grow bag count to craft progress, and solves manually removing extra grow bags in the planter causing the crafting process to cancel. It also solves another bug where a planter with only 1 grow bag (the one it's currently consuming) never has a tree show (though there is the slight side affect that you'll never see the tree actually disappear if there is another grow bag standing by. This can be fixed by changing 0.0 to something like 0.01 (1%), though the == 0.0 would need to be < 0.01, and > 0 should be >= 0.01).

3) Comment out or remove line 209:

item.entity.crafting_progress = 0.0

It's not needed. With the change to line 148, this should only trigger when crafting_progress is already 0.0.

4) Comment out or remove lines 220, 221, & 223. Change line 222 to:

item.planter.entity.crafting_progress = 1.0

By setting progress to 100% (instead of 0) you kick the planter into finishing the craft for you and the rest in completely unneeded. Has the added benefit of it outputting the actual recipe result, in the event you or another mod make a future change, without also having to update the control script code.

5) Line 161, change the or condition to and. This was preventing the saplings from ever actually maturing, even when planted manually.

6) Change game.surfaces[1] on lines 142 & 231 to instead item.entity.surface and item.planter.entity.surface, respectively. This will support making the mod multi-surface friendly. There are a couple more instances in the ConfigChanged function, but these are fine being left as is due to them being for migration from other mod versions.

7) In addition to the unused function allow_productivity_if_needed at line 70, I also found another unused function right next to it at line 60, table_contains (only used in the allow_productivity_if_needed function).

8) The math for determining sapling grow time:

local randnumber = math.random() * 4 - 2
math.floor(1800 * ((randnumber ^ 3) + randnumber + 12))

Can be simplified into one formula that's less complex:

math.floor(((math.random() * 10) + 1) * 3600)

Or even:

math.floor((math.random() * 36000) + 3600)

These achieve the same result, just the first recommended one is easier to see (and understand) what the upper and lower bounds are in minutes (1 to 11). Though I may suggest upping the lower bound, and maybe lowering the upper. In a personal edit, I have 4 & 4 in place of the 10 & 1 values, making it 4 to 8 minutes (same average of 6 minutes).

9) The "grow-sapling" recipe's energy_required should probably be set to being longer than the upper bound of the grow time by at least 1-2 seconds. Benefit of the planters is they automatically harvest the tree when ready. Otherwise, what's the point in all the manual intervening in the craft time of the planter? (If for recommendation #2 you use something other than 0.0, you may want to adjust the buffer here by an appropriate amount as 1% of 11 minutes is 6.6 seconds.)

10) I'd also recommend either getting rid of the grow bags, or getting rid of the sapling item (and their related recipes). (If getting rid of the sapling, merely move the grow bag to the first tech and give it the place_result of the sapling, and the sapling entity's mining result needs to be the grow bag, and of course change the grow bag's ingredients). It makes sense for the sapling to require fertilizer, and it makes sense for the grow bag to need fertilizer, but it doesn't make sense to use fertilizer twice. This would cut the cost requirements in half (which I think was already steep needing 60 raw petrol for 1 fertilizer (I've made a personal edit that uses 15+15 (30 raw petrol) to 5 fertilizer (with adjusted craft time to match) making it 6 raw petrol to 1 fertilizer)) and eliminate 1 assembling step (and the associated pollution from both).

1 year, 11 months ago

I've gone ahead and released a new version of this mod, with a rewrite of the code that fixes these bugs, improves UPS, and adds several new features: https://mods.factorio.com/mod/TreeSaplings-Redux

New response