| 76 | AIPLChunkHandler() : ChunkHandler('AIPL', CH_TABLE) {} |
| 77 | |
| 78 | void Load() const override |
| 79 | { |
| 80 | const std::vector<SaveLoad> slt = SlCompatTableHeader(_ai_company_desc, _ai_company_sl_compat); |
| 81 | |
| 82 | /* Free all current data */ |
| 83 | for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { |
| 84 | AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt); |
| 85 | } |
| 86 | |
| 87 | CompanyID index; |
| 88 | while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) { |
| 89 | if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs"); |
| 90 | |
| 91 | _ai_saveload_is_random = false; |
| 92 | _ai_saveload_version = -1; |
| 93 | SlObject(nullptr, slt); |
| 94 | |
| 95 | if (_game_mode == GM_MENU || (_networking && !_network_server)) { |
| 96 | if (Company::IsValidAiID(index)) { |
| 97 | SlObject(nullptr, _ai_running_desc); |
| 98 | AIInstance::LoadEmpty(); |
| 99 | } |
| 100 | continue; |
| 101 | } |
| 102 | |
| 103 | AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME); |
| 104 | if (_ai_saveload_name.empty() || _ai_saveload_is_random) { |
| 105 | /* A random AI. */ |
| 106 | config->Change(std::nullopt, -1, false); |
| 107 | } else { |
| 108 | config->Change(_ai_saveload_name, _ai_saveload_version, false); |
| 109 | if (!config->HasScript()) { |
| 110 | /* No version of the AI available. Try to configure the |
| 111 | * latest version of the AI instead. */ |
| 112 | config->Change(_ai_saveload_name, -1, false); |
| 113 | if (!config->HasScript()) { |
| 114 | if (_ai_saveload_name != "%_dummy") { |
| 115 | Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); |
| 116 | Debug(script, 0, "Configuration switched to Random AI."); |
| 117 | } |
| 118 | } else { |
| 119 | Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); |
| 120 | Debug(script, 0, "The latest version of that AI has been configured instead"); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | config->StringToSettings(_ai_saveload_settings); |
| 125 | |
| 126 | if (!Company::IsValidAiID(index)) continue; |
| 127 | |
| 128 | /* Load the AI saved data */ |
| 129 | SlObject(nullptr, _ai_running_desc); |
| 130 | |
| 131 | Company::Get(index)->ai_config = std::make_unique<AIConfig>(); |
| 132 | config = Company::Get(index)->ai_config.get(); |
| 133 | config->Change(_ai_saveload_name, _ai_saveload_version, false); |
| 134 | if (!config->HasScript()) { |
| 135 | /* No version of the AI available that can load the data. Try to load the |
nothing calls this directly
no test coverage detected