# ai_sl.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 #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     /* No AI is configured for this so store an empty string as name. */
    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   /* If the AI was active, store his data too */
    00056   if (Company::IsValidAiID(index)) AI::Save(index);
    00057 }
    00058
    00059 static void Load_AIPL()
    00060 {
    00061   /* Free all current data */
    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_is_random = 0;
    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::SSS_FORCE_GAME);
    00080     if (StrEmpty(_ai_saveload_name)) {
    00081       /* A random AI. */
    00082       config->Change(NULL, -1, false, true);
    00083     } else {
    00084       config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
    00085       if (!config->HasScript()) {
    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->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
    00089         if (!config->HasScript()) {
    00090           if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
    00091             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);
    00092             DEBUG(script, 0, "A random other AI will be loaded in its place.");
    00093           } else {
    00094             DEBUG(script, 0, "The savegame had no AIs available at the time of saving.");
    00095             DEBUG(script, 0, "A random available AI will be loaded now.");
    00096           }
    00097         } else {
    00098           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);
    00099           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.");
    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 };