// ============================================================== // 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" 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(); vector results, teamItems, controlItems; //create buttonReturn.init(350, 200, 125); buttonPlayNow.init(525, 200, 125); //map listBox findAll("maps/*.gbm", results, true); if(results.size()==0){ throw runtime_error("There is no maps"); } mapFiles= results; for(int i= 0; isetState(new MenuStateNewGame(program, mainMenu)); } else if(buttonPlayNow.mouseClick(x,y)){ GameSettings gameSettings; closeUnusedSlots(); soundRenderer.playFx(coreData.getClickSoundC()); loadGameSettings(&gameSettings); serverInterface->launchGame(&gameSettings); program->setState(new Game(program, &gameSettings)); } else if(listBoxMap.mouseClick(x, y)){ loadMapInfo(Map::getMapPath(mapFiles[listBoxMap.getSelectedItemIndex()]), &mapInfo); labelMapInfo.setText(mapInfo.desc); updateControlers(); } else if(listBoxTileset.mouseClick(x, y)){ } else if(listBoxTechTree.mouseClick(x, y)){ reloadFactions(); } 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(); } else if(listBoxFactions[i].mouseClick(x, y)){ } else if(listBoxTeams[i].mouseClick(x, y)){ } } } } 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); if(connectionSlot->isConnected()){ labelNetStatus[i].setText(connectionSlot->getName()); } else { labelNetStatus[i].setText(lang.get("NotConnected")); } } else{ labelNetStatus[i].setText(""); } } } void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings){ 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); } // ============ PRIVATE =========================== 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; 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; findAll("techs/"+techTreeFiles[listBoxTechTree.getSelectedItemIndex()]+"/factions/*.", results); 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); } } } }}//end namespace