diff --git a/source/glest_game/facilities/components.h b/source/glest_game/facilities/components.h index dfe9f8e4..4a819f04 100644 --- a/source/glest_game/facilities/components.h +++ b/source/glest_game/facilities/components.h @@ -155,6 +155,7 @@ public: void init(int x, int y, int w=defW, int h=defH); int getItemCount() const {return items.size();} + string getItem(int index) const {return items[index];} int getSelectedItemIndex() const {return selectedItemIndex;} string getSelectedItem() const {return items[selectedItemIndex];} const GraphicButton *getButton1() const {return &graphButton1;} diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index 5d830e52..b8719ae6 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -80,6 +80,7 @@ public: static const char *folder_path_screenshots; static const char *OBSERVER_SLOTNAME; + static const char *RANDOMFACTION_SLOTNAME; }; enum PathType { diff --git a/source/glest_game/global/config.cpp b/source/glest_game/global/config.cpp index a3954238..6d86dc09 100644 --- a/source/glest_game/global/config.cpp +++ b/source/glest_game/global/config.cpp @@ -43,6 +43,7 @@ const char *GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME = "???"; const char *GameConstants::folder_path_screenshots = "screens/"; const char *GameConstants::OBSERVER_SLOTNAME = "*Observer*"; +const char *GameConstants::RANDOMFACTION_SLOTNAME = "*Random*"; // ===================================================== // class Config diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 4fcf9e0f..4df20807 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -1034,6 +1034,7 @@ bool MenuStateConnectedGame::loadFactions(const GameSettings *gameSettings, bool if(gameSettings->getAllowObservers() == true) { results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); } + results.push_back(formatString(GameConstants::RANDOMFACTION_SLOTNAME)); factionFiles= results; for(int i= 0; i randomFactionSelectionList; + int RandomCount = 0; + for(int i= 0; i < mapInfo.players; ++i) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + + // Check for random faction selection and choose the faction now + if(listBoxFactions[i].getSelectedItem() == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d\n",__FILE__,__FUNCTION__,__LINE__,i); + + // Max 1000 tries to get a random, unused faction + for(int findRandomFaction = 1; findRandomFaction < 1000; ++findRandomFaction) { + srand(time(NULL) + findRandomFaction); + int selectedFactionIndex = rand() % listBoxFactions[i].getItemCount(); + string selectedFactionName = listBoxFactions[i].getItem(selectedFactionIndex); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] selectedFactionName [%s] selectedFactionIndex = %d, findRandomFaction = %d\n",__FILE__,__FUNCTION__,__LINE__,selectedFactionName.c_str(),selectedFactionIndex,findRandomFaction); + + if( selectedFactionName != formatString(GameConstants::RANDOMFACTION_SLOTNAME) && + selectedFactionName != formatString(GameConstants::OBSERVER_SLOTNAME) && + std::find(randomFactionSelectionList.begin(),randomFactionSelectionList.end(),selectedFactionName) == randomFactionSelectionList.end()) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + listBoxFactions[i].setSelectedItem(selectedFactionName); + randomFactionSelectionList.push_back(selectedFactionName); + break; + } + } + + if(listBoxFactions[i].getSelectedItem() == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] RandomCount = %d\n",__FILE__,__FUNCTION__,__LINE__,RandomCount); + + listBoxFactions[i].setSelectedItemIndex(RandomCount); + randomFactionSelectionList.push_back(listBoxFactions[i].getItem(RandomCount)); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, listBoxFactions[i].getSelectedItem() [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,listBoxFactions[i].getSelectedItem().c_str()); + + RandomCount++; + } + } + + if(RandomCount > 0) { + needToSetChangedGameSettings = true; + } + safeMutex.ReleaseLock(true); GameSettings gameSettings; loadGameSettings(&gameSettings); @@ -599,7 +644,7 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ safeMutex.Lock(); bool dataSynchCheckOk = true; - for(int i= 0; i 0)) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); serverInterface->setGameSettings(&gameSettings,true); @@ -1669,6 +1714,9 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { labelPlayerNames[i].setText(getHumanPlayerName(i)); } + + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,factionFiles[listBoxFactions[i].getSelectedItemIndex()].c_str()); gameSettings->setFactionTypeName(slotIndex, factionFiles[listBoxFactions[i].getSelectedItemIndex()]); if(factionFiles[listBoxFactions[i].getSelectedItemIndex()] == formatString(GameConstants::OBSERVER_SLOTNAME)) { listBoxTeams[i].setSelectedItem(intToStr(GameConstants::maxPlayers + fpt_Observer)); @@ -1721,6 +1769,8 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { gameSettings->setFactionControl(slotIndex, ct); gameSettings->setTeam(slotIndex, listBoxTeams[i].getSelectedItemIndex()); gameSettings->setStartLocationIndex(slotIndex, i); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,factionFiles[listBoxFactions[i].getSelectedItemIndex()].c_str()); gameSettings->setFactionTypeName(slotIndex, factionFiles[listBoxFactions[i].getSelectedItemIndex()]); gameSettings->setNetworkPlayerName(slotIndex, "Closed"); @@ -1842,6 +1892,9 @@ GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string fileName) gameSettings.setTeam(i,properties.getInt(string("FactionTeamForIndex") + intToStr(i),"0") ); gameSettings.setStartLocationIndex(i,properties.getInt(string("FactionStartLocationForIndex") + intToStr(i),intToStr(i).c_str()) ); gameSettings.setFactionTypeName(i,properties.getString(string("FactionTypeNameForIndex") + intToStr(i),"?") ); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, factionTypeName [%s]\n",__FILE__,__FUNCTION__,__LINE__,i,gameSettings.getFactionTypeName(i).c_str()); + gameSettings.setNetworkPlayerName(i,properties.getString(string("FactionPlayerNameForIndex") + intToStr(i),"") ); } @@ -2041,6 +2094,8 @@ void MenuStateCustomGame::reloadFactions() { results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); } + results.push_back(formatString(GameConstants::RANDOMFACTION_SLOTNAME)); + factionFiles= results; for(int i= 0; i