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