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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gamenetwork): size the run-ahead on the trip, not the router twice
Olcay Seygan8815a0266fa8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c9a554c25..4a8c34607 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -228,6 +228,7 @@ found and fixed — EA's own, not port damage.**- One lost packet costs a round trip instead of a flat two seconds.- A link that drops the odd packet no longer drags the whole room into slow motion. The game held its outgoing packets back for up to half its input delay, replies included, so it measured every connection as far slower than it was and waited longer and longer for anything lost. Three players on a 50 ms link losing one packet in fifty used to sink from 30 frames a second to 13; they hold 21 now, and two players losing one in a hundred hold 27.- A bad connection no longer turns the match into a slideshow. The orders each player sends now go out three times, 30 ms apart, so a lost packet is covered by the next one instead of by a resend that came most of a second later while everyone waited. Three players on 120 ms links losing one packet in twenty went from 6 frames a second to 19; two on 200 ms links losing one in ten went from 6 to 23, and the 50 ms room above from 21 to 26. The copies cost a few bytes and nothing on a clean line.+- Your clicks are answered sooner online. The game worked out how far ahead to schedule orders from the two slowest connections in the room and counted the host as one of them, so a two-player game waited out a whole round trip where half of one is the wire. At a 200 ms ping the delay between a click and the units moving drops from about 310 ms to 215; three players at 250 ms go from 500 to 370. Pings up to 300 ms and more are measured as they are now instead of being cut off at half a second, which left those links a window too short for them.- The readout in the top right corner now carries the frame number, and in a network game how many frames the room can still play before it has to wait on somebody, out of the input delay it is running with, plus the frame rate the slowest machine has set for everyone. "ready 4/7 room 29fps" is a healthy match; ready sitting at 0 is the stall you are about to feel.- Two players who each lost the other's last packet no longer freeze until the disconnect screen. Each asked the other to send it again, and each only resent what it had already played, which was nothing, so on a link with a tenth of a second of delay and one packet in twenty lost the match stopped for good one second in. Now the packet goes again and the match carries on.- A desynced match stops at once instead of playing on as two games.diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.hindex e95f31b63..b8f9825f7 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CushionMetrics.h@@ -69,8 +69,13 @@ enumlink, which is not a margin, it is a rounding error. Two frames - 66 ms at 30 Hz - is theallowance 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,+ and the links that do not stop paying for it.++ Three since the router stopped being counted as a leg of the trip (roomLatencySum). The+ window that came out of that was the honest one, and at 200 to 300 ms of ping with 30 to 60 ms+ of jitter two frames of allowance let the tail through: two players at 200 ms fell from 29+ logic frames a second to 26 on a 5-frame window. A LAN still sits on MIN_RUNAHEAD. */+ RUNAHEAD_JITTER_FRAMES = 3,/* The room never runs slower than this, whatever the metrics say. */ROOM_FRAME_RATE_FLOOR = 5,@@ -88,8 +93,20 @@ enumpacket that is still in flight, too large and every click is answered late. The packet routercomputes 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. */+/* Every command travels through the packet router, so the longest trip in the room is from the+ worst-connected player to the router and on to the second worst: half of each of their round+ trips. The router's own figure is not a leg of any trip. EA's getMaximumLatency said as much+ ("the latency for the packet router is always 0") and then summed it anyway, and the router+ measures a real round trip like everybody else. Three players never showed it, because the two+ worst are usually the other two. Two players always did: the router's round trip plus the+ guest's, which is the same trip twice, so the run-ahead covered a whole round trip where half+ of one was the wire - 9 frames against 6 at a 200 ms ping.++ latencies and connected are indexed by slot; routerSlot is left out. */+Real roomLatencySum( const Real latencies[], const Bool connected[], Int numSlots, Int routerSlot );++/** The run-ahead, in frames. latencySumSeconds is roomLatencySum's, fps the rate the room has+ settled on. */Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,Int minRunAhead, Int maxRunAhead );@@ -110,8 +127,15 @@ Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,run-ahead - 0.6 s - against the 64 frames the raw samples bought. A link genuinely slowerthan this loses at most 33 ms of one-way window, which RUNAHEAD_JITTER_FRAMES already covers.ponytail: one ceiling for every link. The self-calibrating version is the connection's own- srtt + 4*rttvar (Connection.h has both), if a real link is ever found that this undersizes. */-const Real MAX_PLAUSIBLE_LATENCY_SECONDS = 0.5f;+ srtt + 4*rttvar (Connection.h has both), if a real link is ever found that this undersizes.++ It was half a second until the owner said the rooms this fork is played in sit at 200 to 300 ms+ of ping. The sample is that ping plus the game's own share of the trip - a frame of send+ grouping each way and the time until the next pass reads the socket - and a 300 ms ping with+ 60 ms of jitter measured 0.38 s on average with single samples past 0.5, so the ceiling was+ already cutting into links people play on and would have undersized the window on every one of+ them a little slower. */+const Real MAX_PLAUSIBLE_LATENCY_SECONDS = 0.8f;/** Fold a raw round-trip measurement into the range a round trip can actually occupy. */Real sanitizeLatencySample( Real seconds, Real ceilingSeconds );diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex 1766e5717..3941e5647 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -1437,24 +1437,13 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS}Real ConnectionManager::getMaximumLatency() {- // This works for 2 player games because the latency for the packet router is always 0.- Real lat1 = 0.0;- Real lat2 = 0.0;-+ // EA: "This works for 2 player games because the latency for the packet router is always 0."+ // It is not, so it is left out by name - see roomLatencySum.+ Bool connected[MAX_SLOTS];for (Int i = 0; i < MAX_SLOTS; ++i) {- if (isPlayerConnected(i)) {- if (m_latencyAverages[i] != 0.0) {- if (m_latencyAverages[i] > lat1) {- lat2 = lat1;- lat1 = m_latencyAverages[i];- } else if (m_latencyAverages[i] > lat2) {- lat2 = m_latencyAverages[i];- }- }- }+ connected[i] = isPlayerConnected(i);}-- return (lat1 + lat2);+ return roomLatencySum(m_latencyAverages, connected, MAX_SLOTS, m_packetRouterSlot);}void ConnectionManager::getMinimumFps(Int &minFps, Int &minFpsPlayer) {diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cppindex 91e4fba05..fc36a02ae 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/CushionMetrics.cpp@@ -83,6 +83,28 @@ Int selfSlugThreshold( Int runAhead, UnsignedInt slackPercent )return threshold;}+Real roomLatencySum( const Real latencies[], const Bool connected[], Int numSlots, Int routerSlot )+{+ Real worst = 0.0f;+ Real secondWorst = 0.0f;+ for( Int slot = 0; slot < numSlots; ++slot )+ {+ if( slot == routerSlot || !connected[slot] )+ continue;++ if( latencies[slot] > worst )+ {+ secondWorst = worst;+ worst = latencies[slot];+ }+ else if( latencies[slot] > secondWorst )+ {+ secondWorst = latencies[slot];+ }+ }+ return worst + secondWorst;+}+Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,Int minRunAhead, Int maxRunAhead ){@@ -91,7 +113,7 @@ Int computeRunAhead( Real latencySumSeconds, Int fps, UnsignedInt slackPercent,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+ /* roomLatencySum() sums the two worst average round trips, so half of it is the one-way tripa command has to survive. Rounded up, where EA truncated: a 150 ms round trip truncates totwo frames - 66 ms - to cover 75 ms of wire, and a run-ahead shorter than the trip itself isnot a tight window, it is a stall on every command that uses it. Nobody ever saw thatdiff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 2c8b71d53..f82aec640 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -4111,6 +4111,27 @@ TEST(the_run_ahead_covers_the_trip_it_is_sized_for)}}+TEST(the_router_is_not_a_leg_of_any_trip)+{+ /* Two players, slot 0 the router: both measure the same 0.22 s round trip. The one trip in the+ room is guest to router, half of it. EA summed both and gave the room a whole round trip. */+ Real two[2] = { 0.22f, 0.22f };+ Bool twoConnected[2] = { TRUE, TRUE };+ CHECK_NEAR( roomLatencySum( two, twoConnected, 2, 0 ), 0.22f, 0.0001f );+ CHECK( computeRunAhead( roomLatencySum( two, twoConnected, 2, 0 ), 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 )+ < computeRunAhead( 0.44f, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 ) );++ /* Three: the worst trip is the two guests through the router, both of their legs. The router's+ own figure is left out even when it is the largest. */+ Real three[3] = { 0.50f, 0.30f, 0.20f };+ Bool threeConnected[3] = { TRUE, TRUE, TRUE };+ CHECK_NEAR( roomLatencySum( three, threeConnected, 3, 0 ), 0.50f, 0.0001f );++ /* a guest who has left is not a leg either */+ threeConnected[1] = FALSE;+ CHECK_NEAR( roomLatencySum( three, threeConnected, 3, 0 ), 0.20f, 0.0001f );+}+/* Measured in a real LAN match: the host lost one FRAMEINFO packet, sat on the frame for twentyseconds, and its own latency samples came back as 1.79 s and then 7.64 s on a link whose srtt was51 ms. The run-ahead sized on them went 5 -> 29 -> 64 frames, which is 2.1 seconds of input@@ -4120,7 +4141,8 @@ TEST(a_stalled_frame_is_not_filed_as_round_trip_time)/* an ordinary link passes through untouched - the clamp must not be a tax on healthy games */CHECK_NEAR( sanitizeLatencySample( 0.051f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.051f, 0.0001f );CHECK_NEAR( sanitizeLatencySample( 0.300f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.300f, 0.0001f );- CHECK( MAX_PLAUSIBLE_LATENCY_SECONDS <= 0.5f ); // a slower link than this is not a game+ CHECK( MAX_PLAUSIBLE_LATENCY_SECONDS <= 0.8f ); // a slower link than this is not a game+ CHECK_NEAR( sanitizeLatencySample( 0.450f, MAX_PLAUSIBLE_LATENCY_SECONDS ), 0.450f, 0.0001f ); // a 300 ms ping, measuredCHECK_NEAR( sanitizeLatencySample( MAX_PLAUSIBLE_LATENCY_SECONDS, MAX_PLAUSIBLE_LATENCY_SECONDS ),MAX_PLAUSIBLE_LATENCY_SECONDS, 0.0001f );@@ -4139,7 +4161,7 @@ TEST(a_stalled_frame_is_not_filed_as_round_trip_time)+ sanitizeLatencySample( 1.785224f, MAX_PLAUSIBLE_LATENCY_SECONDS );Int runAhead = computeRunAhead( clamped, 30, 10, MIN_RUNAHEAD, MAX_FRAMES_AHEAD / 2 );CHECK( runAhead < 64 );- CHECK( runAhead <= 18 ); // 0.6 s at 30 Hz, and only with both players at the ceiling+ CHECK( runAhead <= 29 ); // 0.97 s at 30 Hz, and only with both players at the ceilingCHECK( runAhead <= (Int)(MAX_PLAUSIBLE_LATENCY_SECONDS * 30.0f * 1.2f) + RUNAHEAD_JITTER_FRAMES );/* a LAN is untouched by any of this */