aliens

Author Topic: [DONE][Suggestion] Don't shuffle discovered section in sav file  (Read 852 times)

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
[DONE][Suggestion] Don't shuffle discovered section in sav file
« on: September 14, 2024, 10:58:58 pm »
Whenever you save your game (after passing a bit of time), the "discovered" section (string list of discovered research topics) gets randomly shuffled, making it impractical when trying to compare differences of two save files.

It would make my life easier if this shuffling didn't occur. Either by sorting this list on game save, or if the newly researched topics were always added to the bottom of the list and then remained in the order they were added.

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [Suggestion] Don't shuffle discovered section in sav file
« Reply #1 on: September 15, 2024, 11:13:57 am »
Just FYI, we don't shuffle it randomly, there's no reason to do that.
The list is ordered by internal ID to allow binary search.

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [Suggestion] Don't shuffle discovered section in sav file
« Reply #2 on: September 15, 2024, 04:04:09 pm »
Hmm. I'm checking the code, but I can't find where this internal ID is.

In the SavedGame.cpp
it's calling sortReserchVector(_discovered);,
which calls std::sort(vec.begin(), vec.end(), researchLess); where researchLess is the element comparator function,
which compares the two elements by calling std::less<const RuleResearch *>{}(a, b);

I don't understand this. The RuleResearch class doesn't overload the < operator, so how exactly are two RuleResearch objects compared?

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [Suggestion] Don't shuffle discovered section in sav file
« Reply #3 on: September 15, 2024, 04:09:08 pm »
They are compared by the numeric value of the pointer to the object.

I used the word ID because I didn't want to explain all this.

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [Suggestion] Don't shuffle discovered section in sav file
« Reply #4 on: September 15, 2024, 05:14:39 pm »
numeric value of the pointer

Oof... but, that numeric value is inherently random because that's how memory allocation works. Every time you re-launch the game, the raw memory address values of objects get randomized (technically they should also get reversed because the last added element has the lowest address on the heap, which would give it the first place on the list).

Yeah, it would be nice if this was changed to something more stable.

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [Suggestion] Don't shuffle discovered section in sav file
« Reply #5 on: September 15, 2024, 05:40:43 pm »
It's not going to change, because that's exactly what we need.

For the purpose of saving (only), I can do something different.

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #6 on: September 21, 2024, 05:46:27 pm »
Done.

In options.cfg, set `oxceSortDiscoveredVectorByName: true`

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #7 on: September 21, 2024, 09:34:11 pm »
Why does it have to be a setting? The order doesn't affect compatibility...

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #8 on: September 21, 2024, 09:53:28 pm »
Saving is slow enough already, no need to make it any slower.

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #9 on: September 21, 2024, 10:29:37 pm »
??

It takes 0.01ms to sort an array of 1000 20-random-character strings.

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #10 on: September 21, 2024, 10:34:33 pm »
Then I saved 0.01ms.
Job well done.

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #11 on: September 21, 2024, 11:11:18 pm »
True, saving 0.01ms is a great achievement, great job. Say, wanna make saving the game even faster? Then why not use a std::set instead of a std::vector for SavedGame->_discovered? ;)

Online Meridian

  • Global Moderator
  • Commander
  • *****
  • Posts: 9078
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #12 on: September 21, 2024, 11:24:26 pm »
Sure, if you'll prove to me it will make standard saving even faster, I'll consider it.

Online Delian

  • Colonel
  • ****
  • Posts: 492
    • View Profile
Re: [DONE][Suggestion] Don't shuffle discovered section in sav file
« Reply #13 on: September 22, 2024, 01:11:29 am »
Ok ok, I was lying, I admit it. It wouldn't be faster, it would be the same speed, because traversing a set and a vector is the same. The only difference is that set itself is always sorted.