1 /* $Id: newgrf_sl.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file newgrf_sl.cpp Code handling saving and loading of newgrf config */
12 #include "../stdafx.h"
16 #include "newgrf_sl.h"
18 #include "../safeguards.h"
20 /** Save and load the mapping between a spec and the NewGRF it came from. */
21 static const SaveLoad _newgrf_mapping_desc
[] = {
22 SLE_VAR(EntityIDMapping
, grfid
, SLE_UINT32
),
23 SLE_VAR(EntityIDMapping
, entity_id
, SLE_UINT8
),
24 SLE_VAR(EntityIDMapping
, substitute_id
, SLE_UINT8
),
29 * Save a GRF ID + local id -> OpenTTD's id mapping.
30 * @param mapping The mapping to save.
32 void Save_NewGRFMapping(const OverrideManagerBase
&mapping
)
34 for (uint i
= 0; i
< mapping
.GetMaxMapping(); i
++) {
36 SlObject(&mapping
.mapping_ID
[i
], _newgrf_mapping_desc
);
41 * Load a GRF ID + local id -> OpenTTD's id mapping.
42 * @param mapping The mapping to load.
44 void Load_NewGRFMapping(OverrideManagerBase
&mapping
)
46 /* Clear the current mapping stored.
47 * This will create the manager if ever it is not yet done */
48 mapping
.ResetMapping();
50 uint max_id
= mapping
.GetMaxMapping();
53 while ((index
= SlIterateArray()) != -1) {
54 if ((uint
)index
>= max_id
) SlErrorCorrupt("Too many NewGRF entity mappings");
55 SlObject(&mapping
.mapping_ID
[index
], _newgrf_mapping_desc
);
60 static const SaveLoad _grfconfig_desc
[] = {
61 SLE_STR(GRFConfig
, filename
, SLE_STR
, 0x40),
62 SLE_VAR(GRFConfig
, ident
.grfid
, SLE_UINT32
),
63 SLE_ARR(GRFConfig
, ident
.md5sum
, SLE_UINT8
, 16),
64 SLE_CONDVAR(GRFConfig
, version
, SLE_UINT32
, 151, SL_MAX_VERSION
),
65 SLE_ARR(GRFConfig
, param
, SLE_UINT32
, 0x80),
66 SLE_VAR(GRFConfig
, num_params
, SLE_UINT8
),
67 SLE_CONDVAR(GRFConfig
, palette
, SLE_UINT8
, 101, SL_MAX_VERSION
),
72 static void Save_NGRF()
76 for (GRFConfig
*c
= _grfconfig
; c
!= nullptr; c
= c
->next
) {
77 if (HasBit(c
->flags
, GCF_STATIC
)) continue;
78 SlSetArrayIndex(index
++);
79 SlObject(c
, _grfconfig_desc
);
84 static void Load_NGRF_common(GRFConfig
*&grfconfig
)
86 ClearGRFConfigList(&grfconfig
);
87 while (SlIterateArray() != -1) {
88 GRFConfig
*c
= new GRFConfig();
89 SlObject(c
, _grfconfig_desc
);
90 if (IsSavegameVersionBefore(101)) c
->SetSuitablePalette();
91 AppendToGRFConfigList(&grfconfig
, c
);
95 static void Load_NGRF()
97 Load_NGRF_common(_grfconfig
);
99 if (_game_mode
== GM_MENU
) {
100 /* Intro game must not have NewGRF. */
101 if (_grfconfig
!= nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
103 /* Activate intro NewGRFs (townnames) */
104 ResetGRFConfig(false);
106 /* Append static NewGRF configuration */
107 AppendStaticGRFConfigs(&_grfconfig
);
111 static void Check_NGRF()
113 Load_NGRF_common(_load_check_data
.grfconfig
);
116 extern const ChunkHandler _newgrf_chunk_handlers
[] = {
117 { 'NGRF', Save_NGRF
, Load_NGRF
, nullptr, Check_NGRF
, CH_ARRAY
| CH_LAST
}