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.

feat(gameengine): count countdowns down at the measured logic rate

Olcay Seyganb4f680011cfe
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
index fcf39bc8f..2b7c843ad 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GameEngine.h
@@ -111,6 +111,7 @@ public:
It will not return until the game exits. */
virtual void setFramesPerSecondLimit( Int fps ); ///< Set the maximum rate engine updates are allowed to occur
virtual Int getFramesPerSecondLimit( void ); ///< Get maxFPS. Not inline since it is called from another lib.
+ virtual Int getLogicFramesPerSecond( void ); ///< logic frames the last second really ran; the rate a countdown on screen divides by
virtual void setQuitting( Bool quitting ); ///< set quitting status
virtual Bool getQuitting(void); ///< is app getting ready to quit.
@@ -135,7 +136,12 @@ protected:
virtual ParticleSystemManager* createParticleSystemManager( void ) = 0;
virtual AudioManager *createAudioManager( void ) = 0; ///< Factory for Audio Manager
+ void sampleLogicRate( void );
+
Int m_maxFPS; ///< Maximum frames per second allowed
+ UnsignedInt m_logicRateSampleMs; ///< wall clock the current logic rate sample started at, 0 for none
+ UnsignedInt m_logicRateSampleFrame; ///< logic frame it started on
+ Int m_measuredLogicFps; ///< the last whole sample's rate, 0 before there is one
Bool m_quitting; ///< true when we need to quit the game
Bool m_isActive; ///< app has OS focus.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index a5136062b..f56023c50 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -196,6 +196,47 @@ Int GameEngine::getFramesPerSecondLimit( void )
return m_maxFPS;
}
+//-------------------------------------------------------------------------------------------------
+static const UnsignedInt LOGIC_RATE_SAMPLE_MS = 1000;
+
+/** A build time on screen is a promise about how long you will wait, and the limit is only the
+ rate the logic is asked for. A match that has sunk to 10 frames a second takes three times as
+ long over a 300 frame barracks as the 30 it was asked for, so the countdown says 30s, not 10s.
+ Client only: the wall clock is in it, and nothing in GameLogic may decide by it. */
+void GameEngine::sampleLogicRate( void )
+{
+ if( TheGameLogic == NULL || TheGameLogic->isGamePaused() )
+ {
+ m_logicRateSampleMs = 0;
+ return;
+ }
+
+ const UnsignedInt nowMs = timeGetTime();
+ const UnsignedInt frame = TheGameLogic->getFrame();
+ if( m_logicRateSampleMs == 0 || frame < m_logicRateSampleFrame )
+ {
+ m_logicRateSampleMs = nowMs;
+ m_logicRateSampleFrame = frame;
+ return;
+ }
+
+ const UnsignedInt elapsedMs = nowMs - m_logicRateSampleMs;
+ if( elapsedMs < LOGIC_RATE_SAMPLE_MS )
+ return;
+
+ m_measuredLogicFps = REAL_TO_INT( (frame - m_logicRateSampleFrame) * 1000.0f / elapsedMs + 0.5f );
+ m_logicRateSampleMs = nowMs;
+ m_logicRateSampleFrame = frame;
+}
+
+Int GameEngine::getLogicFramesPerSecond( void )
+{
+ // a stalled network game has no rate at all, and no countdown can say how long that lasts
+ if( m_measuredLogicFps > 0 )
+ return m_measuredLogicFps;
+ return m_maxFPS > 0 ? m_maxFPS : LOGICFRAMES_PER_SECOND;
+}
+
//-------------------------------------------------------------------------------------------------
GameEngine::GameEngine( void )
{
@@ -204,6 +245,9 @@ GameEngine::GameEngine( void )
// initialize to non garbage values
m_maxFPS = 0;
+ m_logicRateSampleMs = 0;
+ m_logicRateSampleFrame = 0;
+ m_measuredLogicFps = 0;
m_quitting = FALSE;
m_isActive = FALSE;
@@ -2100,6 +2144,7 @@ static void updateHeadlessRun( void )
void GameEngine::update( void )
{
USE_PERF_TIMER(GameEngine_update)
+ sampleLogicRate();
{
#ifdef DEBUG_LOGGING
static Int fpsFrames = 0;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
index 05e91d085..85f734059 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp
@@ -74,6 +74,7 @@
#include "GameClient/CinemaDirector.h"
#include "GameClient/Anim2D.h"
+#include "GameClient/ControlBar.h"
#include "GameClient/Display.h"
#include "GameClient/DisplayStringManager.h"
#include "GameClient/Drawable.h"
@@ -3761,7 +3762,7 @@ void Drawable::drawBombed(const IRegion2D* healthBarRegion)
// hey, I've got an idea! why don't we ASK the anim how long it is?
//
UnsignedInt dieFrame = update->getDetonationFrame();
- UnsignedInt seconds = REAL_TO_INT_CEIL( (dieFrame - now) * SECONDS_PER_LOGICFRAME_REAL);
+ UnsignedInt seconds = ControlBar_secondsFromFrames( (Real)(dieFrame - now) );
UnsignedInt numFrames = getIconInfo()->m_icon[ ICON_BOMB_TIMED ]->getAnimTemplate()->getNumFrames();
// this anim goes from "N" seconds down to zero, so the max seconds we can use is N-1.
@@ -4422,11 +4423,7 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)
Int totalFrames = pe->getProductionType() == PRODUCTION_UNIT
? pe->getProductionObject()->calcTimeToBuild( player )
: pe->getProductionUpgrade()->calcTimeToBuild( player );
- // real seconds: the logic runs at the game-speed rate, not at a fixed 30 frames a second
- Int logicFps = TheGameEngine ? TheGameEngine->getFramesPerSecondLimit() : 0;
- if( logicFps <= 0 )
- logicFps = LOGICFRAMES_PER_SECOND;
- Int secondsLeft = REAL_TO_INT_CEIL( totalFrames * (1.0f - pct * 0.01f) / logicFps );
+ Int secondsLeft = ControlBar_secondsFromFrames( totalFrames * (1.0f - pct * 0.01f) );
// one shared string: draw() renders immediately, and the manager lives for the whole app
static DisplayString *prodTimeString = NULL;
@@ -4588,11 +4585,6 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)
//
if( chargeFramesLeft > 0 )
{
- // real seconds: the logic runs at the game-speed rate, not a fixed 30 a second
- Int logicFps = TheGameEngine ? TheGameEngine->getFramesPerSecondLimit() : 0;
- if( logicFps <= 0 )
- logicFps = LOGICFRAMES_PER_SECOND;
-
// one shared string: draw() renders immediately and the manager lives for the app
static DisplayString *chargeTimeString = NULL;
if( chargeTimeString == NULL )
@@ -4604,7 +4596,7 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)
}
UnicodeString text;
- text.format( L"%ds", REAL_TO_INT_CEIL( INT_TO_REAL( chargeFramesLeft ) / logicFps ) );
+ text.format( L"%ds", ControlBar_secondsFromFrames( INT_TO_REAL( chargeFramesLeft ) ) );
if( chargeTimeString->getText().compare( text ) != 0 )
chargeTimeString->setText( text );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
index f426ebe25..27ab698f3 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
@@ -87,7 +87,9 @@ static Color BuildClockColor = GameMakeColor(0,0,0,120);
*
* Real seconds, not "logic frames over thirty". The logic rate is a knob in this fork (the game
* speed keys move it between 5 and 200), and a build time is a promise about how long you will
- * be waiting - so at double speed a 20s barracks has to read 10s. `LOGICFRAMES_PER_SECOND` is
+ * be waiting - so at double speed a 20s barracks has to read 10s, and in a match that has sunk to
+ * a third of its rate it has to read 60s. The rate is the one measured over the last second, not
+ * the one asked for (`GameEngine::getLogicFramesPerSecond`). `LOGICFRAMES_PER_SECOND` is
* only the fallback for the frames before the engine has a rate at all. */
//-------------------------------------------------------------------------------------------------
Int ControlBar_secondsFromFramesAt( Real frames, Int logicFps )
@@ -105,7 +107,7 @@ Int ControlBar_secondsFromFramesAt( Real frames, Int logicFps )
Int ControlBar_secondsFromFrames( Real frames )
{
return ControlBar_secondsFromFramesAt( frames,
- TheGameEngine ? TheGameEngine->getFramesPerSecondLimit() : 0 );
+ TheGameEngine ? TheGameEngine->getLogicFramesPerSecond() : 0 );
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
index 3cb5c0856..ea69cb708 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
@@ -3302,11 +3302,8 @@ Int Object::getConstructionSecondsRemaining() const
rate = 100.0f / INT_TO_REAL( max( 1, framesToBuild ) );
}
Real left = max( 0.0f, 100.0f - m_constructionPercent );
- // real seconds: the logic runs at the game-speed rate, not at a fixed 30 frames a second
- Int logicFps = TheGameEngine ? TheGameEngine->getFramesPerSecondLimit() : 0;
- if( logicFps <= 0 )
- logicFps = LOGICFRAMES_PER_SECOND;
- return REAL_TO_INT_CEIL( left / rate / logicFps );
+ // for the screen only: the rate it divides by is measured on the wall clock
+ return ControlBar_secondsFromFrames( left / rate );
}
//-------------------------------------------------------------------------------------------------