From 4e195e10af9849290c657ea0f4dd24b87a8a1927 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 27 Sep 2012 06:42:57 +0000 Subject: [PATCH] - updated random seed generation to be more random --- source/glest_game/ai/ai.cpp | 5 ++++- source/glest_game/main/intro.cpp | 21 +++++++++---------- .../menu/menu_state_custom_game.cpp | 10 +++++++-- .../menu/menu_state_masterserver.cpp | 5 ++++- source/glest_game/network/connection_slot.cpp | 5 ++++- source/glest_game/types/skill_type.cpp | 10 +++++++-- source/shared_lib/sources/util/properties.cpp | 4 ++++ 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index f24e3733f..e016c7181 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -341,7 +341,10 @@ void Ai::update() { int factionSwitchTeamRequestCountCurrent = factionSwitchTeamRequestCount[vote->factionIndex]; //int allowJoinTeam = random.randRange(0, 100); - srand(time(NULL) + aiInterface->getMyFaction()->getIndex()); + //srand(time(NULL) + aiInterface->getMyFaction()->getIndex()); + Chrono seed(true); + srand(seed.getCurTicks() + aiInterface->getMyFaction()->getIndex()); + int allowJoinTeam = rand() % 100; SwitchTeamVote *voteResult = aiInterface->getMyFaction()->getSwitchTeamVote(vote->factionIndex); diff --git a/source/glest_game/main/intro.cpp b/source/glest_game/main/intro.cpp index 6451b89ba..6706117c5 100644 --- a/source/glest_game/main/intro.cpp +++ b/source/glest_game/main/intro.cpp @@ -234,8 +234,9 @@ Intro::Intro(Program *program): if(showIntroModelsRandom == true) { std::vector modelList; - unsigned int seed = time(NULL); - srand(seed); + //unsigned int seed = time(NULL); + Chrono seed(true); + srand(seed.getCurTicks()); int failedLookups=0; std::map usedIndex; for(;modelList.size() < models.size();) { @@ -243,14 +244,13 @@ Intro::Intro(Program *program): if(usedIndex.find(index) != usedIndex.end()) { failedLookups++; seed = time(NULL) / failedLookups; - srand(seed); + srand(seed.getCurTicks()); continue; } //printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); modelList.push_back(models[index]); usedIndex[index]=true; - seed = time(NULL) / modelList.size(); - srand(seed); + srand(seed.getCurTicks() / modelList.size()); } models = modelList; } @@ -425,23 +425,22 @@ Intro::Intro(Program *program): std::vector intoTexList; if(showIntroPicsRandom == true) { - unsigned int seed = time(NULL); - srand(seed); + //unsigned int seed = time(NULL); + Chrono seed(true); + srand(seed.getCurTicks()); int failedLookups=0; std::map usedIndex; for(;intoTexList.size() < showIntroPics;) { int picIndex = rand() % coreData.getMiscTextureList().size(); if(usedIndex.find(picIndex) != usedIndex.end()) { failedLookups++; - seed = time(NULL) / failedLookups; - srand(seed); + srand(seed.getCurTicks() / failedLookups); continue; } //printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); intoTexList.push_back(coreData.getMiscTextureList()[picIndex]); usedIndex[picIndex]=true; - seed = time(NULL) / intoTexList.size(); - srand(seed); + srand(seed.getCurTicks() / intoTexList.size()); } } else { diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 4c9d4c6a6..e8299e714 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -253,7 +253,10 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, listBoxTileset.init(xoffset+460, mapPos, 150); //listBoxTileset.setItems(results); setupTilesetList(""); - srand((unsigned int)time(NULL)); + //srand((unsigned int)time(NULL)); + Chrono seed(true); + srand(seed.getCurTicks()); + listBoxTileset.setSelectedItemIndex(rand() % listBoxTileset.getItemCount()); //tech Tree listBox @@ -1556,7 +1559,10 @@ void MenuStateCustomGame::PlayNow(bool saveGame) { // Max 1000 tries to get a random, unused faction for(int findRandomFaction = 1; findRandomFaction < 1000; ++findRandomFaction) { - srand((unsigned int)time(NULL) + findRandomFaction); + //srand((unsigned int)time(NULL) + findRandomFaction); + Chrono seed(true); + srand(seed.getCurTicks() + findRandomFaction); + int selectedFactionIndex = rand() % listBoxFactions[i].getItemCount(); string selectedFactionName = listBoxFactions[i].getItem(selectedFactionIndex); diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 92c050926..951ea1886 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -250,7 +250,10 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen GraphicComponent::applyAllCustomProperties(containerName); char szIRCNick[80]=""; - srand(time(NULL)); + //srand(time(NULL)); + Chrono seed(true); + srand(seed.getCurTicks()); + int randomNickId = rand() % 999; string netPlayerName=Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()); string ircname=netPlayerName.substr(0,9); diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index e592896a4..116fb403a 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -453,7 +453,10 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(this->isConnected() == true) { //RandomGen random; //sessionKey = random.randRange(-100000, 100000); - srand((unsigned int)time(NULL) / (this->playerIndex + 1)); + //srand((unsigned int)time(NULL) / (this->playerIndex + 1)); + Chrono seed(true); + srand(seed.getCurTicks() / (this->playerIndex + 1)); + sessionKey = rand() % 1000000; if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getOpenSlotCount(),sessionKey); diff --git a/source/glest_game/types/skill_type.cpp b/source/glest_game/types/skill_type.cpp index a7ef1a5f0..ff6a134f2 100644 --- a/source/glest_game/types/skill_type.cpp +++ b/source/glest_game/types/skill_type.cpp @@ -536,14 +536,20 @@ Model *SkillType::getAnimation(float animProgress, const Unit *unit, if(foundSpecificAnimation == false) { //int modelIndex = random.randRange(0,animations.size()-1); - srand(time(NULL) + unit->getId()); + //srand(time(NULL) + unit->getId()); + Chrono seed(true); + srand(seed.getCurTicks() + unit->getId()); + modelIndex = rand() % animations.size(); //const AnimationAttributes &attributes = animationAttributes[modelIndex]; //printf("SELECTING RANDOM Model index = %d [%s] model attributes [%d to %d] for unit [%s - %d] with HP = %d\n",modelIndex,animations[modelIndex]->getFileName().c_str(),attributes.fromHp,attributes.toHp,unit->getType()->getName().c_str(),unit->getId(),unit->getHp()); } else { - srand(time(NULL) + unit->getId()); + //srand(time(NULL) + unit->getId()); + Chrono seed(true); + srand(seed.getCurTicks() + unit->getId()); + int filteredModelIndex = rand() % filteredAnimations.size(); modelIndex = filteredAnimations[filteredModelIndex]; } diff --git a/source/shared_lib/sources/util/properties.cpp b/source/shared_lib/sources/util/properties.cpp index 2ed2b490a..26cf4e75c 100644 --- a/source/shared_lib/sources/util/properties.cpp +++ b/source/shared_lib/sources/util/properties.cpp @@ -425,6 +425,10 @@ const string Properties::getRandomKey(const bool realrandom) const{ int max=getPropertyCount(); int randomIndex=-1; if(realrandom == true){ + //srand((unsigned int)time(NULL)); + Chrono seed(true); + srand(seed.getCurTicks()); + randomIndex=rand()%max; } else{