ai_sl.cpp
Go to the documentation of this file.00001
00002
00005 #include "../stdafx.h"
00006 #include "../company_base.h"
00007 #include "../company_func.h"
00008 #include "../debug.h"
00009 #include "saveload.h"
00010 #include "../string_func.h"
00011 #include "../ai/ai.hpp"
00012 #include "../ai/ai_config.hpp"
00013 #include "../network/network.h"
00014 #include "../ai/ai_instance.hpp"
00015
00016 static char _ai_saveload_name[64];
00017 static int _ai_saveload_version;
00018 static char _ai_saveload_settings[1024];
00019
00020 static const SaveLoad _ai_company[] = {
00021 SLEG_STR(_ai_saveload_name, SLE_STRB),
00022 SLEG_STR(_ai_saveload_settings, SLE_STRB),
00023 SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
00024 SLE_END()
00025 };
00026
00027 static void SaveReal_AIPL(int *index_ptr)
00028 {
00029 CompanyID index = (CompanyID)*index_ptr;
00030 AIConfig *config = AIConfig::GetConfig(index);
00031
00032 if (config->HasAI()) {
00033 ttd_strlcpy(_ai_saveload_name, config->GetName(), lengthof(_ai_saveload_name));
00034 _ai_saveload_version = config->GetVersion();
00035 } else {
00036
00037 _ai_saveload_name[0] = '\0';
00038 _ai_saveload_version = -1;
00039 }
00040
00041 _ai_saveload_settings[0] = '\0';
00042 config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
00043
00044 SlObject(NULL, _ai_company);
00045
00046 if (IsValidCompanyID(index) && !IsHumanCompany(index)) AI::Save(index);
00047 }
00048
00049 static void Load_AIPL()
00050 {
00051
00052 for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00053 AIConfig::GetConfig(c)->ChangeAI(NULL);
00054 }
00055
00056 CompanyID index;
00057 while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
00058 _ai_saveload_version = -1;
00059 SlObject(NULL, _ai_company);
00060
00061 if (_networking && !_network_server) {
00062 if (IsValidCompanyID(index) && !IsHumanCompany(index)) AIInstance::LoadEmpty();
00063 continue;
00064 }
00065
00066 AIConfig *config = AIConfig::GetConfig(index);
00067 if (StrEmpty(_ai_saveload_name)) {
00068
00069 config->ChangeAI(NULL);
00070 } else {
00071 config->ChangeAI(_ai_saveload_name, _ai_saveload_version);
00072 if (!config->HasAI()) {
00073 if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
00074 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);
00075 DEBUG(ai, 0, "A random other AI will be loaded in its place.");
00076 } else {
00077 DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
00078 DEBUG(ai, 0, "A random available AI will be loaded now.");
00079 }
00080
00081
00082 _ai_saveload_version = -1;
00083 }
00084 }
00085
00086 config->StringToSettings(_ai_saveload_settings);
00087
00088
00089 if (IsValidCompanyID(index) && !IsHumanCompany(index)) {
00090 AI::StartNew(index);
00091 AI::Load(index, _ai_saveload_version);
00092 }
00093 }
00094 }
00095
00096 static void Save_AIPL()
00097 {
00098 for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00099 SlSetArrayIndex(i);
00100 SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
00101 }
00102 }
00103
00104 extern const ChunkHandler _ai_chunk_handlers[] = {
00105 { 'AIPL', Save_AIPL, Load_AIPL, CH_ARRAY | CH_LAST},
00106 };