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(gameengine): catch up logic in network games instead of pacing to the slowest renderer

Olcay Seygand9eccdda7246
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d6ced39d8..7620b32de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -224,6 +224,7 @@ found and fixed — EA's own, not port damage.**
- A network game runs at the speed it says it does.
- A frame of orders now travels in one datagram instead of four or five: the packet was still the 476 bytes a 2003 modem could carry, and every extra datagram was another chance to arrive late.
- Input delay is measured from simulation speed now, not your graphics card.
+- One player's slow graphics card no longer slows the whole match. A network game runs at the pace of its slowest machine, and a machine that could only draw 16 pictures a second also only advanced the game 16 times a second, so everybody played in slow motion. It now skips pictures to keep up with the game, the way single player already did. Two machines on one desk: with one of them held to 16 pictures a second the match used to run at half speed, and with it held to 10 the match now runs at 28 of its 30 frames a second, the two machines agreeing on every frame.
- One lost packet costs a round trip instead of a flat two seconds.
- A desynced match stops at once instead of playing on as two games.
- A single lost packet no longer freezes the match for twenty seconds. The game now asks for the
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index 876727443..03631ea97 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -445,6 +445,7 @@ public:
Coord2D m_cameraLook; ///< where -camera pointed it
Int m_traceMoveID; ///< -tracemove [id]: log one movement line a frame for this object (0 = off, -1 = the first unit that gets blocked)
Real m_slowFrameMS; ///< -slowframe <ms>: a logic frame over this long logs its own breakdown (default 20)
+ Int m_drawDelayMS; ///< -drawdelay <ms>: sleep this long in every client pass, a slow graphics card on demand (0 = off)
Bool m_showLanes; ///< -showlanes: draw every moving unit's route, the lane it was handed and the offset it kept
Int m_uiDrill; ///< -uidrill <n>: every n frames, minimise the command bar and re-apply its scheme, logging where it landed (0 = off)
Int m_resDrillFrame; ///< -resdrill <frame> [w] [h]: change the resolution at that logic frame, from inside a running match (0 = off)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 92009869c..ba18e806a 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -2010,6 +2010,22 @@ Int parseSlowFrame(char *args[], int num)
return 1;
}
+/* -drawdelay <ms> sleeps that long in every client pass, which is what a weak graphics card looks
+ like to the engine: the picture takes longer while the logic frame costs what it always did.
+ A network game paces itself on the slowest machine in the room, so the question "does one slow
+ renderer slow everybody" needs a slow renderer on this machine, next to a fast one. */
+Int parseDrawDelay(char *args[], int num)
+{
+ if (TheWritableGlobalData && num > 1 && args[1])
+ {
+ const Int ms = atoi(args[1]);
+ if (ms > 0)
+ TheWritableGlobalData->m_drawDelayMS = ms;
+ return 2;
+ }
+ return 1;
+}
+
/* -netgame <ip>[,<ip>...] starts a LAN game against those addresses with no lobby in front of it,
and -netslot <n> says which of them this copy is. Every machine is given the same slot list in
the same order, which is all the lobby ever agreed on: the slot list, the map and the seed. A
@@ -2373,6 +2389,7 @@ static CommandLineParam params[] =
{ "-camera", parseCameraLook },
{ "-tracemove", parseTraceMove },
{ "-slowframe", parseSlowFrame },
+ { "-drawdelay", parseDrawDelay },
{ "-teams", parseTeams },
{ "-peacetime", parsePeaceTime },
{ "-unitlimit", parseUnitLimit },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index 443ca8810..8ab99480c 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -2136,6 +2136,8 @@ void GameEngine::update( void )
QueryPerformanceCounter( (LARGE_INTEGER *)&tAudioEnd );
#endif
TheGameClient->UPDATE();
+ if (TheGlobalData->m_drawDelayMS > 0)
+ ::Sleep( TheGlobalData->m_drawDelayMS );
TheMessageStream->propagateMessages();
if (TheNetwork != NULL)
@@ -2201,12 +2203,8 @@ void GameEngine::update( void )
Bool logicFrameDue;
Bool mayCatchUp = FALSE;
- if (fastMode)
- {
- logicAccumMs = 0.0f;
- logicFrameDue = TRUE;
- }
- else if (TheNetwork != NULL && TheNetwork->isPacingLogicFrames())
+ const Bool networkPaced = TheNetwork != NULL && TheNetwork->isPacingLogicFrames();
+ if (networkPaced)
{
// A network game already has a clock: Network::timeForNewFrame() paces the tick against
// the negotiated frame rate and only then publishes the frame's commands. Gating a
@@ -2214,14 +2212,26 @@ void GameEngine::update( void )
// a frame needs both to say yes, so the effective rate settles *below* either one and
// drifts, which is a systematic multiplayer-only slowdown. Let the network own it and
// keep the accumulator clean for when the game drops back to single player.
+ //
+ // It owns the debt as well. Each logic frame this machine manages a second is what it
+ // reports to the room, and the room runs at the slowest report, so a pass that ran one
+ // logic frame per picture made a slow graphics card everybody's frame rate: 60ms of
+ // drawing on one of two machines held both at 15 logic frames a second. This one runs
+ // ahead in this machine's own simulation by as many frames as the network has ready
+ // and due, and headless is here too, since the network paces it all the same.
+ logicAccumMs = 0.0f;
+ logicFrameDue = TRUE;
+ mayCatchUp = TRUE;
+ }
+ else if (fastMode)
+ {
logicAccumMs = 0.0f;
logicFrameDue = TRUE;
}
else
{
logicFrameDue = GameEngine_isLogicFrameDue(logicAccumMs, elapsedMs, m_maxFPS);
- // Only the wall-clock-paced path has a debt to pay back. Fast mode and the network
- // clock above both mean exactly one logic frame per call, by their own definition.
+ // Fast mode means exactly one logic frame per call, by its own definition.
mayCatchUp = (m_maxFPS > 0);
}
@@ -2245,7 +2255,8 @@ void GameEngine::update( void )
// count and the pacer's own accumulator cap stop at LOGIC_CATCHUP_MAX_FRAMES, so a logic
// frame that is itself over budget cannot pull the loop into a spiral.
Int logicTicksThisPass = 0;
- const Int maxTicksThisPass = GameEngine_logicCatchupMaxFrames(m_maxFPS);
+ const Int maxTicksThisPass = GameEngine_logicCatchupMaxFrames(
+ networkPaced ? (Int)TheNetwork->getFrameRate() : m_maxFPS );
/* Bounded by the clock as well as by the count - see LOGIC_CATCHUP_BUDGET_MS. Three
25ms ticks back to back with no picture in between is the 113ms freeze; one of them
plus the render is a dropped frame nobody files a bug about. */
@@ -2262,8 +2273,33 @@ void GameEngine::update( void )
if (!GameEngine_mayStartAnotherCatchupTick( logicTicksThisPass, maxTicksThisPass,
engineElapsedMS( tCatchupStart, tCatchupNow ) ))
break;
+ if (networkPaced)
+ {
+ /* The frame just run may have ended the match, and clearGameData takes the network
+ down with it. */
+ if (TheNetwork == NULL)
+ break;
+ /* A scripted camera move holds the logic for as many updates as the camera takes to
+ finish (freezeTime in GameLogic::update), and the camera only moves on a client
+ pass. One update a pass is what every machine used to run against it; running
+ several here would make the count depend on how fast this machine draws. */
+ if (TheTacticalView->isTimeFrozen())
+ break;
+ /* What the frame just posted (its CRC, every frame while DEBUG_CRC is on, or a group
+ selection) is sent the way a client pass would send it, before the next frame
+ runs. The network stamps it with the current logic frame plus the run-ahead, and
+ a machine running one frame a pass stamps it with the frame after the one that
+ posted it; sent a frame later, this machine's CRC would be compared against the
+ others' on another frame and read as a desync. Asked last, because it has the
+ side effects: it sends and receives, spends a frame of the network clock, and
+ puts the next frame's commands on the list. */
+ TheMessageStream->propagateMessages();
+ TheNetwork->UPDATE();
+ if (!TheNetwork->isFrameDataReady())
+ break;
+ }
// Asked last, because it is the one with a side effect: it spends the debt it reports.
- if (!GameEngine_isLogicFrameDue(logicAccumMs, 0.0f, m_maxFPS))
+ else if (!GameEngine_isLogicFrameDue(logicAccumMs, 0.0f, m_maxFPS))
break;
}
#ifdef DEBUG_LOGGING
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index b5d588171..d070ac25a 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -722,6 +722,7 @@ GlobalData::GlobalData()
m_cameraLook.x = m_cameraLook.y = 0.0f;
m_traceMoveID = 0; // no movement trace
m_slowFrameMS = 20.0f; // a frame worth a line in the log; -slowframe lowers it for a hunt
+ m_drawDelayMS = 0; // client passes run as fast as the machine does unless -drawdelay slows them
m_showLanes = FALSE; // the lane overlay is a diagnostic, off unless -showlanes asks for it
m_uiDrill = 0; // nobody presses the minimise button; -uidrill is how a script presses it
m_resDrillFrame = 0; // the resolution stays where it started unless -resdrill changes it mid-match
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
index c60f43c59..60ea89473 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp
@@ -192,6 +192,7 @@ protected:
void RelayCommandsToCommandList(UnsignedInt frame); ///< Put the commands for the given frame onto TheCommandList.
Bool isTransferCommand(GameMessage *msg); ///< Is this a command that needs to be transfered to the other clients?
Bool processCommand(GameMessage *msg); ///< Whatever needs to be done as a result of this command, do it now.
+ Bool noteLogicFrameAdvance(); ///< Send frame info for every frame the logic has moved past; FALSE while still in pregame.
void processFrameSynchronizedNetCommand(NetCommandRef *msg); ///< If there is a network command that needs to be executed at the same frame number on all clients, it happens here.
void processRunAheadCommand(NetRunAheadCommandMsg *msg); ///< Do what needs to be done when we get a new run ahead command.
void processDestroyPlayerCommand(NetDestroyPlayerCommandMsg *msg); ///< Do what needs to be done when we need to destroy a player.
@@ -506,7 +507,41 @@ Int Network::getExecutionFrame() {
* send our info for the last frame to the other players.
* Return true if the message should be "eaten" by the network.
*/
-Bool Network::processCommand(GameMessage *msg)
+Bool Network::processCommand(GameMessage *msg)
+{
+ if (!noteLogicFrameAdvance())
+ return FALSE;
+
+ // Are we leaving the game?
+ // This has to happen after the check to see if this is the start of a new logic frame.
+ // The reason is that we have to send all the frame info packets necessary to get to the
+ // frame where everyone else is going to see that we left.
+ if ((msg->getType() == GameMessage::MSG_CLEAR_GAME_DATA) && (m_localStatus == NETLOCALSTATUS_INGAME)) {
+ Int executionFrame = getExecutionFrame();
+ DEBUG_LOG(("Network::processCommand - local player leaving, executionFrame = %d, player leaving on frame %d\n", executionFrame, executionFrame+1));
+
+ m_conMgr->handleLocalPlayerLeaving(executionFrame+1);
+ m_conMgr->processFrameTick(executionFrame); // This is the last command we will execute, so send the command count.
+ // Also, we are guaranteed not to send any more commands for this frame
+ // since the local status will change to "Leaving" so we don't have to
+ // worry about messing up the other players.
+ m_conMgr->processFrameTick(executionFrame+1); // since we send it for executionFrame+1, we need to process both ticks
+ m_lastFrameCompleted = executionFrame;
+ DEBUG_LOG(("Network::processCommand - player leaving on frame %d\n", executionFrame));
+ m_localStatus = NETLOCALSTATUS_LEAVING;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
+ * EA sent the frame info packets from processCommand, which only runs for a message on
+ * TheCommandList, and relied on the client's MSG_FRAME_TICK always being there. It is, once per
+ * client pass - but a pass that is catching up runs several logic frames between two of them, and
+ * the frames it ran would go unannounced until the next picture. update() calls this as well, so
+ * the announcement follows the logic frame rather than the render pass.
+ */
+Bool Network::noteLogicFrameAdvance()
{
if ((m_lastFrame != TheGameLogic->getFrame()) || (m_localStatus == NETLOCALSTATUS_PREGAME)) {
// If this is the start of a new game logic frame, then tell the connection manager that the last
@@ -541,27 +576,7 @@ Bool Network::processCommand(GameMessage *msg)
//DEBUG_LOG(("Next Execution Frame - %d, last frame completed - %d\n", getExecutionFrame(), m_lastFrameCompleted));
m_lastFrame = TheGameLogic->getFrame();
}
-
- // Are we leaving the game?
- // This has to happen after the check to see if this is the start of a new logic frame.
- // The reason is that we have to send all the frame info packets necessary to get to the
- // frame where everyone else is going to see that we left.
- if ((msg->getType() == GameMessage::MSG_CLEAR_GAME_DATA) && (m_localStatus == NETLOCALSTATUS_INGAME)) {
- Int executionFrame = getExecutionFrame();
- DEBUG_LOG(("Network::processCommand - local player leaving, executionFrame = %d, player leaving on frame %d\n", executionFrame, executionFrame+1));
-
- m_conMgr->handleLocalPlayerLeaving(executionFrame+1);
- m_conMgr->processFrameTick(executionFrame); // This is the last command we will execute, so send the command count.
- // Also, we are guaranteed not to send any more commands for this frame
- // since the local status will change to "Leaving" so we don't have to
- // worry about messing up the other players.
- m_conMgr->processFrameTick(executionFrame+1); // since we send it for executionFrame+1, we need to process both ticks
- m_lastFrameCompleted = executionFrame;
- DEBUG_LOG(("Network::processCommand - player leaving on frame %d\n", executionFrame));
- m_localStatus = NETLOCALSTATUS_LEAVING;
- return TRUE;
- }
- return FALSE;
+ return TRUE;
}
/**
@@ -702,6 +717,7 @@ void Network::update( void )
}
#endif
+ noteLogicFrameAdvance();
GetCommandsFromCommandList(); // Remove commands from TheCommandList and send them to the connection manager.
if (m_conMgr != NULL) {
if (m_localStatus == NETLOCALSTATUS_INGAME) {
@@ -788,7 +804,11 @@ Bool Network::timeForNewFrame() {
// DEBUG_LOG(("Allowing a new frame, frameDelay = %I64d, curTime - m_nextFrameTime = %I64d\n", frameDelay, curTime - m_nextFrameTime));
// if (m_nextFrameTime + frameDelay < curTime) {
- if ((m_nextFrameTime + (2 * frameDelay)) < curTime) {
+ /* GameEngine::update pays this debt back with logic frames run between two pictures, up to
+ the same cap the single player pacer keeps. EA forgave it past two frames, when a pass
+ only ever ran one; a slow picture then lost the frames it owed and the whole room slowed
+ down to its rate. */
+ if ((m_nextFrameTime + (GameEngine_logicCatchupMaxFrames(m_frameRate) * frameDelay)) < curTime) {
// If we get too far behind on our framerate we need to reset the nextFrameTime thing.
m_nextFrameTime = curTime;
// DEBUG_LOG(("Initializing m_nextFrameTime to %I64d\n", m_nextFrameTime));