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(gamenetwork): only players still in the game get a vote on the CRC

Olcay Seygane0cf1e5393ec
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c94e896d7..a925b6ca8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -163,6 +163,12 @@ map. Ctrl+A is still everything at once.
- **Loading a save remembers what your guards were guarding.** The guard order was written to the
file one slot too far and overwrote the position it was guarding, so units restored from a save
could come back watching the wrong place.
+- **Somebody quitting no longer ends the game for everyone else.** The game checks that all the
+ machines still agree by comparing a fingerprint of the world every so often, and if two of them
+ differ the match stops. A player leaving sent one last fingerprint from a machine that was already
+ shutting the game down — and that fingerprint was counted. The four people still playing were told
+ they had desynced because a fifth had quit. Only players who are actually still in the game count
+ now, both for the comparison and for deciding whether everyone has reported in yet.
- **A power sabotage does not follow you into the next game.** Shutting a base's power down set a
timer, and nothing cleared that timer when the match ended. Start another game without quitting to
the desktop and the player who had been sabotaged began the new map blacked out — no radar, no
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/CrcAgreement.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CrcAgreement.h
new file mode 100644
index 000000000..2134044de
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/CrcAgreement.h
@@ -0,0 +1,97 @@
+/*
+** 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: CrcAgreement.h ///////////////////////////////////////////////////////////////////////////
+// Desc: Deciding whether the machines in a room actually disagree about a frame.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _CRC_AGREEMENT_H_
+#define _CRC_AGREEMENT_H_
+
+#include "Lib/BaseType.h"
+
+/* Every so often each machine hashes its world and sends the number to everybody else. If two of
+ those numbers differ, the simulations have diverged and the match is over - so this decision has
+ to be right, and calling a mismatch that did not happen is as bad as missing one.
+
+ Two ways to get it wrong, both of them EA's, both of them about players who are on their way out
+ of the game:
+
+ - A player who leaves stays "connected" for a few more frames than their last CRC message
+ survives. At a short CRC interval that gap is hit routinely, and a missing CRC is not
+ evidence that anybody computed anything differently - it is evidence that a packet is not
+ here yet.
+
+ - The reverse: a CRC that arrives from somebody who is *already* gone. EA compared every
+ entry in the map, so the leaver's last hash - computed on a machine that was in the middle
+ of tearing the game down - was held against everybody who is still playing.
+
+ So: only hashes from players who are still connected count, and all of them have to be here
+ before anything is compared. */
+
+enum CrcAgreement
+{
+ CRC_AGREEMENT_TOO_FEW, ///< at least one connected player has not reported yet - decide later
+ CRC_AGREEMENT_AGREE, ///< everybody still playing hashed the same world
+ CRC_AGREEMENT_MISMATCH ///< two players who are both still connected disagree
+};
+
+/**
+ Judge one CRC frame.
+
+ @param reported slot i sent a CRC for this frame
+ @param crc what slot i sent (only read where reported[i])
+ @param connected slot i is still in the game
+ @param slotCount length of the three arrays
+ @param connectedPlayerCount how many players the network says are still connected
+*/
+inline CrcAgreement crcAgreement( const Bool *reported, const UnsignedInt *crc, const Bool *connected,
+ Int slotCount, Int connectedPlayerCount )
+{
+ Int heard = 0;
+ Bool haveReference = FALSE;
+ UnsignedInt reference = 0;
+ Bool disagreed = FALSE;
+
+ for (Int i = 0; i < slotCount; ++i)
+ {
+ if (!reported[i] || !connected[i])
+ continue;
+
+ ++heard;
+
+ if (!haveReference)
+ {
+ haveReference = TRUE;
+ reference = crc[i];
+ }
+ else if (reference != crc[i])
+ {
+ disagreed = TRUE;
+ }
+ }
+
+ if (heard < connectedPlayerCount)
+ return CRC_AGREEMENT_TOO_FEW;
+
+ return disagreed ? CRC_AGREEMENT_MISMATCH : CRC_AGREEMENT_AGREE;
+}
+
+#endif // _CRC_AGREEMENT_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index 5ed47db7b..ccedb7100 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -107,6 +107,7 @@
#include "GameNetwork/GameSpy/PeerDefs.h"
#include "GameNetwork/GameSpy/ThreadUtils.h"
#include "GameNetwork/LANAPICallbacks.h"
+#include "GameNetwork/CrcAgreement.h"
#include "GameNetwork/NetworkInterface.h"
#include "GameNetwork/GameSpy/PersistentStorageThread.h"
@@ -2549,33 +2550,45 @@ void GameLogic::processCommandList( CommandList *list )
++numPlayers;
}
- /* A player who leaves stays "connected" for a few more frames than their last CRC
- message survives, and at a short CRC interval that gap is hit routinely. Missing CRCs
- are not evidence that anybody computed anything differently - they are evidence that a
- packet is not here yet. Say so and let the next CRC frame decide, instead of blaming
- the leaver for a desync that never happened. */
- if (m_cachedCRCs.size() < (size_t)numPlayers)
+ /* The decision itself lives in CrcAgreement.h, where it can be tested without a room
+ full of machines. Two ways to get a mismatch that never happened, both about players
+ on their way out: a CRC that has not arrived yet from somebody the network still calls
+ connected, and a CRC that arrives from somebody who is already gone. Neither is
+ evidence that anyone computed anything differently. */
+ Bool reported[MAX_SLOTS];
+ Bool connected[MAX_SLOTS];
+ UnsignedInt crcs[MAX_SLOTS];
+ for (Int slot = 0; slot < MAX_SLOTS; ++slot)
{
- DEBUG_LOG(("Only %d CRCs from %d connected players on frame %d - not comparing\n",
- m_cachedCRCs.size(), numPlayers, m_frame));
+ reported[slot] = FALSE;
+ crcs[slot] = 0;
+ connected[slot] = TheNetwork->isPlayerConnected(slot);
}
- else
+ for (std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();
+ crcIt != m_cachedCRCs.end(); ++crcIt)
{
- //DEBUG_LOG(("Comparing %d CRCs on frame %d\n", m_cachedCRCs.size(), m_frame));
- std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();
- Int validatorCRC = crcIt->second;
- //DEBUG_LOG(("Validator CRC from player %d is %8.8X\n", crcIt->first, validatorCRC));
- while (++crcIt != m_cachedCRCs.end())
+ if (crcIt->first >= 0 && crcIt->first < MAX_SLOTS)
{
- Int validatedCRC = crcIt->second;
- //DEBUG_LOG(("CRC to validate is from player %d: %8.8X\n", crcIt->first, validatedCRC));
- if (validatorCRC != validatedCRC)
- {
- DEBUG_CRASH(("CRC mismatch!"));
- sawCRCMismatch = TRUE;
- }
+ reported[crcIt->first] = TRUE;
+ crcs[crcIt->first] = crcIt->second;
}
}
+
+ switch (crcAgreement( reported, crcs, connected, MAX_SLOTS, numPlayers ))
+ {
+ case CRC_AGREEMENT_TOO_FEW:
+ DEBUG_LOG(("Only %d CRCs from %d connected players on frame %d - not comparing\n",
+ m_cachedCRCs.size(), numPlayers, m_frame));
+ break;
+
+ case CRC_AGREEMENT_MISMATCH:
+ DEBUG_CRASH(("CRC mismatch!"));
+ sawCRCMismatch = TRUE;
+ break;
+
+ case CRC_AGREEMENT_AGREE:
+ break;
+ }
}
if (sawCRCMismatch)
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 7b3370a23..381097c84 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -36,6 +36,7 @@
#include "GameNetwork/CushionMetrics.h"
#include "GameNetwork/LinkSimulation.h"
#include "Common/Energy.h"
+#include "GameNetwork/CrcAgreement.h"
#include <float.h>
#include "GameClient/Water.h"
#include "GameLogic/Module/PhysicsUpdate.h"
@@ -2606,6 +2607,75 @@ TEST(linksim_the_modulation_has_the_period_it_says_it_has)
CHECK( (c > d ? c - d : d - c) <= 1u );
}
+// ---------------------------------------------------------------------------------------------
+// Whether the room actually disagreed about a frame (MULTIPLAYER 1.1, upstream #2796).
+// ---------------------------------------------------------------------------------------------
+
+TEST(crcagreement_everyone_still_playing_has_to_have_reported)
+{
+ Bool reported[4] = { TRUE, TRUE, TRUE, FALSE };
+ Bool connected[4] = { TRUE, TRUE, TRUE, TRUE };
+ UnsignedInt crc[4] = { 0x1111u, 0x1111u, 0x1111u, 0u };
+
+ // four connected players, three hashes: the fourth packet is not here yet, which is not a
+ // mismatch. Decide on the next CRC frame instead.
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 4 ), CRC_AGREEMENT_TOO_FEW );
+
+ // once it arrives, and it agrees
+ reported[3] = TRUE;
+ crc[3] = 0x1111u;
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 4 ), CRC_AGREEMENT_AGREE );
+}
+
+TEST(crcagreement_a_player_who_has_left_does_not_get_a_vote)
+{
+ // slot 3 is gone, but their last hash - computed on a machine that was already tearing the
+ // game down - arrived this frame. EA compared it against everybody who is still playing and
+ // ended the match on it.
+ Bool reported[4] = { TRUE, TRUE, TRUE, TRUE };
+ Bool connected[4] = { TRUE, TRUE, TRUE, FALSE };
+ UnsignedInt crc[4] = { 0x1111u, 0x1111u, 0x1111u, 0xDEADBEEFu };
+
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 3 ), CRC_AGREEMENT_AGREE );
+
+ // the leaver is also not counted towards the quorum: three connected players, and the two
+ // hashes that are here are not enough.
+ reported[2] = FALSE;
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 3 ), CRC_AGREEMENT_TOO_FEW );
+}
+
+TEST(crcagreement_two_players_who_are_both_here_and_disagree_is_a_mismatch)
+{
+ Bool reported[4] = { TRUE, TRUE, TRUE, TRUE };
+ Bool connected[4] = { TRUE, TRUE, TRUE, TRUE };
+ UnsignedInt crc[4] = { 0x1111u, 0x1111u, 0x2222u, 0x1111u };
+
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 4 ), CRC_AGREEMENT_MISMATCH );
+
+ // the disagreement is found wherever it sits, including against the first reporter
+ crc[0] = 0x2222u; crc[1] = 0x1111u; crc[2] = 0x1111u; crc[3] = 0x1111u;
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 4 ), CRC_AGREEMENT_MISMATCH );
+
+ // but only between players who are both still connected
+ connected[0] = FALSE;
+ CHECK_EQ( crcAgreement( reported, crc, connected, 4, 3 ), CRC_AGREEMENT_AGREE );
+}
+
+TEST(crcagreement_a_room_with_nobody_left_in_it_is_not_a_mismatch)
+{
+ Bool reported[2] = { FALSE, FALSE };
+ Bool connected[2] = { FALSE, FALSE };
+ UnsignedInt crc[2] = { 0u, 0u };
+
+ // nothing to compare, and nothing expected: agreement by default, never a mismatch. EA read
+ // begin() unconditionally here.
+ CHECK_EQ( crcAgreement( reported, crc, connected, 2, 0 ), CRC_AGREEMENT_AGREE );
+
+ // one player alone in the room agrees with themselves
+ reported[0] = TRUE; connected[0] = TRUE; crc[0] = 0x99u;
+ CHECK_EQ( crcAgreement( reported, crc, connected, 2, 1 ), CRC_AGREEMENT_AGREE );
+}
+
// ---------------------------------------------------------------------------------------------
// Per-game state that a reused Player has to give back.
// ---------------------------------------------------------------------------------------------