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(gameengine): average the logic rate every countdown divides by
Olcay Seygan0a2c31c8e6e9
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.hindex 2b7c843ad..6762502ef 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h@@ -141,11 +141,15 @@ protected:Int m_maxFPS; ///< Maximum frames per second allowedUnsignedInt m_logicRateSampleMs; ///< wall clock the current logic rate sample started at, 0 for noneUnsignedInt m_logicRateSampleFrame; ///< logic frame it started on- Int m_measuredLogicFps; ///< the last whole sample's rate, 0 before there is one+ Real m_logicFpsAverage; ///< the samples averaged, 0 before there is one+ Int m_measuredLogicFps; ///< that average as countdowns divide by it, moved only a full step at a timeBool m_quitting; ///< true when we need to quit the gameBool m_isActive; ///< app has OS focus.};+/// fold one half-second rate sample into a running average and the whole number shown for it+extern void smoothRateReading( Real sample, Real &average, Int &shown );+inline void GameEngine::setQuitting( Bool quitting ) { m_quitting = quitting; }inline Bool GameEngine::getQuitting(void) { return m_quitting; }diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cppindex ddbe1ad44..58e4ed74f 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp@@ -197,7 +197,24 @@ Int GameEngine::getFramesPerSecondLimit( void )}//--------------------------------------------------------------------------------------------------static const UnsignedInt LOGIC_RATE_SAMPLE_MS = 1000;+static const UnsignedInt LOGIC_RATE_SAMPLE_MS = 500;++/** A half-second sample is a count of whole frames, so a steady 30Hz reads 28 one sample and 32+ the next, and every countdown dividing by it read 20s, 21s, 19s. The average over the last few+ seconds takes that out, and the shown number only moves once the average has left it by a full+ step, so a rate sitting on x.5 does not flip between two values while a real drop still gets+ through in a second or two. */+void smoothRateReading( Real sample, Real &average, Int &shown )+{+ const Real SMOOTHING = 0.125f; // weight of one sample: about four seconds of them count+ const Real MIN_STEP = 0.75f; // over half, or an average settling on 20 from above sticks at 21+ const Real STEP_FRACTION = 0.02f; // the step at high rates, where one frame is less than 1%++ average = ( average == 0.0f ) ? sample : average + ( sample - average ) * SMOOTHING;+ const Real step = max( MIN_STEP, shown * STEP_FRACTION );+ if( fabs( average - shown ) >= step )+ shown = REAL_TO_INT( average + 0.5f );+}/** A build time on screen is a promise about how long you will wait, and the limit is only therate the logic is asked for. A match that has sunk to 10 frames a second takes three times as@@ -224,7 +241,7 @@ void GameEngine::sampleLogicRate( void )if( elapsedMs < LOGIC_RATE_SAMPLE_MS )return;- m_measuredLogicFps = REAL_TO_INT( (frame - m_logicRateSampleFrame) * 1000.0f / elapsedMs + 0.5f );+ smoothRateReading( (frame - m_logicRateSampleFrame) * 1000.0f / elapsedMs, m_logicFpsAverage, m_measuredLogicFps );m_logicRateSampleMs = nowMs;m_logicRateSampleFrame = frame;}@@ -247,6 +264,7 @@ GameEngine::GameEngine( void )m_maxFPS = 0;m_logicRateSampleMs = 0;m_logicRateSampleFrame = 0;+ m_logicFpsAverage = 0.0f;m_measuredLogicFps = 0;m_quitting = FALSE;m_isActive = FALSE;@@ -330,6 +348,10 @@ void GameEngine::setFramesPerSecondLimit( Int fps ){DEBUG_LOG(("GameEngine::setFramesPerSecondLimit() - setting max fps to %d (TheGlobalData->m_useFpsLimit == %d)\n", fps, TheGlobalData->m_useFpsLimit));m_maxFPS = fps;++ // the speed keys change the rate on purpose; the countdowns start again from the next sample+ // rather than taking the average's seconds to walk over+ m_logicFpsAverage = 0.0f;}/* -replay <file>: the name the command line asked for, opened after init()'s resetAll(). Seediff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex 8408095be..9e98c1fbd 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -9018,22 +9018,6 @@ void InGameUI::drawPeaceCountdown( UnsignedInt framesLeft )peaceTimeColor( alpha ), GameMakeColor( 0, 0, 0, alpha ) );}-//--------------------------------------------------------------------------------------------------// A half-second sample is a count of whole frames, so a steady 30Hz reads 28 one sample and 32-// the next. The average over the last few seconds takes that out, and the number on screen only-// moves once the average has left it by a full step, so a rate sitting on x.5 does not flip-// between two values while a real drop still gets through in a second or two.-static void smoothHudRate( Real sample, Real &average, Int &shown )-{- const Real SMOOTHING = 0.125f; // weight of one sample: about four seconds of them count- const Real STEP_FRACTION = 0.02f; // the step at high rates, where one frame is less than 1%-- average = ( average == 0.0f ) ? sample : average + ( sample - average ) * SMOOTHING;- const Real step = max( 1.0f, shown * STEP_FRACTION );- if( fabs( average - shown ) >= step )- shown = REAL_TO_INT( average + 0.5f );-}-//-------------------------------------------------------------------------------------------------void InGameUI::drawHudOverlay( void ){@@ -9074,8 +9058,8 @@ void InGameUI::drawHudOverlay( void )else if( nowMs - m_hudLastSampleMs >= 500 ){const Real elapsed = (Real)(nowMs - m_hudLastSampleMs);- smoothHudRate( (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed, m_hudFps, m_hudFpsShown );- smoothHudRate( (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed, m_hudLogicHz, m_hudLogicHzShown );+ smoothRateReading( (clientFrame - m_hudLastSampleFrame) * 1000.0f / elapsed, m_hudFps, m_hudFpsShown );+ smoothRateReading( (logicFrame - m_hudLastSampleLogicFrame) * 1000.0f / elapsed, m_hudLogicHz, m_hudLogicHzShown );m_hudLastSampleMs = nowMs;m_hudLastSampleFrame = clientFrame;m_hudLastSampleLogicFrame = logicFrame;diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex c6b18c3e7..49ab46db2 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3015,6 +3015,30 @@ TEST(controlbar_seconds_are_real_seconds_at_the_current_game_speed)CHECK_EQ( ControlBar_secondsFromFramesAt( 0.0f, 200 ), 0 );}+extern void smoothRateReading( Real sample, Real &average, Int &shown );++TEST(rate_reading_holds_through_jitter_and_follows_a_real_drop)+{+ /* A steady 30Hz sampled every half second counts 14, 15 or 16 frames, 28 to 32 a second. The+ first sample is taken as it is; after that the number shown must not move. */+ Real average = 0.0f;+ Int shown = 0;+ smoothRateReading( 30.0f, average, shown );+ CHECK_EQ( shown, 30 );+ const Real jitter[] = { 28.0f, 32.0f, 30.0f, 32.0f, 28.0f, 30.0f, 28.0f, 32.0f };+ for( Int sample = 0; sample < 4; sample++ )+ for( const Real &reading : jitter )+ {+ smoothRateReading( reading, average, shown );+ CHECK_EQ( shown, 30 );+ }++ /* a match that sinks to 20 and stays there reads 20 within a few seconds */+ for( Int sample = 0; sample < 40; sample++ )+ smoothRateReading( 20.0f, average, shown );+ CHECK_EQ( shown, 20 );+}+TEST(controlbar_experience_percent_fills_the_rank_and_clamps){/* a fresh unit at the bottom of its rank */