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): stop -noFPSLimit from running the match in fast-forward

Olcay Seyganc597580e94de
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bf63538f1..22eca0332 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1128,11 +1128,15 @@ every build. They are not decoration: **most of the bugs in this document were f
not by reading the code.** Linking proves nothing; running proves something.
**The game can now play itself.** One command line starts an eight-player skirmish with no menus:
-seven computer opponents, random factions on random corners of the map, and you watching from an
-observer's camera with nothing to command. Nothing holds the simulation back either, so a match that
-would take half an hour runs in a fraction of it. That is how a fault that shows up once in a
-hundred thousand orders gets found - not by playing carefully, but by letting the machine play
-badly, thousands of times, and recording every match it plays.
+eight computer opponents, random factions on random corners of the map, and a free camera with
+nothing to command - watching costs no seat, so the map is full. The match can be told to run at
+several times its normal pace as well, so a game that would take half an hour is over in a fraction
+of it. That is how a fault that shows up once in a hundred thousand orders gets found - not by
+playing carefully, but by letting the machine play badly, thousands of times, and recording every
+match it plays. It also caught itself doing it wrong: for a while that fast-forward switch was
+speeding the *game* up rather than the picture, which is why every countdown on screen ran out
+almost as soon as it appeared. The picture and the rules are on separate clocks now, and that switch
+respects the difference.
**Every fix was proved non-vacuous.** Put the bug back, watch the exact test fail, restore the fix.
A test that passes both ways is worth nothing, and there are none of those here.
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 241eabc6d..b23dae231 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -527,17 +527,6 @@ Int parseUseWaveEditor(char *args[], int num)
return 1;
}
-//=============================================================================
-//=============================================================================
-Int parseFPSLimit(char *args[], int num)
-{
- if (TheWritableGlobalData && num > 1)
- {
- TheWritableGlobalData->m_framesPerSecondLimit = atoi(args[1]);
- }
- return 2;
-}
-
//=============================================================================
Int parseNoViewLimit(char *args[], int)
{
@@ -1110,12 +1099,31 @@ Int parseIgnoreStackTrace(char *args[], int num)
}
#endif
+/* -fps <n> is the game speed itself: the rate the logic tick is paced at, the command-line twin of
+ the skirmish menu's game speed slider. MSG_NEW_GAME only honours 1..1000. */
+
+Int parseFPSLimit(char *args[], int num)
+{
+ if (TheWritableGlobalData && num > 1)
+ {
+ TheWritableGlobalData->m_framesPerSecondLimit = atoi(args[1]);
+ }
+ return 2;
+}
+
+/* -noFPSLimit uncaps the *renderer*, not the simulation. It used to raise
+ m_framesPerSecondLimit to 30000 because the old loop ran one logic frame per pass, so the only
+ way to render freely was to let the logic run freely too - and the game then played at whatever
+ speed the machine managed. GameEngine::update() now paces the logic tick against wall clock
+ and GameEngine::execute() never sleeps for a frame budget, so rendering is already uncapped
+ unconditionally and m_framesPerSecondLimit means game speed and nothing else. Raising it here
+ would just run the match in fast-forward: superweapon and build timers count real seconds off a
+ clock ticking a thousand times too fast. Use -fps to ask for that on purpose. */
Int parseNoFPSLimit(char *args[], int num)
{
if (TheWritableGlobalData)
{
TheWritableGlobalData->m_useFpsLimit = false;
- TheWritableGlobalData->m_framesPerSecondLimit = 30000;
}
return 1;
}
@@ -1253,7 +1261,6 @@ static CommandLineParam params[] =
{ "-noDynamicLOD", parseNoDynamicLOD },
{ "-noStaticLOD", parseNoStaticLOD },
{ "-useWaveEditor", parseUseWaveEditor },
- { "-fps", parseFPSLimit },
{ "-wireframe", parseWireframe },
{ "-showCollision", parseShowCollision },
{ "-noShowClientPhysics", parseNoShowClientPhysics },
@@ -1299,10 +1306,12 @@ static CommandLineParam params[] =
/* Outside the _DEBUG/_INTERNAL block on purpose: an unattended Release run is driven entirely
from the command line. -autoskirmish needs a map to play, -seed makes the run repeatable,
- and without -noFPSLimit the simulation is held to the frame rate a human watches at. */
+ -noFPSLimit lets the renderer run free, and -fps is the one knob that changes how fast the
+ match itself is simulated - the command-line twin of the skirmish menu's game speed slider. */
{ "-map", parseMapName },
{ "-seed", parseSeed },
{ "-noFPSLimit", parseNoFPSLimit },
+ { "-fps", parseFPSLimit },
{ "-autoskirmish", parseAutoSkirmish },
{ "-aidiff", parseAIDifficulty },
{ "-observer", parseObserver },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index a46550bfd..de1305ca0 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -300,19 +300,22 @@ static void startAutoSkirmish( void )
TheSkirmishGameInfo->reset();
TheSkirmishGameInfo->enterGame();
+ /* Watching costs no seat. GameLogic::startNewGame always adds a "ReplayObserver" side after
+ the slots, and PlayerList::newGame makes the first human side the local player - so a slot
+ list with nothing but AI in it leaves that observer holding the camera, exactly the way a
+ replay does. Spending a slot on the observer instead would cost a bot, because MAX_SLOTS is
+ 8 and that is also the most start positions a map has. */
+ const Bool observing = TheGlobalData->m_autoSkirmishObserver;
UnicodeString localName;
localName.translate( AsciiString( "Player" ) );
for( Int i = 0; i < numPlayers; i++ )
{
GameSlot slot;
- if (i == 0)
+ if (i == 0 && !observing)
{
slot.setState( SLOT_PLAYER, localName );
slot.setName( localName );
- /* An observer has no units and no base, so the eight seats a map has all go to the AI:
- the local slot only carries the camera. */
- slot.setPlayerTemplate( TheGlobalData->m_autoSkirmishObserver ? PLAYERTEMPLATE_OBSERVER
- : PLAYERTEMPLATE_RANDOM );
+ slot.setPlayerTemplate( PLAYERTEMPLATE_RANDOM );
}
else
{
@@ -337,9 +340,15 @@ static void startAutoSkirmish( void )
TheWritableGlobalData->m_shellMapOn = FALSE;
TheWritableGlobalData->m_playIntro = FALSE;
- /* The menu passes the game speed slider's position here; -noFPSLimit has already put 30000 in
- m_framesPerSecondLimit, which is what makes this run as fast as the machine can. */
- const Int maxFPS = TheGlobalData->m_framesPerSecondLimit;
+ /* The menu passes the game speed slider's position here, so pass the same thing and clamp it
+ the same way reallyDoStart() does: MSG_NEW_GAME rejects anything outside 1..1000 by falling
+ back to m_framesPerSecondLimit *unclamped*, which is how a stray -fps would still get through.
+ -noFPSLimit deliberately does not appear here - it uncaps the renderer, not the simulation. */
+ Int maxFPS = TheGlobalData->m_framesPerSecondLimit;
+ if (maxFPS < 15)
+ maxFPS = DEFAULT_MAX_FPS;
+ if (maxFPS > 1000)
+ maxFPS = 1000;
InitRandom( seed );
GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
@@ -348,9 +357,9 @@ static void startAutoSkirmish( void )
msg->appendIntegerArgument( 0 );
msg->appendIntegerArgument( maxFPS );
- DEBUG_LOG(("-autoskirmish: %d players on '%s', seed %d, up to %d fps, local slot %s\n",
+ DEBUG_LOG(("-autoskirmish: %d slots on '%s', seed %d, up to %d fps, %s\n",
numPlayers, mapName.str(), seed, maxFPS,
- TheGlobalData->m_autoSkirmishObserver ? "observing" : "playing"));
+ observing ? "every slot AI, watching from the free camera" : "slot 0 is the local player"));
}
/** -----------------------------------------------------------------------------------------------
@@ -787,6 +796,12 @@ void GameEngine::init( int argc, char *argv[] )
}
catch (ErrorCode ec)
{
+ /* Every ErrorCode but ERROR_INVALID_D3D used to leave here without a word, and init then ran
+ on to its tail with half its subsystems missing - the first frame faulted on a NULL
+ TheGameLogic and the crash log blamed GameEngine::update() for a failure that happened
+ during startup. Say which code it was and where it left the engine. */
+ DEBUG_LOG(("GameEngine::init - ErrorCode %d escaped initialization (TheGameLogic=%p, TheGameClient=%p)\n",
+ (Int)ec, TheGameLogic, TheGameClient));
if (ec == ERROR_INVALID_D3D)
{
RELEASE_CRASHLOCALIZED("ERROR:D3DFailurePrompt", "ERROR:D3DFailureMessage");
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
index 057ee6d04..a97cc2769 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
@@ -1654,6 +1654,10 @@ void* MemoryPool::allocateBlockDoNotZeroImplementation(DECLARE_LITERALSTRING_ARG
{
if (m_overflowAllocationCount == 0)
{
+ // Say which pool ran dry. The throw unwinds into a catch that swallows it, so without
+ // this the only trace left is a NULL subsystem faulting several frames later.
+ DEBUG_LOG(("MemoryPool '%s' is out of blocks (%d of %d bytes) and may not grow\n",
+ m_poolName, m_initialAllocationCount, m_allocationSize));
throw ERROR_OUT_OF_MEMORY; // this pool is not allowed to grow
}
else
@@ -2659,6 +2663,10 @@ MemoryPool *MemoryPoolFactory::createMemoryPool(const char *poolName, Int alloca
if (initialAllocationCount <= 0 || overflowAllocationCount < 0)
{
DEBUG_CRASH(("illegal pool size: %d %d\n",initialAllocationCount,overflowAllocationCount));
+ // A pool name absent from MemoryInit.cpp's table arrives here as -1, -1. Name it: the
+ // throw unwinds into a catch that swallows it, so otherwise nothing says which pool.
+ DEBUG_LOG(("createMemoryPool - pool '%s' has an illegal size (%d initial, %d overflow)\n",
+ poolName, initialAllocationCount, overflowAllocationCount));
throw ERROR_OUT_OF_MEMORY;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/LoadScreen.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/LoadScreen.cpp
index c05db181d..5d076da9f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/LoadScreen.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/LoadScreen.cpp
@@ -1331,9 +1331,12 @@ void MultiPlayerLoadScreen::init( GameInfo *game )
m_loadScreen->winHide(FALSE);
m_loadScreen->winBringToTop();
m_mapPreview = TheWindowManager->winGetWindowFromId( m_loadScreen,TheNameKeyGenerator->nameToKey( "MultiplayerLoadScreen.wnd:WinMapPreview"));
+ // getLocalSlotNum() is -1 when no slot in the list is the local player - a game watched from
+ // the observer camera rather than played - and getSlot(-1) hands back NULL. Same guard the
+ // score screen already has.
GameSlot *lSlot = game->getSlot(game->getLocalSlotNum());
const PlayerTemplate* pt;
- if (lSlot->getPlayerTemplate() >= 0)
+ if (lSlot && lSlot->getPlayerTemplate() >= 0)
pt = ThePlayerTemplateStore->getNthPlayerTemplate(lSlot->getPlayerTemplate());
else
pt = ThePlayerTemplateStore->findPlayerTemplate( TheNameKeyGenerator->nameToKey("FactionObserver") );
@@ -1534,10 +1537,15 @@ void MultiPlayerLoadScreen::update( Int percent )
void MultiPlayerLoadScreen::processProgress(Int playerId, Int percentage)
{
- if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 || m_playerLookup[playerId] == -1)
+ // The test named every way playerId can be wrong and then indexed m_playerLookup with
+ // it anyway; in Release the assert is not compiled in at all, so a -1 read past the array.
+ if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 )
{
DEBUG_ASSERTCRASH(FALSE, ("Percentage %d was passed in for Player %d\n", percentage, playerId));
+ return;
}
+ if( m_playerLookup[playerId] == -1)
+ return;
//DEBUG_LOG(("Percentage %d was passed in for Player %d (in loadscreen position %d)\n", percentage, playerId, m_playerLookup[playerId]));
if(m_progressBars[m_playerLookup[playerId]])
GadgetProgressBarSetProgress(m_progressBars[m_playerLookup[playerId]], percentage );
@@ -1601,9 +1609,12 @@ void GameSpyLoadScreen::init( GameInfo *game )
m_mapPreview = TheWindowManager->winGetWindowFromId( m_loadScreen,TheNameKeyGenerator->nameToKey( "GameSpyLoadScreen.wnd:WinMapPreview"));
DEBUG_ASSERTCRASH(TheNetwork, ("Where the Heck is the Network!!!!"));
DEBUG_LOG(("NumPlayers %d\n", TheNetwork->getNumPlayers()));
-GameSlot *lSlot = game->getSlot(game->getLocalSlotNum());
+ // getLocalSlotNum() is -1 when no slot in the list is the local player - a game watched from
+ // the observer camera rather than played - and getSlot(-1) hands back NULL. Same guard the
+ // score screen already has.
+ GameSlot *lSlot = game->getSlot(game->getLocalSlotNum());
const PlayerTemplate* pt;
- if (lSlot->getPlayerTemplate() >= 0)
+ if (lSlot && lSlot->getPlayerTemplate() >= 0)
pt = ThePlayerTemplateStore->getNthPlayerTemplate(lSlot->getPlayerTemplate());
else
pt = ThePlayerTemplateStore->findPlayerTemplate( TheNameKeyGenerator->nameToKey("FactionObserver") );
@@ -1897,10 +1908,15 @@ void GameSpyLoadScreen::update( Int percent )
void GameSpyLoadScreen::processProgress(Int playerId, Int percentage)
{
- if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 || m_playerLookup[playerId] == -1)
+ // The test named every way playerId can be wrong and then indexed m_playerLookup with
+ // it anyway; in Release the assert is not compiled in at all, so a -1 read past the array.
+ if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 )
{
DEBUG_ASSERTCRASH(FALSE, ("Percentage %d was passed in for Player %d\n", percentage, playerId));
+ return;
}
+ if( m_playerLookup[playerId] == -1)
+ return;
//DEBUG_LOG(("Percentage %d was passed in for Player %d (in loadscreen position %d)\n", percentage, playerId, m_playerLookup[playerId]));
if(m_progressBars[m_playerLookup[playerId]])
GadgetProgressBarSetProgress(m_progressBars[m_playerLookup[playerId]], percentage );
@@ -2049,10 +2065,15 @@ void MapTransferLoadScreen::update( Int percent )
void MapTransferLoadScreen::processProgress(Int playerId, Int percentage, AsciiString stateStr)
{
- if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 || m_playerLookup[playerId] == -1)
+ // The test named every way playerId can be wrong and then indexed m_playerLookup with
+ // it anyway; in Release the assert is not compiled in at all, so a -1 read past the array.
+ if( percentage < 0 || percentage > 100 || playerId >= MAX_SLOTS || playerId < 0 )
{
DEBUG_ASSERTCRASH(FALSE, ("Percentage %d was passed in for Player %d\n", percentage, playerId));
+ return;
}
+ if( m_playerLookup[playerId] == -1)
+ return;
if (m_oldProgress[playerId] == percentage)
return;