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.

perf(gameengine): stop every AI doing the same job on the same frame

Olcay Seygan249dd8df7708
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 5525ed62b..d4b06da67 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -166,6 +166,13 @@ public:
virtual Bool computeSuperweaponTarget(const SpecialPowerTemplate *power, Coord3D *pos, Int playerNdx, Real weaponRadius); ///< Calculates best pos for weapon given radius.
+ /** What every computer player spent this logic frame, by job, plus whichever single one cost
+ * the most. The slow-frame report in GameLogic.cpp could only say "players 16.2ms", which is
+ * enough to know a stutter is in here and not enough to do anything about it. Reset once a
+ * frame by AI::update; empty outside a logging build. */
+ static void resetFrameProfile( void );
+ static const char *getProfileReport( void );
+
public: // AIPlayer interface, may be overridden by AISkirmishPlayer. jba.
virtual void update(); ///< simulates the behavior of a player
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index ac3487980..b7ad31a78 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -43,6 +43,7 @@
#include "GameLogic/ScriptEngine.h"
#include "GameLogic/SidesList.h"
#include "GameLogic/AIPathfind.h"
+#include "GameLogic/AIPlayer.h" // for the per-frame AI profile the slow-frame report prints
#include "GameLogic/Weapon.h"
extern void addIcon(const Coord3D *pos, Real width, Int numFramesDuration, RGBColor color);
@@ -381,6 +382,7 @@ void AI::update( void )
// run player updates
{
+ AIPlayer::resetFrameProfile();
ThePlayerList->UPDATE();
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 2cd9a490d..66cc3fe6a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -162,6 +162,22 @@ static Bool playerStartPosition( Int playerNdx, Coord3D *pos )
}
+/* How often each of the periodic jobs re-checks. Up here rather than beside the function that uses
+ each one, because the constructor needs them: it spreads the players across each cycle, and it
+ cannot do that without knowing how long the cycle is. See the staggering block below. */
+
+/** How often the AI looks for somewhere to expand to. Long, because an expansion is a building
+ * order and the base builder has its own rhythm to keep. */
+static const Int EXPANSION_CHECK_SECONDS = 60;
+
+/** How often the scout is looked at. It only ever gets an order when it has arrived, so this is a
+ * check, not a repath. */
+static const Int SCOUT_CHECK_RATE = 2 * LOGICFRAMES_PER_SECOND;
+
+/** How often the AI looks for something to capture. It only ever gets an order when it is idle, so
+ * this is a check, not a re-path. */
+static const Int CAPTURE_CHECK_RATE = 5 * LOGICFRAMES_PER_SECOND;
+
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
AIPlayer::AIPlayer( Player *p ) :
@@ -244,6 +260,27 @@ m_role(AIROLE_AGGRESSIVE)
// ...and EA never initialized this one at all. updateBridgeRepair returns before reading it
// while the repair queue is empty, which it is here, so it has been benign - but only just.
m_bridgeTimer = computeUpdatePhase( playerIndex, LOGICFRAMES_PER_SECOND );
+
+ /* The four jobs this fork added start their timers at 1, so every computer player ran every one
+ of them on the same logic frame - and kept doing so for the rest of the match, because each
+ re-arms with a fixed interval and so never drifts apart again.
+
+ Measured on Twilight Flame with eight Merciless AI: doExpansion alone cost 15.4ms of a
+ 16.7ms frame, all of it landing on one frame a minute. Spread across the cycle it is under
+ 6ms, and nothing runs any less often than it did. */
+ /* What is needed is only that the players land on *different* frames, not that they are spread
+ evenly over the cycle - and the difference matters for the slow ones. Spreading the
+ once-a-minute expansion check over its whole minute delays the last player's first expansion
+ by nearly half of one, which showed up as a smaller army over six matches. A two-second
+ window puts eight players seven frames apart, which is all the convoy needs broken, and
+ costs nobody anything measurable. The offset is permanent either way: each timer re-arms
+ with a fixed interval, so players that start apart stay apart. */
+ const Int SPREAD = 2 * LOGICFRAMES_PER_SECOND;
+ m_expandTimer += computeUpdatePhase( playerIndex, SPREAD );
+ m_captureTimer += computeUpdatePhase( playerIndex, SPREAD );
+ m_retreatTimer += computeUpdatePhase( playerIndex, SPREAD );
+ // The scout check's own cycle is already only two seconds, so its full cycle is the window.
+ m_scoutTimer += computeUpdatePhase( playerIndex, SCOUT_CHECK_RATE );
}
// ------------------------------------------------------------------------------------------------
@@ -3565,30 +3602,100 @@ void AIPlayer::doUpgradesAndSkills( void )
* Perform computer-controlled player AI
*/
//DECLARE_PERF_TIMER(AIPlayer_update)
-void AIPlayer::update( void )
+#ifdef DEBUG_LOGGING
+/* The slow-frame report in GameLogic.cpp could only say "players 16.2ms" - one number for every
+ computer player and all ten of the jobs each one does. That is enough to know the stutter is in
+ here and not enough to do anything about it, so this breaks it down the way ScriptEngine and
+ Pathfinder already break theirs down: per job, plus whichever single player cost the most.
+ Reset once per logic frame by AI::update. */
+enum { AIP_BASE, AIP_READY, AIP_QUEUED, AIP_TEAM, AIP_UPGRADE,
+ AIP_BRIDGE, AIP_SCOUT, AIP_RETREAT, AIP_EXPAND, AIP_CAPTURE, AIP_PHASE_COUNT };
+static const char *theAIPhaseName[ AIP_PHASE_COUNT ] =
+ { "base", "ready", "queued", "team", "upg", "bridge", "scout", "retreat", "expand", "capture" };
+static Real theAIPhaseMS[ AIP_PHASE_COUNT ];
+static Real theAIWorstPlayerMS = 0.0f;
+static Int theAIWorstPlayer = -1;
+static Int theAIPlayersUpdated = 0;
+static Int64 theAIPhaseStart;
+
+static Real aiPlayerElapsedMS( const Int64 &from, const Int64 &to )
{
- //USE_PERF_TIMER(AIPlayer_update)
-
- doBaseBuilding(); // See if it's time to build another building.
-
- checkReadyTeams(); // See if any teams are ready to start.
-
- checkQueuedTeams(); // See if any teams are complete.
-
- doTeamBuilding(); // See if it's time to start another team.
+ static Int64 freq = 0;
+ if( freq == 0 )
+ QueryPerformanceFrequency( (LARGE_INTEGER *)&freq );
+ if( freq == 0 )
+ return 0.0f;
+ return (Real)( (double)(to - from) * 1000.0 / (double)freq );
+}
- doUpgradesAndSkills(); // See if it's time to build an upgrade or buy a skill.
+static void aiPhaseBegin( void )
+{
+ QueryPerformanceCounter( (LARGE_INTEGER *)&theAIPhaseStart );
+}
- updateBridgeRepair(); // Handle any bridge repairs.
+static void aiPhaseEnd( Int phase )
+{
+ Int64 now;
+ QueryPerformanceCounter( (LARGE_INTEGER *)&now );
+ theAIPhaseMS[ phase ] += aiPlayerElapsedMS( theAIPhaseStart, now );
+}
- doScouting(); // Keep one unit looking at the map.
+#define AI_PHASE( phase, call ) do { aiPhaseBegin(); call; aiPhaseEnd( phase ); } while(0)
- doRetreats(); // Break off the fights we are losing.
+/*static*/ void AIPlayer::resetFrameProfile( void )
+{
+ for( Int i = 0; i < AIP_PHASE_COUNT; ++i )
+ theAIPhaseMS[ i ] = 0.0f;
+ theAIWorstPlayerMS = 0.0f;
+ theAIWorstPlayer = -1;
+ theAIPlayersUpdated = 0;
+}
- doExpansion(); // Go and take the money that is lying around.
+/*static*/ const char *AIPlayer::getProfileReport( void )
+{
+ static char report[ 512 ];
+ Int used = sprintf( report, "%d ai players, worst player %d %.1fms |",
+ theAIPlayersUpdated, theAIWorstPlayer, theAIWorstPlayerMS );
+ for( Int phase = 0; phase < AIP_PHASE_COUNT; ++phase )
+ used += sprintf( report + used, " %s %.1f", theAIPhaseName[ phase ], theAIPhaseMS[ phase ] );
+ return report;
+}
+#else
+/*static*/ void AIPlayer::resetFrameProfile( void ) { }
+/*static*/ const char *AIPlayer::getProfileReport( void ) { return ""; }
+#define AI_PHASE( phase, call ) call
+#endif
- doCapture(); // ... and the money that is standing around.
+void AIPlayer::update( void )
+{
+ //USE_PERF_TIMER(AIPlayer_update)
+#ifdef DEBUG_LOGGING
+ Int64 playerStart;
+ QueryPerformanceCounter( (LARGE_INTEGER *)&playerStart );
+#endif
+ AI_PHASE( AIP_BASE, doBaseBuilding() ); // See if it's time to build another building.
+ AI_PHASE( AIP_READY, checkReadyTeams() ); // See if any teams are ready to start.
+ AI_PHASE( AIP_QUEUED, checkQueuedTeams() ); // See if any teams are complete.
+ AI_PHASE( AIP_TEAM, doTeamBuilding() ); // See if it's time to start another team.
+ AI_PHASE( AIP_UPGRADE, doUpgradesAndSkills() ); // See if it's time to build an upgrade or buy a skill.
+ AI_PHASE( AIP_BRIDGE, updateBridgeRepair() ); // Handle any bridge repairs.
+ AI_PHASE( AIP_SCOUT, doScouting() ); // Keep one unit looking at the map.
+ AI_PHASE( AIP_RETREAT, doRetreats() ); // Break off the fights we are losing.
+ AI_PHASE( AIP_EXPAND, doExpansion() ); // Go and take the money that is lying around.
+ AI_PHASE( AIP_CAPTURE, doCapture() ); // ... and the money that is standing around.
+
+#ifdef DEBUG_LOGGING
+ Int64 playerEnd;
+ QueryPerformanceCounter( (LARGE_INTEGER *)&playerEnd );
+ const Real playerMS = aiPlayerElapsedMS( playerStart, playerEnd );
+ ++theAIPlayersUpdated;
+ if( playerMS > theAIWorstPlayerMS )
+ {
+ theAIWorstPlayerMS = playerMS;
+ theAIWorstPlayer = m_player ? m_player->getPlayerIndex() : -1;
+ }
+#endif
}
//----------------------------------------------------------------------------------------------------------
@@ -3711,10 +3818,6 @@ Bool AIPlayer::dozerInQueue( void )
}
//----------------------------------------------------------------------------------------------------------
-/** How often the AI looks for somewhere to expand to. Long, because an expansion is a building
- * order and the base builder has its own rhythm to keep. */
-static const Int EXPANSION_CHECK_SECONDS = 60;
-
//----------------------------------------------------------------------------------------------------------
/** Notice that there is money to be had, and go and take it.
*
@@ -3989,10 +4092,6 @@ void AIPlayer::doRetreats( void )
}
//----------------------------------------------------------------------------------------------------------
-/** How often the scout is looked at. It only ever gets an order when it has arrived, so this is a
- * check, not a repath. */
-static const Int SCOUT_CHECK_RATE = 2 * LOGICFRAMES_PER_SECOND;
-
/** The cheapest thing this player could build that can walk somewhere and is not needed elsewhere.
* Faction-agnostic on purpose - it lands on the Ranger, the Red Guard, the Rebel and the
* Technical without a per-side table to keep, and on whatever a mod's cheapest unit is. */
@@ -4466,10 +4565,6 @@ Bool AIPlayer::nextScoutTarget( Int slot, const Coord3D *from, Coord3D *pos )
}
//----------------------------------------------------------------------------------------------------------
-/** How often the AI looks for something to capture. It only ever gets an order when it is idle, so
- * this is a check, not a re-path. */
-static const Int CAPTURE_CHECK_RATE = 5 * LOGICFRAMES_PER_SECOND;
-
/** The cheapest infantry this player can build that can walk into a building and own it. Read off
* the command set rather than a per-faction table, so it lands on the Ranger, the Rebel and the
* Red Guard - and on whatever a mod gave the ability to. */
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 2a5c790e2..cffdf2e5a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -79,6 +79,7 @@
#include "GameClient/GameWindowTransitions.h"
#include "GameLogic/AI.h"
+#include "GameLogic/AIPlayer.h" // for the per-frame AI profile the slow-frame report prints
#include "GameLogic/AIPathfind.h"
#include "GameLogic/CaveSystem.h"
#include "GameLogic/CrateSystem.h"
@@ -4169,12 +4170,13 @@ void GameLogic::update( void )
const Real stores = logicElapsedMS( tCommandList, tStores );
const Real victory = logicElapsedMS( tStores, tVictory );
const Real disabled = logicElapsedMS( tVictory, tFrameEnd );
- DEBUG_LOG(("SLOW LOGIC FRAME %d: %.1fms | scripts %.1f | objects %.1f | ai %.1f (pathfind %.1f, players %.1f) | partition %.1f | rest %.1f (destroy %.1f, cmdlist %.1f, stores %.1f, victory %.1f, disabled %.1f)\n sc: %s\n ob: %s\n pf: %s\n",
+ DEBUG_LOG(("SLOW LOGIC FRAME %d: %.1fms | scripts %.1f | objects %.1f | ai %.1f (pathfind %.1f, players %.1f) | partition %.1f | rest %.1f (destroy %.1f, cmdlist %.1f, stores %.1f, victory %.1f, disabled %.1f)\n sc: %s\n ob: %s\n pf: %s\n ai: %s\n",
now, total, scripts, objects, ai, AI::getLastPathfindMS(), AI::getLastPlayerUpdateMS(), partition, rest,
destroy, cmdlist, stores, victory, disabled,
TheScriptEngine->getProfileReport(),
getModuleProfileReport(),
- Pathfinder::getProfileReport()));
+ Pathfinder::getProfileReport(),
+ AIPlayer::getProfileReport()));
}
}