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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): keep a player's audio options out of the simulation
Olcay Seygan2b9ec97ffeb7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f72fd461a..87462a1d9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -178,6 +178,12 @@ map. Ctrl+A is still everything at once.the desktop and the player who had been sabotaged began the new map blacked out — no radar, nodefences, nothing building — for the first sixteen minutes of it, and only on the machines thathad watched the sabotage happen.+- **Turning your sound off no longer desyncs the game.** Missions and maps play scripted sounds, and+ the game picks which take of a sound to play from the same shared number sequence that decides+ everything else in the match. If you had sound effects muted, that sound never got as far as being+ picked — so your machine skipped a number every other machine used, and from there on the two were+ playing different games. Same for a player with speech turned off. The take is still identical on+ every screen; it just no longer costs the simulation anything to choose it.- **Two machines playing the same match drift apart less.** Defeating a player, playing a secondmap without restarting, and the fingerprint the game uses to notice that two players have stoppedagreeing — all three had bugs that made one side quietly diverge from the other, and thediff --git a/GeneralsMD/Code/GameEngine/Include/Common/Geometry.h b/GeneralsMD/Code/GameEngine/Include/Common/Geometry.hindex fd35b89f9..049e577bf 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/Geometry.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/Geometry.h@@ -178,7 +178,13 @@ public:void get2DBounds(const Coord3D& geomCenter, Real angle, Region2D& bounds ) const;/// note that the pt is generated using game logic random, not game client random!- void makeRandomOffsetWithinFootprint(Coord3D& pt) const;+ /**+ Pick a random point inside this footprint. Pass useClientRandom when the point is only ever+ going to be looked at - a particle, a spark - because whether that effect exists at all is a+ decision the player's detail settings get to make, and the simulation may not depend on it.+ Anything that places a real object keeps the default and rolls from the shared logic stream.+ */+ void makeRandomOffsetWithinFootprint(Coord3D& pt, Bool useClientRandom = FALSE) const;void makeRandomOffsetOnPerimeter(Coord3D& pt) const; //Chooses a random point on the extent border.void clipPointToFootprint(const Coord3D& geomCenter, Coord3D& ptToClip) const;diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/LogicRandomValue.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/LogicRandomValue.hindex d07a96c65..f009e6d21 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/LogicRandomValue.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/LogicRandomValue.h@@ -38,9 +38,18 @@extern Int GetGameLogicRandomValue( int lo, int hi, char *file, int line );extern Real GetGameLogicRandomValueReal( Real lo, Real hi, char *file, int line );+// These two read the logic random stream WITHOUT advancing it, so the answer is the same on every+// machine but a machine that does not ask stays in step with the ones that do. Use them for+// anything a player's own settings can skip - a muted sound effect, a particle system the detail+// level threw away - and never for a decision the simulation depends on.+extern Int GetGameLogicRandomValueUnchanged( int lo, int hi );+extern Real GetGameLogicRandomValueRealUnchanged( Real lo, Real hi );+// use these macros to access the random value functions#define GameLogicRandomValue( lo, hi ) GetGameLogicRandomValue( lo, hi, __FILE__, __LINE__ )#define GameLogicRandomValueReal( lo, hi ) GetGameLogicRandomValueReal( lo, hi, __FILE__, __LINE__ )+#define GameLogicRandomValueUnchanged( lo, hi ) GetGameLogicRandomValueUnchanged( lo, hi )+#define GameLogicRandomValueRealUnchanged( lo, hi ) GetGameLogicRandomValueRealUnchanged( lo, hi )//--------------------------------------------------------------------------------------------------------------class CColorAlphaDialog;diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cppindex 46800a555..0fcf2db04 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/Audio/AudioEventRTS.cpp@@ -344,7 +344,10 @@ void AudioEventRTS::generateFilename( void ){if (m_isLogicalAudio){- which = GameLogicRandomValue(0, m_eventInfo->m_sounds.size() - 1);+ // Read the shared stream without advancing it: this machine may have sound switched+ // off in the options and never get here at all, and the simulation cannot be allowed+ // to depend on that.+ which = GameLogicRandomValueUnchanged(0, m_eventInfo->m_sounds.size() - 1);}else{@@ -392,7 +395,7 @@ void AudioEventRTS::generatePlayInfo( void )// needs to be logic because it needs to be the same on all systems.Int attackToPlay;if (m_isLogicalAudio) {- attackToPlay = GameLogicRandomValue(0, attackSize - 1);+ attackToPlay = GameLogicRandomValueUnchanged(0, attackSize - 1);} else {attackToPlay = GameAudioRandomValue(0, attackSize - 1);}@@ -410,7 +413,7 @@ void AudioEventRTS::generatePlayInfo( void )// needs to be logic because it needs to be the same on all systems.Int decayToPlay;if (m_isLogicalAudio) {- decayToPlay = GameLogicRandomValue(0, decaySize - 1);+ decayToPlay = GameLogicRandomValueUnchanged(0, decaySize - 1);} else {decayToPlay = GameAudioRandomValue(0, decaySize - 1);}diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cppindex 01879f6a3..7eb123632 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/RandomValue.cpp@@ -233,6 +233,45 @@ DEBUG_LOG(( "%d: GetGameLogicRandomValue = %d (%d - %d), %s line %d\n",return rval;}+//+// Integer random value read from the logic stream without advancing it.+//+// Every machine in a game holds the same logic seed, so a value rolled from a copy of it is the+// same everywhere - but the shared seed does not move, so a machine that never makes the call at+// all stays in step. That is what client-side effects need: they are allowed to want a number+// that everybody agrees on without being allowed to decide how the simulation unfolds.+//+Int GetGameLogicRandomValueUnchanged( int lo, int hi )+{+ UnsignedInt delta = hi - lo + 1;++ if (delta == 0)+ return hi;++ UnsignedInt seedCopy[6];+ for (int i = 0; i < 6; ++i)+ seedCopy[i] = theGameLogicSeed[i];++ return ((Int)(randomValue(seedCopy) % delta)) + lo;+}++//+// Real random value read from the logic stream without advancing it. See above.+//+Real GetGameLogicRandomValueRealUnchanged( Real lo, Real hi )+{+ Real delta = hi - lo;++ if (delta <= 0.0f)+ return hi;++ UnsignedInt seedCopy[6];+ for (int i = 0; i < 6; ++i)+ seedCopy[i] = theGameLogicSeed[i];++ return ((Real)(randomValue(seedCopy)) * theMultFactor ) * delta + lo;+}+//// Integer random value//diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/Geometry.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/Geometry.cppindex 2ed9c2de8..b2c676518 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/System/Geometry.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/Geometry.cpp@@ -34,6 +34,7 @@#include "Common/Geometry.h"#include "Common/INI.h"#include "Common/RandomValue.h"+#include "GameClient/ClientRandomValue.h"#include "Common/Xfer.h"#ifdef _INTERNAL@@ -381,7 +382,13 @@ Bool GeometryInfo::isPointInFootprint(const Coord3D& geomCenter, const Coord3D&}//=============================================================================-void GeometryInfo::makeRandomOffsetWithinFootprint(Coord3D& pt) const+static Real footprintRandom(Real lo, Real hi, Bool useClientRandom)+{+ return useClientRandom ? GameClientRandomValueReal(lo, hi) : GameLogicRandomValueReal(lo, hi);+}++//=============================================================================+void GeometryInfo::makeRandomOffsetWithinFootprint(Coord3D& pt, Bool useClientRandom) const{switch(m_type){@@ -395,8 +402,8 @@ void GeometryInfo::makeRandomOffsetWithinFootprint(Coord3D& pt) constReal distSqr;do{- pt.x = GameLogicRandomValueReal(-m_majorRadius, m_majorRadius);- pt.y = GameLogicRandomValueReal(-m_majorRadius, m_majorRadius);+ pt.x = footprintRandom(-m_majorRadius, m_majorRadius, useClientRandom);+ pt.y = footprintRandom(-m_majorRadius, m_majorRadius, useClientRandom);pt.z = 0.0f;distSqr = sqr(pt.x) + sqr(pt.y);} while (distSqr > maxDistSqr);@@ -412,8 +419,8 @@ void GeometryInfo::makeRandomOffsetWithinFootprint(Coord3D& pt) constcase GEOMETRY_BOX:{- pt.x = GameLogicRandomValueReal(-m_majorRadius, m_majorRadius);- pt.y = GameLogicRandomValueReal(-m_minorRadius, m_minorRadius);+ pt.x = footprintRandom(-m_majorRadius, m_majorRadius, useClientRandom);+ pt.y = footprintRandom(-m_minorRadius, m_minorRadius, useClientRandom);pt.z = 0.0f;break;}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Damage/TransitionDamageFX.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Damage/TransitionDamageFX.cppindex dd4a8fb0a..057fdfc68 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Damage/TransitionDamageFX.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Damage/TransitionDamageFX.cpp@@ -30,6 +30,8 @@// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine+#include "GameClient/ClientRandomValue.h"+#include "Common/Xfer.h"#include "GameClient/Drawable.h"#include "GameClient/FXList.h"@@ -256,7 +258,7 @@ void TransitionDamageFX::onDelete( void )/** Given an FXLoc info struct, return the effect position that we are supposed to use.* The position is local to to the object *///--------------------------------------------------------------------------------------------------static Coord3D getLocalEffectPos( const FXLocInfo *locInfo, Drawable *draw )+static Coord3D getLocalEffectPos( const FXLocInfo *locInfo, Drawable *draw, Bool useClientRandom = FALSE ){DEBUG_ASSERTCRASH( locInfo, ("getLocalEffectPos: locInfo is NULL\n") );@@ -293,7 +295,8 @@ static Coord3D getLocalEffectPos( const FXLocInfo *locInfo, Drawable *draw )return locInfo->loc;// pick one of the bone positions- Int pick = GameLogicRandomValue( 0, boneCount - 1 );+ Int pick = useClientRandom ? GameClientRandomValue( 0, boneCount - 1 )+ : GameLogicRandomValue( 0, boneCount - 1 );return positions[ pick ];} // end else@@ -397,7 +400,10 @@ void TransitionDamageFX::onBodyDamageStateChange( const DamageInfo* damageInfo,{// get the what is the position we're going to playe the effect at- pos = getLocalEffectPos( &modData->m_particleSystem[ newState ][ i ].locInfo, draw );+ // Client stream: we are inside "if (pSystem)", and whether the particle manager+ // had room for this system is a local decision. Rolling the bone pick from the+ // shared logic stream let a player's particle cap desync the whole game.+ pos = getLocalEffectPos( &modData->m_particleSystem[ newState ][ i ].locInfo, draw, TRUE );//// set position on system given any bone position provided, the bone position isdiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/EMPUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/EMPUpdate.cppindex ec9780481..c5d7de728 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/EMPUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/EMPUpdate.cpp@@ -30,6 +30,8 @@// INCLUDES ///////////////////////////////////////////////////////////////////////////////////////#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine+#include "GameClient/ClientRandomValue.h"+#include "Common/Thing.h"#include "Common/ThingTemplate.h"#include "Common/INI.h"@@ -315,8 +317,12 @@ void EMPUpdate::doDisableAttack( void )if (sys){Coord3D offs = {0,0,0};- curVictim->getGeometryInfo().makeRandomOffsetWithinFootprint( offs );- offs.z = GameLogicRandomValue(3, victimHeight);+ // These sparks only exist when createParticleSystem() had room for them, and that+ // depends on the particle cap and the detail settings of the machine we are on.+ // Rolling them from the shared logic stream made every player's graphics options+ // part of the simulation.+ curVictim->getGeometryInfo().makeRandomOffsetWithinFootprint( offs, TRUE );+ offs.z = GameClientRandomValue(3, victimHeight);//This puts all the sparks within a quadrahemicycloid (rectangular dome) volume//The same shape as a four cornered camping dome tent, for those with less Greekdiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cppindex 62882cfed..f6fa9137d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/SpecialAbilityUpdate.cpp@@ -1411,7 +1411,9 @@ void SpecialAbilityUpdate::triggerAbilityEffect()if (sys){Coord3D offs = {0,0,0};- target->getGeometryInfo().makeRandomOffsetWithinFootprint( offs );+ // Client stream: this offset is only ever reached when the particle system was actually+ // created, which the local detail settings decide. See EMPUpdate for the same fix.+ target->getGeometryInfo().makeRandomOffsetWithinFootprint( offs, TRUE );sys->attachToObject(target);sys->setPosition( &offs );diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 381097c84..0f3fb340d 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -36,6 +36,8 @@#include "GameNetwork/CushionMetrics.h"#include "GameNetwork/LinkSimulation.h"#include "Common/Energy.h"+#include "Common/RandomValue.h"+#include "GameLogic/LogicRandomValue.h"#include "GameNetwork/CrcAgreement.h"#include <float.h>#include "GameClient/Water.h"@@ -2676,6 +2678,71 @@ TEST(crcagreement_a_room_with_nobody_left_in_it_is_not_a_mismatch)CHECK_EQ( crcAgreement( reported, crc, connected, 2, 1 ), CRC_AGREEMENT_AGREE );}+// ---------------------------------------------------------------------------------------------+// Reading the shared random stream without spending it. Client-side effects - a sound the player+// muted, a particle the detail settings threw away - used to roll from the logic stream, so+// whether they happened at all changed how the simulation unfolded on that machine.+// ---------------------------------------------------------------------------------------------++TEST(logicrandom_unchanged_does_not_move_the_shared_seed)+{+ InitGameLogicRandom( 12345 );+ const UnsignedInt before = GetGameLogicRandomSeedCRC();++ for (int i = 0; i < 50; ++i)+ {+ GetGameLogicRandomValueUnchanged( 0, 99 );+ GetGameLogicRandomValueRealUnchanged( 0.0f, 1.0f );+ }++ // the whole point: a machine that made these calls and a machine that skipped them are still+ // holding the same seed, so they still agree about the next thing the simulation rolls.+ CHECK_EQ( GetGameLogicRandomSeedCRC(), before );++ // and the ordinary call still does move it, or the check above proves nothing+ GetGameLogicRandomValue( 0, 99, __FILE__, __LINE__ );+ CHECK_NE( GetGameLogicRandomSeedCRC(), before );+}++TEST(logicrandom_unchanged_is_the_same_answer_on_every_machine)+{+ InitGameLogicRandom( 777 );+ const Int first = GetGameLogicRandomValueUnchanged( 0, 1000000 );++ // same seed state, same answer - repeatedly, because it does not consume anything+ for (int i = 0; i < 10; ++i)+ CHECK_EQ( GetGameLogicRandomValueUnchanged( 0, 1000000 ), first );++ // 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 );+ CHECK_EQ( GetGameLogicRandomValueUnchanged( 0, 1000000 ), first );++ // once the simulation itself rolls, the answer moves on with it+ GetGameLogicRandomValue( 0, 99, __FILE__, __LINE__ );+ InitGameLogicRandom( 778 );+ CHECK_NE( GetGameLogicRandomValueUnchanged( 0, 1000000 ), first );+}++TEST(logicrandom_unchanged_honours_its_bounds)+{+ InitGameLogicRandom( 4242 );+ for (int i = 0; i < 200; ++i)+ {+ GetGameLogicRandomValue( 0, 99, __FILE__, __LINE__ ); // walk the seed along++ const Int n = GetGameLogicRandomValueUnchanged( 7, 11 );+ CHECK( n >= 7 && n <= 11 );++ const Real r = GetGameLogicRandomValueRealUnchanged( -2.0f, 2.0f );+ CHECK( r >= -2.0f && r <= 2.0f );+ }++ // degenerate ranges answer the way the ordinary versions do+ CHECK_EQ( GetGameLogicRandomValueUnchanged( 5, 5 ), 5 );+ CHECK_EQ( GetGameLogicRandomValueRealUnchanged( 3.0f, 3.0f ), 3.0f );+}+// ---------------------------------------------------------------------------------------------// Per-game state that a reused Player has to give back.// ---------------------------------------------------------------------------------------------