# ai_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_sl.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    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
    00014 static char _ai_saveload_name[64];
    00015 static int  _ai_saveload_version;
    00016 static char _ai_saveload_settings[1024];
    00017
    00018 static const SaveLoad _ai_company[] = {
    00019   SLEG_STR(_ai_saveload_name,        SLE_STRB),
    00020   SLEG_STR(_ai_saveload_settings,    SLE_STRB),
    00021   SLEG_CONDVAR(_ai_saveload_version, SLE_UINT32, 108, SL_MAX_VERSION),
    00022   SLE_END()
    00023 };
    00024
    00025 static void SaveReal_AIPL(int *index_ptr)
    00026 {
    00027   CompanyID index = (CompanyID)*index_ptr;
    00028   AIConfig *config = AIConfig::GetConfig(index);
    00029
    00030   if (config->HasAI()) {
    00031     ttd_strlcpy(_ai_saveload_name, config->GetName(), lengthof(_ai_saveload_name));
    00032     _ai_saveload_version = config->GetVersion();
    00033   } else {
    00034     /* No AI is configured for this so store an empty string as name. */
    00035     _ai_saveload_name[0] = '\0';
    00036     _ai_saveload_version = -1;
    00037   }
    00038
    00039   _ai_saveload_settings[0] = '\0';
    00040   config->SettingsToString(_ai_saveload_settings, lengthof(_ai_saveload_settings));
    00041
    00042   SlObject(NULL, _ai_company);
    00043   /* If the AI was active, store his data too */
    00044   if (IsValidCompanyID(index) && !IsHumanCompany(index)) AI::Save(index);
    00045 }
    00046
    00047 static void Load_AIPL()
    00048 {
    00049   /* Free all current data */
    00050   for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
    00051     AIConfig::GetConfig(c)->ChangeAI(NULL);
    00052   }
    00053
    00054   CompanyID index;
    00055   while ((index = (CompanyID)SlIterateArray()) != (CompanyID)-1) {
    00056     AIConfig *config = AIConfig::GetConfig(index);
    00057
    00058     _ai_saveload_version = -1;
    00059     SlObject(NULL, _ai_company);
    00060
    00061     if (StrEmpty(_ai_saveload_name)) {
    00062       /* A random AI. */
    00063       config->ChangeAI(NULL);
    00064     } else {
    00065       config->ChangeAI(_ai_saveload_name, _ai_saveload_version);
    00066       if (!config->HasAI()) {
    00067         if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
    00068           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);
    00069           DEBUG(ai, 0, "A random other AI will be loaded in its place.");
    00070         } else {
    00071           DEBUG(ai, 0, "The savegame had no AIs available at the time of saving.");
    00072           DEBUG(ai, 0, "A random available AI will be loaded now.");
    00073         }
    00074         /* Make sure the AI doesn't get the saveload data, as he was not the
    00075          *  writer of the saveload data in the first place */
    00076         _ai_saveload_version = -1;
    00077       }
    00078     }
    00079
    00080     config->StringToSettings(_ai_saveload_settings);
    00081
    00082     /* Start the AI directly if it was active in the savegame */
    00083     if (IsValidCompanyID(index) && !IsHumanCompany(index)) {
    00084       AI::StartNew(index);
    00085       AI::Load(index, _ai_saveload_version);
    00086     }
    00087   }
    00088 }
    00089
    00090 static void Save_AIPL()
    00091 {
    00092   for (int i = COMPANY_FIRST; i < MAX_COMPANIES; i++) {
    00093     SlSetArrayIndex(i);
    00094     SlAutolength((AutolengthProc *)SaveReal_AIPL, &i);
    00095   }
    00096 }
    00097
    00098 extern const ChunkHandler _ai_chunk_handlers[] = {
    00099   { 'AIPL', Save_AIPL, Load_AIPL, CH_ARRAY | CH_LAST},
    00100 };