diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 7e80015f..c9e8d7ca 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -2490,6 +2490,17 @@ void Game::keyDown(SDL_KeyboardEvent key) { gameCamera.setMoveY(-1); } } + + if(isKeyPressed(configKeys.getSDLKey("SaveGame"),key) == true) { + string file = this->saveGame(GameConstants::saveGameFilePattern); + char szBuf[8096]=""; + sprintf(szBuf,lang.get("GameSaved","",true).c_str(),file.c_str()); + console.addLine(szBuf); + + Config &config= Config::getInstance(); + config.setString("LastSavedGame",file); + config.save(); + } } //throw runtime_error("Test Error!"); @@ -2635,9 +2646,12 @@ void Game::keyPress(SDL_KeyboardEvent c) { Stats Game::quitGame() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled == true) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled == true) { world.DumpWorldToLog(); - //} + } + //printf("Check savegame\n"); + //printf("Saving...\n"); + this->saveGame(GameConstants::saveGameFileDefault); //Stats stats = *(world.getStats()); Stats endStats; @@ -3438,7 +3452,18 @@ void Game::toggleTeamColorMarker() { renderExtraTeamColor=renderExtraTeamColor%4; } -void Game::saveGame(string name) { +string Game::saveGame(string name) { + // auto name file if using saved file pattern string + if(name == GameConstants::saveGameFilePattern) { + time_t curTime = time(NULL); + struct tm *loctime = localtime (&curTime); + char szBuf2[100]=""; + strftime(szBuf2,100,"%Y%m%d_%H%M%S",loctime); + + char szBuf[8096]=""; + sprintf(szBuf,name.c_str(),szBuf2); + name = szBuf; + } //XmlTree xmlTree(XML_XERCES_ENGINE); XmlTree xmlTree; xmlTree.init("megaglest-saved-game"); @@ -3595,6 +3620,8 @@ void Game::saveGame(string name) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Saving game to [%s]\n",saveGameFile.c_str()); xmlTree.save(saveGameFile); + + return saveGameFile; } void Game::loadGame(string name,Program *programPtr,bool isMasterserverMode) { diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 289522db..8abe527d 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -229,7 +229,7 @@ public: void endGame(); - void saveGame(string name); + string saveGame(string name); static void loadGame(string name,Program *programPtr,bool isMasterserverMode); private: diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index 494e599c..8660d688 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -126,6 +126,7 @@ public: static const char *application_name; static const char *saveGameFileDefault; + static const char *saveGameFilePattern; // VC++ Chokes on init of non integral static types static const float normalMultiplier; diff --git a/source/glest_game/global/config.cpp b/source/glest_game/global/config.cpp index 1d0bda79..0f98fe7b 100644 --- a/source/glest_game/global/config.cpp +++ b/source/glest_game/global/config.cpp @@ -62,6 +62,7 @@ const char *GameConstants::path_ini_CacheLookupKey = "ini"; const char *GameConstants::path_logs_CacheLookupKey = "logs"; const char *GameConstants::saveGameFileDefault = "megaglest-saved.xml"; +const char *GameConstants::saveGameFilePattern = "megaglest-saved_%s.xml"; const char *Config::glest_ini_filename = "glest.ini"; const char *Config::glestuser_ini_filename = "glestuser.ini"; diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 3ee2039e..9fe3b7a2 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -3313,7 +3313,26 @@ int glestMain(int argc, char** argv) { program->initServer(mainWindow,true,false); } else if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME])) == true) { - program->initSavedGame(mainWindow,false); + string fileName = ""; + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME]) + string("="),&foundParamIndIndex); + if(foundParamIndIndex >= 0) { + string loadfileName = argv[foundParamIndIndex]; + vector paramPartTokens; + Tokenize(loadfileName,paramPartTokens,"="); + if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + fileName = paramPartTokens[1]; + + if(fileExists(fileName) == false) { + char szBuf[8096]=""; + sprintf(szBuf,"File specified for loading a saved game cannot be found: [%s]",fileName.c_str()); + printf("\n\n======================================================================================\n%s\n======================================================================================\n\n\n",szBuf); + + throw runtime_error(szBuf); + } + } + } + program->initSavedGame(mainWindow,false,fileName); } else if(hasCommandArgument(argc, argv,string(GAME_ARGS[GAME_ARG_PREVIEW_MAP])) == true) { int foundParamIndIndex = -1; diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 7cde9854..35aec8cc 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -200,26 +200,33 @@ void Program::initNormal(WindowGl *window){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } -void Program::initSavedGame(WindowGl *window,bool masterserverMode) { +void Program::initSavedGame(WindowGl *window,bool masterserverMode, string saveGameFile) { MainMenu* mainMenu= NULL; init(window); mainMenu= new MainMenu(this); setState(mainMenu); - string saveGameFile = GameConstants::saveGameFileDefault; - if(getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { - saveGameFile = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + saveGameFile; - } - else { - string userData = Config::getInstance().getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - saveGameFile = userData + saveGameFile; + if(saveGameFile == "") { + Config &config = Config::getInstance(); + saveGameFile = config.getString("LastSavedGame",""); + if(saveGameFile == "") { + saveGameFile = GameConstants::saveGameFileDefault; + if(getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { + saveGameFile = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + saveGameFile; + } + else { + string userData = Config::getInstance().getString("UserData_Root",""); + if(userData != "") { + endPathWithSlash(userData); + } + saveGameFile = userData + saveGameFile; + } + } } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Loading game from [%s]\n",saveGameFile.c_str()); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Loading game from [%s]\n",saveGameFile.c_str()); + printf("Loading saved game from [%s]\n",saveGameFile.c_str()); Game::loadGame(saveGameFile,this,masterserverMode); } diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index f58f9bf8..3edda4d4 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -168,7 +168,7 @@ public: void initNormal(WindowGl *window); void initServer(WindowGl *window,bool autostart=false,bool openNetworkSlots=false,bool masterserverMode=false); void initServer(WindowGl *window, GameSettings *settings); - void initSavedGame(WindowGl *window,bool masterserverMode=false); + void initSavedGame(WindowGl *window,bool masterserverMode=false,string saveGameFile=""); void initClient(WindowGl *window, const Ip &serverIp); void initScenario(WindowGl *window, string autoloadScenarioName); diff --git a/source/shared_lib/include/platform/sdl/platform_main.h b/source/shared_lib/include/platform/sdl/platform_main.h index 58abee82..e513b0da 100644 --- a/source/shared_lib/include/platform/sdl/platform_main.h +++ b/source/shared_lib/include/platform/sdl/platform_main.h @@ -143,7 +143,9 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) { printf("\n%s\t\tAutomatically starts a game with the last game",GAME_ARGS[GAME_ARG_AUTOSTART_LASTGAME]); printf("\n\t\t\t\tsettings you played."); - printf("\n%s\t\tLoads the last saved game.",GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME]); + printf("\n%s=x\t\tLoads the last saved game.",GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME]); + printf("\n \t\tWhere x is an optional name of the saved game file to load."); + printf("\n \t\tIf x is not specified we load the last game that was saved."); printf("\n%s=x\t\t\tAuto connect to host server at IP or hostname x",GAME_ARGS[GAME_ARG_CLIENT]); printf("\n%s\t\t\tAuto create a host server.",GAME_ARGS[GAME_ARG_SERVER]);