From b8a05be21eabe80cb0bb09104feeadb798fe6047 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 1 Jun 2010 00:32:24 +0000 Subject: [PATCH] - attempt to use streflop for random number generation to fix AI synch issues on corss platform --- .../menu/menu_state_custom_game.cpp | 40 ++++++++++++++----- .../glest_game/menu/menu_state_custom_game.h | 1 + source/shared_lib/sources/util/randomgen.cpp | 35 +++++++++++++++- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index b3f4e7c0a..52c93fef4 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -73,8 +73,10 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b vector teamItems, controlItems, results; //create - buttonReturn.init(350, 180, 125); - buttonPlayNow.init(525, 180, 125); + buttonReturn.init(300, 180, 125); + buttonRestoreLastSettings.init(440, 180, 125); + buttonPlayNow.init(580, 180, 125); + //map listBox // put them all in a set, to weed out duplicates (gbm & mgm with same name) @@ -162,6 +164,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b //texts buttonReturn.setText(lang.get("Return")); buttonPlayNow.setText(lang.get("PlayNow")); + buttonRestoreLastSettings.setText(lang.get("ReloadLastGameSettings")); controlItems.push_back(lang.get("Closed")); controlItems.push_back(lang.get("CpuEasy")); @@ -238,13 +241,8 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); // Ensure we have set the gamesettings at least once - GameSettings gameSettings = loadGameSettingsFromFile("lastCustomGamSettings.mgg"); - if(gameSettings.getMap() == "") { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - - loadGameSettings(&gameSettings); - } - + GameSettings gameSettings; + loadGameSettings(&gameSettings); ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); serverInterface->setGameSettings(&gameSettings,false); @@ -305,7 +303,6 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ mainMessageBox.setEnabled(false); } } - saveGameSettingsToFile("lastCustomGamSettings.mgg"); } else if(buttonReturn.mouseClick(x,y)){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -322,7 +319,6 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ } */ - saveGameSettingsToFile("lastCustomGamSettings.mgg"); returnToParentMenu(); } else if(buttonPlayNow.mouseClick(x,y) && buttonPlayNow.getEnabled()) { @@ -374,6 +370,26 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); } + else if(buttonRestoreLastSettings.mouseClick(x,y) && buttonRestoreLastSettings.getEnabled()) { + // Ensure we have set the gamesettings at least once + GameSettings gameSettings = loadGameSettingsFromFile("lastCustomGamSettings.mgg"); + if(gameSettings.getMap() == "") { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + + loadGameSettings(&gameSettings); + } + + ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); + serverInterface->setGameSettings(&gameSettings,false); + + needToRepublishToMasterserver = true; + + if(hasNetworkGameSettings() == true) + { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if(listBoxMap.mouseClick(x, y)){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n", mapFiles[listBoxMap.getSelectedItemIndex()].c_str()); @@ -511,6 +527,7 @@ void MenuStateCustomGame::mouseMove(int x, int y, const MouseState *ms){ } buttonReturn.mouseMove(x, y); buttonPlayNow.mouseMove(x, y); + buttonRestoreLastSettings.mouseMove(x, y); for(int i=0; i - +#include "util.h" #include "leak_dumper.h" namespace Shared { namespace Util { @@ -15,30 +15,61 @@ const int RandomGen::b= 150889; RandomGen::RandomGen(){ lastNumber= 0; + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber); } void RandomGen::init(int seed){ + +#ifdef STREFLOP_H + lastNumber = math::RandomInit(seed); // streflop +#else lastNumber= seed % m; +#endif + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] seed = %d, lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,seed,lastNumber); } -int RandomGen::rand(){ +int RandomGen::rand() { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber); + lastNumber= (a*lastNumber + b) % m; + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] lastNumber = %d\n",__FILE__,__FUNCTION__,__LINE__,lastNumber); + return lastNumber; } int RandomGen::randRange(int min, int max){ assert(min<=max); + +#ifdef STREFLOP_H + int res = math::Random(min, max); // streflop +#else int diff= max-min; int res= min + static_cast(static_cast(diff+1)*RandomGen::rand() / m); +#endif assert(res>=min && res<=max); + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] min = %d, max = %d, res = %d\n",__FILE__,__FUNCTION__,__LINE__,min,max,res); + return res; } float RandomGen::randRange(float min, float max){ assert(min<=max); + +#ifdef STREFLOP_H + float res = math::Random(min, max); // streflop +#else float rand01= static_cast(RandomGen::rand())/(m-1); float res= min+(max-min)*rand01; +#endif + assert(res>=min && res<=max); + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] min = %f, max = %f, res = %f\n",__FILE__,__FUNCTION__,__LINE__,min,max,res); + return res; }