Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamenetwork): give the self-slug room before the stall it prevents

Olcay Seyganbb7ed134b2ea
diff --git a/CHANGELOG.md b/CHANGELOG.md
index df3352a42..a86981117 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -231,6 +231,15 @@ map. Ctrl+A is still everything at once.
whenever an order arrived for a moment that had already passed — and the answer it produced was
read as the largest margin possible, so the brake stayed off at precisely the moment it was
needed. It is measured properly now.
+- **And that brake now comes on early enough to be worth having.** It was set to wait until the
+ margin was down to a single frame — a thirtieth of a second, less time than it takes to tell the
+ other machines anything at all. By then the freeze it exists to prevent has already happened.
+ Working out why turned up something worth knowing: the amount the game runs ahead of what you see
+ is supposed to scale with how bad your connection is, but the formula only ever exceeds its own
+ minimum once the two worst connections in the room add up to well over half a second of lag — a
+ game nobody finishes. So every match anyone actually plays runs at that minimum, and the safety
+ margin it was given works out to that one frame. The brake starts twice as early now, which is
+ the difference between easing off and lurching.
## Sharper textures, for free
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index d2578a173..568256e1d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -42,4 +42,18 @@ Int frameCushion( UnsignedInt executionFrame, UnsignedInt currentFrame );
yet, which is not the same as no margin - see the sentinel note in CushionMetrics.cpp. */
Bool shouldSelfSlug( Int minimumCushion, Int runAhead, UnsignedInt slackPercent );
+/** The cushion, in frames, below which the self-slug engages. */
+Int selfSlugThreshold( Int runAhead, UnsignedInt slackPercent );
+
+enum
+{
+ /* The brake needs room to work. NetworkRunAheadSlack is 10 %, and MIN_RUNAHEAD is 10 frames -
+ and that floor is what the run-ahead actually is for every link anyone plays on: the formula
+ in ConnectionManager::updateRunAhead is (lat1 + lat2) / 2 * minFps * 1.1, which at 30 fps
+ does not clear 10 until the two worst average round trips add up to about 600 ms. So the
+ threshold was 10 * 10 % = one frame, and a brake that engages with one frame of margin left
+ is not a brake, it is the stall it was meant to prevent. Two frames is 66 ms at 30 Hz. */
+ SELFSLUG_MIN_THRESHOLD_FRAMES = 2,
+};
+
#endif // _CUSHION_METRICS_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index df3bf31f4..87e525e51 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -50,9 +50,18 @@ Bool shouldSelfSlug( Int minimumCushion, Int runAhead, UnsignedInt slackPercent
if( runAhead <= 0 )
return FALSE;
+ return minimumCushion < selfSlugThreshold( runAhead, slackPercent );
+}
+
+Int selfSlugThreshold( Int runAhead, UnsignedInt slackPercent )
+{
/* The threshold is the slack the run-ahead was given: once the margin has eaten into that, the
- next hiccup is a stall rather than a wobble. */
+ next hiccup is a stall rather than a wobble. With a floor, because the slack is a
+ percentage of a run-ahead that is pinned to MIN_RUNAHEAD for every link in practice - see
+ SELFSLUG_MIN_THRESHOLD_FRAMES. */
Int threshold = (Int)((runAhead * (Int)slackPercent) / 100);
+ if( threshold < SELFSLUG_MIN_THRESHOLD_FRAMES )
+ threshold = SELFSLUG_MIN_THRESHOLD_FRAMES;
- return minimumCushion < threshold;
+ return threshold;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 577829d4e..3400677da 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2361,10 +2361,46 @@ TEST(selfslug_fires_once_the_margin_eats_into_the_slack)
CHECK( shouldSelfSlug( 5, 60, 10 ) );
CHECK( !shouldSelfSlug( 6, 60, 10 ) );
- // and a run-ahead too small for the slack to round to a frame never slugs, which is right:
- // there is no margin to protect
- CHECK( !shouldSelfSlug( 0, 9, 10 ) );
- CHECK( shouldSelfSlug( 0, 10, 10 ) );
+ // a run-ahead too small for the slack to round to a whole frame still gets the brake: see
+ // selfslug_threshold_has_a_floor_the_shipped_run_ahead_cannot_undercut
+ CHECK( shouldSelfSlug( 0, 9, 10 ) );
+ CHECK( shouldSelfSlug( 1, 9, 10 ) );
+ CHECK( !shouldSelfSlug( 2, 9, 10 ) );
+}
+
+TEST(selfslug_threshold_has_a_floor_the_shipped_run_ahead_cannot_undercut)
+{
+ /* ConnectionManager::updateRunAhead computes (lat1 + lat2) / 2 * minFps, adds
+ NetworkRunAheadSlack percent, and clamps to at least MIN_RUNAHEAD (10). Run the numbers at
+ the shipped 30 fps and the clamp is not an edge case, it is the answer: two players at 100 ms
+ average round trip give 1.6 frames, eight players at 300 ms give 9.9 - the formula does not
+ clear 10 until the two worst round trips add up to about 600 ms. So the run-ahead every real
+ game plays on is 10, its 10 % slack is one frame, and a brake that waits until one frame of
+ margin is left has already lost: at 30 Hz that is 33 ms of warning for a hitch that takes
+ longer than that to signal, let alone correct. */
+ CHECK_EQ( selfSlugThreshold( 10, 10 ), SELFSLUG_MIN_THRESHOLD_FRAMES );
+
+ // the floor holds wherever the arithmetic would land under it, including at zero slack
+ CHECK_EQ( selfSlugThreshold( 10, 0 ), SELFSLUG_MIN_THRESHOLD_FRAMES );
+ CHECK_EQ( selfSlugThreshold( 1, 10 ), SELFSLUG_MIN_THRESHOLD_FRAMES );
+ CHECK_EQ( selfSlugThreshold( 19, 10 ), SELFSLUG_MIN_THRESHOLD_FRAMES );
+
+ // and gets out of the way as soon as the configured slack is worth more than it
+ CHECK_EQ( selfSlugThreshold( 30, 10 ), 3 );
+ CHECK_EQ( selfSlugThreshold( 64, 10 ), 6 );
+ CHECK_EQ( selfSlugThreshold( 20, 50 ), 10 );
+
+ // the floor is what shouldSelfSlug actually uses - no second copy of the arithmetic
+ for( Int runAhead = 1; runAhead <= 64; ++runAhead )
+ {
+ Int threshold = selfSlugThreshold( runAhead, 10 );
+ CHECK( threshold >= SELFSLUG_MIN_THRESHOLD_FRAMES );
+ CHECK( shouldSelfSlug( threshold - 1, runAhead, 10 ) );
+ CHECK( !shouldSelfSlug( threshold, runAhead, 10 ) );
+ }
+
+ // a floor is a floor, not a slug-always: the brake still lets go
+ CHECK( !shouldSelfSlug( SELFSLUG_MIN_THRESHOLD_FRAMES, 10, 10 ) );
}
TEST(selfslug_is_monotonic_in_the_cushion)