newgrf_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 "../core/bitmath_func.hpp"
00014 #include "../fios.h"
00015 
00016 #include "saveload.h"
00017 #include "newgrf_sl.h"
00018 
00020 static const SaveLoad _newgrf_mapping_desc[] = {
00021   SLE_VAR(EntityIDMapping, grfid,         SLE_UINT32),
00022   SLE_VAR(EntityIDMapping, entity_id,     SLE_UINT8),
00023   SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
00024   SLE_END()
00025 };
00026 
00031 void Save_NewGRFMapping(const OverrideManagerBase &mapping)
00032 {
00033   for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
00034     SlSetArrayIndex(i);
00035     SlObject(&mapping.mapping_ID[i], _newgrf_mapping_desc);
00036   }
00037 }
00038 
00043 void Load_NewGRFMapping(OverrideManagerBase &mapping)
00044 {
00045   /* Clear the current mapping stored.
00046    * This will create the manager if ever it is not yet done */
00047   mapping.ResetMapping();
00048 
00049   uint max_id = mapping.GetMaxMapping();
00050 
00051   int index;
00052   while ((index = SlIterateArray()) != -1) {
00053     if ((uint)index >= max_id) break;
00054     SlObject(&mapping.mapping_ID[index], _newgrf_mapping_desc);
00055   }
00056 }
00057 
00058 
00059 static const SaveLoad _grfconfig_desc[] = {
00060       SLE_STR(GRFConfig, filename,         SLE_STR,    0x40),
00061       SLE_VAR(GRFConfig, ident.grfid,      SLE_UINT32),
00062       SLE_ARR(GRFConfig, ident.md5sum,     SLE_UINT8,  16),
00063   SLE_CONDVAR(GRFConfig, version,          SLE_UINT32, 151, BEFORE_ADDING_PATCHES_SL),
00064   SLE_CONDVAR(GRFConfig, version,          SLE_UINT32, KEEP_COMPATIBLE_7_SV, SL_MAX_VERSION),
00065       SLE_ARR(GRFConfig, param,            SLE_UINT32, 0x80),
00066       SLE_VAR(GRFConfig, num_params,       SLE_UINT8),
00067   SLE_CONDVAR(GRFConfig, palette,          SLE_UINT8,  101, SL_MAX_VERSION),
00068   SLE_END()
00069 };
00070 
00071 
00072 static void Save_NGRF()
00073 {
00074   int index = 0;
00075 
00076   for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00077     if (HasBit(c->flags, GCF_STATIC)) continue;
00078     SlSetArrayIndex(index++);
00079     SlObject(c, _grfconfig_desc);
00080   }
00081 }
00082 
00083 
00084 static void Load_NGRF_common(GRFConfig *&grfconfig)
00085 {
00086   ClearGRFConfigList(&grfconfig);
00087   while (SlIterateArray() != -1) {
00088     GRFConfig *c = new GRFConfig();
00089     SlObject(c, _grfconfig_desc);
00090     if (IsSavegameVersionBefore(101)) c->SetSuitablePalette();
00091     AppendToGRFConfigList(&grfconfig, c);
00092   }
00093 }
00094 
00095 static void Load_NGRF()
00096 {
00097   Load_NGRF_common(_grfconfig);
00098 
00099   /* Append static NewGRF configuration */
00100   AppendStaticGRFConfigs(&_grfconfig);
00101 }
00102 
00103 static void Check_NGRF()
00104 {
00105   Load_NGRF_common(_load_check_data.grfconfig);
00106 }
00107 
00108 extern const ChunkHandler _newgrf_chunk_handlers[] = {
00109   { 'NGRF', Save_NGRF, Load_NGRF, NULL, Check_NGRF, CH_ARRAY | CH_LAST }
00110 };