[MOD 1.1.x] Tree Saplings (Redux)

Topics and discussion about specific mods
Post Reply
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

[MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

Name: Tree Saplings (Redux)

License: MIT
Initial Release date: 2022/05/31
Version: 1.0.5
Current Version Release date: 2022/08/02
Tested with Factorio version: 1.1.59
Category: General
Mod page: https://mods.factorio.com/mod/TreeSaplings-Redux

Preface note: I’m in the market for a graphic artist to improve/replace the graphics in use with this mod. I’m a coder, not an artist. I’ve done the best I can with scaling existing assets and cropping/merging some images I got online that, imo, is passable, but I’d love to make it better.

This is an update of Tree Saplings (Revisited+RevivePatch), which was an update of Tree Saplings (Revisited), which in turn was an update of Tree Sapling Mod. I’ve fixed several bugs, made significant improvements to UPS, and added several new features.

The basic premise of the mod is to allow growing of trees (and even automate farming them for wood) via craftable saplings.

The whole process starts with petroleum gas and water in a chemical plant to create ammonia. Ammonia is then combined with (more) water to create nitric acid, and then both are combined to create fertilizer. Finally, the fertilizer is combined with wood in an assembler to create your sapling grow bag that you can either plant manually, or place in a planter box to allow it to grow. Saplings manually planted will grow into full grown vanilla trees, at which point you can either leave them for their pollution absorbing properties, or you can harvest them for their wood. Saplings placed in planters will automatically be harvested for their wood as soon as they reach full growth.

And yes, saplings will absorb pollution as they are growing, but they are not as efficient as a full-grown tree. Saying that, I want to make this point: if you're using an automated grow farm, the point behind it is the production of wood. If your goal is pollution absorption, you should manually plant them and then leave the resulting adult trees alone after. Makes sense, yes?

This mod would also pair well with other mods that give a use for wood, like a wood gasification mod, like this one: https://mods.factorio.com/mod/Wood2OilProcessing

For users of the Revisited+RevivePatch and Revisited editions, I have included migration code for switching over to this one. When loading this mod in an existing save with one of these other mods for the first time, please leave all settings from this mod at default, load your save, then make a new save before adjusting the settings to your liking. There are a couple of quirks that happen if you adjust the min/max grow times of the saplings too low before migrating.
New features with this edition of the mod
  • Saplings now have 6 growth stages: planted seed, sprout, seedling, sapling, "young" tree, then the adult full-grown tree.
  • Saplings can now die in environments with too much pollution before reaching maturity (determined by existing game mechanics). Manually planted saplings that die will be converted into a dead tree variant, while saplings in planters will just be harvested for however much wood they were worth when they died.
  • Planters can now be removed/destroyed while leaving the sapling in place (I mean, it's just a wooden frame box laying around the sapling...).
  • Saplings now have a couple snap-to-grid options for manual placement (for the OCD among us...). The traditional no snap/off-grid placement option remains and is default, but it now also includes snap to tile center and snap to tile grid (the space between the tiles) options.
  • There is now a new "UPS Friendly" mode that changes planters so that instead of them having actual, separate sapling entities growing in them, they are now just an animation for the planter. This removes all per entity monitoring of the planters in the code, but has the side effects of all the saplings in the planters looking bland and identical, as well as removing the features of saplings dying (in the planters) in heavily polluted areas and being able to remove the planter and leave the sapling behind. ***Important note:*** It appears as though with animated entities, if you have a lot of them (and possibly even have to have them near each other), the game will automatically offset the animations from each other. My animations are supposed to be of a sapling growing through all of the stages, starting from when the planter starts to when it finishes, but because of this, the animations will be out of sync with the actual planter progress. I have not found a way to correct this.
  • Made the overall costs of the recipes cheaper, giving a net effect of changing the petroleum gas used to saplings produced ratio from 60:1 (manual planting) and 120:1 (for planters) to 6:1 (for both).
Other differences from previous edition of the mod
  • Sapling grow bags are now used directly for manual planting of saplings. As such, the old sapling item and its related recipe and tech unlock have been removed, and the sapling grow bag is now unlocked with the first tech.
  • Productivity modules no longer affect sapling growth in planters. This doesn’t make sense, anyway.
  • Increased (default) sapling grow times from 1-11 minutes to 14-21 minutes (the equivalent of 2-3 in game days).
  • Decreased sapling pollution absorption rate. The previous edition set them to 18x better than a full-grown vanilla tree. Each stage of the sapling grow process has different pollution absorption rates, but all told it averages out to roughly 31% of a vanilla tree over the life of the sapling.
Settings
  • There are 2 startup settings for controlling the minimum and maximum grow times of the saplings. Decreasing these will increase your wood yield over time from your automated grow yards, while increasing them will allow the same infrastructure to support larger grow yards and allow each sapling to absorb more pollution during its life span.
  • Startup setting for the number of ticks in which to spread updating planters over. Decreasing this will decrease the delay between when a sapling grow bag gets placed into a planter and the sapling actually starts growing, while increasing it will reduce the UPS impact it has on your game.
  • Startup setting for controlling the snap to grid/tile center and no snap placement of saplings behavior.
  • Startup setting for toggling UPS Friendly mode.
Technical improvements
  • Massive code rewrite that improves UPS over the previous edition by upwards of 20x. In my test setup, with the previous edition, I experienced UPS drops at around 4k planters with about 3k of them growing saplings. As of this release, I can have roughly 80k planters, with or without saplings, before noticing the same UPS drops. (This was with both mods set for an update interval of 300 ticks.) This code rewrite includes changing the code so it no longer loops over every sapling and instead stores them based on the tick they reach the next stage, allowing faster indexing and processing of only what's needed this tick. Also changed how it loops through and checks available planters, using a different method that appears to be faster and more reliable.
Future plans
  • My next project is a mod that prevents laying of concrete and other artificial tiles under trees and vice versa. Once that mod is released, I will update this mod for support of the new mod.
  • Did I mention I'm looking for better graphics?
  • I want to look into the feasibility of having the tree type the sapling grows into be determined by the tile type it's planted on and auto-placement settings of the trees. It'd be a rather simplistic system (1 sapling type can be any tree type at adult), but I feel that's better than the complexity (both logistically and technically) of having different seed types for each tree type.
  • Add mod support of other mods that add trees and tile types (eg, Alien Biomes)
images
Last edited by FuryoftheStars on Tue Aug 02, 2022 12:37 pm, edited 1 time in total.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

[MOD 1.1.x] Tree Saplings (Redux) Multiplayer Crashing

Post by ThatBitchBooch »

Bugs in the trees oh me oh my.

Game crash bug, caused/replicated by placing wooden box (the tree frame) down anywhere in a multiplayer game, have only tried in one game with one other player but crashes every time. Never tried when another player is online, will test that later today.

Error Text:
The mod Tree Saplings (Redux) (1.0.1) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
invalid key to 'next'
stack traceback:
[C]: in function 'next'
__TreeSaplings-Redux__/control.lua:348: in function <__TreeSaplings-Redux__/control.lua:339>


Hope this helps!

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux) Multiplayer Crashing

Post by FuryoftheStars »

ThatBitchBooch wrote:
Thu Jul 07, 2022 7:56 pm
Bugs in the trees oh me oh my.

Game crash bug, caused/replicated by placing wooden box (the tree frame) down anywhere in a multiplayer game, have only tried in one game with one other player but crashes every time. Never tried when another player is online, will test that later today.

Error Text:
The mod Tree Saplings (Redux) (1.0.1) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
invalid key to 'next'
stack traceback:
[C]: in function 'next'
__TreeSaplings-Redux__/control.lua:348: in function <__TreeSaplings-Redux__/control.lua:339>


Hope this helps!
Thanks. I uploaded a possible fix about a half hour ago (as of when I am posting this). Let me know if it works.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

FuryoftheStars wrote:
Thu Jul 07, 2022 9:22 pm
ThatBitchBooch wrote:
Thu Jul 07, 2022 7:56 pm
Bugs in the trees oh me oh my.

Game crash bug, caused/replicated by placing wooden box (the tree frame) down anywhere in a multiplayer game, have only tried in one game with one other player but crashes every time. Never tried when another player is online, will test that later today.

Error Text:
The mod Tree Saplings (Redux) (1.0.1) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
invalid key to 'next'
stack traceback:
[C]: in function 'next'
__TreeSaplings-Redux__/control.lua:348: in function <__TreeSaplings-Redux__/control.lua:339>


Hope this helps!
Thanks. I uploaded a possible fix about a half hour ago (as of when I am posting this). Let me know if it works.
Works for slightly longer, maybe 2-3 seconds now but still crashes. Error code I believe is the same, just changed numbers. Will post that text below. Thank you for the attempt!

Error Text:
The mod Tree Saplings (Redux) (1.0.2) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
invalid key to 'next'
stack traceback:
[C]: in function 'next'
__TreeSaplings-Redux__/control.lua:342: in function <__TreeSaplings-Redux__/control.lua:329>

Good luck :)

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

ThatBitchBooch wrote:
Thu Jul 07, 2022 11:28 pm
Works for slightly longer, maybe 2-3 seconds now but still crashes. Error code I believe is the same, just changed numbers. Will post that text below. Thank you for the attempt!

Error Text:
The mod Tree Saplings (Redux) (1.0.2) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
invalid key to 'next'
stack traceback:
[C]: in function 'next'
__TreeSaplings-Redux__/control.lua:342: in function <__TreeSaplings-Redux__/control.lua:329>

Good luck :)
Hrm... any chance you can share your save? And then just give quick notes on how to cause the crash.

If the save file isn't too large (I'm not sure what the actual limit is... 100 MB? 200?), you can just attach it to your reply here.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

New issue, crashing on game start (or shortly thereafter) even w/o placing grow boxes.

The mod Tree Saplings (Redux) (1.0.2) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
__TreeSaplings-Redux__/control.lua:316: attempt to call field 'deepcopy' (a nil value)
stack traceback:
__TreeSaplings-Redux__/control.lua:316: in function 'processSaplings'
__TreeSaplings-Redux__/control.lua:350: in function <__TreeSaplings-Redux__/control.lua:329>

Trying to figure out how to drop save, hang on.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

Freindship is fun.zip
(5.96 MiB) Downloaded 28 times
Save file!

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

ThatBitchBooch wrote:
Fri Jul 08, 2022 1:12 am
New issue, crashing on game start (or shortly thereafter) even w/o placing grow boxes.

The mod Tree Saplings (Redux) (1.0.2) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
__TreeSaplings-Redux__/control.lua:316: attempt to call field 'deepcopy' (a nil value)
stack traceback:
__TreeSaplings-Redux__/control.lua:316: in function 'processSaplings'
__TreeSaplings-Redux__/control.lua:350: in function <__TreeSaplings-Redux__/control.lua:329>

Trying to figure out how to drop save, hang on.
Yup. Looking at the code, it makes sense that it should error there, but why it wasn't for me is a mystery. Fixed in 1.0.3.
ThatBitchBooch wrote:
Fri Jul 08, 2022 1:20 am
Freindship is fun.zip
Save file!
First off, love the map labels. :lol:

But as for the error you're getting, as near as I can tell from my understanding of how next works, it should not have been erroring out with what I can see for values. The only way I was able to get it to stop erroring there was to reset one of the variables that is fed to the next statement.

Ok, so you're going to have to do this: when you update to 1.0.3, load your save, place 1 planter, and then immediately mine it. This will trigger a new section of code that will nil out the global.planter_index value. Then you should be able to build normally again from there.
Edit: This is outdated with 1.0.4. See next post. /Edit

If it's ok with you, if I'm unable to figure this out later, I may post a thread in the modding help section and attach your save with it.

But right now I need a bit of sleep. I've been up for 23 hrs and I need to get some sleep before I have to be online for work in... ~3.25 hrs. :P
Last edited by FuryoftheStars on Fri Jul 08, 2022 8:40 pm, edited 1 time in total.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

Ok, after thinking about this some this morning (only got a couple hours sleep, but I feel like I can think better now than when I was working on 1.0.3 :lol: ), I've implemented a "semi automatic" fix for this. Simply put, when the error occurs, reloading your latest save should fix it.

Technical details: The planters, when placed as their actual entity (not a ghost), are stored to a table in global that will persist in the save file. Every tick of the game, I use the "next" statement to loop through a few of the planters in the table, storing the last iterated one's index off in global. When using next, Lua reference says it's safe to remove from the table while iterating over it, but not safe to add. That said, the way I was looping through next I thought would be exempt from this (it's within the for loop, not the for loop itself, so it's not being used as an iterator in that sense).
code
Either way, it seems that on rare circumstances, the next statement can get into a state where it thinks it's actively iterating, even if it's not (such as in a case where the table is empty, the for loop will not execute), and so when you place a planter, it gets added to the table, and then error. However, I've found that if I pass nil for the index argument to next in this case, it won't error and continues on properly.

The problem is that I can't catch this as it's happening and then automatically pass nil. And I can't just always pass nil, otherwise the loop will only ever update the first planter in the table.

So my solution was this: I moved the last iterated planter's index out of global and made it a local var to the file (while still keeping it available to all of the functions in control.lua), so making it that its value will reset to nil every time you load your save. As such, if next gets into this weird state, it'll error, but then when you reload your save, the index will be reset to nil and passed to next like this, allowing it to continue.

I'm going to keep digging at this, though. I want to find a way to prevent this from happening so that the player never even gets the error in the first place.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

FuryoftheStars wrote:
Fri Jul 08, 2022 9:42 am
ThatBitchBooch wrote:
Fri Jul 08, 2022 1:12 am
New issue, crashing on game start (or shortly thereafter) even w/o placing grow boxes.

The mod Tree Saplings (Redux) (1.0.2) caused a non-recoverable error.
Please report this error to the mod author.

Error while running event TreeSaplings-Redux::on_tick (ID 0)
__TreeSaplings-Redux__/control.lua:316: attempt to call field 'deepcopy' (a nil value)
stack traceback:
__TreeSaplings-Redux__/control.lua:316: in function 'processSaplings'
__TreeSaplings-Redux__/control.lua:350: in function <__TreeSaplings-Redux__/control.lua:329>

Trying to figure out how to drop save, hang on.
Yup. Looking at the code, it makes sense that it should error there, but why it wasn't for me is a mystery. Fixed in 1.0.3.
ThatBitchBooch wrote:
Fri Jul 08, 2022 1:20 am
Freindship is fun.zip

Save file!
First off, love the map labels. :lol:

But as for the error you're getting, as near as I can tell from my understanding of how next works, it should not have been erroring out with what I can see for values. The only way I was able to get it to stop erroring there was to reset one of the variables that is fed to the next statement.

Ok, so you're going to have to do this: when you update to 1.0.3, load your save, place 1 planter, and then immediately mine it. This will trigger a new section of code that will nil out the global.planter_index value. Then you should be able to build normally again from there.

If it's ok with you, if I'm unable to figure this out later, I may post a thread in the modding help section and attach your save with it.

But right now I need a bit of sleep. I've been up for 23 hrs and I need to get some sleep before I have to be online for work in... ~3.25 hrs. :P
Glad you enjoyed them, didn't even think about those lol. We have fun here.

As far as the mod fix, I'll hop on after I get off today and tinker with it, looking forward to having saplings again. Got some forests I'm looking to give steroids to.

Feel free to share the map if it helps, ignore the fustercluck in belts and arrangements as it's basically a first world. If there's anything else I can help with lemme know. Will post update later once I test it out, thanks for all the effort!

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

ThatBitchBooch wrote:
Fri Jul 08, 2022 5:13 pm
As far as the mod fix, I'll hop on after I get off today and tinker with it, looking forward to having saplings again. Got some forests I'm looking to give steroids to.
Sounds good. Just keep in mind my instructions on what to do in that post you quoted is out of date. With 1.0.4 of my mod, you should be able to just load your save and viola, it'll be fixed. *crosses fingers* And, if for any reason it comes back later, simply reloading from your most recent save should fix it.

Hopefully, though, you never see this error again.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

FuryoftheStars wrote:
Fri Jul 08, 2022 8:43 pm
ThatBitchBooch wrote:
Fri Jul 08, 2022 5:13 pm
As far as the mod fix, I'll hop on after I get off today and tinker with it, looking forward to having saplings again. Got some forests I'm looking to give steroids to.
Sounds good. Just keep in mind my instructions on what to do in that post you quoted is out of date. With 1.0.4 of my mod, you should be able to just load your save and viola, it'll be fixed. *crosses fingers* And, if for any reason it comes back later, simply reloading from your most recent save should fix it.

Hopefully, though, you never see this error again.
Two days of testing and so far it's worked like a charm. No issues, the trees are back, life is good. Time to clean up some bugs and name new lands :) Thanks for the work and quick turnaround


ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

Ok, first off I promise I'm not trying to break the mod. I adore this thing. However, I think something in it is causing desync problems, if that's possible. Had three instances now where upon trying to join the server, second player constantly and repeatedly desyncs and is unable to stay connected. As soon as saplings is disabled, desync issues go away. No error codes, both parties versions are up to date.

Help me, senpai :(

Also, for clarity, I don't think we ever got both of us on. Been having the desync issue for about a week, and usually just disable the mod. Finally got around to posting something

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

Does anything show in the logs? I don’t play MP myself, so I’m not sure off hand.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

It just shows as "---- joined/is downloading map" then immediately after downloading "---- disconnected due to desync" rinse and repeat. I'll try to pull the logs if we get on today for ya to peek at. It might be something else, but turning on/off saplings causes the problem to appear/disappear (respectively)

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

Yeah, I’ll need those logs. I really have no way of tracking this down, otherwise, and if the logs are really not giving a reason, then hopefully if I bring it to the devs attention they might be able to add something. I think the recent version of the save where this happens could help, too.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

ThatBitchBooch wrote:
Sun Jul 17, 2022 10:09 pm
It just shows as "---- joined/is downloading map" then immediately after downloading "---- disconnected due to desync" rinse and repeat. I'll try to pull the logs if we get on today for ya to peek at. It might be something else, but turning on/off saplings causes the problem to appear/disappear (respectively)
I've been thinking about this and have a theory. If can please, you and whoever else you are attempting this with, please downgrade your mod version to 1.0.3 and give it another try.

ThatBitchBooch
Burner Inserter
Burner Inserter
Posts: 9
Joined: Thu Jul 07, 2022 7:50 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by ThatBitchBooch »

Sorry for the radio silence, work has been busy. Will try and get the other person on today for the 1.03 test, will post updates. Might run it on 1.04 as well to try and grab logs

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 1256
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [MOD 1.1.x] Tree Saplings (Redux)

Post by FuryoftheStars »

I've also just released a v1.0.5 following someone else's reports on the mod portal of the same issues.

Post Reply

Return to “Mods”