Fix plant growth quantization drift in save compression#936
Closed
MhaWay wants to merge 1 commit into
Closed
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR changes how plant growth is quantized into a byte when saving, replacing a direct Math.Ceiling(... * 255) with a dedicated helper that rounds and clamps values.
Changes:
- Replaced inline plant growth quantization with a new
EncodePlantGrowth(float growth)helper. - Implemented rounding-to-nearest and explicit clamping to
[0, 255]for the encoded growth byte.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+112
to
+118
| int quantized = (int)(growth * 255f + 0.5f); | ||
|
|
||
| if (quantized < 0) | ||
| quantized = 0; | ||
| else if (quantized > 255) | ||
| quantized = 255; | ||
|
|
|
|
||
| private static byte EncodePlantGrowth(float growth) | ||
| { | ||
| int quantized = (int)(growth * 255f + 0.5f); |
Contributor
Author
|
Useless |
notfood
reviewed
May 24, 2026
Member
notfood
left a comment
There was a problem hiding this comment.
I wouldn't call it useless, it does fix an issue, even if it ultimately doesn't affect sync.
For clarity, use Math.Clamp with Byte.MaxValue. Since RimWorld code uses the + 0.5f pattern, it's fine to keep that as-is.
Given how short this is, I think it should be simplified into a one-liner.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This fixes a save compression round-trip issue where plant growth could drift upward after a save/load cycle.
Root cause
I tracked this down while comparing normal hosted saves against the standalone/joinpoint path that goes through SaveAndReload.
With cache re-arming disabled for diagnostics, the payload diff consistently showed plant state changing across a save+reload roundtrip even when session data and queued commands stayed stable. The focused diff showed the same plant entry keeping the same id, HP, age, and flags, but with growthByte increasing by 1 after reload.
This comes from the plant compression format introduced in 2019 in commit e7076df (the save compression change). That commit changed plant growth serialization from a float to a byte using:
That encoding is not round-trip stable, so repeated SaveAndReload cycles can ratchet plant growth upward.
Change
Replace the ceil-based quantization with a round-to-nearest quantization that preserves byte -> float -> byte roundtrips.
Validation