Skip to content

Commit 8ca70df

Browse files
committed
refactor(network): extract slot index lookup to PlayerList
1 parent eff3e12 commit 8ca70df

8 files changed

Lines changed: 107 additions & 40 deletions

File tree

Core/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,15 +2393,8 @@ bool GameLogic::onLogicCrc(MAYBE_UNUSED GameMessage *msg)
23932393
Player *msgPlayer = getMessagePlayer(msg);
23942394
if (TheNetwork)
23952395
{
2396-
Int slotIndex = -1;
2397-
for (Int i=0; i<MAX_SLOTS; ++i)
2398-
{
2399-
if (msgPlayer->getPlayerType() == PLAYER_HUMAN && TheNetwork->getPlayerName(i) == msgPlayer->getPlayerDisplayName())
2400-
{
2401-
slotIndex = i;
2402-
break;
2403-
}
2404-
}
2396+
// GeneralsX @bugfix felipebraz 05/07/2026 Use pre-computed slot index to avoid string comparisons
2397+
Int slotIndex = ThePlayerList->getSlotIndex(msgPlayer->getPlayerIndex());
24052398

24062399
if (slotIndex < 0 || !TheNetwork->isPlayerConnected(slotIndex))
24072400
return false;

Generals/Code/GameEngine/Include/Common/PlayerList.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class PlayerList : public SubsystemInterface,
156156
*/
157157
PlayerMaskType getPlayersWithRelationship( Int srcPlayerIndex, UnsignedInt allowedRelationships );
158158

159+
void resolveSlotIndices();
160+
void setSlotIndex(Int playerIndex, Byte slotIndex);
161+
Byte getSlotIndex(Int playerIndex) const;
162+
159163
protected:
160164

161165
// snapshot methods
@@ -168,6 +172,7 @@ class PlayerList : public SubsystemInterface,
168172
Player *m_local;
169173
Int m_playerCount;
170174
Player *m_players[MAX_PLAYER_COUNT];
175+
Byte m_slotIndices[MAX_PLAYER_COUNT];
171176

172177
};
173178

Generals/Code/GameEngine/Source/Common/RTS/PlayerList.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
#endif
6060
#include "GameLogic/SidesList.h"
6161
#include "GameNetwork/NetworkDefs.h"
62+
#include "GameNetwork/GameInfo.h"
63+
#include "GameNetwork/GameSlot.h"
6264

6365

6466
//-----------------------------------------------------------------------------
@@ -69,9 +71,11 @@ PlayerList::PlayerList() :
6971
m_local(nullptr),
7072
m_playerCount(0)
7173
{
72-
// we only allocate a few of these, so don't bother pooling 'em
7374
for (Int i = 0; i < MAX_PLAYER_COUNT; i++)
75+
{
7476
m_players[ i ] = NEW Player( i );
77+
m_slotIndices[ i ] = -1;
78+
}
7579
init();
7680
}
7781

@@ -239,6 +243,8 @@ void PlayerList::newGame()
239243
p->setDefaultTeam();
240244
}
241245

246+
// GeneralsX @bugfix felipebraz 05/07/2026 Pre-compute the network slot to player mapping
247+
resolveSlotIndices();
242248
}
243249

244250
//-----------------------------------------------------------------------------
@@ -495,3 +501,41 @@ void PlayerList::loadPostProcess()
495501

496502
}
497503

504+
505+
void PlayerList::setSlotIndex(Int playerIndex, Byte slotIndex)
506+
{
507+
if (playerIndex >= 0 && playerIndex < ARRAY_SIZE(m_slotIndices))
508+
{
509+
m_slotIndices[playerIndex] = slotIndex;
510+
}
511+
}
512+
513+
Byte PlayerList::getSlotIndex(Int playerIndex) const
514+
{
515+
if (playerIndex >= 0 && playerIndex < ARRAY_SIZE(m_slotIndices))
516+
{
517+
return m_slotIndices[playerIndex];
518+
}
519+
520+
return -1;
521+
}
522+
523+
void PlayerList::resolveSlotIndices()
524+
{
525+
AsciiString playerName;
526+
527+
for (Int i = 0; i < MAX_SLOTS; ++i)
528+
{
529+
const GameSlot* slot = TheGameInfo->getSlot(i);
530+
if (!slot || !slot->isOccupied())
531+
continue;
532+
533+
playerName.format("player%d", i);
534+
535+
Player* player = findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(playerName));
536+
if (player)
537+
{
538+
setSlotIndex(player->getPlayerIndex(), i);
539+
}
540+
}
541+
}

Generals/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2379,20 +2379,8 @@ void GameLogic::processCommandList( CommandList *list )
23792379
{
23802380
// TheSuperHackers @bugfix Caball009 14/06/2026 Check if player is still connected,
23812381
// to avoid spurious mismatches at low CRC intervals, e.g. every frame.
2382-
// GeneralsX @bugfix felipebraz 05/07/2026 Map PlayerIndex to SlotIndex since isPlayerConnected expects a SlotIndex.
2383-
Player *player = ThePlayerList->getNthPlayer(it->first);
2384-
Int slotIndex = -1;
2385-
if (player)
2386-
{
2387-
for (Int i=0; i<MAX_SLOTS; ++i)
2388-
{
2389-
if (player->getPlayerType() == PLAYER_HUMAN && TheNetwork->getPlayerName(i) == player->getPlayerDisplayName())
2390-
{
2391-
slotIndex = i;
2392-
break;
2393-
}
2394-
}
2395-
}
2382+
// GeneralsX @bugfix felipebraz 05/07/2026 Use pre-computed slot index.
2383+
const Int slotIndex = ThePlayerList->getSlotIndex(it->first);
23962384
if (slotIndex >= 0 && !TheNetwork->isPlayerConnected(slotIndex))
23972385
continue;
23982386

GeneralsMD/Code/GameEngine/Include/Common/PlayerList.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class PlayerList : public SubsystemInterface,
156156
*/
157157
PlayerMaskType getPlayersWithRelationship( Int srcPlayerIndex, UnsignedInt allowedRelationships );
158158

159+
void resolveSlotIndices();
160+
void setSlotIndex(Int playerIndex, Byte slotIndex);
161+
Byte getSlotIndex(Int playerIndex) const;
162+
159163
protected:
160164

161165
// snapshot methods
@@ -168,6 +172,7 @@ class PlayerList : public SubsystemInterface,
168172
Player *m_local;
169173
Int m_playerCount;
170174
Player *m_players[MAX_PLAYER_COUNT];
175+
Byte m_slotIndices[MAX_PLAYER_COUNT];
171176

172177
};
173178

GeneralsMD/Code/GameEngine/Source/Common/RTS/PlayerList.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
#endif
6060
#include "GameLogic/SidesList.h"
6161
#include "GameNetwork/NetworkDefs.h"
62+
#include "GameNetwork/GameInfo.h"
63+
#include "GameNetwork/GameSlot.h"
6264

6365

6466
//-----------------------------------------------------------------------------
@@ -69,9 +71,11 @@ PlayerList::PlayerList() :
6971
m_local(nullptr),
7072
m_playerCount(0)
7173
{
72-
// we only allocate a few of these, so don't bother pooling 'em
7374
for (Int i = 0; i < MAX_PLAYER_COUNT; i++)
75+
{
7476
m_players[ i ] = NEW Player( i );
77+
m_slotIndices[ i ] = -1;
78+
}
7579
init();
7680
}
7781

@@ -241,6 +245,8 @@ void PlayerList::newGame()
241245
p->setDefaultTeam();
242246
}
243247

248+
// GeneralsX @bugfix felipebraz 05/07/2026 Pre-compute the network slot to player mapping
249+
resolveSlotIndices();
244250
}
245251

246252
//-----------------------------------------------------------------------------
@@ -497,3 +503,41 @@ void PlayerList::loadPostProcess()
497503

498504
}
499505

506+
void PlayerList::setSlotIndex(Int playerIndex, Byte slotIndex)
507+
{
508+
if (playerIndex >= 0 && playerIndex < ARRAY_SIZE(m_slotIndices))
509+
{
510+
m_slotIndices[playerIndex] = slotIndex;
511+
}
512+
}
513+
514+
Byte PlayerList::getSlotIndex(Int playerIndex) const
515+
{
516+
if (playerIndex >= 0 && playerIndex < ARRAY_SIZE(m_slotIndices))
517+
{
518+
return m_slotIndices[playerIndex];
519+
}
520+
521+
return -1;
522+
}
523+
524+
void PlayerList::resolveSlotIndices()
525+
{
526+
AsciiString playerName;
527+
528+
for (Int i = 0; i < MAX_SLOTS; ++i)
529+
{
530+
const GameSlot* slot = TheGameInfo->getSlot(i);
531+
if (!slot || !slot->isOccupied())
532+
continue;
533+
534+
playerName.format("player%d", i);
535+
536+
Player* player = findPlayerWithNameKey(TheNameKeyGenerator->nameToKey(playerName));
537+
if (player)
538+
{
539+
setSlotIndex(player->getPlayerIndex(), i);
540+
}
541+
}
542+
}
543+

GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,20 +2730,8 @@ void GameLogic::processCommandList( CommandList *list )
27302730
{
27312731
// TheSuperHackers @bugfix Caball009 14/06/2026 Check if player is still connected,
27322732
// to avoid spurious mismatches at low CRC intervals, e.g. every frame.
2733-
// GeneralsX @bugfix felipebraz 05/07/2026 Map PlayerIndex to SlotIndex since isPlayerConnected expects a SlotIndex.
2734-
Player *player = ThePlayerList->getNthPlayer(it->first);
2735-
Int slotIndex = -1;
2736-
if (player)
2737-
{
2738-
for (Int i=0; i<MAX_SLOTS; ++i)
2739-
{
2740-
if (player->getPlayerType() == PLAYER_HUMAN && TheNetwork->getPlayerName(i) == player->getPlayerDisplayName())
2741-
{
2742-
slotIndex = i;
2743-
break;
2744-
}
2745-
}
2746-
}
2733+
// GeneralsX @bugfix felipebraz 05/07/2026 Use pre-computed slot index.
2734+
const Int slotIndex = ThePlayerList->getSlotIndex(it->first);
27472735
if (slotIndex >= 0 && !TheNetwork->isPlayerConnected(slotIndex))
27482736
continue;
27492737

references/fbraz3-dxvk

0 commit comments

Comments
 (0)