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): seed all three random streams when a game starts

Olcay Seygan8271b0377f02
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7ce11df22..7bbcd0f48 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -167,6 +167,14 @@ map. Ctrl+A is still everything at once.
match was built from and started the next attempt on a different one, so the replay it was writing
no longer matched the game being played and would not play back. Restart now hands the game back
the number it had.
+- **A replay now sounds and looks the way it did when you played it.** The game runs three separate
+ streams of random numbers: one for the simulation, one for what you see, one for what you hear —
+ which voice line a unit picks, which way a spark flies. Starting a match set the simulation's
+ stream from the number the match was built on and left the other two on whatever the clock said
+ when the program opened. So the battle replayed exactly, and everything around it did not: a
+ different clock, a different set of voices and sparks, every single time. All three start from the
+ match's own number now. The same thing was true of the two machines in a network game, which is a
+ worse place for a number that differs per machine to be sitting.
- **And it restarts with the same colours, the same corners and the same armies.** "Random" colour,
"random" starting position and "random" army are drawn from that same shared number the moment the
match begins. On a restart they had already been drawn once — the slots now held real answers — so
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/RandomValue.h b/GeneralsMD/Code/GameEngine/Include/Common/RandomValue.h
index bc174821f..b18b4896e 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/RandomValue.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/RandomValue.h
@@ -35,7 +35,6 @@
extern void InitRandom( void );
extern void InitRandom( UnsignedInt seed );
-extern void InitGameLogicRandom( UnsignedInt seed ); ///< Set the GameLogic seed to a known value at game start
extern UnsignedInt GetGameLogicRandomSeed( void ); ///< Get the seed (used for replays)
extern UnsignedInt GetGameLogicRandomSeedCRC( void );///< Get the seed (used for CRCs)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp
index 7eb123632..23daa80f1 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp
@@ -171,6 +171,15 @@ void InitRandom( void )
#endif
}
+//
+// Starting a game seeds all three streams from the one number. It used to seed only the logic
+// stream (InitGameLogicRandom, now gone): the client and audio streams kept whatever the process
+// had been running on since startup, which is a time_t, so they held a different value on every
+// machine in the room and a different one again on every playback of the same replay. The logic
+// stream lands in exactly the same place either way - this costs the simulation nothing - but any
+// draw that leaks out of the client stream into something the simulation can see now reads the
+// same on every machine instead of being a divergence waiting for a chance.
+//
void InitRandom( UnsignedInt seed )
{
seedRandom(seed, theGameAudioSeed);
@@ -182,21 +191,6 @@ DEBUG_LOG(( "InitRandom %08lx\n",seed));
#endif
}
-void InitGameLogicRandom( UnsignedInt seed )
-{
-#ifdef DETERMINISTIC
- // needs to be the same every time
- seedRandom(0, theGameLogicSeed);
- theGameLogicBaseSeed = 0;
-#else
- seedRandom(seed, theGameLogicSeed);
- theGameLogicBaseSeed = seed;
-#endif
-#ifdef DEBUG_RANDOM_LOGIC
-DEBUG_LOG(( "InitRandom Logic %08lx\n",seed));
-#endif
-}
-
//
// Integer random value
//
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
index 17147d394..bdab7facf 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Recorder.cpp
@@ -1181,7 +1181,6 @@ Bool RecorderClass::playbackFile(AsciiString filename)
msg->appendIntegerArgument(rankPoints);
if( maxFPS != 0 )
msg->appendIntegerArgument(maxFPS);
- //InitGameLogicRandom( m_gameInfo.getSeed());
InitRandom( m_gameInfo.getSeed() );
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MapSelectMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MapSelectMenu.cpp
index bb068a7ec..bc5cce0ce 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MapSelectMenu.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MapSelectMenu.cpp
@@ -77,14 +77,7 @@ static void doGameStart( void )
msg->appendIntegerArgument(s_AIDiff);
msg->appendIntegerArgument(0);
- /// @todo: when Campaign & skirmish are separated, make campaign have fixed seed and skirmish random.
InitRandom(0);
- /*
- if (TheGlobalData->m_fixedSeed >= 0)
- InitGameLogicRandom(TheGlobalData->m_fixedSeed);
- else
- InitGameLogicRandom(GameClientRandomValue(0, INT_MAX - 1));
- */
isShuttingDown = true;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
index 13fcd0b26..8c46a2a29 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
@@ -441,7 +441,7 @@ void reallyDoStart( void )
if (isSkirmish)
{
- InitGameLogicRandom(TheSkirmishGameInfo->getSeed());
+ InitRandom(TheSkirmishGameInfo->getSeed());
GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
msg->appendIntegerArgument(GAME_SKIRMISH);
@@ -455,7 +455,7 @@ void reallyDoStart( void )
GAME_SINGLE_PLAYER - which is seeded with 0 everywhere else in the game. EA seeded it
with the skirmish lobby's seed here and nowhere else, so the same mission played from the
two menus was not the same mission. */
- InitGameLogicRandom(0);
+ InitRandom(0);
GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
msg->appendIntegerArgument(GAME_SINGLE_PLAYER);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/Shell/Shell.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/Shell/Shell.cpp
index 4fb745533..87ba59af4 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/Shell/Shell.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/Shell/Shell.cpp
@@ -484,7 +484,7 @@ void Shell::showShellMap(Bool useShellMap )
TheMessageStream->appendMessage( GameMessage::MSG_CLEAR_GAME_DATA );
TheWritableGlobalData->m_pendingFile = TheGlobalData->m_shellMapName;
- InitGameLogicRandom(0);
+ InitRandom(0);
GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
msg->appendIntegerArgument(GAME_SHELL);
m_shellMapOn = TRUE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/StagingRoomGameInfo.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/StagingRoomGameInfo.cpp
index df34ea870..de83de0bd 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/StagingRoomGameInfo.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/StagingRoomGameInfo.cpp
@@ -863,8 +863,8 @@ void GameSpyStagingRoom::launchGame( void )
TheWritableGlobalData->m_useFpsLimit = false;
// Set the random seed
- InitGameLogicRandom( getSeed() );
- DEBUG_LOG(("InitGameLogicRandom( %d )\n", getSeed()));
+ InitRandom( getSeed() );
+ DEBUG_LOG(("InitRandom( %d )\n", getSeed()));
// mark us as "Loading" in the buddy list
BuddyRequest req;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpyGameInfo.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpyGameInfo.cpp
index 3bccc0a63..e063b53e0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpyGameInfo.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpyGameInfo.cpp
@@ -566,8 +566,8 @@ void GameSpyLaunchGame( void )
TheGlobalData->m_useFpsLimit = false;
// Set the random seed
- InitGameLogicRandom( TheGameSpyGame->getSeed() );
- DEBUG_LOG(("InitGameLogicRandom( %d )\n", TheGameSpyGame->getSeed()));
+ InitRandom( TheGameSpyGame->getSeed() );
+ DEBUG_LOG(("InitRandom( %d )\n", TheGameSpyGame->getSeed()));
if (TheNAT != NULL) {
delete TheNAT;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
index 0e1e22707..9cfe45346 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPICallbacks.cpp
@@ -268,8 +268,8 @@ void LANAPI::OnGameStart( void )
TheWritableGlobalData->m_useFpsLimit = false;
// Set the random seed
- InitGameLogicRandom( m_currentGame->getSeed() );
- DEBUG_LOG(("InitGameLogicRandom( %d )\n", m_currentGame->getSeed()));
+ InitRandom( m_currentGame->getSeed() );
+ DEBUG_LOG(("InitRandom( %d )\n", m_currentGame->getSeed()));
}
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 46c7e811d..e2e35699c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -38,6 +38,8 @@
#include "Common/Energy.h"
#include "Common/RandomValue.h"
#include "GameLogic/LogicRandomValue.h"
+#include "GameClient/ClientRandomValue.h"
+#include "Common/AudioRandomValue.h"
#include "GameNetwork/CrcAgreement.h"
#include "GameNetwork/NetworkUtil.h"
#include "GameNetwork/NetCommandList.h"
@@ -2697,7 +2699,7 @@ TEST(crcagreement_a_room_with_nobody_left_in_it_is_not_a_mismatch)
TEST(logicrandom_unchanged_does_not_move_the_shared_seed)
{
- InitGameLogicRandom( 12345 );
+ InitRandom( 12345 );
const UnsignedInt before = GetGameLogicRandomSeedCRC();
for (int i = 0; i < 50; ++i)
@@ -2717,7 +2719,7 @@ TEST(logicrandom_unchanged_does_not_move_the_shared_seed)
TEST(logicrandom_unchanged_is_the_same_answer_on_every_machine)
{
- InitGameLogicRandom( 777 );
+ InitRandom( 777 );
const Int first = GetGameLogicRandomValueUnchanged( 0, 1000000 );
// same seed state, same answer - repeatedly, because it does not consume anything
@@ -2726,18 +2728,18 @@ TEST(logicrandom_unchanged_is_the_same_answer_on_every_machine)
// a second machine that reached this point through the same logic holds the same seed and gets
// the same answer, which is what made this safe to use for a scripted sound's variant pick
- InitGameLogicRandom( 777 );
+ InitRandom( 777 );
CHECK_EQ( GetGameLogicRandomValueUnchanged( 0, 1000000 ), first );
// once the simulation itself rolls, the answer moves on with it
GetGameLogicRandomValue( 0, 99, __FILE__, __LINE__ );
- InitGameLogicRandom( 778 );
+ InitRandom( 778 );
CHECK_NE( GetGameLogicRandomValueUnchanged( 0, 1000000 ), first );
}
TEST(logicrandom_unchanged_honours_its_bounds)
{
- InitGameLogicRandom( 4242 );
+ InitRandom( 4242 );
for (int i = 0; i < 200; ++i)
{
GetGameLogicRandomValue( 0, 99, __FILE__, __LINE__ ); // walk the seed along
@@ -3745,3 +3747,51 @@ TEST(a_player_index_from_outside_this_machine_is_bounded_before_use)
acceptedOnlyTheList = FALSE;
CHECK( acceptedOnlyTheList );
}
+
+/* Starting a game used to seed only the logic stream. The client and audio streams kept the
+ time_t the process was seeded with at startup, so they read differently on every machine in the
+ room and differently again on every playback of the same replay - and any draw that leaks out of
+ the client stream into something the simulation can see is then a divergence. A game's seed now
+ sets all three, and the logic stream still lands exactly where it did. */
+TEST(a_games_seed_sets_the_client_and_audio_streams_as_well_as_the_logic_one)
+{
+ const Int DRAWS = 16;
+ Int client[DRAWS], audio[DRAWS], logic[DRAWS];
+
+ InitRandom( 31337 );
+ const UnsignedInt logicSeedAfterInit = GetGameLogicRandomSeedCRC();
+ for( Int i = 0; i < DRAWS; ++i )
+ {
+ client[i] = GameClientRandomValue( 0, 1000000 );
+ audio[i] = GameAudioRandomValue( 0, 1000000 );
+ logic[i] = GetGameLogicRandomValue( 0, 1000000, __FILE__, __LINE__ );
+ }
+
+ // the same seed on the machine next to you, or on the same machine tomorrow, reads the same
+ InitRandom( 31337 );
+ CHECK_EQ( GetGameLogicRandomSeedCRC(), logicSeedAfterInit );
+ Bool clientRepeated = TRUE, audioRepeated = TRUE, logicRepeated = TRUE;
+ for( Int i = 0; i < DRAWS; ++i )
+ {
+ if( GameClientRandomValue( 0, 1000000 ) != client[i] ) clientRepeated = FALSE;
+ if( GameAudioRandomValue( 0, 1000000 ) != audio[i] ) audioRepeated = FALSE;
+ if( GetGameLogicRandomValue( 0, 1000000, __FILE__, __LINE__ ) != logic[i] ) logicRepeated = FALSE;
+ }
+ CHECK( clientRepeated );
+ CHECK( audioRepeated );
+ CHECK( logicRepeated );
+
+ // and a different game is a different game, in all three
+ InitRandom( 31338 );
+ CHECK_NE( GetGameLogicRandomSeedCRC(), logicSeedAfterInit );
+ Bool clientMoved = FALSE, audioMoved = FALSE, logicMoved = FALSE;
+ for( Int i = 0; i < DRAWS; ++i )
+ {
+ if( GameClientRandomValue( 0, 1000000 ) != client[i] ) clientMoved = TRUE;
+ if( GameAudioRandomValue( 0, 1000000 ) != audio[i] ) audioMoved = TRUE;
+ if( GetGameLogicRandomValue( 0, 1000000, __FILE__, __LINE__ ) != logic[i] ) logicMoved = TRUE;
+ }
+ CHECK( clientMoved );
+ CHECK( audioMoved );
+ CHECK( logicMoved );
+}