News:

SimuTranslator
Make Simutrans speak your language.

trees that spawned seedlings move on their tile after loading

Started by wasname123, Today at 03:09:37 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

wasname123

Hello! Trees that have spawned seedlings jump to a different spot on their tile when the game is loaded.

Since r9954 a tree's offset is calculated from its birth month (baum_t::calc_off()), and the trees on a tile are kept sorted by that offset. When a tree spawns, check_season() makes it four months older (geburt -= SPAWN_PERIOD_LENGTH) but doesn't recalculate the offset or re-sort the tile. The save only has type and age, so loading recalculates both and the tree moves. The order on the tile changes too, and that is also the order in which check_season() runs for those trees, so after a load their random draws for spawning come in a different order.

To reproduce:
  • Start a new pak64 game and let it run until trees start spawning (about two months).
  • Look at a tree that has just spawned seedlings, then save and load. It is now drawn somewhere else on its tile.

In my test save (seed 7, r12418), 184 trees move on loading and the tree order changes on 39 tiles (124 and 162 trees in two other maps). For example on tile (108,120) the tree of age 519 is at offset (-1,-2) and second in the list; after loading it is at (-13,-4) and first.

This is also one of the reasons why saving a just-loaded game gives a different file (topic 20299). On r12117 the tree records alone differed in 140 to 306 lines per save.

Patch against r12418 below. It recalculates the offset and re-sorts the tile when the tree gets older; objlist_t::check_season() works on a copy of the list, so sorting there is safe. With it no trees moved in the same three maps.

diff --git a/src/simutrans/obj/baum.cc b/src/simutrans/obj/baum.cc
index 0b21faf39..bcaf8ea63 100644
--- a/src/simutrans/obj/baum.cc
+++ b/src/simutrans/obj/baum.cc
@@ -213,6 +213,12 @@ bool baum_t::check_season(const bool)
 
  // we make the tree four months older to avoid second spawning
  geburt -= baum_t::SPAWN_PERIOD_LENGTH;
+ // the position on the tile depends on geburt (calc_off), and loading recomputes it from the saved age:
+ // recompute it now, as loading does, and keep the tile's trees sorted (objlist_t::check_season iterates a copy)
+ if(  grund_t *gr = welt->lookup( get_pos() )  ) {
+ calc_off( gr->get_grund_hang() );
+ gr->sort_trees();
+ }
  }
 
  // tree will die after 704 months (i.e. 58 years 8 months)

I couldn't attach files to this post. I can upload the savegame somewhere if it helps.