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): ai income bonus compares teams

Olcay Seygan958f08ebc5e5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6c65baf8e..ed88aec68 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,7 +28,7 @@ found and fixed, EA's own, not port damage.**
## Computer players earn half as much again, and more when they fall behind
- A computer player in a skirmish or a network match earns half as much again as you would, on every difficulty, where it used to earn double. A truckload worth 75 banks 112, and the 1000 for capturing an oil derrick is 1500. It counts everything the computer makes by playing: supply docks, oil derricks and other tech buildings, black markets, drop zones, hackers, bounties, crates, salvage and stolen cash.
-- A computer that falls behind earns more. Every thirty seconds it weighs what it owns, its army, its base and its bank at their prices, against the strongest enemy still in the match. Level or ahead, it stays at half again. Behind, it gains up to 25 points of income at each weighing, toward double at three quarters of the enemy's worth and triple at a quarter and below, so a computer that is being crushed reaches triple in three minutes. It gives the points back the same way as it catches up.
+- A computer that falls behind earns more. Every thirty seconds it weighs its team against the strongest enemy team still in the match, each team's armies, bases and banks at their prices, divided by its living players, so a team of three does not look rich next to a lone enemy just for having three bases. Its human allies count toward its team, and in a free-for-all every enemy is a team of one. Level or ahead, it stays at half again. Behind, it gains up to 25 points of income at each weighing, toward double at three quarters of the enemy team's worth and triple at a quarter and below, so a computer that is being crushed reaches triple in three minutes. It gives the points back the same way as it catches up.
- The score screen and its charts show what the computer would have earned without the bonus, so its money earned reads against yours on the same terms.
- Money it gets back stays what it was. Selling a building or cancelling a queue returns the same sum as yours does, or a computer could queue and cancel its way to a fortune, and cash from a map's script or from an ally gets no bonus either. The money figure floating over a computer's truck or building shows what it really banked.
- Your own income does not change, and your cut of a computer ally's income under Share Income is the plain cut. Campaign missions and Generals' Challenge opponents earn what they always did.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/Player.h b/GeneralsMD/Code/GameEngine/Include/Common/Player.h
index c80ff707b..5ad6655d9 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/Player.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/Player.h
@@ -193,6 +193,11 @@ UnsignedInt EarnedAmount( UnsignedInt amount, Int percent );
// strength against its strongest living enemy's: 0 while it holds level, rising in a line to
// AI_INCOME_BONUS_MAX at a quarter of it and below. Integers only.
Int IncomeBonusTarget( Int ownStrength, Int strongestEnemyStrength );
+// The strengths that target is fed: of count living players in list order, worth[i] each and allies[i]
+// a bit for every player mutually allied with i, self's side (itself and its allies) a player at a
+// time, and the highest per-player average of the other sides, each a leader in list order and the
+// players still ungrouped who are its mutual allies. Free-for-all, every enemy is a side of one.
+void IncomeSideWorths( const Int *worth, const UnsignedInt *allies, Int count, Int self, Int *ownAverage, Int *strongestEnemyAverage );
// One weighing's move from the bonus it has toward that target, at most AI_INCOME_BONUS_STEP.
Int IncomeBonusStep( Int bonus, Int target );
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
index e84ecfab4..23328c667 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
@@ -3604,6 +3604,39 @@ Int IncomeBonusTarget( Int ownStrength, Int strongestEnemyStrength )
return bonus < AI_INCOME_BONUS_MAX ? bonus : AI_INCOME_BONUS_MAX;
}
+void IncomeSideWorths( const Int *worth, const UnsignedInt *allies, Int count, Int self, Int *ownAverage, Int *strongestEnemyAverage )
+{
+ UnsignedInt grouped = 0;
+ *ownAverage = 0;
+ *strongestEnemyAverage = 0;
+ for( Int lead = 0; lead < count; ++lead )
+ {
+ if( grouped & ( 1u << lead ) )
+ continue;
+ // the leader's own side when it is this player or one of its allies, else the next enemy side
+ const Bool ours = lead == self || ( allies[ self ] & ( 1u << lead ) ) != 0;
+ Int64 sum = 0;
+ Int size = 0;
+ for( Int i = lead; i < count; ++i )
+ {
+ if( grouped & ( 1u << i ) )
+ continue;
+ const Bool member = ours ? ( i == self || ( allies[ self ] & ( 1u << i ) ) != 0 )
+ : ( i == lead || ( allies[ lead ] & ( 1u << i ) ) != 0 );
+ if( !member )
+ continue;
+ grouped |= 1u << i;
+ sum += worth[ i ];
+ ++size;
+ }
+ const Int average = (Int)( sum / size );
+ if( ours )
+ *ownAverage = average;
+ else if( average > *strongestEnemyAverage )
+ *strongestEnemyAverage = average;
+ }
+}
+
Int IncomeBonusStep( Int bonus, Int target )
{
if( target > bonus + AI_INCOME_BONUS_STEP )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 3f97055c3..7acf0f9bf 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -8449,29 +8449,44 @@ static Int incomeStrength( Player *player )
//----------------------------------------------------------------------------------------------------------
/** The owner, 2026-11-02: a computer that starts losing earns more. Every AI_INCOME_BONUS_FRAMES it weighs
- * itself against the strongest enemy still in the match and moves the bonus a step toward what
+ * its side, itself and every ally humans included, a player at a time against the strongest enemy side
+ * still in the match (IncomeSideWorths), and moves the bonus a step toward what
* IncomeBonusTarget says that is owed, so the percent climbs and falls by at most AI_INCOME_BONUS_STEP
* points at a time. The score screen still counts the plain amount. */
//----------------------------------------------------------------------------------------------------------
void AIPlayer::stepIncomeBonus( void )
{
- Int strongest = 0;
+ // the living sides in the match that are this player, its allies or its enemies, in player list order
+ Player *seat[ MAX_PLAYER_COUNT ];
+ Int worth[ MAX_PLAYER_COUNT ];
+ Int count = 0;
+ Int self = 0;
for( Int i = 0; i < ThePlayerList->getPlayerCount(); ++i )
{
Player *other = ThePlayerList->getNthPlayer( i );
- if( other != m_player && other->isPlayableSide() && other->isPlayerActive()
- && m_player->getRelationship( other->getDefaultTeam() ) == ENEMIES )
- {
- const Int strength = incomeStrength( other );
- if( strength > strongest )
- strongest = strength;
- }
+ if( other == m_player )
+ self = count;
+ else if( !other->isPlayableSide() || !other->isPlayerActive() || m_player->getRelationship( other->getDefaultTeam() ) == NEUTRAL )
+ continue;
+ seat[ count ] = other;
+ worth[ count ] = incomeStrength( other );
+ ++count;
+ }
+ UnsignedInt allies[ MAX_PLAYER_COUNT ];
+ for( Int i = 0; i < count; ++i )
+ {
+ allies[ i ] = 0;
+ for( Int j = 0; j < count; ++j )
+ if( j != i && seat[ i ]->getRelationship( seat[ j ]->getDefaultTeam() ) == ALLIES
+ && seat[ j ]->getRelationship( seat[ i ]->getDefaultTeam() ) == ALLIES )
+ allies[ i ] |= 1u << j;
}
- const Int own = incomeStrength( m_player );
+ Int own, strongest;
+ IncomeSideWorths( worth, allies, count, self, &own, &strongest );
const Int bonus = IncomeBonusStep( m_incomeBonus, IncomeBonusTarget( own, strongest ) );
if( bonus == m_incomeBonus )
return;
- DEBUG_LOG(( "AI INCOME frame %d player %d worth %d against %d, income %d%% -> %d%%\n",
+ DEBUG_LOG(( "AI INCOME frame %d player %d side worth %d a player against %d, income %d%% -> %d%%\n",
TheGameLogic->getFrame(), m_player->getPlayerIndex(), own, strongest,
m_player->incomePercent(), m_player->incomePercent() - m_incomeBonus + bonus ));
m_incomeBonus = bonus;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 4eb902a85..8d8daf30b 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -2424,6 +2424,26 @@ TEST(skirmish_ai_earnings_bonus_when_behind)
CHECK_EQ( IncomeBonusStep( 5, 0 ), 0 );
}
+/* Player.cpp: the bonus weighs sides, a player at a time, not one seat against another. */
+TEST(skirmish_ai_earnings_bonus_weighs_sides)
+{
+ Int own = -1, enemy = -1;
+ /* four teams of two, seats 0-1, 2-3, 4-5, 6-7; the computer is seat 1 and its partner carries it */
+ const Int worth[ 8 ] = { 1000, 0, 3000, 1000, 4000, 0, 500, 500 };
+ const UnsignedInt allies[ 8 ] = { 1u << 1, 1u << 0, 1u << 3, 1u << 2, 1u << 5, 1u << 4, 1u << 7, 1u << 6 };
+ IncomeSideWorths( worth, allies, 8, 1, &own, &enemy );
+ CHECK_EQ( own, 500 );
+ CHECK_EQ( enemy, 2000 );
+ /* a quarter of the strongest side's average: the ceiling */
+ CHECK_EQ( IncomeBonusTarget( own, enemy ), 150 );
+ /* free for all: every enemy is a side of one, the richest counts */
+ const Int ffa[ 3 ] = { 300, 100, 200 };
+ const UnsignedInt none[ 3 ] = { 0, 0, 0 };
+ IncomeSideWorths( ffa, none, 3, 1, &own, &enemy );
+ CHECK_EQ( own, 100 );
+ CHECK_EQ( enemy, 300 );
+}
+
/* ExperienceTracker.cpp: a kill's experience split by the damage each attacker dealt. */
TEST(kill_xp_splits_by_damage_and_the_killing_blow_keeps_the_remainder)
{