Skip to content

Commit 813284b

Browse files
refactor: Refactor GameLogic::startNewGame
Remove lines already processed by reset(), which is called right before startNewGame(). Replace local var GameInfo* game with TheGameInfo as these are always the same. Move shared Int i to their own loops.
1 parent b878990 commit 813284b

File tree

2 files changed

+76
-105
lines changed

2 files changed

+76
-105
lines changed

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

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,12 @@ static void checkForDuplicateColors( GameInfo *game )
672672
{
673673
if(!game)
674674
return;
675-
Int i;
676675

677676
// In QuickMatch, people can possibly set preferred color and house.
678677
// Here, we check for collisions in the color choice. If there is a
679678
// collision, the first player will get the color, and the other(s)
680679
// will map to random.
681-
for (i=MAX_SLOTS-1; i>=0; --i)
680+
for (Int i = MAX_SLOTS - 1; i>=0; --i)
682681
{
683682
GameSlot *slot = game->getSlot(i);
684683

@@ -708,12 +707,11 @@ static void populateRandomSideAndColor( GameInfo *game )
708707
{
709708
if(!game)
710709
return;
711-
Int i;
712710

713711
#define MORE_RANDOM
714712
#ifdef MORE_RANDOM
715713
std::vector<Int> startSlots;
716-
for (i = 0; i < ThePlayerTemplateStore->getPlayerTemplateCount(); ++i)
714+
for (Int i = 0; i < ThePlayerTemplateStore->getPlayerTemplateCount(); ++i)
717715
{
718716
const PlayerTemplate* ptTest = ThePlayerTemplateStore->getNthPlayerTemplate(i);
719717
if (!ptTest || ptTest->getStartingBuilding().isEmpty())
@@ -722,7 +720,7 @@ static void populateRandomSideAndColor( GameInfo *game )
722720
}
723721
#endif
724722

725-
for (i=0; i<MAX_SLOTS; ++i)
723+
for (Int i=0; i<MAX_SLOTS; ++i)
726724
{
727725
GameSlot *slot = game->getSlot(i);
728726

@@ -789,7 +787,6 @@ static void populateRandomStartPosition( GameInfo *game )
789787
if(!game)
790788
return;
791789

792-
Int i;
793790
Int numPlayers = MAX_SLOTS;
794791
const MapMetaData *md = TheMapCache->findMap( game->getMap() );
795792
if (md)
@@ -800,7 +797,7 @@ static void populateRandomStartPosition( GameInfo *game )
800797

801798
// generate a map of start spot distances
802799
Real startSpotDistance[MAX_SLOTS][MAX_SLOTS];
803-
for (i=0; i<MAX_SLOTS; ++i)
800+
for (Int i=0; i<MAX_SLOTS; ++i)
804801
{
805802
for (Int j=0; j<MAX_SLOTS; ++j)
806803
{
@@ -834,27 +831,27 @@ static void populateRandomStartPosition( GameInfo *game )
834831
// see if a start spot has been chosen at all yet
835832
Bool hasStartSpotBeenPicked = FALSE;
836833
Bool taken[MAX_SLOTS];
837-
for (i=0; i<MAX_SLOTS; ++i)
834+
for (Int i=0; i<MAX_SLOTS; ++i)
838835
{
839836
taken[i] = (i<numPlayers)?FALSE:TRUE;
840837
}
841-
for (i=0; i<MAX_SLOTS; ++i)
838+
for (Int i=0; i<MAX_SLOTS; ++i)
842839
{
843840
GameSlot *slot = game->getSlot(i);
844841

845842
if (!slot || !slot->isOccupied() || slot->getPlayerTemplate() == PLAYERTEMPLATE_OBSERVER)
846843
continue;
847844

848845
Int posIdx = slot->getStartPos();
849-
if (posIdx >= 0 || posIdx >= numPlayers)
846+
if (posIdx >= 0 || posIdx >= numPlayers) // TheSuperHackers @todo stm 19/12/2025 this is not a proper bounds check
850847
{
851848
hasStartSpotBeenPicked = TRUE;
852849
taken[posIdx] = TRUE;
853850
}
854851
}
855852

856853
// now pick non-observer spots
857-
for (i=0; i<MAX_SLOTS; ++i)
854+
for (Int i=0; i<MAX_SLOTS; ++i)
858855
{
859856
GameSlot *slot = game->getSlot(i);
860857

@@ -924,13 +921,13 @@ static void populateRandomStartPosition( GameInfo *game )
924921

925922
// now go back & assign observer spots
926923
Int numPlayersInGame = 0;
927-
for (i=0; i<MAX_SLOTS; ++i)
924+
for (Int i=0; i<MAX_SLOTS; ++i)
928925
{
929926
const GameSlot *slot = game->getConstSlot(i);
930927
if (slot->isOccupied() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
931928
++numPlayersInGame;
932929
}
933-
for (i=0; i<MAX_SLOTS; ++i)
930+
for (Int i=0; i<MAX_SLOTS; ++i)
934931
{
935932
GameSlot *slot = game->getSlot(i);
936933

@@ -1075,60 +1072,53 @@ void GameLogic::startNewGame( Bool saveGame )
10751072

10761073
}
10771074

1078-
m_rankLevelLimit = 1000; // this is reset every game.
10791075
setDefaults( saveGame );
10801076
TheWritableGlobalData->m_loadScreenRender = TRUE; ///< mark it so only a few select things are rendered during load
10811077
TheWritableGlobalData->m_TiVOFastMode = FALSE; //always disable the TIVO fast-forward mode at the start of a new game.
10821078

1083-
m_showBehindBuildingMarkers = TRUE;
1084-
m_drawIconUI = TRUE;
1085-
m_showDynamicLOD = TRUE;
1086-
m_scriptHulkMaxLifetimeOverride = -1;
1087-
10881079
// Fill in the game color and Factions before we do the Load Screen
1089-
GameInfo *game = NULL;
10901080
TheGameInfo = NULL;
10911081
Int localSlot = 0;
10921082
if (TheNetwork)
10931083
{
10941084
if (TheLAN)
10951085
{
10961086
DEBUG_LOG(("Starting network game"));
1097-
TheGameInfo = game = TheLAN->GetMyGame();
1087+
TheGameInfo = TheLAN->GetMyGame();
10981088
}
10991089
else
11001090
{
11011091
DEBUG_LOG(("Starting gamespy game"));
1102-
TheGameInfo = game = TheGameSpyGame; /// @todo: MDC add back in after demo
1092+
TheGameInfo = TheGameSpyGame; /// @todo: MDC add back in after demo
11031093
}
11041094
}
11051095
else
11061096
{
11071097
if (TheRecorder && TheRecorder->isPlaybackMode())
11081098
{
1109-
TheGameInfo = game = TheRecorder->getGameInfo();
1099+
TheGameInfo = TheRecorder->getGameInfo();
11101100
}
11111101
else if(m_gameMode == GAME_SKIRMISH)
11121102
{
1113-
TheGameInfo = game = TheSkirmishGameInfo;
1103+
TheGameInfo = TheSkirmishGameInfo;
11141104
}
1105+
// TheSuperHackers @info stm 18/12/2025 TheGameInfo is nullptr when you load a savegame of a regular campaign mission
11151106
}
11161107

1117-
checkForDuplicateColors( game );
1108+
checkForDuplicateColors( TheGameInfo );
11181109

11191110
Bool isSkirmishOrSkirmishReplay = FALSE;
1120-
if (game)
1111+
if (TheGameInfo)
11211112
{
11221113
for (Int i=0; i<MAX_SLOTS; ++i)
11231114
{
1124-
GameSlot *slot = game->getSlot(i);
1115+
GameSlot *slot = TheGameInfo->getSlot(i);
11251116
if (!saveGame) {
11261117
slot->saveOffOriginalInfo();
11271118
}
11281119
if (slot->isAI())
11291120
{
11301121
isSkirmishOrSkirmishReplay = TRUE;
1131-
continue;
11321122
}
11331123
}
11341124
} else {
@@ -1138,21 +1128,21 @@ void GameLogic::startNewGame( Bool saveGame )
11381128
}
11391129
}
11401130

1141-
populateRandomSideAndColor( game );
1142-
populateRandomStartPosition( game );
1131+
populateRandomSideAndColor( TheGameInfo );
1132+
populateRandomStartPosition( TheGameInfo );
11431133

11441134
//****************************//
11451135
// Start the LoadScreen Now! //
11461136
//****************************//
11471137

11481138
// Get the m_loadScreen for this kind of game
1149-
if(!m_loadScreen && !(TheRecorder && TheRecorder->getMode() == RECORDERMODETYPE_SIMULATION_PLAYBACK))
1139+
if(!m_loadScreen && (!TheRecorder || TheRecorder->getMode() != RECORDERMODETYPE_SIMULATION_PLAYBACK))
11501140
{
11511141
m_loadScreen = getLoadScreen( saveGame );
11521142
if(m_loadScreen && !TheGlobalData->m_headless)
11531143
{
11541144
TheMouse->setVisibility(FALSE);
1155-
m_loadScreen->init(game);
1145+
m_loadScreen->init(TheGameInfo);
11561146

11571147
updateLoadProgress( LOAD_PROGRESS_START );
11581148
}
@@ -1194,7 +1184,7 @@ void GameLogic::startNewGame( Bool saveGame )
11941184
#endif
11951185

11961186
Int progressCount = LOAD_PROGRESS_SIDE_POPULATION;
1197-
if (game)
1187+
if (TheGameInfo)
11981188
{
11991189

12001190
if (TheGameEngine->isMultiplayerSession() || isSkirmishOrSkirmishReplay)
@@ -1203,12 +1193,12 @@ void GameLogic::startNewGame( Bool saveGame )
12031193
TheSidesList->prepareForMP_or_Skirmish();
12041194
}
12051195

1206-
//DEBUG_LOG(("Starting LAN game with %d players", game->getNumPlayers()));
1196+
//DEBUG_LOG(("Starting LAN game with %d players", TheGameInfo->getNumPlayers()));
12071197
Dict d;
12081198
for (int i=0; i<MAX_SLOTS; ++i)
12091199
{
12101200
// Add a Side to TheSidesList
1211-
GameSlot *slot = game->getSlot(i);
1201+
GameSlot *slot = TheGameInfo->getSlot(i);
12121202

12131203
if (!slot || !slot->isHuman())
12141204
{
@@ -1235,7 +1225,7 @@ void GameLogic::startNewGame( Bool saveGame )
12351225
d.setAsciiString(TheKey_playerFaction, KEYNAME(pt->getNameKey()));
12361226
}
12371227

1238-
if (game->isPlayerPreorder(i))
1228+
if (TheGameInfo->isPlayerPreorder(i))
12391229
{
12401230
d.setBool(TheKey_playerIsPreorder, TRUE);
12411231
}
@@ -1245,7 +1235,7 @@ void GameLogic::startNewGame( Bool saveGame )
12451235
DEBUG_LOG(("Looking for allies of player %d, team %d", i, team));
12461236
for(int j=0; j < MAX_SLOTS; ++j)
12471237
{
1248-
GameSlot *teamSlot = game->getSlot(j);
1238+
GameSlot *teamSlot = TheGameInfo->getSlot(j);
12491239
// for check to see if we're trying to add ourselves
12501240
if(i == j || !teamSlot->isOccupied())
12511241
continue;
@@ -1299,7 +1289,7 @@ void GameLogic::startNewGame( Bool saveGame )
12991289
d.setInt(TheKey_multiplayerStartIndex, slot->getStartPos());
13001290
// d.setBool(TheKey_multiplayerIsLocal, slot->isLocalPlayer());
13011291
// d.setBool(TheKey_multiplayerIsLocal, slot->getIP() == game->getLocalIP());
1302-
d.setBool(TheKey_multiplayerIsLocal, slot->isHuman() && (slot->getName().compare(game->getSlot(game->getLocalSlotNum())->getName().str()) == 0));
1292+
d.setBool(TheKey_multiplayerIsLocal, slot->isHuman() && (slot->getName().compare(TheGameInfo->getSlot(TheGameInfo->getLocalSlotNum())->getName().str()) == 0));
13031293

13041294
/*
13051295
if (slot->getIP() == game->getLocalIP())
@@ -1322,7 +1312,7 @@ void GameLogic::startNewGame( Bool saveGame )
13221312

13231313
AsciiString slotNameAscii;
13241314
slotNameAscii.translate(slot->getName());
1325-
if (slot->isHuman() && game->getSlotNum(slotNameAscii) == game->getLocalSlotNum()) {
1315+
if (slot->isHuman() && TheGameInfo->getSlotNum(slotNameAscii) == TheGameInfo->getLocalSlotNum()) {
13261316
localSlot = i;
13271317
}
13281318
TheSidesList->addSide(&d);
@@ -1392,11 +1382,11 @@ void GameLogic::startNewGame( Bool saveGame )
13921382
// if there are no other teams (happens for debugging) don't end the game immediately
13931383
Int numTeams = 0; // this can be higher than expected, but is accurate for determining 0, 1, 2+
13941384
Int lastTeam = -1;
1395-
if (game)
1385+
if (TheGameInfo)
13961386
{
13971387
for (int i=0; i<MAX_SLOTS; ++i)
13981388
{
1399-
const GameSlot *slot = game->getConstSlot(i);
1389+
const GameSlot *slot = TheGameInfo->getConstSlot(i);
14001390
if (slot->isOccupied() && slot->getPlayerTemplate() != PLAYERTEMPLATE_OBSERVER)
14011391
{
14021392
if (slot->getTeamNumber() == -1 || slot->getTeamNumber() != lastTeam)
@@ -1625,11 +1615,11 @@ void GameLogic::startNewGame( Bool saveGame )
16251615
ThePartitionManager->revealMapForPlayerPermanently( observerPlayer->getPlayerIndex() );
16261616
DEBUG_LOG(("Reveal shroud for %ls whose index is %d", observerPlayer->getPlayerDisplayName().str(), observerPlayer->getPlayerIndex()));
16271617

1628-
if (game)
1618+
if (TheGameInfo)
16291619
{
16301620
for (int i=0; i<MAX_SLOTS; ++i)
16311621
{
1632-
GameSlot *slot = game->getSlot(i);
1622+
GameSlot *slot = TheGameInfo->getSlot(i);
16331623

16341624
if (!slot || !slot->isOccupied())
16351625
continue;
@@ -1763,11 +1753,11 @@ void GameLogic::startNewGame( Bool saveGame )
17631753

17641754
progressCount = LOAD_PROGRESS_LOOP_INITIAL_NETWORK_BUILDINGS;
17651755
// place initial network buildings/units
1766-
if (game && !saveGame)
1756+
if (TheGameInfo && !saveGame)
17671757
{
17681758
for (int i=0; i<MAX_SLOTS; ++i)
17691759
{
1770-
GameSlot *slot = game->getSlot(i);
1760+
GameSlot *slot = TheGameInfo->getSlot(i);
17711761

17721762
if (!slot || !slot->isOccupied())
17731763
continue;
@@ -1842,9 +1832,9 @@ void GameLogic::startNewGame( Bool saveGame )
18421832
// Note - We construct the multiplayer start spot name manually here, so change this if you
18431833
// change TheKey_Player_1_Start etc. mdc
18441834
AsciiString startingCamName = TheNameKeyGenerator->keyToName(TheKey_InitialCameraPosition);
1845-
if (game)
1835+
if (TheGameInfo)
18461836
{
1847-
GameSlot *slot = game->getSlot(localSlot);
1837+
GameSlot *slot = TheGameInfo->getSlot(localSlot);
18481838
DEBUG_ASSERTCRASH(slot, ("Starting a LAN game without ourselves!"));
18491839

18501840
if (slot->isHuman())
@@ -2042,10 +2032,6 @@ void GameLogic::startNewGame( Bool saveGame )
20422032
TheTacticalView->lookAt( &thePos );
20432033
#endif
20442034

2045-
2046-
// @todo remove this hack
2047-
// TheGlobalData->m_inGame = TRUE;
2048-
20492035
// If we are now starting a multiplayer or skirmish game, let us set the local players selectionto be the command center
20502036
// We'll ask the Recorder, so we survive replays
20512037
if( TheRecorder->isMultiplayer() )

0 commit comments

Comments
 (0)