(svn r13309) -Fix: the difficulty settings from the savegame would be reset by the ones of the intro game.
authorrubidium
Wed, 28 May 2008 14:36:06 +0000
changeset 9398 d60d75cb053d
parent 9397 3939a8d41911
child 9399 3090d36f3436
(svn r13309) -Fix: the difficulty settings from the savegame would be reset by the ones of the intro game.
src/settings.cpp
--- a/src/settings.cpp	Wed May 28 11:56:44 2008 +0000
+++ b/src/settings.cpp	Wed May 28 14:36:06 2008 +0000
@@ -1793,26 +1793,37 @@
 #undef NO
 #undef CR
 
+/**
+ * Prepare for reading and old diff_custom by zero-ing the memory.
+ */
 static void PrepareOldDiffCustom()
 {
 	memset(_old_diff_custom, 0, sizeof(_old_diff_custom));
 }
 
-static void HandleOldDiffCustom()
+/**
+ * Reading of the old diff_custom array and transforming it to the new format.
+ * @param savegame is it read from the config or savegame. In the latter case
+ *                 we are sure there is an array; in the former case we have
+ *                 to check that.
+ */
+static void HandleOldDiffCustom(bool savegame)
 {
-	uint options_to_load = GAME_DIFFICULTY_NUM - (CheckSavegameVersion(4) ? 1 : 0);
+	uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && CheckSavegameVersion(4)) ? 1 : 0);
 
-	/* If we did read to old_diff_custom, then at least one value must be non 0. */
-	bool old_diff_custom_used = false;
-	for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
-		old_diff_custom_used = (_old_diff_custom[i] != 0);
+	if (!savegame) {
+		/* If we did read to old_diff_custom, then at least one value must be non 0. */
+		bool old_diff_custom_used = false;
+		for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
+			old_diff_custom_used = (_old_diff_custom[i] != 0);
+		}
+
+		if (!old_diff_custom_used) return;
 	}
 
-	if (!old_diff_custom_used) return;
-
 	for (uint i = 0; i < options_to_load; i++) {
 		const SettingDesc *sd = &_patch_settings[i];
-		void *var = GetVariableAddress((_game_mode == GM_MENU) ? &_settings_newgame : &_settings, &sd->save);
+		void *var = GetVariableAddress(savegame ? &_settings : &_settings_newgame, &sd->save);
 		Write_ValidateSetting(var, sd, (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]));
 	}
 }
@@ -1983,10 +1994,6 @@
 	proc(ini, (const SettingDesc*)_win32_settings,   "win32", NULL);
 #endif /* WIN32 */
 
-	PrepareOldDiffCustom();
-	proc(ini, _gameopt_settings, "gameopt",  &_settings_newgame);
-	HandleOldDiffCustom();
-
 	proc(ini, _patch_settings,   "patches",  &_settings_newgame);
 	proc(ini, _currency_settings,"currency", &_custom_currency);
 
@@ -2002,6 +2009,11 @@
 {
 	IniFile *ini = ini_load(_config_file);
 	ResetCurrencies(false); // Initialize the array of curencies, without preserving the custom one
+
+	PrepareOldDiffCustom();
+	ini_load_settings(ini, _gameopt_settings, "gameopt",  &_settings_newgame);
+	HandleOldDiffCustom(false);
+
 	HandleSettingDescs(ini, ini_load_settings, ini_load_setting_list);
 	_grfconfig_newgame = GRFLoadConfig(ini, "newgrf", false);
 	_grfconfig_static  = GRFLoadConfig(ini, "newgrf-static", true);
@@ -2247,7 +2259,7 @@
 	 * autosave-frequency stays when joining a network-server */
 	PrepareOldDiffCustom();
 	LoadSettings(_gameopt_settings, &_settings);
-	HandleOldDiffCustom();
+	HandleOldDiffCustom(true);
 }
 
 static void Load_PATS()