The International Simutrans Forum

 

Author Topic: Fixes for RNG state  (Read 376 times)

0 Members and 1 Guest are viewing this topic.

Offline ceeac

  • Devotee
  • *
  • Posts: 219
Fixes for RNG state
« on: October 12, 2020, 06:36:48 AM »
This patch fixes 2 bugs:
  • When reloading a game with the default non-scripted passenger AI activated, the game searches for a random road vehicle for the AI, which might be different than the one used before saving. Instead, the default road vehicle needs to be saved and reloaded, falling back to the semi-random search if reloading fails (for example because the pakset has changed and the vehicle is no longer available).
  • To ensure the RNG provides consistent output across a save/reload cycle, the entire state of the RNG needs to be saved. While the state is fairly large (2.5 kB binary uncompressed), the increase in size is small compared to the total size of the saved game (< 0.1%, except for very small maps).

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10439
  • Languages: De,EN,JP
Re: Fixes for RNG state
« Reply #1 on: October 12, 2020, 07:16:28 AM »
The reinitialisation with the saved random seed is enough to ensure that the same sequence of random numbers is created when loading this game again. Ok, they won't be the same if no save/load cycle occurred, but then it does not matter, since those are random numbers and the save load cycle will anyway mess up several things, as discussed previously. Or do I miss something here?

And why does the AI need to save the road vehicle? Variation was intended for this. Also before each building of new routes there is a vehicle search.

Offline ceeac

  • Devotee
  • *
  • Posts: 219
Re: Fixes for RNG state
« Reply #2 on: October 13, 2020, 08:02:50 AM »
My goal is for a save/reload cycle to be both idempotent (i.e. save/reload/save is the same as just saving) and transparent (i.e. save/reload is the same as doing nothing), which is currently not the case. This patch is just a small step towards that goal; I want to make several small patches which can be easily reviewed instead of disappearing for several weeks and coming up with a huge patch which cannot be reviewed as easily.

Eventually, when saving/reloading is both idempotent and transparent, one can use loadsave_t for thorough desync checking (by running the game through a SHA1 message digest instead of using checklist_t; I have a working proof-of-concept of this already), and remove the need for other clients to reload the save when a client joins, as mentioned previously. The latter is especially important for Extended because the save games are much larger, for example on the Bridgewater-Brunel server.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10439
  • Languages: De,EN,JP
Re: Fixes for RNG state
« Reply #3 on: October 13, 2020, 02:06:39 PM »
Such a save will require to stop the map much longer while doing the autosave (i.e. no screen updating and time passing), and also needs implementation of many dialoges saving and reloading without applying their settings.

All this is fixable in principle. But this will be a very very long project ahead.

Maybe this is better done a as branch than in trunk, so there will not be lots of not clearly unrelated changes to the savegame. Since it will not afffect the saving data nor the logic, I think it makes much more sense to add it as one big patch.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4821
  • Languages: EN, DE, AT
Re: Fixes for RNG state
« Reply #4 on: October 13, 2020, 06:20:38 PM »
Maybe this pays off some time? I think it is better to have small patches. As long as the savegame format does not change, i see no problems.

Offline freddyhayward

  • Devotee
  • *
  • Posts: 594
  • Languages: EN
Re: Fixes for RNG state
« Reply #5 on: October 13, 2020, 10:46:41 PM »
Eventually, when saving/reloading is both idempotent and transparent, one can use loadsave_t for thorough desync checking (by running the game through a SHA1 message digest instead of using checklist_t; I have a working proof-of-concept of this already), and remove the need for other clients to reload the save when a client joins, as mentioned previously. The latter is especially important for Extended because the save games are much larger, for example on the Bridgewater-Brunel server.
This would be extremely helpful, so I hope it succeeds.

Offline prissi

  • Developer
  • Administrator
  • *
  • Posts: 10439
  • Languages: De,EN,JP
Re: Fixes for RNG state
« Reply #6 on: October 14, 2020, 02:43:06 AM »
So it seems there is a majority in favour of small patches.

I think you best submit those patches yourself. Check you pm, ceeac.
« Last Edit: October 14, 2020, 02:57:37 AM by prissi »

Offline ceeac

  • Devotee
  • *
  • Posts: 219
Re: Fixes for RNG state
« Reply #7 on: October 14, 2020, 04:44:26 PM »
Thanks! I submitted the changes in r9283 + r9284. (Someone should contact aburch again so the github mirror updates correctly.)