00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../company_base.h"
00014 #include "../debug.h"
00015 #include "saveload.h"
00016 #include "../string_func.h"
00017 #include "../ai/ai.hpp"
00018 #include "../ai/ai_config.hpp"
00019 #include "../network/network.h"
00020 #include "../ai/ai_instance.hpp"
00021
00022 static char _ai_saveload_name[64];
00023 static int _ai_saveload_version;
00024 static char _ai_saveload_settings[1024];
00025 static bool _ai_saveload_is_random;
00026
00027 static const SaveLoad _ai_company[] = {
00028 SLEG_STR(_ai_saveload_name, SLE_STRB),
00029 SLEG_STR(_ai_saveload_settings, SLE_STRB),
00030 SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
00031 SLEG_CONDVAR(_ai_saveload_is_random, SLE_BOOL, 136, SL_MAX_VERSION),
00032 SLE_END()
00033 };
00034
00035 static void SaveReal_AIPL(int *index_ptr)
00036 {
00037 CompanyID index = (CompanyID)*index_ptr;
00038 AIConfig *config = AIConfig::GetConfig(index);
00039
00040 if (config->HasAI()) {
00041 ttd_strlcpy(_ai_saveload_name, config->GetName(), lengthof(_ai_saveload_name));
00042 _ai_saveload_version = config->GetVersion();
00043 } else {
00044
00045 _ai_saveload_name[0] = '\0';
00046 _ai_saveload_version = -1;
00047 }
00048
00049 _ai_saveload_is_random = config->IsRandomAI();
00050 _ai_saveload_settings[0] = '\0';
00051 config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
00052
00053 SlObject(NULL, _ai_company);
00054
00055 if (Company::IsValidAiID(index)) AI::Save(index);
00056 }
00057
00058 static void Load_AIPL()
00059 {
00060
00061 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00062 AIConfig::GetConfig(c)->ChangeAI(NULL);
00063 }
00064
00065 CompanyID index;
00066 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
00067 _ai_saveload_version = -1;
00068 SlObject(NULL, _ai_company);
00069
00070 if (_networking && !_network_server) {
00071 if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
00072 continue;
00073 }
00074
00075 AIConfig *config = AIConfig::GetConfig(index);
00076 if (StrEmpty(_ai_saveload_name)) {
00077
00078 config->ChangeAI(NULL, -1, false, true);
00079 } else {
00080 config->ChangeAI(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
00081 if (!config->HasAI()) {
00082
00083
00084 config->ChangeAI(_ai_saveload_name, -1, false, _ai_saveload_is_random);
00085 if (!config->HasAI()) {
00086 if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
00087 DEBUG(ai, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
00088 DEBUG(ai, 0, "A random other AI will be loaded in its place.");
00089 } else {
00090 DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
00091 DEBUG(ai, 0, "A random available AI will be loaded now.");
00092 }
00093 } else {
00094 DEBUG(ai, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
00095 DEBUG(ai, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
00096 }
00097
00098
00099 _ai_saveload_version = -1;
00100 }
00101 }
00102
00103 config->StringToSettings(_ai_saveload_settings);
00104
00105
00106 if (Company::IsValidAiID(index)) {
00107 AI::StartNew(index, false);
00108 AI::Load(index, _ai_saveload_version);
00109 }
00110 }
00111 }
00112
00113 static void Save_AIPL()
00114 {
00115 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00116 SlSetArrayIndex(i);
00117 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
00118 }
00119 }
00120
00121 extern const ChunkHandler _ai_chunk_handlers[] = {
00122 { 'AIPL', Save_AIPL, Load_AIPL, NULL, CH_ARRAY | CH_LAST},
00123 };