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): pace the logic tick once, and measure the rate that matters
Olcay Seygan02cfa628b6f3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3f4e92d4e..16e362b36 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -164,6 +164,19 @@ map. Ctrl+A is still everything at once.fingerprint itself was checking the same number 128 times instead of the wall it was supposed tobe checking. Fixed. This is groundwork: network play is still not something this build claims.+- **A network game runs at the speed it says it does.** Playing against other people, the+ simulation was gated by two separate clocks at once — the network's and the game's own — and a+ tick only happened when both agreed. Two free-running clocks of the same speed agree less often+ than either one alone, so a multiplayer match quietly ran slower than the identical skirmish+ offline, and drifted further the longer it went. There is one clock now.+- **Your input delay is no longer set by your graphics card.** The game decides how far ahead of+ itself everyone has to play from the slowest machine in the room — but it was measuring "slowest"+ by how many frames each player *drew*, not how many the simulation actually stepped. Since this+ build lets the picture run free of the simulation, someone with a modest graphics card and a+ perfectly healthy processor reported a third of their real speed, got a window too small to+ deliver their orders in, and stalled on almost every frame — and the game's response to one+ player stalling is to slow everyone down. It measures the simulation now.+## Sharper textures, for freeZero Hour shipped downscaled copies of a lot of the base game's textures, and because of the orderdiff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.hindex 265db1440..898224701 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/FrameMetrics.h@@ -53,6 +53,7 @@ protected:// frames per second history variables.Real *m_fpsList; ///< A record of how many game logic frames per second there were for the last 60 seconds.time_t m_lastFpsTimeThing; ///< The time when the last fps entry started being recorded.+ UnsignedInt m_fpsStartingFrame; ///< The logic frame the window above opened on, so the sample is a logic rate.Int m_fpsListIndex; ///< Index into the array of the current fps list entry being measured.Real m_averageFps; ///< The current average logic fps, computed just like m_averageLatency below but with the fps numbers.diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkInterface.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkInterface.hindex 2ca8b36ce..48a6b6a9a 100644--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkInterface.h+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/NetworkInterface.h@@ -68,6 +68,7 @@ public:virtual void setLocalAddress(UnsignedInt ip, UnsignedInt port) = 0; ///< Tell the network what local ip and port to bind to.virtual Bool isFrameDataReady( void ) = 0; ///< Are the commands for the next frame available?+ virtual Bool isPacingLogicFrames( void ) = 0; ///< Is the network deciding when a logic frame runs? (See GameEngine::update.)virtual void parseUserList( const GameInfo *game ) = 0; ///< Parse a userlist, creating connectionsvirtual void startGame(void) = 0; ///< Sets the network game frame counter to -1virtual UnsignedInt getRunAhead(void) = 0; ///< Get the current RunAhead valuediff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex e1143cb29..3966fdc3c 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -853,6 +853,17 @@ void GameEngine::update( void )logicAccumMs = 0.0f;logicFrameDue = TRUE;}+ else if (TheNetwork != NULL && TheNetwork->isPacingLogicFrames())+ {+ // 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+ // second time here against m_maxFPS beats two independent clocks against each other -+ // 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.+ logicAccumMs = 0.0f;+ logicFrameDue = TRUE;+ }else{logicFrameDue = GameEngine_isLogicFrameDue(logicAccumMs, elapsedMs, m_maxFPS);diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cppindex e1c6b006c..6c91e8483 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp@@ -1225,13 +1225,16 @@ void ConnectionManager::updateRunAhead(Int oldRunAhead, Int frameRate, Bool didS// We are the packet router, time to compute a new run ahead for this game.m_latencyAverages[m_localSlot] = m_frameMetrics.getAverageLatency();- // since we are now using the display frame rate rather than the logic frame rate to get our average FPS,- // it doesn't make sense to send the desired logic frame rate if we "slugged" ourself.-// if (didSelfSlug) {-// m_fpsAverages[m_localSlot] = frameRate;-// } else {+ // FrameMetrics measures the logic frame rate again (it briefly measured the display rate,+ // see FrameMetrics::doPerFrameMetrics), so EA's original rule applies once more: a+ // self-slug is a *voluntary* slowdown to let the room catch up, not this machine running+ // out of capacity. Reporting the slugged rate as our capability would feed the slowdown+ // back into minFps and shrink the run-ahead that the slug was buying margin for.+ if (didSelfSlug) {+ m_fpsAverages[m_localSlot] = frameRate;+ } else {m_fpsAverages[m_localSlot] = m_frameMetrics.getAverageFPS();-// }+ }if (didSelfSlug) {//DEBUG_LOG(("ConnectionManager::updateRunAhead - local player run ahead metrics, fps = %d, actual fps = %d, latency = %f, didSelfSlug = true\n", m_fpsAverages[m_localSlot], m_frameMetrics.getAverageFPS(), m_latencyAverages[m_localSlot]));} else {diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cppindex 0149a6703..27c3af0e2 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/FrameMetrics.cpp@@ -27,9 +27,20 @@#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine#include "GameNetwork/FrameMetrics.h"-#include "GameClient/Display.h"#include "GameNetwork/NetworkUtil.h"+/** -----------------------------------------------------------------------------------------------+ * The fps number the run-ahead equation wants: logic frames per second, from the logic frame the+ * sampling window opened on, the frame it closed on, and how long the window really lasted.+ * Free function (not a member, not static) so test_gameengine can link straight to it.+ */+Real FrameMetrics_logicFpsSample( UnsignedInt frame, UnsignedInt windowStartFrame, time_t windowMS )+{+ if (windowMS <= 0)+ return 0.0f;+ return (Real)(frame - windowStartFrame) * (Real)1000.0 / (Real)windowMS;+}+FrameMetrics::FrameMetrics(){//Added By Sadullah Nader@@ -39,6 +50,7 @@ FrameMetrics::FrameMetrics()m_cushionIndex = 0;m_fpsListIndex = 0;m_lastFpsTimeThing = 0;+ m_fpsStartingFrame = 0;m_minimumCushion = 0;m_pendingLatencies = NEW time_t[MAX_FRAMES_AHEAD];@@ -78,6 +90,11 @@ void FrameMetrics::init() {m_fpsList[i] = 30.0;}m_fpsListIndex = 0;+ // The fps window has to open on a real time and a real frame. Left at zero, the first sample+ // is taken against a zero start time, reads as ~0 fps, and drags the whole room's run-ahead+ // down for the first NetworkFPSHistoryLength seconds of the game.+ m_lastFpsTimeThing = timeGetTime();+ m_fpsStartingFrame = 0;for (i = 0; i < TheGlobalData->m_networkLatencyHistoryLength; ++i) {m_latencyList[i] = (Real)0.2;}@@ -90,19 +107,32 @@ void FrameMetrics::reset() {void FrameMetrics::doPerFrameMetrics(UnsignedInt frame) {// Do the measurement of the fps.+ //+ // This number is what the run-ahead equation calls "how fast can this machine advance the+ // simulation" - it becomes minFps in ConnectionManager::updateRunAhead, and through that the+ // input delay every player in the room lives with. EA sampled TheDisplay->getAverageFPS()+ // here because their renderer was locked to the logic rate, so the two were the same number.+ // They are not any more: this fork's renderer runs free while logic ticks on its own clock, so+ // a player with a weak GPU and a healthy CPU reported 12 while still stepping logic at 30, and+ // the run-ahead came out at a third of what the game needed - that player then stalled+ // constantly and the room stalled with them.+ //+ // doPerFrameMetrics() is called once per *logic* frame (ConnectionManager::processFrameTick),+ // so the honest measurement is in the argument: count the logic frames the window covered.+ // This is EA's own commented-out alternative, with the logic frame instead of the client one+ // and divided by the real window length rather than assuming it was exactly 1000ms.time_t curTime = timeGetTime();- if ((curTime - m_lastFpsTimeThing) >= 1000) {-// if ((m_fpsListIndex % 16) == 0) {-// DEBUG_LOG(("FrameMetrics::doPerFrameMetrics - adding %f to fps history. average before: %f ", m_fpsList[m_fpsListIndex], m_averageFps));-// }+ time_t windowMS = curTime - m_lastFpsTimeThing;+ if (windowMS >= 1000) {+ Real logicFps = FrameMetrics_logicFpsSample(frame, m_fpsStartingFrame, windowMS);m_averageFps -= ((m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // subtract out the old value from the average.- m_fpsList[m_fpsListIndex] = TheDisplay->getAverageFPS();-// m_fpsList[m_fpsListIndex] = TheGameClient->getFrame() - m_fpsStartingFrame;+ m_fpsList[m_fpsListIndex] = logicFps;m_averageFps += ((Real)(m_fpsList[m_fpsListIndex])) / TheGlobalData->m_networkFPSHistoryLength; // add the new value to the average.// DEBUG_LOG(("average after: %f\n", m_averageFps));++m_fpsListIndex;m_fpsListIndex %= TheGlobalData->m_networkFPSHistoryLength;m_lastFpsTimeThing = curTime;+ m_fpsStartingFrame = frame;}Int pendingLatenciesIndex = frame % MAX_FRAMES_AHEAD;diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cppindex e1d33961f..9711bae2d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/Network.cpp@@ -121,6 +121,7 @@ public:inline UnsignedInt getFrameRate(void) { return m_frameRate; }UnsignedInt getPacketArrivalCushion(void); ///< Returns the smallest packet arrival cushion since this was last called.Bool isFrameDataReady( void );+ Bool isPacingLogicFrames( void );void parseUserList( const GameInfo *game );void startGame(void); ///< Sets the network game frame counter to -1@@ -808,6 +809,17 @@ Bool Network::isFrameDataReady() {return (m_frameDataReady || (m_localStatus == NETLOCALSTATUS_LEFT));}+/**+ * True while timeForNewFrame() above is the thing deciding when a logic frame runs, which is any+ * time isFrameDataReady() is answering from m_frameDataReady rather than short-circuiting on+ * NETLOCALSTATUS_LEFT. GameEngine::update asks so it can stand its own wall-clock pacing down and+ * leave the network holding the only clock - two independent gates on the same tick multiply, and+ * the simulation ends up slower than either of them alone.+ */+Bool Network::isPacingLogicFrames() {+ return (m_localStatus != NETLOCALSTATUS_LEFT);+}+/*** returns the number of incoming bytes per second averaged over the last 30 sec.*/diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 32db3e0aa..4e1ea3d1a 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -1811,3 +1811,39 @@ TEST(controlbar_experience_percent_fills_the_rank_and_clamps)CHECK_EQ( ControlBar_experiencePercent( 0, 0, 0 ), -1 );CHECK_EQ( ControlBar_experiencePercent( 10, 300, 100 ), -1 );}++/* ---------------------------------------------------------------------------------------------+ Multiplayer pacing.++ FrameMetrics feeds ConnectionManager::updateRunAhead the answer to "how fast can this machine+ advance the simulation", and that number becomes the input delay everyone in the room lives+ with. It used to be sampled from TheDisplay->getAverageFPS(), which was the same number as the+ logic rate only while EA's renderer was locked to the logic tick. This fork uncapped the+ renderer, so the two came apart and the run-ahead started being sized off a GPU measurement.+ --------------------------------------------------------------------------------------------- */+extern Real FrameMetrics_logicFpsSample( UnsignedInt frame, UnsignedInt windowStartFrame, time_t windowMS );++TEST(network_fps_metric_measures_logic_frames_not_rendered_ones)+{+ /* The case that broke: a weak GPU drawing 12fps while the CPU still steps logic 30 times a+ second must report 30, because 30 is what the run-ahead has to cover. Nothing in the sample+ can see the render rate - the only inputs are logic frame numbers and a wall-clock window. */+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1030, 1000, 1000 ), 30.0f, 0.001f );++ /* A machine that genuinely cannot keep up reports what it managed, so the run-ahead grows to+ cover it. */+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1012, 1000, 1000 ), 12.0f, 0.001f );++ /* The window is whatever it turned out to be, not an assumed 1000ms: the sampler fires on the+ first logic frame at or past a second, which on a stuttering machine can be well past it.+ 30 frames spread over 1500ms is 20fps, not 30. */+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1030, 1000, 1500 ), 20.0f, 0.001f );+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1030, 1000, 500 ), 60.0f, 0.001f );++ /* A window with no time in it is not a division to attempt. */+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1030, 1000, 0 ), 0.0f, 0.001f );++ /* A window with no frames in it is a real answer - the simulation did not advance at all -+ and must read zero rather than wrapping the unsigned subtraction. */+ CHECK_NEAR( FrameMetrics_logicFpsSample( 1000, 1000, 1000 ), 0.0f, 0.001f );+}