MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / HandleOldDiffCustom

Function HandleOldDiffCustom

src/saveload/settings_sl.cpp:37–70  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

35 * to check that.
36 */
37void HandleOldDiffCustom(bool savegame)
38{
39 /* Savegames before v4 didn't have "town_council_tolerance" in savegame yet. */
40 bool has_no_town_council_tolerance = savegame && IsSavegameVersionBefore(SLV_4);
41 uint options_to_load = GAME_DIFFICULTY_NUM - (has_no_town_council_tolerance ? 1 : 0);
42
43 if (!savegame) {
44 /* If we did read to old_diff_custom, then at least one value must be non 0. */
45 bool old_diff_custom_used = false;
46 for (uint i = 0; i < options_to_load && !old_diff_custom_used; i++) {
47 old_diff_custom_used = (_old_diff_custom[i] != 0);
48 }
49
50 if (!old_diff_custom_used) return;
51 }
52
53 /* Iterate over all the old difficulty settings, and convert the list-value to the new setting. */
54 uint i = 0;
55 for (const auto &name : _old_diff_settings) {
56 if (has_no_town_council_tolerance && name == "town_council_tolerance") continue;
57
58 std::string fullname = "difficulty." + name;
59 const SettingDesc *sd = GetSettingFromName(fullname);
60
61 /* Some settings are no longer in use; skip reading those. */
62 if (sd == nullptr) {
63 i++;
64 continue;
65 }
66
67 int32_t value = (int32_t)((name == "max_loan" ? 1000 : 1) * _old_diff_custom[i++]);
68 sd->AsIntSetting()->MakeValueValidAndWrite(savegame ? &_settings_game : &_settings_newgame, value);
69 }
70}
71
72/**
73 * Get the SaveLoad description for the SettingTable.

Callers 2

LoadFromConfigFunction · 0.85
LoadMethod · 0.85

Calls 4

IsSavegameVersionBeforeFunction · 0.85
GetSettingFromNameFunction · 0.85
AsIntSettingMethod · 0.80

Tested by

no test coverage detected