Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: Don't store tree counter in the map array #10018

Merged
merged 1 commit into from
Feb 26, 2023

Conversation

ldpl
Copy link
Contributor

@ldpl ldpl commented Sep 10, 2022

Motivation / Problem

Trees add a lot of entropy to the map array drastically increasing the size of the save file.
According to my estimations, this change decreases the size by 10-15%.

Description

The tree counter is just used to determine each 8th or 16th call to the TileLoop_Trees and that can easily be done without storing it in the map array. I plan to do some follow-up tree patches but this one is so free I decided to PR it separately.

Limitations

It does change the order in which tiles are processed in the tile loop so it's not a perfect replica but it's ok as the rate is still the same. Regression tests had to be updated though as the random state blew up.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@LordAro LordAro added the preview This PR is receiving preview builds label Sep 11, 2022
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 September 11, 2022 06:43 Inactive
JGRennison pushed a commit to JGRennison/OpenTTD-patches that referenced this pull request Oct 1, 2022
(cherry picked from commit 6c3f351d5a4b0d19a1a265f04f44c70c24c19cd2)

See: OpenTTD/OpenTTD#10018
JGRennison added a commit to JGRennison/OpenTTD-patches that referenced this pull request Oct 1, 2022
Copy link
Member

@2TallTyler 2TallTyler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly: Instead of keeping track of when each tree should grow based on a random initial counter, we randomly pick trees to grow and assume that the result is approximately the same? Trees also have a growth/density state, which is not affected.

Is that correct?

src/tree_cmd.cpp Outdated Show resolved Hide resolved
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 December 28, 2022 23:38 Inactive
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 December 28, 2022 23:39 Inactive
@2TallTyler
Copy link
Member

From a discussion on Discord:

@2TallTyler:

JGR It looks like you cherry-picked #10018 for your patchpack — does it do what the tin says? Any oddities to report? 🙂

@JGRennison:

It does what it says on the tin, there's nothing wrong with it that i can see
I haven't made any actual measurements of savegame sizes, however
There isn't any observable change in tree behaviour

...

@ldpl:

tbh randomization in 10018 is kinda temporary and can be improved but I doubt anyone would notice that anyway

@PeterN:

I would :p

@ldpl:

only if you're looking for it on fast forward :p
just don't say it changed in the changelog xDD
or, whoever has fast pc can can try to find some decent looking primes for lcg :p

@PeterN:

I find it odd to consider changing behaviour purely to decrease savegame size. 🤷

@ldpl:

I find it odd to store random values in the savegame :p

...

Based on my understanding of how trees currently work and how this changes that, I think this PR is a worthwhile change. If we can get the same results without bloating the savegame with unnecessary noise, we should. I imagine it would most benefit people joining network games with large maps, which is a known issue. Most network games don't have trees for this very reason.

That said, if the current randomization is "kinda temporary and can be improved" let's wait for that before we merge. 😄

@2TallTyler 2TallTyler added the work: needs more work This Pull Request needs more work before it can be accepted label Dec 29, 2022
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 December 30, 2022 14:37 Inactive
@ldpl
Copy link
Contributor Author

ldpl commented Dec 30, 2022

It was temporary in a sense that I planned more entropy reduction patches for trees that would need a better random/hash anyway. Unfortunately, circumstances changed so I probably won't return to that work for a while.

In any case, this is a fine (and simple) change on its own. Also, I improved hash a little bit so now I can't see any pattern whatsoever even on fast forward. Btw, keep in mind that tile loop adds its own randomization on top of this.

@2TallTyler
Copy link
Member

Sure, fine by me. Except it doesn't build now. 😉

@2TallTyler 2TallTyler removed the work: needs more work This Pull Request needs more work before it can be accepted label Dec 30, 2022
@PeterN
Copy link
Member

PeterN commented Dec 30, 2022

It isn't (and cannot be) the same result, but it might be close enough.

As the usage of the map array has changed, it might be desirable to bump the savegame version and clear the existing data on existing maps, and ensure those bits are cleared when creating tree tiles, otherwise the benefits only appear for new maps.

@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 December 30, 2022 16:49 Inactive
@glx22 glx22 added preview This PR is receiving preview builds and removed preview This PR is receiving preview builds labels Jan 5, 2023
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 January 5, 2023 23:44 Inactive
@@ -720,11 +721,7 @@ static void TileLoop_Trees(TileIndex tile)

if (_settings_game.construction.extra_tree_placement == ETP_NO_GROWTH_NO_SPREAD) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an interesting behavioural question to answer here. Should the grass grow or not?
With this setting on 'no growth and no spread', it previously disabled it for about 87.5% of the tree tiles, and it introduced an 8x speed up for the remaining 12.5% of the tiles. Since either the treeCounter & 7 is actually 7 and it triggers every time because the tree counter does not get updated, or it never triggers as (again) the tree counter does not get updated.

As it stands now the grass is now allowed to grow for 100% of the tiles. If grass should not grow like on 87.5% of the tiles previously, then this check needs to be moved up to just after AmbientSoundEffect(tile);.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say it was introduced in #8415 accidentally so this fixes it. Even though non-growing grass is an interesting feature to have doing that only under the trees is a bug. Also, idk what 87.5% you're talking about, I just tested and no grass grows under trees in 13.0.

src/tree_cmd.cpp Outdated Show resolved Hide resolved
src/tree_map.h Show resolved Hide resolved
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 February 26, 2023 20:55 Inactive
@DorpsGek DorpsGek temporarily deployed to preview-pr-10018 February 26, 2023 21:14 Inactive
@2TallTyler 2TallTyler merged commit b0542c8 into OpenTTD:master Feb 26, 2023
@@ -3190,6 +3189,15 @@ bool AfterLoadGame()
}
}

if (IsSavegameVersionBeforeOrAt(SLV_MULTITRACK_LEVEL_CROSSINGS)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure you want this savegame version? Last is (since not long) now SLV_LINKGRAPH_EDGES (with a wrong comment).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it should indeed have been SLV_LINKGRAPH_EDGES, but given the speed the two savegame versions after SLV_MULTITRACK_LEVEL_CROSSINGS got added to master, I doubt there are many (if any) savegames that are made between NewGRF road stops got added and this was merged. So, in the grand scheme of things it does not really matter.
I fixed the comment with #10471 by just squashing that tiny change into the same savegame version. Similarly, technically not the right thing to do, but essentially harmless.

@ldpl ldpl deleted the remove-tree-counter branch March 3, 2023 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview This PR is receiving preview builds
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants