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(gameclient): read the income rate off earnings, not the balance

Olcay Seygan82254a2299d7
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 1a8f90af5..849e5b853 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -786,9 +786,14 @@ protected:
UnsignedInt m_hudLastSampleFrame; ///< client frame the fps sample was last refreshed on
UnsignedInt m_hudLastSampleMs; ///< wall clock of that sample
Real m_hudFps; ///< smoothed render rate
- Int m_hudLastMoney; ///< player money at the last income sample
- UnsignedInt m_hudLastMoneyFrame; ///< logic frame of that sample
- Int m_hudIncomePerMin; ///< most recent income estimate, cash per minute
+ // Income is sampled from the player's cumulative earnings every INCOME_SAMPLE_SECONDS and
+ // averaged over the whole ring, so a lumpy supply run reads as a rate instead of a spike.
+ enum { INCOME_SAMPLES = 16, INCOME_SAMPLE_SECONDS = 2 }; ///< 16 buckets of 2s = a 30s window
+ Int m_incomeSamples[ INCOME_SAMPLES ]; ///< cumulative money earned, one per bucket
+ UnsignedInt m_incomeSampleCount; ///< buckets taken so far, the ring index is this mod INCOME_SAMPLES
+ Int m_incomeSamplePlayer; ///< whose earnings those are, so an observer switching players restarts
+ UnsignedInt m_hudLastMoneyFrame; ///< logic frame of the last sample
+ Int m_hudIncomePerMin; ///< most recent income estimate, cash per minute, -1 until the window is warm
//
// The global production strip: every unit the local player has queued anywhere, one cameo
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 03bc25449..dabc3111a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1029,9 +1029,12 @@ InGameUI::InGameUI()
m_hudLastSampleFrame = 0;
m_hudLastSampleMs = 0;
m_hudFps = 0.0f;
- m_hudLastMoney = -1;
+ for( Int incomeBucket = 0; incomeBucket < INCOME_SAMPLES; incomeBucket++ )
+ m_incomeSamples[ incomeBucket ] = 0;
+ m_incomeSampleCount = 0;
+ m_incomeSamplePlayer = -1;
m_hudLastMoneyFrame = 0;
- m_hudIncomePerMin = 0;
+ m_hudIncomePerMin = -1;
for( Int stripRow = 0; stripRow < PRODUCTION_STRIP_ROWS; stripRow++ )
{
m_productionStripCount[ stripRow ] = 0;
@@ -5267,35 +5270,62 @@ void InGameUI::updateFloatingText( void )
* only behind -displayDebug together with a screenful of engine internals. */
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
-/** Income per minute for the local player, as the change in the balance over the last sampling
- * window. Deliberately a delta of the balance rather than a tap into the deposit paths: it
- * counts crates and refunds too, which is what you actually want to compare a build against. */
+/** The ring math behind the income estimate: cash per minute from the oldest bucket we still hold
+ * to the newest. A free function, not a member, so test_gameengine can drive it without a
+ * Player. Returns -1 while there is only one bucket, i.e. no span to average over yet. */
+//-------------------------------------------------------------------------------------------------
+Int computeIncomePerMinute( const Int *samples, UnsignedInt ringSize, UnsignedInt count, Int sampleSeconds )
+{
+ if( samples == NULL || ringSize == 0 || count < 2 || sampleSeconds <= 0 )
+ return -1;
+
+ const UnsignedInt newest = count - 1;
+ const UnsignedInt oldest = (newest >= ringSize) ? newest - (ringSize - 1) : 0;
+ const Int span = (Int)(newest - oldest); // buckets spanned, at least one
+
+ return (samples[ newest % ringSize ] - samples[ oldest % ringSize ]) * 60 / (span * sampleSeconds);
+}
+
+//-------------------------------------------------------------------------------------------------
+/** Income per minute for the local player, averaged over the last 30 seconds of play.
+ * Sampled off the score keeper's cumulative earnings, not off the balance: the balance falls
+ * every time you build, and a balance delta therefore reads spending as zero income - which is
+ * what you have while doing anything at all. What the score keeper counts is money that came
+ * in (supplies, crates, oil, hackers, bounties); production refunds and script grants do not
+ * count as income, which is the answer you want when comparing a rate against a build. */
//-------------------------------------------------------------------------------------------------
void InGameUI::updateIncomeEstimate( Player *player )
{
if( player == NULL || TheGameLogic == NULL )
return;
- const UnsignedInt SAMPLE_FRAMES = 10 * LOGICFRAMES_PER_SECOND;
+ const UnsignedInt SAMPLE_FRAMES = INCOME_SAMPLE_SECONDS * LOGICFRAMES_PER_SECOND;
const UnsignedInt logicFrame = TheGameLogic->getFrame();
- Int money = player->getMoney()->countMoney();
+ const Int playerIndex = player->getPlayerIndex();
+ const Int earned = player->getScoreKeeper()->getTotalMoneyEarned();
- if( m_hudLastMoney < 0 )
+ // a new game, or an observer looking at somebody else: start the window over
+ if( m_incomeSampleCount == 0 || playerIndex != m_incomeSamplePlayer || logicFrame < m_hudLastMoneyFrame )
{
- m_hudLastMoney = money;
+ m_incomeSamplePlayer = playerIndex;
+ m_incomeSamples[ 0 ] = earned;
+ m_incomeSampleCount = 1;
m_hudLastMoneyFrame = logicFrame;
+ m_hudIncomePerMin = -1;
return;
}
- if( logicFrame >= m_hudLastMoneyFrame + SAMPLE_FRAMES )
- {
- Int delta = money - m_hudLastMoney;
- if( delta < 0 )
- delta = 0; // spending is not negative income
- m_hudIncomePerMin = delta * (60 * LOGICFRAMES_PER_SECOND) / (Int)SAMPLE_FRAMES;
- m_hudLastMoney = money;
- m_hudLastMoneyFrame = logicFrame;
- }
+ if( logicFrame < m_hudLastMoneyFrame + SAMPLE_FRAMES )
+ return;
+
+ m_incomeSamples[ m_incomeSampleCount % INCOME_SAMPLES ] = earned;
+ ++m_incomeSampleCount;
+ m_hudLastMoneyFrame = logicFrame;
+
+ // average over every bucket we hold, so the number is live 2 seconds in and settles into a
+ // true 30 second rate once the ring is full
+ m_hudIncomePerMin = computeIncomePerMinute( m_incomeSamples, INCOME_SAMPLES,
+ m_incomeSampleCount, INCOME_SAMPLE_SECONDS );
}
//-------------------------------------------------------------------------------------------------
@@ -5305,7 +5335,7 @@ void InGameUI::updateIncomeEstimate( Player *player )
//-------------------------------------------------------------------------------------------------
void InGameUI::drawIncomeRate( void )
{
- if( !TheGlobalData->m_showHudOverlay || m_hudIncomePerMin <= 0 )
+ if( !TheGlobalData->m_showHudOverlay || m_hudIncomePerMin < 0 )
return;
if( TheGameLogic == NULL || !TheGameLogic->isInGame() || TheGameLogic->isInShellGame() )
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index cc4c62d24..3379ec175 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -618,3 +618,40 @@ TEST(particle_already_leaving_the_surface_is_only_lifted_clear)
CHECK_NEAR(vel.z, 2.0f, 1e-5f);
CHECK_NEAR(vel.x, 1.0f, 1e-5f);
}
+
+// ---------------------------------------------------------------------------------------------
+// The HUD income estimate (InGameUI.cpp). It samples the score keeper's cumulative earnings
+// into a ring of buckets and averages over every bucket it holds.
+// ---------------------------------------------------------------------------------------------
+extern Int computeIncomePerMinute( const Int *samples, UnsignedInt ringSize, UnsignedInt count, Int sampleSeconds );
+
+TEST(income_one_bucket_is_not_a_rate_yet)
+{
+ const Int samples[4] = { 500, 0, 0, 0 };
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 1, 2), -1);
+}
+
+TEST(income_averages_over_the_buckets_held)
+{
+ // two 2s buckets, 100 earned over them -> 100 per 2s -> 3000 a minute
+ const Int samples[4] = { 1000, 1050, 1100, 0 };
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 2, 2), 1500);
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 3, 2), 1500);
+}
+
+TEST(income_window_slides_once_the_ring_wraps)
+{
+ // ring of 4, so at most 3 buckets (6s) are spanned. Earnings: 0,10,20,30 then a 300 jump.
+ Int samples[4] = { 0, 10, 20, 30 };
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 4, 2), 300); // (30-0) over 6s
+
+ samples[4 % 4] = 330; // bucket 4 lands on index 0
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 5, 2), 3200); // (330-10) over 6s, bucket 0 gone
+}
+
+TEST(income_is_zero_not_negative_when_nothing_comes_in)
+{
+ // cumulative earnings never fall, so a dry spell reads as a real zero
+ const Int samples[4] = { 7000, 7000, 7000, 7000 };
+ CHECK_EQ(computeIncomePerMinute(samples, 4, 4, 2), 0);
+}