ai_sl.cpp

Go to the documentation of this file.
00001 /* $Id: ai_sl.cpp 18763 2010-01-09 14:41:22Z rubidium $ */
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 #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     /* No AI is configured for this so store an empty string as name. */
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   /* If the AI was active, store his data too */
00055   if (Company::IsValidAiID(index)) AI::Save(index);
00056 }
00057 
00058 static void Load_AIPL()
00059 {
00060   /* Free all current data */
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       /* A random AI. */
00078       config->ChangeAI(NULL, -1, true);
00079     } else {
00080       config->ChangeAI(_ai_saveload_name, _ai_saveload_version, _ai_saveload_is_random);
00081       if (!config->HasAI()) {
00082         /* No version of the AI available that can load the data. Try to load the
00083          * latest version of the AI instead. */
00084         config->ChangeAI(_ai_saveload_name, -1, _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         /* Make sure the AI doesn't get the saveload data, as he was not the
00098          *  writer of the saveload data in the first place */
00099         _ai_saveload_version = -1;
00100       }
00101     }
00102 
00103     config->StringToSettings(_ai_saveload_settings);
00104 
00105     /* Start the AI directly if it was active in the savegame */
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 };

Generated on Wed Jan 20 23:38:39 2010 for OpenTTD by  doxygen 1.5.6