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): size the run-ahead from the trip, not from a floor

Olcay Seygan8d8c2fab66a8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5ac267496..5a3ce753c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -304,6 +304,18 @@ 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.
+- **Online, your orders now happen a third of a second sooner.** Following on from that: the
+ amount the game runs ahead of you is your input delay, and because the formula never beat its own
+ minimum, every game was played at that minimum — a full third of a second between clicking and
+ the unit moving, on a local network as much as on a transatlantic one. That minimum is now less
+ than half what it was. Two things had to be fixed first, because the arithmetic underneath it was
+ never safe to rely on: it rounded the travel time *down*, so on some connections the window it
+ allowed was shorter than the trip the orders had to make — late, every single time — and the
+ safety margin it added was a percentage of a number so small it rounded away to nothing. Both are
+ fixed, and the margin is now a fixed amount of real time rather than a percentage. The result
+ cuts both ways on purpose: a good connection stops paying for a delay it never needed, and a bad
+ one is given a *wider* window than the old minimum ever gave it — a room on half a second of lag
+ used to get thirty-three milliseconds of margin, and now gets a hundred.
- **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
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
index dbc7ce53f..9160f591e 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h
@@ -56,14 +56,22 @@ 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. */
+ /* The brake needs room to work. NetworkRunAheadSlack is 10 %, and the run-ahead it is a
+ percentage of is small: MIN_RUNAHEAD is four frames, and computeRunAhead only clears that on
+ links slower than about 240 ms round trip. Ten percent of four is nothing, and a brake that
+ engages with less than a 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 fixed part of the run-ahead's safety margin. The transit term computeRunAhead sizes the
+ window from is built out of *average* round trips, and an average is exceeded half the time;
+ the proportional slack on top of it is worth 0.4 frames on a LAN and one frame on a 300 ms
+ link, which is not a margin, it is a rounding error. Two frames - 66 ms at 30 Hz - is the
+ allowance for the jitter the average hides, and it is what makes a low MIN_RUNAHEAD safe:
+ the links that need the window get more of it than the old flat floor of ten ever gave them,
+ and the links that do not stop paying for it. */
+ RUNAHEAD_JITTER_FRAMES = 2,
+
/* The room never runs slower than this, whatever the metrics say. */
ROOM_FRAME_RATE_FLOOR = 5,
@@ -74,6 +82,17 @@ enum
ROOM_FRAME_RATE_PROBE_PERCENT = 10,
};
+/* How far into the future the room schedules its commands. Every machine runs the same logic
+ frame at the same time, so a command has to be on every machine before that frame arrives; the
+ run-ahead is how much head start it is given. Too small and the room stalls waiting for a
+ packet that is still in flight, too large and every click is answered late. The packet router
+ computes this for the whole room and broadcasts it, so it is one number everybody obeys. */
+
+/** The run-ahead, in frames. latencySumSeconds is the sum of the two worst average round trips
+ (ConnectionManager::getMaximumLatency), fps the rate the room has settled on. */
+Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,
+ Int minRunAhead, Int maxRunAhead );
+
/* 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
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
index b4d1c5dff..9e3449215 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp
@@ -1278,19 +1278,12 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS
/* 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. */
+ room for the rest of the match - see CushionMetrics.h. The run-ahead below is sized
+ on the settled rate rather than this one: it is the room's honest measured speed, and
+ sizing a safety window on the rate we are hoping for is how the window comes up short. */
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) {
- newRunAhead = MIN_RUNAHEAD; // make sure its at least MIN_RUNAHEAD.
- }
-
- if (newRunAhead > (MAX_FRAMES_AHEAD / 2)) {
- newRunAhead = MAX_FRAMES_AHEAD / 2; // dont let run ahead get out of hand.
- }
+ Int newRunAhead = computeRunAhead(getMaximumLatency(), minFps,
+ TheGlobalData->m_networkRunAheadSlack, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2);
NetRunAheadCommandMsg *msg = newInstance(NetRunAheadCommandMsg);
msg->setPlayerID(m_localSlot);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
index f520bca8e..7f5df4d33 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp
@@ -23,6 +23,8 @@
#include "PreRTS.h"
#include "GameNetwork/CushionMetrics.h"
+#include <math.h>
+
Int frameCushion( UnsignedInt executionFrame, UnsignedInt currentFrame )
{
/* EA wrote this as `UnsignedInt cushion = executionFrame - TheGameLogic->getFrame()`, which is
@@ -72,8 +74,8 @@ 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. 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. */
+ percentage of a run-ahead that is a handful of frames on the links most rooms are played on
+ - see SELFSLUG_MIN_THRESHOLD_FRAMES. */
Int threshold = (Int)((runAhead * (Int)slackPercent) / 100);
if( threshold < SELFSLUG_MIN_THRESHOLD_FRAMES )
threshold = SELFSLUG_MIN_THRESHOLD_FRAMES;
@@ -81,6 +83,35 @@ Int selfSlugThreshold( Int runAhead, UnsignedInt slackPercent )
return threshold;
}
+Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,
+ Int minRunAhead, Int maxRunAhead )
+{
+ if( latencySumSeconds < 0.0f )
+ latencySumSeconds = 0.0f;
+ if( fps < 1 )
+ fps = 1; // the room rate is floored long before it gets here, but the division below is real
+
+ /* getMaximumLatency() sums the two worst average round trips, so half of it is the one-way trip
+ a command has to survive. Rounded up, where EA truncated: a 150 ms round trip truncates to
+ two frames - 66 ms - to cover 75 ms of wire, and a run-ahead shorter than the trip itself is
+ not a tight window, it is a stall on every command that uses it. Nobody ever saw that
+ because the old floor of ten frames was larger than the formula's answer on every link
+ anyone plays on; lowering the floor is what makes the arithmetic underneath it matter. */
+ Int frames = (Int)ceilf( (latencySumSeconds / 2.0f) * (Real)fps );
+
+ // EA's proportional slack, and then the fixed allowance an average and a percentage cannot give
+ frames += (Int)((frames * (Int)slackPercent) / 100);
+ frames += RUNAHEAD_JITTER_FRAMES;
+
+ if( frames < minRunAhead )
+ frames = minRunAhead;
+
+ if( frames > maxRunAhead )
+ frames = maxRunAhead; // don't let the run ahead get out of hand
+
+ return frames;
+}
+
Int settleRoomFrameRate( Int minFps, Int fpsLimit )
{
if( minFps < ROOM_FRAME_RATE_FLOOR )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
index ec2175b93..754521b1a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/NetworkUtil.cpp
@@ -28,7 +28,15 @@
#include "GameNetwork/NetworkUtil.h"
Int MAX_FRAMES_AHEAD = 128;
-Int MIN_RUNAHEAD = 10;
+
+/* The shortest run-ahead the room is ever commanded to use: 133 ms of input delay at 30 Hz. EA
+ shipped ten frames - a third of a second on every link, LAN included - and because the formula
+ in computeRunAhead does not clear ten until the two worst average round trips add up to about
+ 600 ms, ten frames is what every game anyone actually played ran at. The window can come down
+ this far because computeRunAhead now rounds the trip up rather than down and carries a fixed
+ jitter allowance on top (RUNAHEAD_JITTER_FRAMES), so a link that genuinely needs a wide window
+ is given a wider one than the old floor ever gave it, and a link that does not stops paying. */
+Int MIN_RUNAHEAD = 4;
Int FRAME_DATA_LENGTH = (MAX_FRAMES_AHEAD+1)*2;
Int FRAMES_TO_KEEP = (MAX_FRAMES_AHEAD/2) + 1;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index f15b4d267..4b07bdd64 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -3040,14 +3040,11 @@ TEST(selfslug_fires_once_the_margin_eats_into_the_slack)
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. */
+ /* The run-ahead a real room plays on is a handful of frames - MIN_RUNAHEAD is four, and
+ computeRunAhead only clears that above about 240 ms round trip - so NetworkRunAheadSlack's
+ 10 % of it is worth well under a frame. A brake that waits until less than one frame of
+ margin is left has already lost: at 30 Hz that is under 33 ms of warning for a hitch that
+ takes longer than that to signal, let alone correct. Hence the floor. */
CHECK_EQ( selfSlugThreshold( 10, 10 ), SELFSLUG_MIN_THRESHOLD_FRAMES );
// the floor holds wherever the arithmetic would land under it, including at zero slack
@@ -3073,6 +3070,102 @@ TEST(selfslug_threshold_has_a_floor_the_shipped_run_ahead_cannot_undercut)
CHECK( !shouldSelfSlug( SELFSLUG_MIN_THRESHOLD_FRAMES, 10, 10 ) );
}
+/* How far ahead the room schedules its commands. This is the number that decides whether a
+ click is answered promptly or the match stalls waiting for a packet, and until now it was
+ neither: the floor under it (MIN_RUNAHEAD, ten frames) was larger than the formula's own answer
+ on every link anyone plays on, so every game ran at 333 ms of input delay and the arithmetic
+ that was supposed to adapt the window never got a vote. */
+
+TEST(the_run_ahead_covers_the_trip_it_is_sized_for)
+{
+ /* The one thing a run-ahead must never do is come out shorter than the wire. getMaximumLatency
+ sums the two worst average round trips, so the trip a command has to survive is half of it,
+ and the window has to cover that at the rate the room is running. EA truncated the division:
+ a 150 ms round trip is 2.25 frames of wire at 30 Hz and truncates to 2 - 66 ms of window for
+ 75 ms of travel, which arrives late every single time. */
+ for( Int ms = 0; ms <= 800; ms += 5 )
+ {
+ Real latency = (Real)ms / 1000.0f;
+ for( Int fps = 5; fps <= 30; fps += 5 )
+ {
+ Int runAhead = computeRunAhead( latency, fps, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 );
+
+ // the window, in seconds, against the one-way trip it has to cover
+ CHECK( (Real)runAhead / (Real)fps >= (latency / 2.0f) );
+ }
+ }
+}
+
+TEST(the_run_ahead_carries_a_margin_the_percentage_alone_cannot_give)
+{
+ /* An average round trip is exceeded half the time, so a window sized to exactly the average is
+ wrong half the time. The proportional slack was meant to be that margin and cannot be: 10 %
+ of a four frame window is zero frames, 10 % of a ten frame window is one. The fixed
+ allowance is what actually covers the jitter, and it is what makes a low floor safe. */
+ for( Int ms = 0; ms <= 800; ms += 5 )
+ {
+ Real latency = (Real)ms / 1000.0f;
+ Int runAhead = computeRunAhead( latency, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 );
+
+ // at least RUNAHEAD_JITTER_FRAMES of room beyond the trip itself, at every latency
+ Real slackSeconds = ( (Real)runAhead / 30.0f ) - ( latency / 2.0f );
+ CHECK( slackSeconds >= ( (Real)RUNAHEAD_JITTER_FRAMES / 30.0f ) - 0.0005f );
+ }
+}
+
+TEST(the_run_ahead_is_shorter_than_the_shipped_floor_on_the_links_that_do_not_need_it)
+{
+ /* What the change is for. A LAN game and a good broadband game used to be given the same third
+ of a second of input delay as a transatlantic one, because the floor was ten frames and the
+ formula never beat it. */
+ const Int shippedFloor = 10; // EA's MIN_RUNAHEAD, 333 ms of input delay at 30 Hz
+
+ CHECK( computeRunAhead( 0.000f, 30, 10, MIN_RUNAHEAD, 64 ) < shippedFloor ); // LAN
+ CHECK( computeRunAhead( 0.040f, 30, 10, MIN_RUNAHEAD, 64 ) < shippedFloor ); // 40 ms
+ CHECK( computeRunAhead( 0.100f, 30, 10, MIN_RUNAHEAD, 64 ) < shippedFloor ); // 100 ms
+ CHECK( computeRunAhead( 0.160f, 30, 10, MIN_RUNAHEAD, 64 ) < shippedFloor ); // 160 ms
+
+ // and the shortest of them is the floor itself, not something under it
+ CHECK_EQ( computeRunAhead( 0.000f, 30, 10, MIN_RUNAHEAD, 64 ), MIN_RUNAHEAD );
+
+ /* And what it is not: the links that were relying on the floor get more window than the floor
+ gave them, not less. 600 ms summed round trip is where EA's formula finally reached ten. */
+ CHECK( computeRunAhead( 0.300f, 30, 10, MIN_RUNAHEAD, 64 ) >= 7 );
+ CHECK( computeRunAhead( 0.600f, 30, 10, MIN_RUNAHEAD, 64 ) > shippedFloor );
+ CHECK( computeRunAhead( 1.000f, 30, 10, MIN_RUNAHEAD, 64 ) > 16 );
+}
+
+TEST(the_run_ahead_stays_inside_the_bounds_the_network_buffers_are_built_for)
+{
+ // the window indexes frame buffers sized from MAX_FRAMES_AHEAD; it may not walk out of them
+ for( Int ms = 0; ms <= 20000; ms += 25 )
+ {
+ Int runAhead = computeRunAhead( (Real)ms / 1000.0f, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 );
+ CHECK( runAhead >= MIN_RUNAHEAD );
+ CHECK( runAhead <= MAX_FRAMES_AHEAD / 2 );
+ }
+
+ // a rate of zero is not a division by zero, and a negative latency is not a negative window
+ CHECK_EQ( computeRunAhead( 0.0f, 0, 10, MIN_RUNAHEAD, 64 ), MIN_RUNAHEAD );
+ CHECK_EQ( computeRunAhead( -1.0f, 30, 10, MIN_RUNAHEAD, 64 ), MIN_RUNAHEAD );
+}
+
+TEST(the_shipped_run_ahead_floor_leaves_the_self_slug_brake_room_to_work)
+{
+ /* The floor and the brake are one decision, not two. The self-slug fires when the measured
+ cushion drops below selfSlugThreshold(runAhead), so a floor at or below that threshold means
+ a room at its shortest window is braking permanently - the stall the brake exists to avoid,
+ applied continuously. */
+ CHECK( MIN_RUNAHEAD > SELFSLUG_MIN_THRESHOLD_FRAMES );
+ CHECK( selfSlugThreshold( MIN_RUNAHEAD, 10 ) < MIN_RUNAHEAD );
+ CHECK( !shouldSelfSlug( MIN_RUNAHEAD, MIN_RUNAHEAD, 10 ) );
+
+ // and the buffers the window is drawn from are big enough for the largest window it can ask for
+ CHECK( MIN_RUNAHEAD <= MAX_FRAMES_AHEAD / 2 );
+ CHECK( FRAME_DATA_LENGTH > 2 * MAX_FRAMES_AHEAD );
+ CHECK( FRAMES_TO_KEEP > MAX_FRAMES_AHEAD / 2 );
+}
+
/* 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