# game_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 "../debug.h"
    00014 #include "saveload.h"
    00015 #include "../string_func.h"
    00016
    00017 #include "../game/game.hpp"
    00018 #include "../game/game_config.hpp"
    00019 #include "../network/network.h"
    00020 #include "../game/game_instance.hpp"
    00021 #include "../game/game_text.hpp"
    00022
    00023 static char _game_saveload_name[64];
    00024 static int  _game_saveload_version;
    00025 static char _game_saveload_settings[1024];
    00026 static bool _game_saveload_is_random;
    00027
    00028 static const SaveLoad _game_script[] = {
    00029       SLEG_STR(_game_saveload_name,        SLE_STRB),
    00030       SLEG_STR(_game_saveload_settings,    SLE_STRB),
    00031       SLEG_VAR(_game_saveload_version,   SLE_UINT32),
    00032       SLEG_VAR(_game_saveload_is_random,   SLE_BOOL),
    00033        SLE_END()
    00034 };
    00035
    00036 static void SaveReal_GSDT(int *index_ptr)
    00037 {
    00038   GameConfig *config = GameConfig::GetConfig();
    00039
    00040   if (config->HasScript()) {
    00041     ttd_strlcpy(_game_saveload_name, config->GetName(), lengthof(_game_saveload_name));
    00042     _game_saveload_version = config->GetVersion();
    00043   } else {
    00044     /* No GameScript is configured for this so store an empty string as name. */
    00045     _game_saveload_name[0] = '\0';
    00046     _game_saveload_version = -1;
    00047   }
    00048
    00049   _game_saveload_is_random = config->IsRandom();
    00050   _game_saveload_settings[0] = '\0';
    00051   config->SettingsToString(_game_saveload_settings, lengthof(_game_saveload_settings));
    00052
    00053   SlObject(NULL, _game_script);
    00054   Game::Save();
    00055 }
    00056
    00057 static void Load_GSDT()
    00058 {
    00059   /* Free all current data */
    00060   GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(NULL);
    00061
    00062   if ((CompanyID)SlIterateArray() == (CompanyID)-1) return;
    00063
    00064   _game_saveload_version = -1;
    00065   SlObject(NULL, _game_script);
    00066
    00067   if (_networking && !_network_server) {
    00068     GameInstance::LoadEmpty();
    00069     if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
    00070     return;
    00071   }
    00072
    00073   GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
    00074   if (StrEmpty(_game_saveload_name)) {
    00075   } else {
    00076     config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
    00077     if (!config->HasScript()) {
    00078       /* No version of the GameScript available that can load the data. Try to load the
    00079         * latest version of the GameScript instead. */
    00080       config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
    00081       if (!config->HasScript()) {
    00082         if (strcmp(_game_saveload_name, "%_dummy") != 0) {
    00083           DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
    00084           DEBUG(script, 0, "This game wil continue to run without GameScript.");
    00085         } else {
    00086           DEBUG(script, 0, "The savegame had no GameScript available at the time of saving.");
    00087           DEBUG(script, 0, "This game wil continue to run without GameScript.");
    00088         }
    00089       } else {
    00090         DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
    00091         DEBUG(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible.");
    00092       }
    00093       /* Make sure the GameScript doesn't get the saveload data, as he was not the
    00094         *  writer of the saveload data in the first place */
    00095       _game_saveload_version = -1;
    00096     }
    00097   }
    00098
    00099   config->StringToSettings(_game_saveload_settings);
    00100
    00101   /* Start the GameScript directly if it was active in the savegame */
    00102   Game::StartNew();
    00103   Game::Load(_game_saveload_version);
    00104
    00105   if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs");
    00106 }
    00107
    00108 static void Save_GSDT()
    00109 {
    00110   SlSetArrayIndex(0);
    00111   SlAutolength((AutolengthProc *)SaveReal_GSDT, NULL);
    00112 }
    00113
    00114 extern GameStrings *_current_data;
    00115
    00116 static const char *_game_saveload_string;
    00117 static uint _game_saveload_strings;
    00118
    00119 static const SaveLoad _game_language_header[] = {
    00120   SLEG_STR(_game_saveload_string, SLE_STR),
    00121   SLEG_VAR(_game_saveload_strings, SLE_UINT32),
    00122    SLE_END()
    00123 };
    00124
    00125 static const SaveLoad _game_language_string[] = {
    00126   SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
    00127    SLE_END()
    00128 };
    00129
    00130 static void SaveReal_GSTR(LanguageStrings *ls)
    00131 {
    00132   _game_saveload_string  = ls->language;
    00133   _game_saveload_strings = ls->lines.Length();
    00134
    00135   SlObject(NULL, _game_language_header);
    00136   for (uint i = 0; i < _game_saveload_strings; i++) {
    00137     _game_saveload_string = ls->lines[i];
    00138     SlObject(NULL, _game_language_string);
    00139   }
    00140 }
    00141
    00142 static void Load_GSTR()
    00143 {
    00144   delete _current_data;
    00145   _current_data = new GameStrings();
    00146
    00147   while (SlIterateArray() != -1) {
    00148     _game_saveload_string = NULL;
    00149     SlObject(NULL, _game_language_header);
    00150
    00151     LanguageStrings *ls = new LanguageStrings(_game_saveload_string);
    00152     for (uint i = 0; i < _game_saveload_strings; i++) {
    00153       SlObject(NULL, _game_language_string);
    00154       *ls->lines.Append() = strdup(_game_saveload_string != NULL ? _game_saveload_string : "");
    00155     }
    00156
    00157     *_current_data->raw_strings.Append() = ls;
    00158   }
    00159
    00160   /* If there were no strings in the savegame, set GameStrings to NULL */
    00161   if (_current_data->raw_strings.Length() == 0) {
    00162     delete _current_data;
    00163     _current_data = NULL;
    00164     return;
    00165   }
    00166
    00167   _current_data->Compile();
    00168   ReconsiderGameScriptLanguage();
    00169 }
    00170
    00171 static void Save_GSTR()
    00172 {
    00173   if (_current_data == NULL) return;
    00174
    00175   for (uint i = 0; i < _current_data->raw_strings.Length(); i++) {
    00176     SlSetArrayIndex(i);
    00177     SlAutolength((AutolengthProc *)SaveReal_GSTR, _current_data->raw_strings[i]);
    00178   }
    00179 }
    00180
    00181 extern const ChunkHandler _game_chunk_handlers[] = {
    00182   { 'GSTR', Save_GSTR, Load_GSTR, NULL, NULL, CH_ARRAY },
    00183   { 'GSDT', Save_GSDT, Load_GSDT, NULL, NULL, CH_ARRAY | CH_LAST},
    00184 };