summaryrefslogtreecommitdiff
path: root/src/settings.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-05-30 10:40:59 +0200
committerGitHub <noreply@github.com>2021-05-30 10:40:59 +0200
commite9e4588db105f5827c5356933023f6ce698fe6aa (patch)
tree064b5588764e6198578f6530616a231f04704057 /src/settings.cpp
parentbcd7a7aafe3d63117be74580298dcb20a53e4121 (diff)
downloadopenttd-e9e4588db105f5827c5356933023f6ce698fe6aa.tar.xz
Codechange: use setting name instead of index for HandleOldDiffCustom() (#9311)
Diffstat (limited to 'src/settings.cpp')
-rw-r--r--src/settings.cpp25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
index 56c02e5e1..d39c04593 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -22,6 +22,7 @@
*/
#include "stdafx.h"
+#include <array>
#include <limits>
#include "currency.h"
#include "screenshot.h"
@@ -1227,7 +1228,9 @@ static void PrepareOldDiffCustom()
*/
static void HandleOldDiffCustom(bool savegame)
{
- uint options_to_load = GAME_DIFFICULTY_NUM - ((savegame && IsSavegameVersionBefore(SLV_4)) ? 1 : 0);
+ /* Savegames before v4 didn't have "town_council_tolerance" in savegame yet. */
+ bool has_no_town_council_tolerance = savegame && IsSavegameVersionBefore(SLV_4);
+ uint options_to_load = GAME_DIFFICULTY_NUM - (has_no_town_council_tolerance ? 1 : 0);
if (!savegame) {
/* If we did read to old_diff_custom, then at least one value must be non 0. */
@@ -1239,11 +1242,21 @@ static void HandleOldDiffCustom(bool savegame)
if (!old_diff_custom_used) return;
}
- for (uint i = 0; i < options_to_load; i++) {
- const SettingDesc *sd = GetSettingDescription(i);
- /* Skip deprecated options */
- if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
- int32 value = (int32)((i == 4 ? 1000 : 1) * _old_diff_custom[i]);
+ /* Iterate over all the old difficulty settings, and convert the list-value to the new setting. */
+ uint i = 0;
+ for (const auto &name : _old_diff_settings) {
+ if (has_no_town_council_tolerance && name == "town_council_tolerance") continue;
+
+ std::string fullname = "difficulty." + name;
+ const SettingDesc *sd = GetSettingFromName(fullname.c_str());
+
+ /* Some settings are no longer in use; skip reading those. */
+ if (sd == nullptr) {
+ i++;
+ continue;
+ }
+
+ int32 value = (int32)((name == "max_loan" ? 1000 : 1) * _old_diff_custom[i++]);
sd->AsIntSetting()->MakeValueValidAndWrite(savegame ? &_settings_game : &_settings_newgame, value);
}
}