From 2d42f59fe1a638a49126e3a907adc2ee213b321a Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 6 Oct 2011 18:04:59 +0000 Subject: [PATCH] - added a new lua function to allow loading a new scenario from inside an existing scenario loadScenario('capture_the_flag2') --- source/glest_game/game/game.cpp | 80 ++++- source/glest_game/game/game.h | 5 +- source/glest_game/game/script_manager.cpp | 69 +++- source/glest_game/game/script_manager.h | 9 +- source/glest_game/graphics/renderer.cpp | 24 ++ source/glest_game/graphics/renderer.h | 1 + source/glest_game/main/main.cpp | 5 +- source/glest_game/menu/main_menu.h | 22 -- .../glest_game/menu/menu_state_scenario.cpp | 322 +++++++++--------- source/glest_game/menu/menu_state_scenario.h | 9 - source/glest_game/world/scenario.cpp | 213 ++++++++++++ source/glest_game/world/scenario.h | 42 +++ source/glest_game/world/world.cpp | 42 ++- source/glest_game/world/world.h | 12 +- .../include/platform/sdl/platform_main.h | 6 + 15 files changed, 647 insertions(+), 214 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 5a6ffbfe4..3bac9c3c3 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -577,7 +577,7 @@ void Game::load() { load(lgt_All); } -void Game::load(LoadGameItem loadTypes) { +void Game::load(int loadTypes) { std::map > > loadedFileList; originalDisplayMsgCallback = NetworkInterface::getDisplayMessageFunction(); NetworkInterface::setDisplayMessageFunction(ErrorDisplayMessage); @@ -704,8 +704,7 @@ void Game::init() { init(false); } -void Game::init(bool initForPreviewOnly) -{ +void Game::init(bool initForPreviewOnly) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] initForPreviewOnly = %d\n",__FILE__,__FUNCTION__,__LINE__,initForPreviewOnly); Lang &lang= Lang::getInstance(); @@ -1125,6 +1124,81 @@ void Game::update() { if(Config::getInstance().getBool("AutoTest")){ AutoTest::getInstance().updateGame(this); } + + if(world.getQueuedScenario() != "") { + string name = world.getQueuedScenario(); + world.setQueuedScenario(""); + + vector results; + const vector &dirList = Config::getInstance().getPathListForType(ptScenarios); + string scenarioFile = Scenario::getScenarioPath(dirList, name); + + //printf("\nname [%s] scenarioFile [%s] results.size() = %lu\n",name.c_str(),scenarioFile.c_str(),results.size()); + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + ScenarioInfo scenarioInfo; + Scenario::loadScenarioInfo(scenarioFile, &scenarioInfo); + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + GameSettings gameSettings; + Scenario::loadGameSettings(dirList,&scenarioInfo, &gameSettings, scenarioFile); + + //Program *program = world->getGame()->getProgram(); + //program->setState(new Game(program, &gameSettings, false)); + + //world->end(); + this->setGameSettings(&gameSettings); + //world->getMapPtr()->end(); + //world.end(); + world.endScenario(); + Renderer &renderer= Renderer::getInstance(); + renderer.endScenario(); + world.clearTileset(); + //this->load(lgt_FactionPreview | lgt_TileSet | lgt_TechTree | lgt_Map | lgt_Scenario); + this->load(lgt_FactionPreview | lgt_TileSet | lgt_Map | lgt_Scenario); + //this->init(); + world.init(this, gameSettings.getDefaultUnits(),false); + Map *map= world.getMap(); + gameCamera.init(map->getW(), map->getH()); + + // camera default height calculation + if(map->getCameraHeight()>0 && gameCamera.getCalculatedDefault()getCameraHeight()){ + gameCamera.setCalculatedDefault(map->getCameraHeight()); + } + else if(gameCamera.getCalculatedDefault()getMaxMapHeight()+13.0f){ + gameCamera.setCalculatedDefault(map->getMaxMapHeight()+13.0f); + } + + renderer.initGame(this); + + //sounds + SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(); + soundRenderer= SoundRenderer::getInstance(); + + Tileset *tileset= world.getTileset(); + AmbientSounds *ambientSounds= tileset->getAmbientSounds(); + + //rain + if(tileset->getWeather() == wRainy && ambientSounds->isEnabledRain()) { + //logger.add("Starting ambient stream", true); + soundRenderer.playAmbient(ambientSounds->getRain()); + } + + //snow + if(tileset->getWeather() == wSnowy && ambientSounds->isEnabledSnow()) { + //logger.add("Starting ambient stream", true); + soundRenderer.playAmbient(ambientSounds->getSnow()); + } + + if(this->masterserverMode == false) { + StrSound *gameMusic= world.getThisFaction()->getType()->getMusic(); + soundRenderer.playMusic(gameMusic); + } + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + //Checksum checksum; + //world->loadScenario(scenarioFile, &checksum, true); + } } catch(const exception &ex) { char szBuf[4096]=""; diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index d112d0372..5f5971a1e 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -148,7 +148,8 @@ public: ~Game(); //get - GameSettings *getGameSettings() {return &gameSettings;} + GameSettings *getGameSettings() {return &gameSettings;} + void setGameSettings(GameSettings *settings) { gameSettings = *settings;} const GameSettings *getReadOnlyGameSettings() const {return &gameSettings;} const GameCamera *getGameCamera() const {return &gameCamera;} @@ -168,7 +169,7 @@ public: void toggleTeamColorMarker(); //init - virtual void load(LoadGameItem loadTypes); + virtual void load(int loadTypes); virtual void load(); virtual void init(); virtual void init(bool initForPreviewOnly); diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index 5dcd3fccd..e721c7b21 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -15,6 +15,7 @@ #include "lang.h" #include "game_camera.h" #include "game.h" +#include "config.h" #include "leak_dumper.h" @@ -60,6 +61,14 @@ ScriptManager* ScriptManager::thisScriptManager= NULL; const int ScriptManager::messageWrapCount= 30; const int ScriptManager::displayTextWrapCount= 64; +ScriptManager::ScriptManager() { + +} + +ScriptManager::~ScriptManager() { + +} + void ScriptManager::init(World* world, GameCamera *gameCamera){ if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -136,6 +145,8 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){ luaScript.registerFunction(getGameWon, "gameWon"); + luaScript.registerFunction(loadScenario, "loadScenario"); + //load code for(int i= 0; igetScriptCount(); ++i){ const Script* script= scenario->getScript(i); @@ -250,8 +261,10 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { inCellTriggerEvent = true; if(movingUnit != NULL) { + ScenarioInfo scenarioInfoStart = world->getScenario()->getInfo(); + for(std::map::iterator iterMap = CellTriggerEventList.begin(); - iterMap != CellTriggerEventList.end(); ++iterMap) { + iterMap != CellTriggerEventList.end(); ++iterMap) { CellTriggerEvent &event = iterMap->second; if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s\n", @@ -344,6 +357,11 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) { luaScript.beginCall("cellTriggerEvent"); luaScript.endCall(); } + + ScenarioInfo scenarioInfoEnd = world->getScenario()->getInfo(); + if(scenarioInfoStart.file != scenarioInfoEnd.file) { + break; + } } } @@ -840,6 +858,48 @@ int ScriptManager::getUnitCountOfType(int factionIndex, const string &typeName) return world->getUnitCountOfType(factionIndex, typeName); } +void ScriptManager::loadScenario(const string &name) { + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + ScriptManager_STREFLOP_Wrapper streflopWrapper; + + world->setQueuedScenario(name); +/* + vector results; + const vector &dirList = Config::getInstance().getPathListForType(ptScenarios); + //findDirs(dirList, results); + string scenarioFile = Scenario::getScenarioPath(dirList, name); + + //printf("\nname [%s] scenarioFile [%s] results.size() = %lu\n",name.c_str(),scenarioFile.c_str(),results.size()); + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + ScenarioInfo scenarioInfo; + Scenario::loadScenarioInfo(scenarioFile, &scenarioInfo); + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + GameSettings gameSettings; + Scenario::loadGameSettings(dirList,&scenarioInfo, &gameSettings, scenarioFile); + + //Program *program = world->getGame()->getProgram(); + //program->setState(new Game(program, &gameSettings, false)); + + //world->end(); + world->getGame()->setGameSettings(&gameSettings); + //world->getMapPtr()->end(); + world->end(); + world->clearTileset(); + world->getGame()->load(); + world->getGame()->init(); + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + //Checksum checksum; + //world->loadScenario(scenarioFile, &checksum, true); + + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + */ +} + // ========================== lua callbacks =============================================== int ScriptManager::showMessage(LuaHandle* luaHandle){ @@ -1321,5 +1381,12 @@ int ScriptManager::getGameWon(LuaHandle* luaHandle){ return luaArguments.getReturnCount(); } +int ScriptManager::loadScenario(LuaHandle* luaHandle) { + //printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + + LuaArguments luaArguments(luaHandle); + thisScriptManager->loadScenario(luaArguments.getString(-1)); + return luaArguments.getReturnCount(); +} }}//end namespace diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index ca8a8f243..4c3185372 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -102,7 +102,7 @@ public: }; -class ScriptManager{ +class ScriptManager { private: typedef queue MessageQueue; @@ -150,6 +150,9 @@ private: static const int displayTextWrapCount; public: + + ScriptManager(); + ~ScriptManager(); void init(World* world, GameCamera *gameCamera); //message box functions @@ -239,6 +242,8 @@ private: bool getGameWon(); + void loadScenario(const string &name); + //callbacks, commands static int showMessage(LuaHandle* luaHandle); static int setDisplayText(LuaHandle* luaHandle); @@ -307,6 +312,8 @@ private: static int getUnitCountOfType(LuaHandle* luaHandle); static int getGameWon(LuaHandle* luaHandle); + + static int loadScenario(LuaHandle* luaHandle); }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index a1d9859bc..8fa5e1246 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -518,6 +518,30 @@ void Renderer::end() { Renderer::rendererEnded = true; } +void Renderer::endScenario() { + game= NULL; + + if(this->masterserverMode == true) { + return; + } + + //delete resources + //modelManager[rsGame]->end(); + //textureManager[rsGame]->end(); + //fontManager[rsGame]->end(); + //particleManager[rsGame]->end(); + + if(shadows == sProjected || shadows == sShadowMapping) { + glDeleteTextures(1, &shadowMapHandle); + } + + glDeleteLists(list3d, 1); + + //worldToScreenPosCache.clear(); + ReleaseSurfaceVBOs(); + mapSurfaceData.clear(); +} + void Renderer::endGame() { game= NULL; diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 79bdad33d..3a7bdacb6 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -355,6 +355,7 @@ public: //end void end(); + void endScenario(); void endMenu(); void endGame(); diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index aaa038b7b..1d76e61d8 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -3481,7 +3481,10 @@ __try { //printf("\n\nargv0 [%s] application_binary [%s]\n\n",argv[0],application_binary.c_str()); #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) - signal(SIGSEGV, handleSIGSEGV); + + if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_DISABLE_SIGSEGV_HANDLER])) == false) { + signal(SIGSEGV, handleSIGSEGV); + } // http://developerweb.net/viewtopic.php?id=3013 //signal(SIGPIPE, SIG_IGN); diff --git a/source/glest_game/menu/main_menu.h b/source/glest_game/menu/main_menu.h index fa69ff925..632713fe9 100644 --- a/source/glest_game/menu/main_menu.h +++ b/source/glest_game/menu/main_menu.h @@ -39,28 +39,6 @@ public: } }; -struct ScenarioInfo -{ - int difficulty; - ControlType factionControls[GameConstants::maxPlayers]; - int teams[GameConstants::maxPlayers]; - string factionTypeNames[GameConstants::maxPlayers]; - float resourceMultipliers[GameConstants::maxPlayers]; - - string mapName; - string tilesetName; - string techTreeName; - - bool defaultUnits; - bool defaultResources; - bool defaultVictoryConditions; - - string desc; - - bool fogOfWar; - bool fogOfWar_exploredFlag; -}; - class MenuState; // ===================================================== diff --git a/source/glest_game/menu/menu_state_scenario.cpp b/source/glest_game/menu/menu_state_scenario.cpp index 9856a10e2..8ede1b1d2 100644 --- a/source/glest_game/menu/menu_state_scenario.cpp +++ b/source/glest_game/menu/menu_state_scenario.cpp @@ -81,6 +81,8 @@ MenuStateScenario::MenuStateScenario(Program *program, MainMenu *mainMenu, const //scenario listbox findDirs(dirList, results); scenarioFiles = results; + //printf("scenarioFiles[0] [%s]\n",scenarioFiles[0].c_str()); + if(results.empty() == true) { throw runtime_error("There are no scenarios found to load"); } @@ -243,111 +245,114 @@ void MenuStateScenario::setScenario(int i) { } void MenuStateScenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) { - Lang &lang= Lang::getInstance(); - - XmlTree xmlTree; - xmlTree.load(file,Properties::getTagReplacementValues()); - - const XmlNode *scenarioNode= xmlTree.getRootNode(); - const XmlNode *difficultyNode= scenarioNode->getChild("difficulty"); - scenarioInfo->difficulty = difficultyNode->getAttribute("value")->getIntValue(); - if( scenarioInfo->difficulty < dVeryEasy || scenarioInfo->difficulty > dInsane ) { - char szBuf[4096]=""; - sprintf(szBuf,"Invalid difficulty value specified in scenario: %d must be between %d and %d",scenarioInfo->difficulty,dVeryEasy,dInsane); - throw std::runtime_error(szBuf); - } - - const XmlNode *playersNode= scenarioNode->getChild("players"); - - for(int i= 0; i < GameConstants::maxPlayers; ++i) { - XmlNode* playerNode=NULL; - string factionTypeName=""; - ControlType factionControl; - - if(playersNode->hasChildAtIndex("player",i)){ - playerNode = playersNode->getChild("player", i); - factionControl = strToControllerType( playerNode->getAttribute("control")->getValue() ); - - if(playerNode->getAttribute("resource_multiplier",false)!=NULL) { - // if a multiplier exists use it - scenarioInfo->resourceMultipliers[i]=playerNode->getAttribute("resource_multiplier")->getFloatValue(); - } - else { - // if no multiplier exists use defaults - scenarioInfo->resourceMultipliers[i]=GameConstants::normalMultiplier; - if(factionControl==ctCpuEasy) { - scenarioInfo->resourceMultipliers[i]=GameConstants::easyMultiplier; - } - if(factionControl==ctCpuUltra) { - scenarioInfo->resourceMultipliers[i]=GameConstants::ultraMultiplier; - } - else if(factionControl==ctCpuMega) { - scenarioInfo->resourceMultipliers[i]=GameConstants::megaMultiplier; - } - } - - } - else { - factionControl=ctClosed; - } - - scenarioInfo->factionControls[i] = factionControl; - - if(factionControl != ctClosed){ - int teamIndex = playerNode->getAttribute("team")->getIntValue(); - - if( teamIndex < 1 || teamIndex > GameConstants::maxPlayers ) { - char szBuf[4096]=""; - sprintf(szBuf,"Invalid team value specified in scenario: %d must be between %d and %d",teamIndex,1,GameConstants::maxPlayers); - throw std::runtime_error(szBuf); - } - - scenarioInfo->teams[i]= playerNode->getAttribute("team")->getIntValue(); - scenarioInfo->factionTypeNames[i]= playerNode->getAttribute("faction")->getValue(); - } - - scenarioInfo->mapName = scenarioNode->getChild("map")->getAttribute("value")->getValue(); - scenarioInfo->tilesetName = scenarioNode->getChild("tileset")->getAttribute("value")->getValue(); - scenarioInfo->techTreeName = scenarioNode->getChild("tech-tree")->getAttribute("value")->getValue(); - scenarioInfo->defaultUnits = scenarioNode->getChild("default-units")->getAttribute("value")->getBoolValue(); - scenarioInfo->defaultResources = scenarioNode->getChild("default-resources")->getAttribute("value")->getBoolValue(); - scenarioInfo->defaultVictoryConditions = scenarioNode->getChild("default-victory-conditions")->getAttribute("value")->getBoolValue(); - } - - //add player info - scenarioInfo->desc= lang.get("Player") + ": "; - for(int i=0; ifactionControls[i] == ctHuman) { - scenarioInfo->desc+= formatString(scenarioInfo->factionTypeNames[i]); - break; - } - } - - //add misc info - string difficultyString = "Difficulty" + intToStr(scenarioInfo->difficulty); - - scenarioInfo->desc+= "\n"; - scenarioInfo->desc+= lang.get("Difficulty") + ": " + lang.get(difficultyString) +"\n"; - scenarioInfo->desc+= lang.get("Map") + ": " + formatString(scenarioInfo->mapName) + "\n"; - scenarioInfo->desc+= lang.get("Tileset") + ": " + formatString(scenarioInfo->tilesetName) + "\n"; - scenarioInfo->desc+= lang.get("TechTree") + ": " + formatString(scenarioInfo->techTreeName) + "\n"; - - if(scenarioNode->hasChild("fog-of-war") == true) { - if(scenarioNode->getChild("fog-of-war")->getAttribute("value")->getValue() == "explored") { - scenarioInfo->fogOfWar = true; - scenarioInfo->fogOfWar_exploredFlag = true; - } - else { - scenarioInfo->fogOfWar = scenarioNode->getChild("fog-of-war")->getAttribute("value")->getBoolValue(); - scenarioInfo->fogOfWar_exploredFlag = false; - } - //printf("\nFOG OF WAR is set to [%d]\n",scenarioInfo->fogOfWar); - } - else { - scenarioInfo->fogOfWar = true; - scenarioInfo->fogOfWar_exploredFlag = false; - } +// Lang &lang= Lang::getInstance(); +// +// XmlTree xmlTree; +// xmlTree.load(file,Properties::getTagReplacementValues()); +// +// const XmlNode *scenarioNode= xmlTree.getRootNode(); +// const XmlNode *difficultyNode= scenarioNode->getChild("difficulty"); +// scenarioInfo->difficulty = difficultyNode->getAttribute("value")->getIntValue(); +// if( scenarioInfo->difficulty < dVeryEasy || scenarioInfo->difficulty > dInsane ) { +// char szBuf[4096]=""; +// sprintf(szBuf,"Invalid difficulty value specified in scenario: %d must be between %d and %d",scenarioInfo->difficulty,dVeryEasy,dInsane); +// throw std::runtime_error(szBuf); +// } +// +// const XmlNode *playersNode= scenarioNode->getChild("players"); +// +// for(int i= 0; i < GameConstants::maxPlayers; ++i) { +// XmlNode* playerNode=NULL; +// string factionTypeName=""; +// ControlType factionControl; +// +// if(playersNode->hasChildAtIndex("player",i)){ +// playerNode = playersNode->getChild("player", i); +// factionControl = strToControllerType( playerNode->getAttribute("control")->getValue() ); +// +// if(playerNode->getAttribute("resource_multiplier",false)!=NULL) { +// // if a multiplier exists use it +// scenarioInfo->resourceMultipliers[i]=playerNode->getAttribute("resource_multiplier")->getFloatValue(); +// } +// else { +// // if no multiplier exists use defaults +// scenarioInfo->resourceMultipliers[i]=GameConstants::normalMultiplier; +// if(factionControl==ctCpuEasy) { +// scenarioInfo->resourceMultipliers[i]=GameConstants::easyMultiplier; +// } +// if(factionControl==ctCpuUltra) { +// scenarioInfo->resourceMultipliers[i]=GameConstants::ultraMultiplier; +// } +// else if(factionControl==ctCpuMega) { +// scenarioInfo->resourceMultipliers[i]=GameConstants::megaMultiplier; +// } +// } +// +// } +// else { +// factionControl=ctClosed; +// } +// +// scenarioInfo->factionControls[i] = factionControl; +// +// if(factionControl != ctClosed){ +// int teamIndex = playerNode->getAttribute("team")->getIntValue(); +// +// if( teamIndex < 1 || teamIndex > GameConstants::maxPlayers ) { +// char szBuf[4096]=""; +// sprintf(szBuf,"Invalid team value specified in scenario: %d must be between %d and %d",teamIndex,1,GameConstants::maxPlayers); +// throw std::runtime_error(szBuf); +// } +// +// scenarioInfo->teams[i]= playerNode->getAttribute("team")->getIntValue(); +// scenarioInfo->factionTypeNames[i]= playerNode->getAttribute("faction")->getValue(); +// } +// +// scenarioInfo->mapName = scenarioNode->getChild("map")->getAttribute("value")->getValue(); +// scenarioInfo->tilesetName = scenarioNode->getChild("tileset")->getAttribute("value")->getValue(); +// scenarioInfo->techTreeName = scenarioNode->getChild("tech-tree")->getAttribute("value")->getValue(); +// scenarioInfo->defaultUnits = scenarioNode->getChild("default-units")->getAttribute("value")->getBoolValue(); +// scenarioInfo->defaultResources = scenarioNode->getChild("default-resources")->getAttribute("value")->getBoolValue(); +// scenarioInfo->defaultVictoryConditions = scenarioNode->getChild("default-victory-conditions")->getAttribute("value")->getBoolValue(); +// } +// +// //add player info +// scenarioInfo->desc= lang.get("Player") + ": "; +// for(int i=0; ifactionControls[i] == ctHuman) { +// scenarioInfo->desc+= formatString(scenarioInfo->factionTypeNames[i]); +// break; +// } +// } +// +// //add misc info +// string difficultyString = "Difficulty" + intToStr(scenarioInfo->difficulty); +// +// scenarioInfo->desc+= "\n"; +// scenarioInfo->desc+= lang.get("Difficulty") + ": " + lang.get(difficultyString) +"\n"; +// scenarioInfo->desc+= lang.get("Map") + ": " + formatString(scenarioInfo->mapName) + "\n"; +// scenarioInfo->desc+= lang.get("Tileset") + ": " + formatString(scenarioInfo->tilesetName) + "\n"; +// scenarioInfo->desc+= lang.get("TechTree") + ": " + formatString(scenarioInfo->techTreeName) + "\n"; +// +// if(scenarioNode->hasChild("fog-of-war") == true) { +// if(scenarioNode->getChild("fog-of-war")->getAttribute("value")->getValue() == "explored") { +// scenarioInfo->fogOfWar = true; +// scenarioInfo->fogOfWar_exploredFlag = true; +// } +// else { +// scenarioInfo->fogOfWar = scenarioNode->getChild("fog-of-war")->getAttribute("value")->getBoolValue(); +// scenarioInfo->fogOfWar_exploredFlag = false; +// } +// //printf("\nFOG OF WAR is set to [%d]\n",scenarioInfo->fogOfWar); +// } +// else { +// scenarioInfo->fogOfWar = true; +// scenarioInfo->fogOfWar_exploredFlag = false; +// } //scenarioLogoTexture = NULL; + + Scenario::loadScenarioInfo(file, scenarioInfo); + cleanupPreviewTexture(); previewLoadDelayTimer=time(NULL); needToLoadTextures=true; @@ -388,73 +393,50 @@ void MenuStateScenario::loadGameSettings(const ScenarioInfo *scenarioInfo, GameS throw runtime_error(szBuf); } - int factionCount= 0; + Scenario::loadGameSettings(dirList,scenarioInfo, gameSettings, formatString(scenarioFiles[listBoxScenario.getSelectedItemIndex()])); - //printf("\n\n\n$$$$$$$$$$$$$ [%s]\n\n\n",scenarioFiles[listBoxScenario.getSelectedItemIndex()].c_str()); - - gameSettings->setDescription(formatString(scenarioFiles[listBoxScenario.getSelectedItemIndex()])); - gameSettings->setMap(scenarioInfo->mapName); - gameSettings->setTileset(scenarioInfo->tilesetName); - gameSettings->setTech(scenarioInfo->techTreeName); - gameSettings->setScenario(scenarioFiles[listBoxScenario.getSelectedItemIndex()]); - gameSettings->setScenarioDir(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()])); - gameSettings->setDefaultUnits(scenarioInfo->defaultUnits); - gameSettings->setDefaultResources(scenarioInfo->defaultResources); - gameSettings->setDefaultVictoryConditions(scenarioInfo->defaultVictoryConditions); - - for(int i=0; i(scenarioInfo->factionControls[i]); - if(ct!=ctClosed){ - if(ct==ctHuman){ - gameSettings->setThisFactionIndex(factionCount); - } - gameSettings->setFactionControl(factionCount, ct); - gameSettings->setResourceMultiplierIndex(factionCount, (scenarioInfo->resourceMultipliers[i]-0.5f)/0.1f); - gameSettings->setTeam(factionCount, scenarioInfo->teams[i]-1); - gameSettings->setStartLocationIndex(factionCount, i); - gameSettings->setFactionTypeName(factionCount, scenarioInfo->factionTypeNames[i]); - factionCount++; - } - } - - gameSettings->setFactionCount(factionCount); - gameSettings->setFogOfWar(scenarioInfo->fogOfWar); - uint32 valueFlags1 = gameSettings->getFlagTypes1(); - if(scenarioInfo->fogOfWar == false || scenarioInfo->fogOfWar_exploredFlag) { - valueFlags1 |= ft1_show_map_resources; - gameSettings->setFlagTypes1(valueFlags1); - } - else { - valueFlags1 &= ~ft1_show_map_resources; - gameSettings->setFlagTypes1(valueFlags1); - } - - gameSettings->setPathFinderType(static_cast(Config::getInstance().getInt("ScenarioPathFinderType",intToStr(pfBasic).c_str()))); -} - -ControlType MenuStateScenario::strToControllerType(const string &str){ - if(str=="closed"){ - return ctClosed; - } - else if(str=="cpu-easy"){ - return ctCpuEasy; - } - else if(str=="cpu"){ - return ctCpu; - } - else if(str=="cpu-ultra"){ - return ctCpuUltra; - } - else if(str=="cpu-mega"){ - return ctCpuMega; - } - else if(str=="human"){ - return ctHuman; - } - - char szBuf[4096]=""; - sprintf(szBuf,"Invalid controller value specified in scenario: [%s] must be one of the following: closed, cpu-easy, cpu, cpu-ultra, cpu-mega, human",str.c_str()); - throw std::runtime_error(szBuf); +// int factionCount= 0; +// +// //printf("\n\n\n$$$$$$$$$$$$$ [%s]\n\n\n",scenarioFiles[listBoxScenario.getSelectedItemIndex()].c_str()); +// +// gameSettings->setDescription(formatString(scenarioFiles[listBoxScenario.getSelectedItemIndex()])); +// gameSettings->setMap(scenarioInfo->mapName); +// gameSettings->setTileset(scenarioInfo->tilesetName); +// gameSettings->setTech(scenarioInfo->techTreeName); +// gameSettings->setScenario(scenarioFiles[listBoxScenario.getSelectedItemIndex()]); +// gameSettings->setScenarioDir(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()])); +// gameSettings->setDefaultUnits(scenarioInfo->defaultUnits); +// gameSettings->setDefaultResources(scenarioInfo->defaultResources); +// gameSettings->setDefaultVictoryConditions(scenarioInfo->defaultVictoryConditions); +// +// for(int i=0; i(scenarioInfo->factionControls[i]); +// if(ct!=ctClosed){ +// if(ct==ctHuman){ +// gameSettings->setThisFactionIndex(factionCount); +// } +// gameSettings->setFactionControl(factionCount, ct); +// gameSettings->setResourceMultiplierIndex(factionCount, (scenarioInfo->resourceMultipliers[i]-0.5f)/0.1f); +// gameSettings->setTeam(factionCount, scenarioInfo->teams[i]-1); +// gameSettings->setStartLocationIndex(factionCount, i); +// gameSettings->setFactionTypeName(factionCount, scenarioInfo->factionTypeNames[i]); +// factionCount++; +// } +// } +// +// gameSettings->setFactionCount(factionCount); +// gameSettings->setFogOfWar(scenarioInfo->fogOfWar); +// uint32 valueFlags1 = gameSettings->getFlagTypes1(); +// if(scenarioInfo->fogOfWar == false || scenarioInfo->fogOfWar_exploredFlag) { +// valueFlags1 |= ft1_show_map_resources; +// gameSettings->setFlagTypes1(valueFlags1); +// } +// else { +// valueFlags1 &= ~ft1_show_map_resources; +// gameSettings->setFlagTypes1(valueFlags1); +// } +// +// gameSettings->setPathFinderType(static_cast(Config::getInstance().getInt("ScenarioPathFinderType",intToStr(pfBasic).c_str()))); } void MenuStateScenario::showMessageBox(const string &text, const string &header, bool toggle){ diff --git a/source/glest_game/menu/menu_state_scenario.h b/source/glest_game/menu/menu_state_scenario.h index dbb70607f..975a2b09c 100644 --- a/source/glest_game/menu/menu_state_scenario.h +++ b/source/glest_game/menu/menu_state_scenario.h @@ -23,14 +23,6 @@ namespace Glest{ namespace Game{ class MenuStateScenario: public MenuState { private: - enum Difficulty { - dVeryEasy, - dEasy, - dMedium, - dHard, - dVeryHard, - dInsane - }; GraphicButton buttonReturn; GraphicButton buttonPlayNow; @@ -76,7 +68,6 @@ private: void loadGameSettings(const ScenarioInfo *scenarioInfo, GameSettings *gameSettings); void loadScenarioPreviewTexture(); Difficulty computeDifficulty(const ScenarioInfo *scenarioInfo); - ControlType strToControllerType(const string &str); void showMessageBox(const string &text, const string &header, bool toggle); void cleanupPreviewTexture(); diff --git a/source/glest_game/world/scenario.cpp b/source/glest_game/world/scenario.cpp index 8956a803d..f511651f7 100644 --- a/source/glest_game/world/scenario.cpp +++ b/source/glest_game/world/scenario.cpp @@ -20,6 +20,9 @@ #include #include "platform_common.h" #include "properties.h" +#include "lang.h" +#include "config.h" + #include "leak_dumper.h" using namespace Shared::Xml; @@ -38,6 +41,8 @@ Scenario::~Scenario() { } Checksum Scenario::load(const string &path) { + //printf("[%s:%s] Line: %d path [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); + Checksum scenarioChecksum; try { scenarioChecksum.addFile(path); @@ -46,6 +51,8 @@ Checksum Scenario::load(const string &path) { string name= cutLastExt(lastDir(path)); Logger::getInstance().add("Scenario: " + formatString(name), true); + Scenario::loadScenarioInfo(path, &info); + //parse xml XmlTree xmlTree; xmlTree.load(path,Properties::getTagReplacementValues()); @@ -88,6 +95,9 @@ string Scenario::getScenarioPath(const vector dirList, const string &sce string currentPath = dirList[idx]; endPathWithSlash(currentPath); scenarioFile = currentPath + scenarioName + "/" + scenarioName + ".xml"; + + //printf("\n[%s:%s] Line: %d scenarioName [%s] scenarioFile [%s]\n",__FILE__,__FUNCTION__,__LINE__,scenarioName.c_str(),scenarioFile.c_str()); + if(fileExists(scenarioFile) == true) { if(getMatchingRootScenarioPathOnly == true) { scenarioFile = dirList[idx]; @@ -118,4 +128,207 @@ string Scenario::getFunctionName(const XmlNode *scriptNode){ return name; } +void Scenario::loadScenarioInfo(string file, ScenarioInfo *scenarioInfo) { + //printf("[%s:%s] Line: %d file [%s]\n",__FILE__,__FUNCTION__,__LINE__,file.c_str()); + + Lang &lang= Lang::getInstance(); + + XmlTree xmlTree; + xmlTree.load(file,Properties::getTagReplacementValues()); + + const XmlNode *scenarioNode= xmlTree.getRootNode(); + const XmlNode *difficultyNode= scenarioNode->getChild("difficulty"); + scenarioInfo->difficulty = difficultyNode->getAttribute("value")->getIntValue(); + if( scenarioInfo->difficulty < dVeryEasy || scenarioInfo->difficulty > dInsane ) { + char szBuf[4096]=""; + sprintf(szBuf,"Invalid difficulty value specified in scenario: %d must be between %d and %d",scenarioInfo->difficulty,dVeryEasy,dInsane); + throw std::runtime_error(szBuf); + } + + const XmlNode *playersNode= scenarioNode->getChild("players"); + + for(int i= 0; i < GameConstants::maxPlayers; ++i) { + XmlNode* playerNode=NULL; + string factionTypeName=""; + ControlType factionControl; + + if(playersNode->hasChildAtIndex("player",i)){ + playerNode = playersNode->getChild("player", i); + factionControl = strToControllerType( playerNode->getAttribute("control")->getValue() ); + + if(playerNode->getAttribute("resource_multiplier",false)!=NULL) { + // if a multiplier exists use it + scenarioInfo->resourceMultipliers[i]=playerNode->getAttribute("resource_multiplier")->getFloatValue(); + } + else { + // if no multiplier exists use defaults + scenarioInfo->resourceMultipliers[i]=GameConstants::normalMultiplier; + if(factionControl==ctCpuEasy) { + scenarioInfo->resourceMultipliers[i]=GameConstants::easyMultiplier; + } + if(factionControl==ctCpuUltra) { + scenarioInfo->resourceMultipliers[i]=GameConstants::ultraMultiplier; + } + else if(factionControl==ctCpuMega) { + scenarioInfo->resourceMultipliers[i]=GameConstants::megaMultiplier; + } + } + + } + else { + factionControl=ctClosed; + } + + scenarioInfo->factionControls[i] = factionControl; + + if(factionControl != ctClosed){ + int teamIndex = playerNode->getAttribute("team")->getIntValue(); + + if( teamIndex < 1 || teamIndex > GameConstants::maxPlayers ) { + char szBuf[4096]=""; + sprintf(szBuf,"Invalid team value specified in scenario: %d must be between %d and %d",teamIndex,1,GameConstants::maxPlayers); + throw std::runtime_error(szBuf); + } + + scenarioInfo->teams[i]= playerNode->getAttribute("team")->getIntValue(); + scenarioInfo->factionTypeNames[i]= playerNode->getAttribute("faction")->getValue(); + } + + scenarioInfo->mapName = scenarioNode->getChild("map")->getAttribute("value")->getValue(); + scenarioInfo->tilesetName = scenarioNode->getChild("tileset")->getAttribute("value")->getValue(); + scenarioInfo->techTreeName = scenarioNode->getChild("tech-tree")->getAttribute("value")->getValue(); + scenarioInfo->defaultUnits = scenarioNode->getChild("default-units")->getAttribute("value")->getBoolValue(); + scenarioInfo->defaultResources = scenarioNode->getChild("default-resources")->getAttribute("value")->getBoolValue(); + scenarioInfo->defaultVictoryConditions = scenarioNode->getChild("default-victory-conditions")->getAttribute("value")->getBoolValue(); + } + + //add player info + scenarioInfo->desc= lang.get("Player") + ": "; + for(int i=0; ifactionControls[i] == ctHuman) { + scenarioInfo->desc+= formatString(scenarioInfo->factionTypeNames[i]); + break; + } + } + + //add misc info + string difficultyString = "Difficulty" + intToStr(scenarioInfo->difficulty); + + scenarioInfo->desc+= "\n"; + scenarioInfo->desc+= lang.get("Difficulty") + ": " + lang.get(difficultyString) +"\n"; + scenarioInfo->desc+= lang.get("Map") + ": " + formatString(scenarioInfo->mapName) + "\n"; + scenarioInfo->desc+= lang.get("Tileset") + ": " + formatString(scenarioInfo->tilesetName) + "\n"; + scenarioInfo->desc+= lang.get("TechTree") + ": " + formatString(scenarioInfo->techTreeName) + "\n"; + + if(scenarioNode->hasChild("fog-of-war") == true) { + if(scenarioNode->getChild("fog-of-war")->getAttribute("value")->getValue() == "explored") { + scenarioInfo->fogOfWar = true; + scenarioInfo->fogOfWar_exploredFlag = true; + } + else { + scenarioInfo->fogOfWar = scenarioNode->getChild("fog-of-war")->getAttribute("value")->getBoolValue(); + scenarioInfo->fogOfWar_exploredFlag = false; + } + //printf("\nFOG OF WAR is set to [%d]\n",scenarioInfo->fogOfWar); + } + else { + scenarioInfo->fogOfWar = true; + scenarioInfo->fogOfWar_exploredFlag = false; + } + + scenarioInfo->file = file; + scenarioInfo->name = extractFileFromDirectoryPath(file); + scenarioInfo->name = cutLastExt(scenarioInfo->name); + + //scenarioLogoTexture = NULL; + //cleanupPreviewTexture(); + //previewLoadDelayTimer=time(NULL); + //needToLoadTextures=true; +} + +ControlType Scenario::strToControllerType(const string &str) { + if(str=="closed"){ + return ctClosed; + } + else if(str=="cpu-easy"){ + return ctCpuEasy; + } + else if(str=="cpu"){ + return ctCpu; + } + else if(str=="cpu-ultra"){ + return ctCpuUltra; + } + else if(str=="cpu-mega"){ + return ctCpuMega; + } + else if(str=="human"){ + return ctHuman; + } + + char szBuf[4096]=""; + sprintf(szBuf,"Invalid controller value specified in scenario: [%s] must be one of the following: closed, cpu-easy, cpu, cpu-ultra, cpu-mega, human",str.c_str()); + throw std::runtime_error(szBuf); +} + +void Scenario::loadGameSettings(const vector &dirList, const ScenarioInfo *scenarioInfo, GameSettings *gameSettings, string scenarioDescription) { +// if(listBoxScenario.getSelectedItemIndex() < 0) { +// char szBuf[1024]=""; +// sprintf(szBuf,"listBoxScenario.getSelectedItemIndex() < 0, = %d",listBoxScenario.getSelectedItemIndex()); +// throw runtime_error(szBuf); +// } +// else if(listBoxScenario.getSelectedItemIndex() >= scenarioFiles.size()) { +// char szBuf[1024]=""; +// sprintf(szBuf,"listBoxScenario.getSelectedItemIndex() >= scenarioFiles.size(), = [%d][%d]",listBoxScenario.getSelectedItemIndex(),(int)scenarioFiles.size()); +// throw runtime_error(szBuf); +// } + + int factionCount= 0; + + //printf("\n\n\n$$$$$$$$$$$$$ [%s]\n\n\n",scenarioFiles[listBoxScenario.getSelectedItemIndex()].c_str()); + + //gameSettings->setDescription(formatString(scenarioFiles[listBoxScenario.getSelectedItemIndex()])); + gameSettings->setDescription(scenarioDescription); + gameSettings->setMap(scenarioInfo->mapName); + gameSettings->setTileset(scenarioInfo->tilesetName); + gameSettings->setTech(scenarioInfo->techTreeName); + //gameSettings->setScenario(scenarioFiles[listBoxScenario.getSelectedItemIndex()]); + gameSettings->setScenario(scenarioInfo->name); + //gameSettings->setScenarioDir(Scenario::getScenarioPath(dirList, scenarioFiles[listBoxScenario.getSelectedItemIndex()])); + gameSettings->setScenarioDir(Scenario::getScenarioPath(dirList, scenarioInfo->name)); + gameSettings->setDefaultUnits(scenarioInfo->defaultUnits); + gameSettings->setDefaultResources(scenarioInfo->defaultResources); + gameSettings->setDefaultVictoryConditions(scenarioInfo->defaultVictoryConditions); + + for(int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType ct= static_cast(scenarioInfo->factionControls[i]); + if(ct != ctClosed) { + if(ct == ctHuman) { + gameSettings->setThisFactionIndex(factionCount); + } + gameSettings->setFactionControl(factionCount, ct); + gameSettings->setResourceMultiplierIndex(factionCount, (scenarioInfo->resourceMultipliers[i]-0.5f)/0.1f); + gameSettings->setTeam(factionCount, scenarioInfo->teams[i]-1); + gameSettings->setStartLocationIndex(factionCount, i); + gameSettings->setFactionTypeName(factionCount, scenarioInfo->factionTypeNames[i]); + factionCount++; + } + } + + gameSettings->setFactionCount(factionCount); + gameSettings->setFogOfWar(scenarioInfo->fogOfWar); + uint32 valueFlags1 = gameSettings->getFlagTypes1(); + if(scenarioInfo->fogOfWar == false || scenarioInfo->fogOfWar_exploredFlag) { + valueFlags1 |= ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + else { + valueFlags1 &= ~ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + + gameSettings->setPathFinderType(static_cast(Config::getInstance().getInt("ScenarioPathFinderType",intToStr(pfBasic).c_str()))); +} + + }}//end namespace diff --git a/source/glest_game/world/scenario.h b/source/glest_game/world/scenario.h index 7249f1e82..629dd8e96 100644 --- a/source/glest_game/world/scenario.h +++ b/source/glest_game/world/scenario.h @@ -16,6 +16,8 @@ #include #include "xml_parser.h" #include "checksum.h" +#include "game_settings.h" + #include "leak_dumper.h" using std::string; @@ -27,6 +29,39 @@ using namespace Shared::Util; namespace Glest { namespace Game { +enum Difficulty { + dVeryEasy, + dEasy, + dMedium, + dHard, + dVeryHard, + dInsane +}; + +struct ScenarioInfo { + int difficulty; + ControlType factionControls[GameConstants::maxPlayers]; + int teams[GameConstants::maxPlayers]; + string factionTypeNames[GameConstants::maxPlayers]; + float resourceMultipliers[GameConstants::maxPlayers]; + + string mapName; + string tilesetName; + string techTreeName; + + bool defaultUnits; + bool defaultResources; + bool defaultVictoryConditions; + + string desc; + + bool fogOfWar; + bool fogOfWar_exploredFlag; + + string file; + string name; +}; + // ===================================================== // class Script // ===================================================== @@ -52,6 +87,7 @@ private: typedef pair NameScriptPair; typedef vector