Skip to content

Commit 7e6516d

Browse files
committed
Refactored RandomSeed function
1 parent fb5534b commit 7e6516d

File tree

13 files changed

+103
-260
lines changed

13 files changed

+103
-260
lines changed

Core/GameEngine/Include/Common/RandomValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
extern void InitRandom( void );
3434
extern void InitRandom( UnsignedInt seed );
35+
extern void InitRandomType( const AsciiString& type, UnsignedInt tickCount = 0, Bool gameLogicOnly = FALSE);
3536
extern void InitGameLogicRandom( UnsignedInt seed ); ///< Set the GameLogic seed to a known value at game start
3637
extern UnsignedInt GetGameLogicRandomSeed( void ); ///< Get the seed (used for replays)
3738
extern UnsignedInt GetGameLogicRandomSeedCRC( void );///< Get the seed (used for CRCs)

Core/GameEngine/Include/GameNetwork/GameInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class GameInfo
247247
UnsignedInt m_mapSize;
248248
Int m_mapMask;
249249
Int m_seed;
250+
//std::vector<std::pair<UnsignedInt, Int>> m_seedsInFrames;
250251
Int m_useStats;
251252
Money m_startingCash;
252253
UnsignedShort m_superweaponRestriction;

Core/GameEngine/Source/Common/RandomValue.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,42 @@ DEBUG_LOG(( "InitRandom Logic %08lx",seed));
192192
#endif
193193
}
194194

195+
void InitRandomType( const AsciiString& type, UnsignedInt tickCount, Bool gameLogicOnly )
196+
{
197+
Int value = tickCount;
198+
if(type == "MORE_RANDOM")
199+
{
200+
Real val = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
201+
val*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
202+
value = REAL_TO_INT(val);
203+
}
204+
else if(type == "EXHAUSTIVE")
205+
{
206+
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
207+
Int verysilly = fabs(silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3)));
208+
silly = GameLogicRandomValue(0, verysilly);
209+
for (UnsignedInt poo = 0; poo < silly; ++poo)
210+
{
211+
GameLogicRandomValue(0, 1); // ignore result
212+
}
213+
silly *= silly;
214+
Int fullsilly = max(Int(silly+1), Int(1e10));
215+
value = GameLogicRandomValue(silly, fullsilly);
216+
}
217+
else if(type == "TIME" || type == "DETERMINISTIC")
218+
{
219+
time_t seconds = time( NULL );
220+
value = seconds;
221+
}
222+
223+
//DEBUG_LOG(("Seed value: %d", value));
224+
225+
if(gameLogicOnly)
226+
InitGameLogicRandom(value);
227+
else
228+
InitRandom(value);
229+
}
230+
195231
//
196232
// Integer random value
197233
//

Core/GameEngine/Source/GameNetwork/GameInfo.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,18 @@ void GameInfo::setSeed( Int seed )
682682
m_seed = seed;
683683
}
684684

685+
/*void GameInfo::setNewSeedInFrame( UnsignedInt frame, Int seed )
686+
{
687+
if(frame == 0)
688+
m_seedsInFrames.clear();
689+
690+
std::pair<UnsignedInt, Int> seedInFrame;
691+
seedInFrame.first = frame;
692+
seedInFrame.second = seed;
693+
694+
m_seedsInFrames.push_back(seedInFrame);
695+
}*/
696+
685697
void GameInfo::setSlotPointer( Int index, GameSlot *slot )
686698
{
687699
if (index < 0 || index >= MAX_SLOTS)
@@ -1614,6 +1626,44 @@ void SkirmishGameInfo::xfer( Xfer *xfer )
16141626
xfer->xferInt(&m_mapMask);
16151627
xfer->xferInt(&m_seed);
16161628

1629+
/*UnsignedShort seedFrameSize = m_seedsInFrames.size();
1630+
xfer->xferUnsignedShort(seedFrameSize);
1631+
1632+
UnsignedInt frame;
1633+
Int seed;
1634+
if(xfer->getXferMode() == XFER_SAVE)
1635+
{
1636+
for(int i = 0; i < seedFrameSize; i++)
1637+
{
1638+
frame = m_seedsInFrames.first;
1639+
xfer->xferUnsignedInt(&frame);
1640+
1641+
seed = m_seedsInFrames.second;
1642+
xfer->xferInt(&seed);
1643+
}
1644+
}
1645+
else
1646+
{
1647+
if (m_seedsInFrames.empty() == false)
1648+
{
1649+
DEBUG_CRASH(( "GameInfo::xfer - m_seedsInFrames should be empty, but is not"));
1650+
//throw SC_INVALID_DATA;
1651+
}
1652+
1653+
for(int i = 0; i < seedFrameSize; i++)
1654+
{
1655+
xfer->xferUnsignedInt(&frame);
1656+
xfer->xferInt(&seed);
1657+
1658+
std::pair<UnsignedInt, Int> seedInFrame;
1659+
seedInFrame.first = frame;
1660+
seedInFrame.second = seed;
1661+
1662+
m_seedsInFrames.push_back(seedInFrame);
1663+
}
1664+
}
1665+
*/
1666+
16171667
if ( version >= 3 )
16181668
{
16191669
xfer->xferUnsignedShort( &m_superweaponRestriction );

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -715,35 +715,7 @@ void GameEngine::init()
715715
msg->appendIntegerArgument(GAME_SINGLE_PLAYER);
716716
msg->appendIntegerArgument(DIFFICULTY_NORMAL);
717717
msg->appendIntegerArgument(0);
718-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
719-
{
720-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
721-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
722-
InitRandom(Int(value));
723-
}
724-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
725-
{
726-
//
727-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
728-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
729-
silly = GameLogicRandomValue(0, verysilly);
730-
for (UnsignedInt poo = 0; poo < silly; ++poo)
731-
{
732-
GameLogicRandomValue(0, 1); // ignore result
733-
}
734-
silly *= silly;
735-
Int fullsilly = max(Int(silly+1), Int(1e10));
736-
Int value = GameLogicRandomValue(silly, fullsilly);
737-
InitRandom(value);
738-
}
739-
else if(TheGlobalData->m_initRandomType == "TIME")
740-
{
741-
InitRandom();
742-
}
743-
else
744-
{
745-
InitRandom(0);
746-
}
718+
InitRandomType(TheGlobalData->m_initRandomType);
747719

748720
//DEBUG_LOG(("Playing Game. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
749721
}

GeneralsMD/Code/GameEngine/Source/Common/System/SaveGame/GameState.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -739,35 +739,7 @@ SaveCode GameState::loadGame( AvailableGameInfo gameInfo )
739739
if( getSaveGameInfo()->saveFileType == SAVE_FILE_TYPE_MISSION )
740740
{
741741

742-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
743-
{
744-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
745-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
746-
InitRandom(Int(value));
747-
}
748-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
749-
{
750-
//
751-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
752-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
753-
silly = GameLogicRandomValue(0, verysilly);
754-
for (UnsignedInt poo = 0; poo < silly; ++poo)
755-
{
756-
GameLogicRandomValue(0, 1); // ignore result
757-
}
758-
silly *= silly;
759-
Int fullsilly = max(Int(silly+1), Int(1e10));
760-
Int value = GameLogicRandomValue(silly, fullsilly);
761-
InitRandom(value);
762-
}
763-
else if(TheGlobalData->m_initRandomType == "TIME")
764-
{
765-
InitRandom();
766-
}
767-
else
768-
{
769-
InitRandom(0);
770-
}
742+
InitRandomType(TheGlobalData->m_initRandomType);
771743
//DEBUG_LOG(("Saving Game. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
772744

773745
TheWritableGlobalData->m_pendingFile = getSaveGameInfo()->missionMapName;

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ChallengeMenu.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -686,35 +686,7 @@ WindowMsgHandledType ChallengeMenuSystem( GameWindow *window, UnsignedInt msg, W
686686
// GameEngine will still apply the default "FRAME CAP" as it does during "Solo Missions."
687687
msg->appendIntegerArgument(LOGICFRAMES_PER_SECOND); // FPS limit
688688

689-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
690-
{
691-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
692-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
693-
InitRandom(Int(value));
694-
}
695-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
696-
{
697-
//
698-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
699-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
700-
silly = GameLogicRandomValue(0, verysilly);
701-
for (UnsignedInt poo = 0; poo < silly; ++poo)
702-
{
703-
GameLogicRandomValue(0, 1); // ignore result
704-
}
705-
silly *= silly;
706-
Int fullsilly = max(Int(silly+1), Int(1e10));
707-
Int value = GameLogicRandomValue(silly, fullsilly);
708-
InitRandom(value);
709-
}
710-
else if(TheGlobalData->m_initRandomType == "TIME")
711-
{
712-
InitRandom();
713-
}
714-
else
715-
{
716-
InitRandom(0);
717-
}
689+
InitRandomType(TheGlobalData->m_initRandomType);
718690
//DEBUG_LOG(("Playing Challenge. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
719691
}
720692
else if( controlID == buttonBackID )

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MainMenu.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -329,35 +329,7 @@ static void doGameStart( void )
329329
msg->appendIntegerArgument(GAME_SINGLE_PLAYER);
330330
msg->appendIntegerArgument(TheCampaignManager->getGameDifficulty());
331331
msg->appendIntegerArgument(TheCampaignManager->getRankPoints());
332-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
333-
{
334-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
335-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
336-
InitRandom(Int(value));
337-
}
338-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
339-
{
340-
//
341-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
342-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
343-
silly = GameLogicRandomValue(0, verysilly);
344-
for (UnsignedInt poo = 0; poo < silly; ++poo)
345-
{
346-
GameLogicRandomValue(0, 1); // ignore result
347-
}
348-
silly *= silly;
349-
Int fullsilly = max(Int(silly+1), Int(1e10));
350-
Int value = GameLogicRandomValue(silly, fullsilly);
351-
InitRandom(value);
352-
}
353-
else if(TheGlobalData->m_initRandomType == "TIME")
354-
{
355-
InitRandom();
356-
}
357-
else
358-
{
359-
InitRandom(0);
360-
}
332+
InitRandomType(TheGlobalData->m_initRandomType);
361333
//DEBUG_LOG(("Starting Single Player Game. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
362334

363335
isShuttingDown = TRUE;

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/MapSelectMenu.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,7 @@ static void doGameStart( void )
8585
else
8686
InitGameLogicRandom(GameClientRandomValue(0, INT_MAX - 1));
8787
*/
88-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
89-
{
90-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
91-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
92-
InitRandom(Int(value));
93-
}
94-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
95-
{
96-
//
97-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
98-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
99-
silly = GameLogicRandomValue(0, verysilly);
100-
for (UnsignedInt poo = 0; poo < silly; ++poo)
101-
{
102-
GameLogicRandomValue(0, 1); // ignore result
103-
}
104-
silly *= silly;
105-
Int fullsilly = max(Int(silly+1), Int(1e10));
106-
Int value = GameLogicRandomValue(silly, fullsilly);
107-
InitRandom(value);
108-
}
109-
else if(TheGlobalData->m_initRandomType == "TIME")
110-
{
111-
InitRandom();
112-
}
113-
else
114-
{
115-
InitRandom(0);
116-
}
88+
InitRandomType(TheGlobalData->m_initRandomType);
11789
//DEBUG_LOG(("Starting Skirmish. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
11890

11991
isShuttingDown = true;

GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/QuitMenu.cpp

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -240,35 +240,7 @@ static void restartMissionMenu()
240240
);
241241
//if (TheGlobalData->m_fixedSeed >= 0)
242242
//InitRandom(TheGlobalData->m_fixedSeed);
243-
if(TheGlobalData->m_initRandomType == "MORE_RANDOM")
244-
{
245-
Real value = GameLogicRandomValueReal(-PI,PI)*GameLogicRandomValue(0,100);
246-
value*= GameLogicRandomValueReal(0.0f,max(Real(GameLogicRandomValue(10,1e8)), Real(fabs(GetGameLogicRandomSeed()*GameLogicRandomValueReal(-value, value)))));
247-
InitRandom(Int(value));
248-
}
249-
else if(TheGlobalData->m_initRandomType == "EXHAUSTIVE")
250-
{
251-
//
252-
UnsignedInt silly = UnsignedInt((GetGameLogicRandomSeed()*GameLogicRandomValueReal(-2.0f,2.0f))) % 7;
253-
Int verysilly = silly * GameLogicRandomValueReal(0.0f, Real(GetGameLogicRandomSeed() % 3));
254-
silly = GameLogicRandomValue(0, verysilly);
255-
for (UnsignedInt poo = 0; poo < silly; ++poo)
256-
{
257-
GameLogicRandomValue(0, 1); // ignore result
258-
}
259-
silly *= silly;
260-
Int fullsilly = max(Int(silly+1), Int(1e10));
261-
Int value = GameLogicRandomValue(silly, fullsilly);
262-
InitRandom(value);
263-
}
264-
else if(TheGlobalData->m_initRandomType == "TIME")
265-
{
266-
InitRandom();
267-
}
268-
else
269-
{
270-
InitRandom(0);
271-
}
243+
InitRandomType(TheGlobalData->m_initRandomType);
272244
//DEBUG_LOG(("Restarting Game. Random Type: %s. Seed: %d", TheGlobalData->m_initRandomType.str(), GetGameLogicRandomSeed()));
273245
//else
274246
// InitGameLogicRandom(GameClientRandomValue(0, INT_MAX - 1));

0 commit comments

Comments
 (0)