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): let the room's frame rate climb back after a hitch

Olcay Seyganf7ee4edbc050
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c6aa81b62..1e4420f10 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -245,6 +245,21 @@ map. Ctrl+A is still everything at once.
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.
+- **One player's two-second hiccup used to slow the match down for the rest of the game.** An online
+ game runs everyone at the speed of the slowest machine in the room, which is fair enough while
+ somebody is actually struggling. The problem was getting back out of it. Each machine reports how
+ fast it is running, and a machine that is keeping up reports exactly the speed it was told to run
+ at — so once the room had been dropped to twelve frames a second, twelve was what everyone
+ reported, and twelve was the answer for the rest of the match. Nothing was wrong on anyone's
+ machine any more; the game had simply agreed with itself to stay slow. The original code has a
+ half-built escape hatch for this — the slowest player is allowed to run a fraction faster "just in
+ case they are able to" — but it is handed to whichever player happens to sit in the lowest slot
+ among everyone tied at the bottom, which is almost never the player who was slow, and everyone
+ else stays pinned. Now every machine is given that fraction. Whoever genuinely cannot keep up
+ still holds the room back, exactly as intended, and the moment they recover the speed climbs back
+ on its own — from twelve frames a second to thirty in about six seconds. A room held down by a
+ machine that really is that slow settles just above it and stays there instead of surging and
+ falling back.
## Sharper textures, for free
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index 568256e1d..79f3c11d1 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -54,6 +54,30 @@ enum
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,
+
+ /* The room never runs slower than this, whatever the metrics say. */
+ ROOM_FRAME_RATE_FLOOR = 5,
+
+ /* How far above the slowest reported rate the room is actually told to run. This is EA's own
+ step - they applied it to the slowest player alone, "just in case they are able to" - given
+ to everybody, which is what lets the rate climb at all. Ten percent, the same slack the
+ run-ahead is sized with. */
+ ROOM_FRAME_RATE_PROBE_PERCENT = 10,
};
+/* The room's logic rate is set by the packet router to the slowest rate any player reports, and
+ every machine then paces its own logic on it (Network::timeForNewFrame). The trap is that the
+ rate a player reports is the rate they *achieved*, and a player pinned at the room's rate
+ achieves exactly it - so the reported minimum can never rise above the rate that produced it,
+ and one player's two-second hitch used to hold the whole room at that speed for the rest of the
+ match. The way out is to command a rate slightly above the measured minimum: whoever cannot
+ follow reports short and pins the room honestly, everybody else follows and the minimum climbs
+ with the room until it hits the limit. */
+
+/** The slowest reported rate, brought inside the range the room is allowed to run at. */
+Int settleRoomFrameRate( Int minFps, Int fpsLimit );
+
+/** The rate to actually command: one probe step above the settled rate, never above the limit. */
+Int probeRoomFrameRate( Int settledFps, Int fpsLimit );
+
#endif // _CUSHION_METRICS_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index 1072352dc..124f1c812 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -1266,17 +1266,22 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
Int minFpsPlayer;
getMinimumFps(minFps, minFpsPlayer);
DEBUG_LOG(("ConnectionManager::updateRunAhead - max latency = %f, min fps = %d, min fps player = %d old FPS = %d\n", getMaximumLatency(), minFps, minFpsPlayer, frameRate));
- if ((minFps >= ((frameRate * 9) / 10)) && (minFps < frameRate)) {
- // if the minimum fps is within 10% of the desired framerate, then keep the current minimum fps.
- minFps = frameRate;
- }
- if (minFps < 5) {
- minFps = 5; // Absolutely do not run below 5 fps.
- }
- if (minFps > TheGlobalData->m_framesPerSecondLimit) {
- minFps = TheGlobalData->m_framesPerSecondLimit; // Cap to 30 FPS.
- }
+ /* EA kept the current rate whenever the reported minimum was within 10 % below it, to stop
+ the measurement's own rounding from ratcheting the room down a frame at a time. The
+ probe below does that job (a reported 29 is probed straight back to 30) and the two
+ together oscillate: the band swallows a genuinely slow player for as long as the probe
+ keeps the rate within 10 % of them, so a room held at 12 by one machine walked
+ 12-13-14-15-12 for ever. One mechanism, not two. */
+ minFps = settleRoomFrameRate(minFps, TheGlobalData->m_framesPerSecondLimit);
DEBUG_LOG(("ConnectionManager::updateRunAhead - minFps after adjustment is %d\n", minFps));
+
+ /* The rate everybody is actually told to run at. Deliberately a step above the slowest
+ reported rate: a machine pinned at the room's rate reports exactly that rate back, so
+ without the step the reported minimum can never rise and one player's hitch pins the
+ room for the rest of the match - see CushionMetrics.h. The run-ahead below is left on
+ the settled rate rather than this one: it is the room's honest measured speed, and the
+ formula's result is clamped to MIN_RUNAHEAD for every real link anyway. */
+ Int commandedFps = probeRoomFrameRate(minFps, TheGlobalData->m_framesPerSecondLimit);
Int newRunAhead = (Int)((getMaximumLatency() / 2.0) * (Real)minFps);
newRunAhead += (newRunAhead * TheGlobalData->m_networkRunAheadSlack) / 100; // Add in 10% of slack to the run ahead in case of network hiccups.
if (newRunAhead < MIN_RUNAHEAD) {
@@ -1310,7 +1315,7 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
}
msg->setRunAhead(newRunAhead);
- msg->setFrameRate(minFps);
+ msg->setFrameRate(commandedFps);
//DEBUG_LOG(("ConnectionManager::updateRunAhead - new run ahead = %d, new frame rate = %d, execution frame %d\n", newRunAhead, minFps, msg->getExecutionFrame()));
sendLocalCommand(msg, 0xff ^ (1 << minFpsPlayer)); // Send the packet to everyone but the lowest FPS player.
@@ -1341,17 +1346,14 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
msg2->setExecutionFrame(TheGameLogic->getFrame() + oldRunAhead);
}
- // Let the player with the slowest FPS run a little faster than the other computers...
- // just in case they are able to. Then we might be able to run the game faster which would be good.
- Int newMinFps = (minFps * 11) / 10;
- if (newMinFps == minFps) {
- newMinFps = minFps + 1;
- }
- if (newMinFps > 30) {
- newMinFps = 30; // Cap FPS to 30.
- }
+ /* EA gave this step to the slowest player alone; it now goes to everyone, so the two
+ messages carry the same rate. They stay split because the recipients still have to be
+ split - see the command ID note above, which is about disconnect recovery, not about
+ the frame rate. (The step used to be capped at a hardcoded 30 while the settled rate
+ was capped at FramesPerSecondLimit, so a room configured above 30 slowed its slowest
+ player down to 30 while everybody else ran faster.) */
msg2->setRunAhead(newRunAhead);
- msg2->setFrameRate(newMinFps);
+ msg2->setFrameRate(commandedFps);
sendLocalCommand(msg2, 1 << minFpsPlayer);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index 87e525e51..ee0bbcb12 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -65,3 +65,29 @@ Int selfSlugThreshold( Int runAhead, UnsignedInt slackPercent )
return threshold;
}
+
+Int settleRoomFrameRate( Int minFps, Int fpsLimit )
+{
+ if( minFps < ROOM_FRAME_RATE_FLOOR )
+ minFps = ROOM_FRAME_RATE_FLOOR;
+
+ if( minFps > fpsLimit )
+ minFps = fpsLimit;
+
+ return minFps;
+}
+
+Int probeRoomFrameRate( Int settledFps, Int fpsLimit )
+{
+ Int probed = (settledFps * (100 + ROOM_FRAME_RATE_PROBE_PERCENT)) / 100;
+
+ /* Integer division eats the step below ten frames a second, and a step of zero is the latch
+ this whole function exists to break. */
+ if( probed <= settledFps )
+ probed = settledFps + 1;
+
+ if( probed > fpsLimit )
+ probed = fpsLimit;
+
+ return probed;
+}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 43a6d6d00..011bf5327 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2448,6 +2448,130 @@ TEST(selfslug_threshold_has_a_floor_the_shipped_run_ahead_cannot_undercut)
CHECK( !shouldSelfSlug( SELFSLUG_MIN_THRESHOLD_FRAMES, 10, 10 ) );
}
+/* The room's logic rate. ConnectionManager::updateRunAhead runs on the packet router: it takes
+ the slowest frame rate any player reported, settles it into the allowed range, and broadcasts
+ the result as the rate every machine paces its logic on. The reported rate is the rate a
+ player *achieved*, so a player who is keeping up reports back exactly the rate they were told
+ to run at - which is why the broadcast rate has to be a step above the measured minimum or it
+ can never rise again. simulateRoom below is that whole loop, with each player achieving the
+ smaller of their own capability and the rate they were commanded. */
+
+static Int simulateRoom( const Int *capability, Int numPlayers, Int startRate, Int rounds,
+ Int fpsLimit )
+{
+ Int rate = startRate;
+ for( Int round = 0; round < rounds; ++round )
+ {
+ Int minFps = -1;
+ for( Int player = 0; player < numPlayers; ++player )
+ {
+ Int reported = capability[player] < rate ? capability[player] : rate;
+ if( minFps == -1 || reported < minFps )
+ minFps = reported;
+ }
+ rate = probeRoomFrameRate( settleRoomFrameRate( minFps, fpsLimit ), fpsLimit );
+ }
+ return rate;
+}
+
+TEST(room_frame_rate_settles_into_the_allowed_range)
+{
+ CHECK_EQ( settleRoomFrameRate( 30, 30 ), 30 );
+ CHECK_EQ( settleRoomFrameRate( 17, 30 ), 17 );
+
+ // nobody plays at two frames a second, whatever the metrics claim
+ CHECK_EQ( settleRoomFrameRate( 2, 30 ), ROOM_FRAME_RATE_FLOOR );
+ CHECK_EQ( settleRoomFrameRate( 0, 30 ), ROOM_FRAME_RATE_FLOOR );
+ CHECK_EQ( settleRoomFrameRate( -1, 30 ), ROOM_FRAME_RATE_FLOOR );
+
+ // and the room never runs faster than the game is configured to
+ CHECK_EQ( settleRoomFrameRate( 100, 30 ), 30 );
+ CHECK_EQ( settleRoomFrameRate( 100, 60 ), 60 );
+ CHECK_EQ( settleRoomFrameRate( 45, 60 ), 45 );
+}
+
+TEST(room_frame_rate_probe_always_moves_by_at_least_one_frame)
+{
+ /* The step is what breaks the latch, so a step of zero is the bug. Integer division eats it
+ below ten frames a second: (9 * 110) / 100 is 9. */
+ for( Int settled = ROOM_FRAME_RATE_FLOOR; settled < 30; ++settled )
+ CHECK( probeRoomFrameRate( settled, 30 ) > settled );
+
+ CHECK_EQ( probeRoomFrameRate( 5, 30 ), 6 );
+ CHECK_EQ( probeRoomFrameRate( 9, 30 ), 10 );
+ CHECK_EQ( probeRoomFrameRate( 10, 30 ), 11 );
+ CHECK_EQ( probeRoomFrameRate( 20, 30 ), 22 );
+
+ // but never past the limit, and at the limit it is a no-op rather than an overshoot
+ CHECK_EQ( probeRoomFrameRate( 30, 30 ), 30 );
+ CHECK_EQ( probeRoomFrameRate( 29, 30 ), 30 );
+ CHECK_EQ( probeRoomFrameRate( 28, 30 ), 30 );
+ CHECK_EQ( probeRoomFrameRate( 55, 60 ), 60 );
+
+ /* EA capped this step at a hardcoded 30 while the settled rate was capped at
+ FramesPerSecondLimit, so a room configured above 30 told its slowest player to slow down. */
+ CHECK_EQ( probeRoomFrameRate( 40, 60 ), 44 );
+}
+
+TEST(room_frame_rate_climbs_back_after_one_player_hitches)
+{
+ /* The defect this pins: every machine paces its logic on the broadcast rate, so a machine that
+ is keeping up measures exactly that rate and reports it back. With the room commanded at
+ the reported minimum, the minimum is then whatever it already was - for ever. One player's
+ two second hitch dropped the room to 12 and the whole match stayed in slow motion. */
+ const Int fpsLimit = 30;
+ Int capable[4] = { 30, 30, 30, 30 };
+
+ // the room is at 12 because somebody hitched; they have recovered, everyone can do 30 now
+ Int rate = simulateRoom( capable, 4, 12, 1, fpsLimit );
+ CHECK( rate > 12 );
+
+ // and it keeps climbing, round after round, until it is back at the limit
+ Int previous = 12;
+ for( Int round = 1; round <= 20; ++round )
+ {
+ rate = simulateRoom( capable, 4, 12, round, fpsLimit );
+ CHECK( rate >= previous );
+ previous = rate;
+ }
+ CHECK_EQ( simulateRoom( capable, 4, 12, 20, fpsLimit ), 30 );
+
+ // from the floor as well, and from a two player room, and with a raised limit
+ CHECK_EQ( simulateRoom( capable, 4, ROOM_FRAME_RATE_FLOOR, 40, fpsLimit ), 30 );
+ CHECK_EQ( simulateRoom( capable, 2, 6, 40, fpsLimit ), 30 );
+
+ Int capable60[2] = { 60, 60 };
+ CHECK_EQ( simulateRoom( capable60, 2, 10, 40, 60 ), 60 );
+}
+
+TEST(room_frame_rate_still_pins_to_a_genuinely_slow_player_without_oscillating)
+{
+ /* The step must not turn into a speed wobble: a player who really cannot do better than 12
+ has to hold the room near 12 and hold it *steady*. EA's "keep the current rate if the
+ minimum is within 10 % of it" band is what would break this - it ignores the slow player
+ for exactly as long as the step keeps the rate within 10 % of them, so the room walks
+ 12-13-14-15 and falls back to 12. The band is gone; the step does its job alone. */
+ const Int fpsLimit = 30;
+ Int mixed[3] = { 30, 30, 12 };
+
+ Int rate = simulateRoom( mixed, 3, 30, 30, fpsLimit );
+ CHECK( rate >= 12 );
+ CHECK( rate <= 14 );
+
+ // settled means settled: over the last twenty rounds the rate must not move at all
+ Int settledRate = simulateRoom( mixed, 3, 30, 10, fpsLimit );
+ for( Int round = 10; round <= 30; ++round )
+ CHECK_EQ( simulateRoom( mixed, 3, 30, round, fpsLimit ), settledRate );
+
+ // the room does slow down for them, though - that part is the point of the mechanism
+ CHECK( settledRate < 30 );
+
+ // a machine below the floor cannot drag the room under it either
+ Int hopeless[2] = { 30, 1 };
+ CHECK_EQ( simulateRoom( hopeless, 2, 30, 30, fpsLimit ),
+ probeRoomFrameRate( ROOM_FRAME_RATE_FLOOR, fpsLimit ) );
+}
+
TEST(selfslug_is_monotonic_in_the_cushion)
{
// once there is enough margin the answer must stay no, or the frame rate would oscillate