ai_sl.cpp

Go to the documentation of this file.
00001 /* $Id: ai_sl.cpp 22884 2011-09-03 18:50:20Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
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 static char _ai_saveload_name[64];
00019 static int  _ai_saveload_version;
00020 static char _ai_saveload_settings[1024];
00021 static bool _ai_saveload_is_random;
00022 
00023 static const SaveLoad _ai_company[] = {
00024       SLEG_STR(_ai_saveload_name,        SLE_STRB),
00025       SLEG_STR(_ai_saveload_settings,    SLE_STRB),
00026   SLEG_CONDVAR(_ai_saveload_version,   SLE_UINT32, 108, SL_MAX_VERSION),
00027   SLEG_CONDVAR(_ai_saveload_is_random,   SLE_BOOL, 136, SL_MAX_VERSION),
00028        SLE_END()
00029 };
00030 
00031 #ifdef ENABLE_AI
00032 #include "../ai/ai.hpp"
00033 #include "../ai/ai_config.hpp"
00034 #include "../network/network.h"
00035 #include "../ai/ai_instance.hpp"
00036 
00037 static void SaveReal_AIPL(int *index_ptr)
00038 {
00039   CompanyID index = (CompanyID)*index_ptr;
00040   AIConfig *config = AIConfig::GetConfig(index);
00041 
00042   if (config->HasAI()) {
00043     ttd_strlcpy(_ai_saveload_name, config->GetName(), lengthof(_ai_saveload_name));
00044     _ai_saveload_version = config->GetVersion();
00045   } else {
00046     /* No AI is configured for this so store an empty string as name. */
00047     _ai_saveload_name[0] = '\0';
00048     _ai_saveload_version = -1;
00049   }
00050 
00051   _ai_saveload_is_random = config->IsRandomAI();
00052   _ai_saveload_settings[0] = '\0';
00053   config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
00054 
00055   SlObject(NULL, _ai_company);
00056   /* If the AI was active, store his data too */
00057   if (Company::IsValidAiID(index)) AI::Save(index);
00058 }
00059 
00060 static void Load_AIPL()
00061 {
00062   /* Free all current data */
00063   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
00064     AIConfig::GetConfig(c, AIConfig::AISS_FORCE_GAME)->ChangeAI(NULL);
00065   }
00066 
00067   CompanyID index;
00068   while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
00069     if (index >= MAX_COMPANIES) SlErrorCorrupt("Too many AI configs");
00070 
00071     _ai_saveload_version = -1;
00072     SlObject(NULL, _ai_company);
00073 
00074     if (_networking && !_network_server) {
00075       if (Company::IsValidAiID(index)) AIInstance::LoadEmpty();
00076       continue;
00077     }
00078 
00079     AIConfig *config = AIConfig::GetConfig(index, AIConfig::AISS_FORCE_GAME);
00080     if (StrEmpty(_ai_saveload_name)) {
00081       /* A random AI. */
00082       config->ChangeAI(NULL, -1, false, true);
00083     } else {
00084       config->ChangeAI(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
00085       if (!config->HasAI()) {
00086         /* No version of the AI available that can load the data. Try to load the
00087          * latest version of the AI instead. */
00088         config->ChangeAI(_ai_saveload_name, -1, false, _ai_saveload_is_random);
00089         if (!config->HasAI()) {
00090           if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
00091             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);
00092             DEBUG(ai, 0, "A random other AI will be loaded in its place.");
00093           } else {
00094             DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
00095             DEBUG(ai, 0, "A random available AI will be loaded now.");
00096           }
00097         } else {
00098           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);
00099           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.");
00100         }
00101         /* Make sure the AI doesn't get the saveload data, as he was not the
00102          *  writer of the saveload data in the first place */
00103         _ai_saveload_version = -1;
00104       }
00105     }
00106 
00107     config->StringToSettings(_ai_saveload_settings);
00108 
00109     /* Start the AI directly if it was active in the savegame */
00110     if (Company::IsValidAiID(index)) {
00111       AI::StartNew(index, false);
00112       AI::Load(index, _ai_saveload_version);
00113     }
00114   }
00115 }
00116 
00117 static void Save_AIPL()
00118 {
00119   for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00120     SlSetArrayIndex(i);
00121     SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
00122   }
00123 }
00124 
00125 extern const ChunkHandler _ai_chunk_handlers[] = {
00126   { 'AIPL', Save_AIPL, Load_AIPL, NULL, NULL, CH_ARRAY | CH_LAST},
00127 };
00128 #else
00129 
00131 enum SQSaveLoadType {
00132   SQSL_INT             = 0x00, 
00133   SQSL_STRING          = 0x01, 
00134   SQSL_ARRAY           = 0x02, 
00135   SQSL_TABLE           = 0x03, 
00136   SQSL_BOOL            = 0x04, 
00137   SQSL_NULL            = 0x05, 
00138   SQSL_ARRAY_TABLE_END = 0xFF, 
00139 };
00140 
00141 static byte _ai_sl_byte;
00142 
00143 static const SaveLoad _ai_byte[] = {
00144   SLEG_VAR(_ai_sl_byte, SLE_UINT8),
00145   SLE_END()
00146 };
00147 
00148 static bool LoadObjects()
00149 {
00150   SlObject(NULL, _ai_byte);
00151   switch (_ai_sl_byte) {
00152     case SQSL_INT: {
00153       int value;
00154       SlArray(&value, 1, SLE_INT32);
00155       return true;
00156     }
00157 
00158     case SQSL_STRING: {
00159       SlObject(NULL, _ai_byte);
00160       static char buf[256];
00161       SlArray(buf, _ai_sl_byte, SLE_CHAR);
00162       return true;
00163     }
00164 
00165     case SQSL_ARRAY:
00166       while (LoadObjects()) { }
00167       return true;
00168 
00169     case SQSL_TABLE:
00170       while (LoadObjects()) { LoadObjects(); }
00171       return true;
00172 
00173     case SQSL_BOOL:
00174       SlObject(NULL, _ai_byte);
00175       return true;
00176 
00177     case SQSL_NULL:
00178       return true;
00179 
00180     case SQSL_ARRAY_TABLE_END:
00181       return false;
00182 
00183     default: SlErrorCorrupt("Invalid AI data type");
00184   }
00185 }
00186 
00187 static void Load_AIPL()
00188 {
00189   CompanyID index;
00190   while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
00191     SlObject(NULL, _ai_company);
00192 
00193     if (!Company::IsValidAiID(index)) continue;
00194     SlObject(NULL, _ai_byte);
00195     /* Check if there was anything saved at all. */
00196     if (_ai_sl_byte == 0) continue;
00197     LoadObjects();
00198   }
00199 }
00200 
00201 extern const ChunkHandler _ai_chunk_handlers[] = {
00202   { 'AIPL', NULL, Load_AIPL, NULL, NULL, CH_ARRAY | CH_LAST},
00203 };
00204 #endif /* ENABLE_AI */