src/openttd.cpp
changeset 6929 56470c1b8a66
parent 6928 f1d8bf0d6f45
child 6935 0a2a174d3f6a
equal deleted inserted replaced
6928:f1d8bf0d6f45 6929:56470c1b8a66
   310 	free(_config_file);
   310 	free(_config_file);
   311 }
   311 }
   312 
   312 
   313 static void LoadIntroGame()
   313 static void LoadIntroGame()
   314 {
   314 {
   315 	char filename[256];
       
   316 
       
   317 	_game_mode = GM_MENU;
   315 	_game_mode = GM_MENU;
   318 
   316 
   319 	/* Clear transparency options */
   317 	/* Clear transparency options */
   320 	_transparent_opt = 0;
   318 	_transparent_opt = 0;
   321 
   319 
   324 
   322 
   325 	/* Setup main window */
   323 	/* Setup main window */
   326 	ResetWindowSystem();
   324 	ResetWindowSystem();
   327 	SetupColorsAndInitialWindow();
   325 	SetupColorsAndInitialWindow();
   328 
   326 
   329 	/* Generate a world. */
   327 	/* Load the default opening screen savegame */
   330 	snprintf(filename, lengthof(filename), "%sopntitle.dat",  _paths.data_dir);
   328 	if (SaveOrLoad("opntitle.dat", SL_LOAD, DATA_DIR) != SL_OK) {
   331 #if defined SECOND_DATA_DIR
       
   332 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
       
   333 		snprintf(filename, lengthof(filename), "%sopntitle.dat",  _paths.second_data_dir);
       
   334 	}
       
   335 #endif
       
   336 	if (SaveOrLoad(filename, SL_LOAD) != SL_OK) {
       
   337 		GenerateWorld(GW_EMPTY, 64, 64); // if failed loading, make empty world.
   329 		GenerateWorld(GW_EMPTY, 64, 64); // if failed loading, make empty world.
   338 		WaitTillGeneratedWorld();
   330 		WaitTillGeneratedWorld();
   339 	}
   331 	}
   340 
   332 
   341 	_pause_game = 0;
   333 	_pause_game = 0;
   758 	SetupColorsAndInitialWindow();
   750 	SetupColorsAndInitialWindow();
   759 
   751 
   760 	ResetGRFConfig(true);
   752 	ResetGRFConfig(true);
   761 
   753 
   762 	/* Load game */
   754 	/* Load game */
   763 	if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode) != SL_OK) {
   755 	if (SaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, SCENARIO_DIR) != SL_OK) {
   764 		LoadIntroGame();
   756 		LoadIntroGame();
   765 		ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   757 		ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   766 	}
   758 	}
   767 
   759 
   768 	_opt_ptr = &_opt;
   760 	_opt_ptr = &_opt;
   780 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
   772 	DoCommandP(0, (_patches.autorenew << 15 ) | (_patches.autorenew_months << 16) | 4, _patches.autorenew_money, NULL, CMD_SET_AUTOREPLACE);
   781 
   773 
   782 	MarkWholeScreenDirty();
   774 	MarkWholeScreenDirty();
   783 }
   775 }
   784 
   776 
   785 bool SafeSaveOrLoad(const char *filename, int mode, int newgm)
   777 /** Load the specified savegame but on error do different things.
       
   778  * If loading fails due to corrupt savegame, bad version, etc. go back to
       
   779  * a previous correct state. In the menu for example load the intro game again.
       
   780  * @param filename file to be loaded
       
   781  * @param mode mode of loading, either SL_LOAD or SL_OLD_LOAD
       
   782  * @param newgm switch to this mode of loading fails due to some unknown error
       
   783  * @param subdir default directory to look for filename, set to 0 if not needed */
       
   784 bool SafeSaveOrLoad(const char *filename, int mode, int newgm, Subdirectory subdir)
   786 {
   785 {
   787 	byte ogm = _game_mode;
   786 	byte ogm = _game_mode;
   788 
   787 
   789 	_game_mode = newgm;
   788 	_game_mode = newgm;
   790 	switch (SaveOrLoad(filename, mode)) {
   789 	assert(mode == SL_LOAD || mode == SL_OLD_LOAD);
       
   790 	switch (SaveOrLoad(filename, mode, subdir)) {
   791 		case SL_OK: return true;
   791 		case SL_OK: return true;
   792 
   792 
   793 		case SL_REINIT:
   793 		case SL_REINIT:
   794 			switch (ogm) {
   794 			switch (ogm) {
   795 				case GM_MENU:   LoadIntroGame();      break;
   795 				case GM_MENU:   LoadIntroGame();      break;
   858 
   858 
   859 	case SM_LOAD: { /* Load game, Play Scenario */
   859 	case SM_LOAD: { /* Load game, Play Scenario */
   860 		_opt_ptr = &_opt;
   860 		_opt_ptr = &_opt;
   861 		ResetGRFConfig(true);
   861 		ResetGRFConfig(true);
   862 
   862 
   863 		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL)) {
   863 		if (!SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_NORMAL, BASE_DIR)) {
   864 			LoadIntroGame();
   864 			LoadIntroGame();
   865 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   865 			ShowErrorMessage(INVALID_STRING_ID, STR_4009_GAME_LOAD_FAILED, 0, 0);
   866 		} else {
   866 		} else {
   867 			/* Update the local player for a loaded game. It is either always
   867 			/* Update the local player for a loaded game. It is either always
   868 			 * player #1 (eg 0) or in the case of a dedicated server a spectator */
   868 			 * player #1 (eg 0) or in the case of a dedicated server a spectator */
   892 		GenerateWorld(GW_HEIGHTMAP, 1 << _patches.map_x, 1 << _patches.map_y);
   892 		GenerateWorld(GW_HEIGHTMAP, 1 << _patches.map_x, 1 << _patches.map_y);
   893 		MarkWholeScreenDirty();
   893 		MarkWholeScreenDirty();
   894 		break;
   894 		break;
   895 
   895 
   896 	case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
   896 	case SM_LOAD_SCENARIO: { /* Load scenario from scenario editor */
   897 		if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR)) {
   897 		if (SafeSaveOrLoad(_file_to_saveload.name, _file_to_saveload.mode, GM_EDITOR, BASE_DIR)) {
   898 			_opt_ptr = &_opt;
   898 			_opt_ptr = &_opt;
   899 
   899 
   900 			SetLocalPlayer(OWNER_NONE);
   900 			SetLocalPlayer(OWNER_NONE);
   901 			_patches_newgame.starting_year = _cur_year;
   901 			_patches_newgame.starting_year = _cur_year;
   902 		} else {
   902 		} else {
   908 	case SM_MENU: /* Switch to game intro menu */
   908 	case SM_MENU: /* Switch to game intro menu */
   909 		LoadIntroGame();
   909 		LoadIntroGame();
   910 		break;
   910 		break;
   911 
   911 
   912 	case SM_SAVE: /* Save game */
   912 	case SM_SAVE: /* Save game */
   913 		if (SaveOrLoad(_file_to_saveload.name, SL_SAVE) != SL_OK) {
   913 		if (SaveOrLoad(_file_to_saveload.name, SL_SAVE, BASE_DIR) != SL_OK) {
   914 			ShowErrorMessage(INVALID_STRING_ID, STR_4007_GAME_SAVE_FAILED, 0, 0);
   914 			ShowErrorMessage(INVALID_STRING_ID, STR_4007_GAME_SAVE_FAILED, 0, 0);
   915 		} else {
   915 		} else {
   916 			DeleteWindowById(WC_SAVELOAD, 0);
   916 			DeleteWindowById(WC_SAVELOAD, 0);
   917 		}
   917 		}
   918 		break;
   918 		break;
   965 		NewsLoop();
   965 		NewsLoop();
   966 		_current_player = p;
   966 		_current_player = p;
   967 	}
   967 	}
   968 }
   968 }
   969 
   969 
       
   970 /** Create an autosave. The default name is "autosave#.sav". However with
       
   971  * the patch setting 'keep_all_autosave' the name defaults to company-name + date */
   970 static void DoAutosave()
   972 static void DoAutosave()
   971 {
   973 {
   972 	char buf[200];
   974 	char buf[MAX_PATH];
   973 
   975 
   974 #if defined(PSP)
   976 #if defined(PSP)
   975 	/* Autosaving in networking is too time expensive for the PSP */
   977 	/* Autosaving in networking is too time expensive for the PSP */
   976 	if (_networking)
   978 	if (_networking)
   977 		return;
   979 		return;
   978 #endif /* PSP */
   980 #endif /* PSP */
   979 
   981 
   980 	if (_patches.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
   982 	if (_patches.keep_all_autosave && _local_player != PLAYER_SPECTATOR) {
   981 		const Player *p = GetPlayer(_local_player);
   983 		const Player *p = GetPlayer(_local_player);
   982 		char* s = buf;
       
   983 
       
   984 		s += snprintf(buf, lengthof(buf), "%s%s", _paths.autosave_dir, PATHSEP);
       
   985 
   984 
   986 		SetDParam(0, p->name_1);
   985 		SetDParam(0, p->name_1);
   987 		SetDParam(1, p->name_2);
   986 		SetDParam(1, p->name_2);
   988 		SetDParam(2, _date);
   987 		SetDParam(2, _date);
   989 		s = GetString(s, STR_4004, lastof(buf));
   988 		GetString(buf, STR_4004, lastof(buf));
   990 		strecpy(s, ".sav", lastof(buf));
   989 		ttd_strlcpy(buf, ".sav", sizeof(buf));
   991 	} else { // generate a savegame name and number according to _patches.max_num_autosaves
   990 	} else {
   992 		snprintf(buf, lengthof(buf), "%s%sautosave%d.sav", _paths.autosave_dir, PATHSEP, _autosave_ctr);
   991 		/* generate a savegame name and number according to _patches.max_num_autosaves */
   993 
   992 		snprintf(buf, sizeof(buf), "autosave%d.sav", _autosave_ctr);
   994 		_autosave_ctr++;
   993 
   995 		if (_autosave_ctr >= _patches.max_num_autosaves) {
   994 		if (++_autosave_ctr >= _patches.max_num_autosaves) _autosave_ctr = 0;
   996 			/* we reached the limit for numbers of autosaves. We will start over */
       
   997 			_autosave_ctr = 0;
       
   998 		}
       
   999 	}
   995 	}
  1000 
   996 
  1001 	DEBUG(sl, 2, "Autosaving to '%s'", buf);
   997 	DEBUG(sl, 2, "Autosaving to '%s'", buf);
  1002 	if (SaveOrLoad(buf, SL_SAVE) != SL_OK)
   998 	if (SaveOrLoad(buf, SL_SAVE, AUTOSAVE_DIR) != SL_OK)
  1003 		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
   999 		ShowErrorMessage(INVALID_STRING_ID, STR_AUTOSAVE_FAILED, 0, 0);
  1004 }
  1000 }
  1005 
  1001 
  1006 static void ScrollMainViewport(int x, int y)
  1002 static void ScrollMainViewport(int x, int y)
  1007 {
  1003 {