Skip to content

Conversation

@SteelFill
Copy link
Contributor

AnimatedPart.SetFrameWrap uses a while loop to gradually step a frame that's gone negative back to the appropriate positive range. While this works fine for frames that are negative by small amounts, I encountered a game freeze when an animation frame was set to an extremely negative value (due to wheel slip, it seems) and the while loop used all processing time to attempt to move the animation frame from a value in the negative billions back to a positive value 12 at a time (ie: requiring hundreds of millions of iterations).

While loops should be used very sparingly and other options that can't hang the entire process should replace while loops where possible. In this case, a simple modulus operation can achieve the same goal of "restore variable to a given range" in a constant number of steps, regardless of how extremely negative the variable originally was.

As pseudocode, the old method was this:

while (frame < 0)
     frame += MaxFrame;
frame %= MaxFrame;

And the new method is this:

frame %= MaxFrame;
if (frame < 0)
     frame += MaxFrame;

The new algorithm produces identical results in dramatically less time, needing to add MaxFrame only once regardless of the magnitude of frame. When frame is negative, frame %= MaxFrame immediately moves frame to the range of -1 to -(MaxFrame - 1) while maintaining the proper "wrap" of the frame counter, so adding on MaxFrame one more time is all that's required to finally restore frame to the range of 0 to (MaxFrame - 1).

For example:

frame = -33                        MaxFrame = 12
Old:                             | New:
frame += MaxFrame (repeatedly)   | frame %= MaxFrame (one time)
-33 += 12                        | -33 %= 12
-21 += 12                        | -9 (not done yet)
-9 += 12                         | frame += MaxFrame (one time)
3 (final result)                 | -9 += 12
                                 | 3 (same final result)

For slightly negative values, this doesn't save much time but I saw frame values in the negative billions, in which case avoiding a while loop is dramatically faster.

Another minor change is that if MaxFrame = 0, the old code would produce frame = NaN as it would run frame %= MaxFrame even in this case, but now that calculation is skipped instead, avoiding the NaN which could cause other problems.

I'm also wondering if this caused the problem mentioned in this bug report as I encountered this when wheel slipping, and an adhesion change was introduced before the bug was reported.

@SteelFill SteelFill added the bug Something isn't working label Jun 27, 2025
twpol pushed a commit that referenced this pull request Jun 27, 2025
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1107 at f5eb3dc: Fix DPMode when last remote is moved to front.
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced.
- Pull request #1115 at 01c0838: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1104 at f9691cf: Handle simple adhesion within the axle module
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 27, 2025
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1107 at f5eb3dc: Fix DPMode when last remote is moved to front.
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced.
- Pull request #1115 at 01c0838: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1104 at 128520d: Handle simple adhesion within the axle module
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 27, 2025
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1107 at f5eb3dc: Fix DPMode when last remote is moved to front.
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced.
- Pull request #1115 at 01c0838: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1104 at 03ccdc6: Handle simple adhesion within the axle module
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 27, 2025
- Pull request #1104 at 03ccdc6: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1107 at f5eb3dc: Fix DPMode when last remote is moved to front.
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced.
- Pull request #1115 at 01c0838: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 27, 2025
- Pull request #1104 at 03ccdc6: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1107 at f5eb3dc: Fix DPMode when last remote is moved to front.
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced.
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 28, 2025
- Pull request #1104 at 03ccdc6: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced. (r1.6)
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 28, 2025
- Pull request #1104 at 03ccdc6: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced. (r1.6)
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1118 at f1e0f35: Fix DPMode when last remote is moved to front. (r1.6)
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 30, 2025
- Pull request #1104 at f15bb6b: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced. (r1.6)
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1118 at f1e0f35: Fix DPMode when last remote is moved to front. (r1.6)
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jun 30, 2025
- Pull request #1104 at f15bb6b: Handle simple adhesion within the axle module
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced. (r1.6)
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1118 at f1e0f35: Fix DPMode when last remote is moved to front. (r1.6)
- Pull request #1119 at 57243f0: Re-add missing PR #1103
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jul 1, 2025
- Pull request #1104 at f15bb6b: Handle simple adhesion within the axle module
- Pull request #1057 at 1c2bcb4: Switchable brake system
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1114 at 9a4a873: Fix color of DB in DrivingInfo when in setup with DPU fenced. (r1.6)
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1118 at f1e0f35: Fix DPMode when last remote is moved to front. (r1.6)
- Pull request #1119 at 57243f0: Re-add missing PR #1103
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
twpol pushed a commit that referenced this pull request Jul 1, 2025
- Pull request #1104 at f15bb6b: Handle simple adhesion within the axle module
- Pull request #1057 at 1c2bcb4: Switchable brake system
- Pull request #1086 at e10390b: Add Settings Exporter tool (copy settings to INI, etc)
- Pull request #1091 at 2391bc0: Automatic speed control
- Pull request #1110 at 387388e: Fix Activity Runner persists after loading exception
- Pull request #1115 at 270f22f: Do not activate ETS switch if no suitable cars are attached
- Pull request #1116 at 721d118: Fix Potential Hang in AnimatedPart.SetFrameWrap
- Pull request #1118 at f1e0f35: Fix DPMode when last remote is moved to front. (r1.6)
- Pull request #1082 at 5845a1a: Allow variable water level in glass gauge
- Pull request #1081 at 689494b: Brake cuts power unification
@twpol
Copy link
Member

twpol commented Jul 1, 2025

Thanks for the PR description with more lines than the code change, genuinely useful for review 👍

@twpol twpol merged commit 47e9b25 into openrails:release/1.6 Jul 1, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Development

Successfully merging this pull request may close these issues.

2 participants