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(gameengine): ai rates its chance side against side

Olcay Seygan666e88343cab
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 25fe4c86d..8736e1531 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -153,6 +153,7 @@ found and fixed, EA's own, not port damage.**
- **Random** picks one of the above when the match starts and does not tell you which.
- Computer allies with a personality answer their team. When an ally's base is hit, an Air Force strikes the attacker, a Supporter comes home to defend, an Attacker or Rusher hits back at the attacker's base and a Tech turns its powers on him. An Economist sends money to an ally who runs dry.
- Allied computer players join the attack a teammate has started, so a team hits one enemy together.
+- Seven computers against one strong player now count each other. Each used to weigh its own army against all of yours, decide it was outmatched and keep its tanks at home: seven Hard players sat on defence for fourteen minutes against 180 tanks, with 455 units between them. A computer now adds its allies' armies, Hard all of them, Medium three quarters and Easy half, and counts your allies against it the same way. The same 180 tanks fell 100 seconds sooner to Hard and three minutes sooner to Medium, and even teams play as they did. Replays recorded before this change play out differently.
- You can call them too. A beacon you place asks your computer allies to attack there, and typing `help`, `attack` or `money` in chat (`yardim`, `saldir`, `para` in Turkish) asks for help where you are looking, an attack there, or cash.
- Or without typing a word. Alt+Z asks your allies to attack the spot under the pointer, Alt+X to come and defend it, Alt+V for money, and Alt+C only marks a spot to look at. The pointer can be on the radar as well. People on your team see the smoke, the radar ping and a line in the chat; computer allies with a personality act on it, in a skirmish against the computer too, where the keys used to do nothing. The keys come from the launcher and move on its Hotkeys page like any other.
- A personality is a preference, never a bonus: no personality gets extra money, faster building or tougher units.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index db0c981c3..6c220faf7 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -926,6 +926,10 @@ enum AIPressure
* have, so one rifleman against nothing is not a rout. */
Real aiWinChance( Real myPower, Real enemyPower );
+/** The same chance, side against side. An ally's army counts by allyShare, and each ally of the enemy
+ * counts by the same share as an army like his, so even teams rate the way one against one does. */
+Real aiSideWinChance( Real myPower, Real alliedPower, Real enemyPower, Int enemyAllies, Real allyShare );
+
/** The level that chance asks for. A level is left later than it is entered, so a chance sitting on
* a threshold does not change the orders every two seconds: a Hard AI watching an army walk in and
* out of its sight changed level six times in a minute on a chance between 0.58 and 0.66. PRESS and
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index bd3312c4e..1c011d9b6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -2247,6 +2247,12 @@ Real aiWinChance( Real myPower, Real enemyPower )
return (myPower + PRIOR) / (myPower + enemyPower + 2.0f * PRIOR);
}
+//-------------------------------------------------------------------------------------------------
+Real aiSideWinChance( Real myPower, Real alliedPower, Real enemyPower, Int enemyAllies, Real allyShare )
+{
+ return aiWinChance( myPower + alliedPower * allyShare, enemyPower * (1.0f + INT_TO_REAL( enemyAllies ) * allyShare) );
+}
+
//-------------------------------------------------------------------------------------------------
AIPressure aiPressureLevel( Real chance, Bool enemyBaseInSight, AIPressure current )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 35a6b5f9d..b9e916186 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -10313,6 +10313,8 @@ static void addKnownArmy( Object *obj, void *userData )
* it, and how much of the way back to an army like its own the belief goes for each look not taken. */
static const Real KNOWN_ARMY_KEPT = 0.9f;
static const Real UNSEEN_ARMY_GROWS = 0.02f;
+/** How much of an ally's army, or an enemy ally's, this AI counts on when it rates its chance, by rung. */
+static const Real ALLY_ARMY_SHARE[ AISKILL_COUNT ] = { 0.5f, 0.75f, 1.0f };
/** How often the guards at home are sent hunting once the enemy is finished. Not every wave tick: a
* guard whose script puts it straight back on guard would be sent again every two seconds. */
@@ -10408,7 +10410,12 @@ Bool AIPlayer::leavesToFinish( const Object *obj ) const
* had both sides four minutes in at 21,300 against 800 and 14,700 against 1,200, each pressing an
* enemy it had seen one corner of, and the waves were never massed again. Looking at the guessed
* start had a USA at (4367,3119) watching an empty start at (4426,1630) beside its own base, its
- * belief falling a tenth every two seconds, while the GLA it was fighting stood at (460,1679). */
+ * belief falling a tenth every two seconds, while the GLA it was fighting stood at (460,1679).
+ *
+ * It is rated side against side: seven Hard AIs against one player with 180 tanks each weighed its own
+ * army against all of his, and all seven sat at home on defend for fourteen minutes with 230 of their 455
+ * units in their bases. An ally's army counts by the rung's ALLY_ARMY_SHARE, and so does each ally of his,
+ * at what is believed of him, so an even four against four rates as one against one did. */
//----------------------------------------------------------------------------------------------------------
void AIPlayer::updatePressure( void )
{
@@ -10460,7 +10467,21 @@ void AIPlayer::updatePressure( void )
rememberedArmy( &theirArmy );
myPower *= (Real)sqrt( armyAdvantage( myArmy, theirArmy ) );
}
- const Real chance = aiWinChance( myPower, m_knownEnemyPower );
+ KnownArmy allies = { -1, 0.0f, 0, 0.0f, 0.0f };
+ Int enemyAllies = 0;
+ for( Int i = 0; i < ThePlayerList->getPlayerCount(); ++i )
+ {
+ Player *other = ThePlayerList->getNthPlayer( i );
+ if( other == m_player || other == enemy )
+ continue;
+ const Team *otherTeam = other->getDefaultTeam();
+ if( m_player->getRelationship( otherTeam ) == ALLIES && other->getRelationship( enemy->getDefaultTeam() ) == ENEMIES )
+ other->iterateObjects( addKnownArmy, &allies );
+ else if( m_player->getRelationship( otherTeam ) == ENEMIES && enemy->getRelationship( otherTeam ) == ALLIES &&
+ other->hasAnyUnits() && other->hasAnyBuildFacility() )
+ ++enemyAllies;
+ }
+ const Real chance = aiSideWinChance( myPower, allies.power, m_knownEnemyPower, enemyAllies, ALLY_ARMY_SHARE[ m_skillLevel ] );
AIPressure level = aiPressureLevel( chance, baseInSight, m_pressure );
// the guards stay while anybody else is still in the game to walk into the base they left
@@ -10477,8 +10498,8 @@ void AIPlayer::updatePressure( void )
if( level == m_pressure )
return;
static const char *const names[ AIPRESSURE_COUNT ] = { "defend", "normal", "press", "finish" };
- DEBUG_LOG(("AI PRESSURE frame %d player %d chance %.2f level %s, %.0f power (%.0f by the units) against %.0f believed\n", TheGameLogic->getFrame(),
- me, chance, names[ level ], mine.power, myPower, m_knownEnemyPower));
+ DEBUG_LOG(("AI PRESSURE frame %d player %d chance %.2f level %s, %.0f power (%.0f by the units) and %.0f allied against %.0f believed and %d allies\n", TheGameLogic->getFrame(),
+ me, chance, names[ level ], mine.power, myPower, allies.power, m_knownEnemyPower, enemyAllies));
m_pressure = level;
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 35cb2b249..cc41e0b5c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -15059,6 +15059,13 @@ TEST(pressure_rises_with_the_chance_of_winning_and_falls_back_with_it)
// one rifleman against an empty field is not a rout
CHECK( aiWinChance( 200.0f, 0.0f ) < 0.65f );
+ // no allies on either side is the plain chance, and even teams are the plain even match
+ CHECK_NEAR( aiWinChance( 3000.0f, 9000.0f ), aiSideWinChance( 3000.0f, 0.0f, 9000.0f, 0, 1.0f ), 0.0001f );
+ CHECK_NEAR( 0.5f, aiSideWinChance( 4000.0f, 12000.0f, 4000.0f, 3, 1.0f ), 0.0001f );
+ // seven against one player three times the size of each: not outmatched, though each is alone
+ CHECK_EQ( AIPRESSURE_PRESS, aiPressureLevel( aiSideWinChance( 3000.0f, 18000.0f, 9000.0f, 0, 1.0f ), TRUE, AIPRESSURE_DEFEND ) );
+ CHECK_EQ( AIPRESSURE_DEFEND, aiPressureLevel( aiSideWinChance( 3000.0f, 18000.0f, 9000.0f, 5, 1.0f ), FALSE, AIPRESSURE_NORMAL ) );
+
CHECK_EQ( AIPRESSURE_NORMAL, aiPressureLevel( 0.5f, TRUE, AIPRESSURE_NORMAL ) );
CHECK_EQ( AIPRESSURE_PRESS, aiPressureLevel( aiWinChance( 9000.0f, 3000.0f ), TRUE, AIPRESSURE_NORMAL ) );
CHECK_EQ( AIPRESSURE_DEFEND, aiPressureLevel( aiWinChance( 3000.0f, 9000.0f ), FALSE, AIPRESSURE_NORMAL ) );