ai_sl.cpp

Go to the documentation of this file.
00001 /* $Id: ai_sl.cpp 16704 2009-06-30 20:11:36Z rubidium $ */
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     /* No AI is configured for this so store an empty string as name. */
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   /* If the AI was active, store his data too */
00046   if (IsValidCompanyID(index) && !IsHumanCompany(index)) AI::Save(index);
00047 }
00048 
00049 static void Load_AIPL()
00050 {
00051   /* Free all current data */
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       /* A random AI. */
00069       config->ChangeAI(NULL);
00070     } else {
00071       config->ChangeAI(_ai_saveload_name, _ai_saveload_version);
00072       if (!config->HasAI()) {
00073         /* No version of the AI available that can load the data. Try to load the
00074          * latest version of the AI instead. */
00075         config->ChangeAI(_ai_saveload_name, -1);
00076         if (!config->HasAI()) {
00077           if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
00078             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);
00079             DEBUG(ai, 0, "A random other AI will be loaded in its place.");
00080           } else {
00081             DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
00082             DEBUG(ai, 0, "A random available AI will be loaded now.");
00083           }
00084         } else {
00085           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);
00086           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.");
00087         }
00088         /* Make sure the AI doesn't get the saveload data, as he was not the
00089          *  writer of the saveload data in the first place */
00090         _ai_saveload_version = -1;
00091       }
00092     }
00093 
00094     config->StringToSettings(_ai_saveload_settings);
00095 
00096     /* Start the AI directly if it was active in the savegame */
00097     if (IsValidCompanyID(index) && !IsHumanCompany(index)) {
00098       AI::StartNew(index);
00099       AI::Load(index, _ai_saveload_version);
00100     }
00101   }
00102 }
00103 
00104 static void Save_AIPL()
00105 {
00106   for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
00107     SlSetArrayIndex(i);
00108     SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
00109   }
00110 }
00111 
00112 extern const ChunkHandler _ai_chunk_handlers[] = {
00113   { 'AIPL', Save_AIPL, Load_AIPL, CH_ARRAY | CH_LAST},
00114 };

Generated on Tue Dec 1 00:06:18 2009 for OpenTTD by  doxygen 1.5.6