Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fixedSeed param to GameSetup and use it to initialize RNG #875

Merged
2 changes: 2 additions & 0 deletions doc/StartScriptFormat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
NumTeams=y; // same here, set this to check if the script is right
NumAllyTeams=z; // see above

FixedSeed = 1; //Initialize the synced RNG with a specific seed for reproducible benchmarking runs only
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 'benchmarking runs only'? Surely it's useful for things like say reproducible cutscenes in missions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didnt think of that, thats a good use case!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed - check out the new description


// A player (controlls a team, player 0 is the host only if IsHost is not set)
[PLAYER0]
{
Expand Down
3 changes: 3 additions & 0 deletions rts/Game/GameSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ CR_REG_METADATA(CGameSetup, (
CR_IGNORED(dsMapHash),
CR_IGNORED(dsModHash),
CR_IGNORED(mapSeed),
CR_IGNORED(fixedSeed),

CR_IGNORED(gameStartDelay),

Expand Down Expand Up @@ -180,6 +181,7 @@ void CGameSetup::ResetState()
std::memset(dsMapHash, 0, sizeof(dsMapHash));
std::memset(dsModHash, 0, sizeof(dsModHash));
mapSeed = 0;
fixedSeed = 0;

gameStartDelay = 0;
numDemoPlayers = 0;
Expand Down Expand Up @@ -582,6 +584,7 @@ bool CGameSetup::Init(const std::string& buf)

file.GetTDef(mapSeed, unsigned(0), "GAME\\MapSeed");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like "map seed" exists which makes "fixed seed" not obviously RNG related. Perhaps call it "FixedRNGSeed" or something? Otherwise looks good though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FixedRNGSeed is much more clear. Changing that 🙌

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍


file.GetTDef(fixedSeed, unsigned(0), "GAME\\FixedSeed"); // 0 means use random seed
gameID = file.SGetValueDef("", "GAME\\GameID");
modName = file.SGetValueDef("", "GAME\\Gametype");
mapName = file.SGetValueDef("", "GAME\\MapName");
Expand Down
2 changes: 2 additions & 0 deletions rts/Game/GameSetup.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class CGameSetup
std::copy(gs.dsMapHash, gs.dsMapHash + sizeof(dsMapHash), dsMapHash);
std::copy(gs.dsModHash, gs.dsModHash + sizeof(dsModHash), dsModHash);
mapSeed = gs.mapSeed;
fixedSeed = gs.fixedSeed;

gameStartDelay = gs.gameStartDelay;

Expand Down Expand Up @@ -174,6 +175,7 @@ class CGameSetup
StartPos_Last = 3 // last entry in enum (for user input check)
};

uint32_t fixedSeed;

bool fixedAllies;
bool useLuaGaia;
Expand Down
6 changes: 5 additions & 1 deletion rts/Game/PreGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ void CPreGame::StartServer(const std::string& setupscript)
std::shared_ptr<CGameSetup> startGameSetup(new CGameSetup());

startGameSetup->Init(setupscript);
startGameData->SetRandomSeed(static_cast<unsigned>(guRNG.NextInt()));
if (startGameSetup->fixedSeed == 0) {
startGameData->SetRandomSeed(static_cast<unsigned>(guRNG.NextInt()));
} else {
startGameData->SetRandomSeed(startGameSetup->fixedSeed);
}

if (startGameSetup->mapName.empty())
throw content_error("No map selected in startscript");
Expand Down
10 changes: 7 additions & 3 deletions rts/Net/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,13 @@ void CGameServer::Initialize()
streflop::streflop_init<streflop::Simple>();

if (!demoReader) {
GenerateAndSendGameID();
rng.Seed(gameID.intArray[0] ^ gameID.intArray[1] ^ gameID.intArray[2] ^ gameID.intArray[3]);
Broadcast(CBaseNetProtocol::Get().SendRandSeed(rng()));
if (myGameSetup->fixedSeed == 0) {
GenerateAndSendGameID();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't gameID be sent regardless?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good find, you're absolutely right this should live outside the condition

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

rng.Seed(gameID.intArray[0] ^ gameID.intArray[1] ^ gameID.intArray[2] ^ gameID.intArray[3]);
Broadcast(CBaseNetProtocol::Get().SendRandSeed(rng()));
} else {
Broadcast(CBaseNetProtocol::Get().SendRandSeed(myGameSetup->fixedSeed));
}
}
}

Expand Down