// ============================================================== // This file is part of Glest (www.glest.org) // // Copyright (C) 2001-2005 Marti�o Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published // by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version // ============================================================== #include "menu_state_custom_game.h" #include "renderer.h" #include "sound_renderer.h" #include "core_data.h" #include "config.h" #include "menu_state_new_game.h" #include "metrics.h" #include "network_manager.h" #include "network_message.h" #include "client_interface.h" #include "conversion.h" #include "socket.h" #include "game.h" #include "leak_dumper.h" #include namespace Glest{ namespace Game{ using namespace Shared::Util; // ===================================================== // class MenuStateCustomGame // ===================================================== MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, bool openNetworkSlots): MenuState(program, mainMenu, "new-game") { Lang &lang= Lang::getInstance(); NetworkManager &networkManager= NetworkManager::getInstance(); needToSetChangedGameSettings = false; lastSetChangedGameSettings = time(NULL);; vector glestMaps, megaMaps, teamItems, controlItems; //create buttonReturn.init(350, 180, 125); buttonPlayNow.init(525, 180, 125); //map listBox Config &config = Config::getInstance(); findAll(config.getPathListForType(ptMaps), "*.gbm", glestMaps, true, false); findAll(config.getPathListForType(ptMaps), "*.mgm", megaMaps, true, false); mapFiles.resize(glestMaps.size() + megaMaps.size()); if (!glestMaps.empty()) { copy(glestMaps.begin(), glestMaps.end(), mapFiles.begin()); } if (!megaMaps.empty()) { copy(megaMaps.begin(), megaMaps.end(), mapFiles.begin() + glestMaps.size()); } if(mapFiles.size()==0){ throw runtime_error("There are no maps"); } vector results; for(int i= 0; i < mapFiles.size(); ++i){ results.push_back(formatString(mapFiles[i])); } listBoxMap.init(200, 260, 150); listBoxMap.setItems(results); labelMap.init(200, 290); labelMapInfo.init(200, 230, 200, 40); //tileset listBox findDirs(config.getPathListForType(ptTilesets), results); if(results.size() == 0) { throw runtime_error("There is no tile set"); } tilesetFiles= results; for(int i= 0; i techPaths = config.getPathListForType(ptTechs); for(int idx = 0; idx < techPaths.size(); idx++) { string &techPath = techPaths[idx]; findAll(techPath + "/" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "/factions/*.", results, false, false); if(results.size() > 0) { break; } } if(results.size() == 0) { throw runtime_error("There is no factions for this tech tree"); } for(int i=0; isetState(new MenuStateNewGame(program, mainMenu)); } else if(buttonPlayNow.mouseClick(x,y) && buttonPlayNow.getEnabled()) { closeUnusedSlots(); soundRenderer.playFx(coreData.getClickSoundC()); GameSettings gameSettings; loadGameSettings(&gameSettings); // Send the game settings to each client if we have at least one networked client if( hasNetworkGameSettings() == true && needToSetChangedGameSettings == true) { serverInterface->setGameSettings(&gameSettings,true); needToSetChangedGameSettings = false; lastSetChangedGameSettings = time(NULL); } bool bOkToStart = serverInterface->launchGame(&gameSettings); if(bOkToStart == true) { program->setState(new Game(program, &gameSettings)); } } else if(listBoxMap.mouseClick(x, y)){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s\n", mapFiles[listBoxMap.getSelectedItemIndex()].c_str()); loadMapInfo(Map::getMapPath(mapFiles[listBoxMap.getSelectedItemIndex()]), &mapInfo); labelMapInfo.setText(mapInfo.desc); updateControlers(); if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } else if(listBoxTileset.mouseClick(x, y)){ if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } else if(listBoxTechTree.mouseClick(x, y)){ reloadFactions(); if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } else { for(int i=0; i(listBoxControls[j].getSelectedItemIndex()); if(ct==ctHuman){ if(humanIndex1==-1){ humanIndex1= j; } else{ humanIndex2= j; } } } //no human if(humanIndex1==-1 && humanIndex2==-1){ listBoxControls[i].setSelectedItemIndex(ctHuman); } //2 humans if(humanIndex1!=-1 && humanIndex2!=-1){ listBoxControls[humanIndex1==i? humanIndex2: humanIndex1].setSelectedItemIndex(ctClosed); } updateNetworkSlots(); if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } else if(listBoxFactions[i].mouseClick(x, y)){ if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } else if(listBoxTeams[i].mouseClick(x, y)) { if(hasNetworkGameSettings() == true) { needToSetChangedGameSettings = true; lastSetChangedGameSettings = time(NULL);; } } } } } void MenuStateCustomGame::mouseMove(int x, int y, const MouseState *ms){ buttonReturn.mouseMove(x, y); buttonPlayNow.mouseMove(x, y); for(int i=0; igetSlot(i); assert(connectionSlot!=NULL); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] A - ctNetwork\n",__FILE__,__FUNCTION__); if(connectionSlot->isConnected()) { haveAtLeastOneNetworkClientConnected = true; //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] B - ctNetwork\n",__FILE__,__FUNCTION__); string label = connectionSlot->getName(); if(connectionSlot->getAllowDownloadDataSynch() == true && connectionSlot->getAllowGameDataSynchCheck() == true) { if(connectionSlot->getNetworkGameDataSynchCheckOk() == false) { label = connectionSlot->getName() + " - waiting to synch:"; if(connectionSlot->getNetworkGameDataSynchCheckOkMap() == false) { label = label + " map"; } if(connectionSlot->getNetworkGameDataSynchCheckOkTile() == false) { label = label + " tile"; } if(connectionSlot->getNetworkGameDataSynchCheckOkTech() == false) { label = label + " techtree"; } if(connectionSlot->getNetworkGameDataSynchCheckOkFogOfWar() == false) { label = label + " FogOfWar == false"; } } else { label = connectionSlot->getName() + " - data synch is ok"; } } else { label = connectionSlot->getName(); if(connectionSlot->getAllowGameDataSynchCheck() == true) { label += " - warning synch mismatch for:"; if(connectionSlot->getNetworkGameDataSynchCheckOkMap() == false) { label = label + " map"; } if(connectionSlot->getNetworkGameDataSynchCheckOkTile() == false) { label = label + " tile"; } if(connectionSlot->getNetworkGameDataSynchCheckOkTech() == false) { label = label + " techtree"; } if(connectionSlot->getNetworkGameDataSynchCheckOkFogOfWar() == false) { label = label + " FogOfWar == false"; } } } labelNetStatus[i].setText(label); } else { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] C - ctNetwork\n",__FILE__,__FUNCTION__); labelNetStatus[i].setText(lang.get("NotConnected")); } //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] END - ctNetwork\n",__FILE__,__FUNCTION__); } else{ labelNetStatus[i].setText(""); } } // Send the game settings to each client if we have at least one networked client if( serverInterface->getAllowDownloadDataSynch() == true && haveAtLeastOneNetworkClientConnected == true && needToSetChangedGameSettings == true && difftime(time(NULL),lastSetChangedGameSettings) >= 2) { GameSettings gameSettings; loadGameSettings(&gameSettings); serverInterface->setGameSettings(&gameSettings); needToSetChangedGameSettings = false; lastSetChangedGameSettings = time(NULL); } //call the chat manager chatManager.updateNetwork(); //console console.update(); } void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings) { //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] START\n",__FILE__,__FUNCTION__); int factionCount= 0; gameSettings->setDescription(formatString(mapFiles[listBoxMap.getSelectedItemIndex()])); gameSettings->setMap(mapFiles[listBoxMap.getSelectedItemIndex()]); gameSettings->setTileset(tilesetFiles[listBoxTileset.getSelectedItemIndex()]); gameSettings->setTech(techTreeFiles[listBoxTechTree.getSelectedItemIndex()]); gameSettings->setDefaultUnits(true); gameSettings->setDefaultResources(true); gameSettings->setDefaultVictoryConditions(true); for(int i=0; i(listBoxControls[i].getSelectedItemIndex()); if(ct != ctClosed) { if(ct == ctHuman) { gameSettings->setThisFactionIndex(factionCount); } gameSettings->setFactionControl(factionCount, ct); gameSettings->setTeam(factionCount, listBoxTeams[i].getSelectedItemIndex()); gameSettings->setStartLocationIndex(factionCount, i); gameSettings->setFactionTypeName(factionCount, factionFiles[listBoxFactions[i].getSelectedItemIndex()]); factionCount++; } } gameSettings->setFactionCount(factionCount); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getTileset() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getTileset().c_str()); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getTech() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getTech().c_str()); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getMap() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getMap().c_str()); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] END\n",__FILE__,__FUNCTION__); } // ============ PRIVATE =========================== bool MenuStateCustomGame::hasNetworkGameSettings() { bool hasNetworkSlot = false; for(int i=0; i(listBoxControls[i].getSelectedItemIndex()); if(ct != ctClosed) { if(ct == ctNetwork) { hasNetworkSlot = true; break; } } } return hasNetworkSlot; } void MenuStateCustomGame::loadMapInfo(string file, MapInfo *mapInfo){ struct MapFileHeader{ int32 version; int32 maxPlayers; int32 width; int32 height; int32 altFactor; int32 waterLevel; int8 title[128]; }; Lang &lang= Lang::getInstance(); try{ FILE *f= fopen(file.c_str(), "rb"); if(f==NULL) throw runtime_error("Can't open file"); MapFileHeader header; size_t readBytes = fread(&header, sizeof(MapFileHeader), 1, f); mapInfo->size.x= header.width; mapInfo->size.y= header.height; mapInfo->players= header.maxPlayers; mapInfo->desc= lang.get("MaxPlayers")+": "+intToStr(mapInfo->players)+"\n"; mapInfo->desc+=lang.get("Size")+": "+intToStr(mapInfo->size.x) + " x " + intToStr(mapInfo->size.y); fclose(f); } catch(exception e){ throw runtime_error("Error loading map file: "+file+'\n'+e.what()); } } void MenuStateCustomGame::reloadFactions(){ vector results; Config &config = Config::getInstance(); vector techPaths = config.getPathListForType(ptTechs); for(int idx = 0; idx < techPaths.size(); idx++) { string &techPath = techPaths[idx]; findAll(techPath + "/" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "/factions/*.", results, false, false); if(results.size() > 0) { break; } } if(results.size() == 0) { throw runtime_error("There is no factions for this tech tree"); } factionFiles= results; for(int i= 0; igetSlot(i)->isConnected()){ listBoxControls[i].setSelectedItemIndex(ctClosed); } } } updateNetworkSlots(); } void MenuStateCustomGame::updateNetworkSlots() { ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface(); for(int i= 0; igetSlot(i) == NULL && listBoxControls[i].getSelectedItemIndex() == ctNetwork) { serverInterface->addSlot(i); } if(serverInterface->getSlot(i) != NULL && listBoxControls[i].getSelectedItemIndex() != ctNetwork) { serverInterface->removeSlot(i); } } } void MenuStateCustomGame::keyDown(char key) { //send key to the chat manager chatManager.keyDown(key); } void MenuStateCustomGame::keyPress(char c) { chatManager.keyPress(c); } }}//end namespace