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): ask about the right player when validating a CRC

Olcay Seygan957e7ba391ea
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/PlayerList.h b/GeneralsMD/Code/GameEngine/Include/Common/PlayerList.h
index 5099ad586..93ba9a077 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/PlayerList.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/PlayerList.h
@@ -55,6 +55,7 @@
class DataChunkInput;
struct DataChunkInfo;
class DataChunkOutput;
+class GameInfo;
class Player;
class Team;
class TeamFactory;
@@ -153,6 +154,15 @@ public:
*/
PlayerMaskType getPlayersWithRelationship( Int srcPlayerIndex, UnsignedInt allowedRelationships );
+ /**
+ The network slot a player index came from, or -1 if this player is not somebody's seat in the
+ game (the neutral player, and every player in a game that has no GameInfo behind it).
+
+ These two numbers are not interchangeable and the network code is indexed by the slot: player
+ 0 is always the neutral player, so the slot a human sits in is not the index their Player has.
+ */
+ Int getSlotIndex( Int playerIndex ) const;
+
protected:
// snapshot methods
@@ -162,9 +172,13 @@ protected:
private:
+ void assignSlotIndices( const GameInfo *gameInfo );
+ void setSlotIndex( Int playerIndex, Int slotIndex );
+
Player *m_local;
Int m_playerCount;
Player *m_players[MAX_PLAYER_COUNT];
+ Int m_slotIndices[MAX_PLAYER_COUNT]; ///< which network slot each player index came from, -1 for none
};
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp
index 500062689..078de37ac 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp
@@ -58,6 +58,7 @@
#include "GameLogic/Object.h"
#endif
#include "GameLogic/SidesList.h"
+#include "GameNetwork/GameInfo.h"
#include "GameNetwork/NetworkDefs.h"
#ifdef _INTERNAL
@@ -233,6 +234,10 @@ void PlayerList::newGame()
p->setDefaultTeam();
}
+ /* Now that the players exist and are named, write down which network slot each one came from.
+ The two numbers are not the same and never were: player 0 is the neutral player, so a human
+ in slot 0 is player 1. Everything in GameNetwork is indexed by the slot. */
+ assignSlotIndices( TheGameInfo );
}
//-----------------------------------------------------------------------------
@@ -244,6 +249,9 @@ void PlayerList::init()
for (int i = 1; i < MAX_PLAYER_COUNT; i++)
m_players[i]->init(NULL);
+ for (int j = 0; j < MAX_PLAYER_COUNT; j++)
+ m_slotIndices[j] = -1;
+
// call setLocalPlayer so that becomingLocalPlayer() gets called appropriately
setLocalPlayer(m_players[0]);
@@ -491,3 +499,43 @@ void PlayerList::loadPostProcess( void )
} // end postProcessLoad
+//-----------------------------------------------------------------------------
+void PlayerList::setSlotIndex( Int playerIndex, Int slotIndex )
+{
+ if (playerIndex >= 0 && playerIndex < MAX_PLAYER_COUNT)
+ m_slotIndices[playerIndex] = slotIndex;
+}
+
+//-----------------------------------------------------------------------------
+Int PlayerList::getSlotIndex( Int playerIndex ) const
+{
+ if (playerIndex >= 0 && playerIndex < MAX_PLAYER_COUNT)
+ return m_slotIndices[playerIndex];
+
+ return -1;
+}
+
+//-----------------------------------------------------------------------------
+/** Map every occupied network slot onto the player index that was built from it. The slots are
+ named "player0".."playerN" when the game is set up, which is the only link between the two. */
+void PlayerList::assignSlotIndices( const GameInfo *gameInfo )
+{
+ for (int i = 0; i < MAX_PLAYER_COUNT; i++)
+ m_slotIndices[i] = -1;
+
+ if (gameInfo == NULL)
+ return;
+
+ AsciiString playerName;
+ for (Int slot = 0; slot < MAX_SLOTS; ++slot)
+ {
+ const GameSlot *gameSlot = gameInfo->getConstSlot( slot );
+ if (gameSlot == NULL || !gameSlot->isOccupied())
+ continue;
+
+ playerName.format( "player%d", slot );
+ Player *player = findPlayerWithNameKey( TheNameKeyGenerator->nameToKey( playerName ) );
+ if (player)
+ setSlotIndex( player->getPlayerIndex(), slot );
+ }
+}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index ccedb7100..9dc1dabfc 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -2555,26 +2555,32 @@ void GameLogic::processCommandList( CommandList *list )
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)
+ /* m_cachedCRCs is keyed by player index and the network is indexed by slot, which are not the
+ same number - player 0 is the neutral player, so the human sitting in slot 0 is player 1.
+ EA handed the player index straight to isPlayerConnected(). A player index with no slot
+ behind it (no GameInfo at all, for instance) counts as present rather than as gone: the
+ point of this is to stop blaming people who left, not to switch the check off. */
+ Bool reported[MAX_PLAYER_COUNT];
+ Bool connected[MAX_PLAYER_COUNT];
+ UnsignedInt crcs[MAX_PLAYER_COUNT];
+ for (Int p = 0; p < MAX_PLAYER_COUNT; ++p)
{
- reported[slot] = FALSE;
- crcs[slot] = 0;
- connected[slot] = TheNetwork->isPlayerConnected(slot);
+ reported[p] = FALSE;
+ crcs[p] = 0;
+ const Int slot = ThePlayerList->getSlotIndex(p);
+ connected[p] = (slot < 0) ? TRUE : TheNetwork->isPlayerConnected(slot);
}
for (std::map<Int, UnsignedInt>::const_iterator crcIt = m_cachedCRCs.begin();
crcIt != m_cachedCRCs.end(); ++crcIt)
{
- if (crcIt->first >= 0 && crcIt->first < MAX_SLOTS)
+ if (crcIt->first >= 0 && crcIt->first < MAX_PLAYER_COUNT)
{
reported[crcIt->first] = TRUE;
crcs[crcIt->first] = crcIt->second;
}
}
- switch (crcAgreement( reported, crcs, connected, MAX_SLOTS, numPlayers ))
+ switch (crcAgreement( reported, crcs, connected, MAX_PLAYER_COUNT, numPlayers ))
{
case CRC_AGREEMENT_TOO_FEW:
DEBUG_LOG(("Only %d CRCs from %d connected players on frame %d - not comparing\n",