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
feat(gameengine): fingerprint the machine's math in the mismatch dump
Olcay Seygand62e3616e5a4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d7c3fc297..5ac267496 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -260,7 +260,10 @@ map. Ctrl+A is still everything at once.world — where it was, how hurt it was, and its own fingerprint — for the last several checkpoints,and writes it out on both machines when the check fails. Lining up the two files points at thesingle unit, building or projectile that went its own way, which is the difference between fixing- a desync and guessing at it.+ a desync and guessing at it. The report also starts with a fingerprint of the machine's own+ arithmetic — the same sums, run on constants, on both machines. If that one number differs, the+ two computers were never doing the same maths and nothing further down the file is worth reading;+ if it matches, the fault really is in the game.- **A LAN game refuses to start between two machines with different game files.** Two people whosedata files differ cannot play the same match, however well the connection behaves — one hand-editedfile and the two simulations part company a few minutes in, with nothing on screen to say why. Thediff --git a/GeneralsMD/Code/GameEngine/Include/Common/SimulationMathCrc.h b/GeneralsMD/Code/GameEngine/Include/Common/SimulationMathCrc.hnew file mode 100644index 000000000..d7477e599--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Include/Common/SimulationMathCrc.h@@ -0,0 +1,53 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// FILE: SimulationMathCrc.h //////////////////////////////////////////////////////////////////////+// Desc: A fingerprint of the floating point math the simulation is built on.+///////////////////////////////////////////////////////////////////////////////////////////////////++#pragma once++#ifndef __SIMULATIONMATHCRC_H__+#define __SIMULATIONMATHCRC_H__++#include "Lib/BaseType.h"++/**+ * A mismatch says two machines disagreed about the world; it does not say why. One of the+ * answers is that they never agreed about arithmetic in the first place - a different C runtime,+ * a different FPU control word left behind by a driver, a different implementation of sin(). The+ * game's own CRC cannot tell that apart from a logic bug, because both come out as "the numbers+ * differ".+ *+ * This runs a fixed sequence of the transcendental and matrix operations the simulation actually+ * uses, over constants, and CRCs the result. It depends on nothing but the machine's math, so two+ * players comparing this one number learn immediately which kind of divergence they have: the same+ * value means the arithmetic agrees and the difference is in the game state, a different value+ * means the game states were never going to agree.+ */+class SimulationMathCrc+{+public:++ /** Compute the fingerprint. Sets the simulation's own FPU mode for the duration and puts the+ * caller's mode back afterwards, so this is safe to call from anywhere, including mid-match. */+ static UnsignedInt calculate( void );++};++#endif // __SIMULATIONMATHCRC_H__diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/SimulationMathCrc.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/SimulationMathCrc.cppnew file mode 100644index 000000000..281cc335b--- /dev/null+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/SimulationMathCrc.cpp@@ -0,0 +1,88 @@+/*+** Command & Conquer Generals Zero Hour(tm)+** Copyright 2025 Electronic Arts Inc.+**+** This program is free software: you can redistribute it and/or modify+** it under the terms of the GNU General Public License as published by+** the Free Software Foundation, either version 3 of the License, or+** (at your option) any later version.+**+** This program is distributed in the hope that it will be useful,+** but WITHOUT ANY WARRANTY; without even the implied warranty of+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+** GNU General Public License for more details.+**+** You should have received a copy of the GNU General Public License+** along with this program. If not, see <http://www.gnu.org/licenses/>.+*/++// FILE: SimulationMathCrc.cpp ////////////////////////////////////////////////////////////////////+// Desc: A fingerprint of the floating point math the simulation is built on.+///////////////////////////////////////////////////////////////////////////////////////////////////++#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine++#include "Common/SimulationMathCrc.h"+#include "Common/XferCRC.h"+#include "GameLogic/FPUControl.h"+#include "WWMath/matrix3d.h"+#include "WWMath/wwmath.h"++#include <float.h>+#include <math.h>++// ------------------------------------------------------------------------------------------------+/** One pass of the arithmetic, into the CRC. Every operation here is one the simulation itself+ * performs; nothing in it reads the clock, the machine, or any game state. */+// ------------------------------------------------------------------------------------------------+static void appendSimulationMathCrc( XferCRC &xfer )+{+ Matrix3D matrix;+ Matrix3D factorsMatrix;++ matrix.Set(+ 4.1f, 1.2f, 0.3f, 0.4f,+ 0.5f, 3.6f, 0.7f, 0.8f,+ 0.9f, 1.0f, 2.1f, 1.2f );++ factorsMatrix.Set(+ WWMath::Sin( 0.7f ) * log10f( 2.3f ),+ WWMath::Cos( 1.1f ) * powf( 1.1f, 2.0f ),+ tanf( 0.3f ),+ asinf( 0.967302263f ),+ acosf( 0.967302263f ),+ atanf( 0.967302263f ) * powf( 1.1f, 2.0f ),+ atan2f( 0.4f, 1.3f ),+ sinhf( 0.2f ),+ coshf( 0.4f ) * tanhf( 0.5f ),+ sqrtf( 55788.84375f ),+ expf( 0.1f ) * log10f( 2.3f ),+ logf( 1.4f ) );++ Matrix3D::Multiply( matrix, factorsMatrix, &matrix );+ matrix.Get_Inverse( matrix );++ xfer.xferMatrix3D( &matrix );+}++// ------------------------------------------------------------------------------------------------+// ------------------------------------------------------------------------------------------------+/*static*/ UnsignedInt SimulationMathCrc::calculate( void )+{+ XferCRC xfer;+ xfer.open( "SimulationMathCrc" );++ /* The answer is only comparable between machines if it is computed in the mode the simulation+ runs in, so set that mode - and put the caller's back, rather than _fpreset()ing to the C+ runtime default. This is called from the mismatch dump, which happens mid-match. */+ const UnsignedInt callersMode = _controlfp( 0, 0 );+ setFPMode();++ appendSimulationMathCrc( xfer );++ _controlfp( callersMode, _MCW_PC | _MCW_RC );++ xfer.close();++ return xfer.getCRC();+}diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cppindex 45fbb6d3a..6e0847aa8 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp@@ -59,6 +59,7 @@#include "Common/Xfer.h"#include "Common/XferCRC.h"#include "Common/XferDeepCRC.h"+#include "Common/SimulationMathCrc.h"#include "Common/GameSpyMiscPreferences.h"#include "GameClient/ControlBar.h"@@ -2670,6 +2671,13 @@ void GameLogic::writeMismatchDump( Int numPlayers )fprintf( fp, "Local player is slot %d of %d connected players; %d CRCs were compared\n\n",TheNetwork ? TheNetwork->getLocalPlayerID() : 0, numPlayers, m_cachedCRCs.size() );+ /* Two machines that never agreed about arithmetic produce a mismatch that looks exactly like a+ logic bug. This number depends on the machine's math and nothing else, so the first thing to+ compare between two dumps is this line: same value, the game states diverged; different value,+ they were never going to agree and no amount of reading the object table below will say why. */+ fprintf( fp, "Simulation math fingerprint 0x%8.8X (FPU mode 0x%4.4X, expected 0x%4.4X)\n\n",+ SimulationMathCrc::calculate(), getFPMode(), expectedFPMode() );+for( std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();crcIt != m_cachedCRCs.end(); ++crcIt ){diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex df883bbb4..f15b4d267 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -40,6 +40,9 @@#include "GameLogic/LogicRandomValue.h"#include "GameClient/ClientRandomValue.h"#include "Common/ThingTemplate.h"+#include "Common/SimulationMathCrc.h"+#include "GameLogic/FPUControl.h"+#include <float.h>#include "Common/AudioRandomValue.h"#include "GameNetwork/CrcAgreement.h"#include "GameNetwork/NetworkUtil.h"@@ -3835,3 +3838,38 @@ TEST(a_template_looked_up_by_name_is_taken_to_the_end_of_its_override_chain)// one delete, not three: ~Overridable deletes whatever the chain still points atbase->deleteInstance();}++/* The simulation math fingerprint exists so that two players comparing mismatch dumps can tell+ "our arithmetic differs" from "our game states diverged". That only works if the number is a+ function of the machine's math alone - in particular, it must not depend on whatever FPU mode+ the caller happened to be in, and it must not leave that mode changed behind it. */+TEST(the_simulation_math_fingerprint_is_the_machines_math_and_not_the_callers_fpu_mode)+{+ /* Three callers, three FPU modes, none of them the one the simulation runs in unless it happens+ to be: 53-bit precision is what the C runtime starts a process in and what a driver that+ resets the FPU leaves behind, and round-to-chop is a mode the game never wants but nothing+ stops a plugin from leaving set. All three must produce the same fingerprint. */++ setFPMode();+ const UnsignedInt fromSimulationMode = SimulationMathCrc::calculate();++ // a real value, not a CRC of nothing at all, and repeatable+ CHECK( fromSimulationMode != 0 );+ CHECK_EQ( SimulationMathCrc::calculate(), fromSimulationMode );++ _controlfp( _PC_53, _MCW_PC );+ const UnsignedInt modeIn53 = getFPMode();+ CHECK( modeIn53 != expectedFPMode() );+ CHECK_EQ( SimulationMathCrc::calculate(), fromSimulationMode );+ CHECK_EQ( getFPMode(), modeIn53 ); // and the caller's mode is still the caller's++ _controlfp( _PC_64 | _RC_CHOP, _MCW_PC | _MCW_RC );+ const UnsignedInt modeInChop = getFPMode();+ CHECK( modeInChop != expectedFPMode() );+ CHECK_EQ( SimulationMathCrc::calculate(), fromSimulationMode );+ CHECK_EQ( getFPMode(), modeInChop );++ // and back where the rest of the tests expect to find it+ setFPMode();+ CHECK_EQ( getFPMode(), expectedFPMode() );+}