OpenTTD Source: src/saveload/newgrf_sl.cpp Source File

OpenTTD Source 1.11.0-RC1

newgrf_sl.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../fios.h"
12
13 #include "saveload.h"
14 #include "newgrf_sl.h"
15
16 #include "../safeguards.h"
17
19 static const SaveLoad _newgrf_mapping_desc[] = {
20 SLE_VAR(EntityIDMapping, grfid, SLE_UINT32),
21 SLE_VAR(EntityIDMapping, entity_id, SLE_UINT8),
22 SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
23 SLE_END()
24 };
25
30 void Save_NewGRFMapping(const OverrideManagerBase &mapping)
31 {
32 for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
33 SlSetArrayIndex(i);
34 SlObject(&mapping.mapping_ID[i], _newgrf_mapping_desc);
35 }
36 }
37
42 void Load_NewGRFMapping(OverrideManagerBase &mapping)
43 {
44 /* Clear the current mapping stored.
45 * This will create the manager if ever it is not yet done */
46 mapping.ResetMapping();
47
48 uint max_id = mapping.GetMaxMapping();
49
50 int index;
51 while ((index = SlIterateArray()) != -1) {
52 if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
53 SlObject(&mapping.mapping_ID[index], _newgrf_mapping_desc);
54 }
55 }
56
57
58 static const SaveLoad _grfconfig_desc[] = {
59 SLE_STR(GRFConfig, filename, SLE_STR, 0x40),
60 SLE_VAR(GRFConfig, ident.grfid, SLE_UINT32),
61 SLE_ARR(GRFConfig, ident.md5sum, SLE_UINT8, 16),
62 SLE_CONDVAR(GRFConfig, version, SLE_UINT32, SLV_151, SL_MAX_VERSION),
63 SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80),
64 SLE_VAR(GRFConfig, num_params, SLE_UINT8),
65 SLE_CONDVAR(GRFConfig, palette, SLE_UINT8, SLV_101, SL_MAX_VERSION),
66 SLE_END()
67 };
68
69
70 static void Save_NGRF()
71 {
72 int index = 0;
73
74 for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
75 if (HasBit(c->flags, GCF_STATIC)) continue;
76 SlSetArrayIndex(index++);
77 SlObject(c, _grfconfig_desc);
78 }
79 }
80
81
82 static void Load_NGRF_common(GRFConfig *&grfconfig)
83 {
84 ClearGRFConfigList(&grfconfig);
85 while (SlIterateArray() != -1) {
86 GRFConfig *c = new GRFConfig();
87 SlObject(c, _grfconfig_desc);
88 if (IsSavegameVersionBefore(SLV_101)) c->SetSuitablePalette();
89 AppendToGRFConfigList(&grfconfig, c);
90 }
91 }
92
93 static void Load_NGRF()
94 {
95 Load_NGRF_common(_grfconfig);
96
97 if (_game_mode == GM_MENU) {
98 /* Intro game must not have NewGRF. */
99 if (_grfconfig != nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
100
101 /* Activate intro NewGRFs (townnames) */
102 ResetGRFConfig(false);
103 } else {
104 /* Append static NewGRF configuration */
105 AppendStaticGRFConfigs(&_grfconfig);
106 }
107 }
108
109 static void Check_NGRF()
110 {
111 Load_NGRF_common(_load_check_data.grfconfig);
112 }
113
114 extern const ChunkHandler _newgrf_chunk_handlers[] = {
115 { 'NGRF', Save_NGRF, Load_NGRF, nullptr, Check_NGRF, CH_ARRAY | CH_LAST }
116 };
GRFConfig::SetSuitablePalette
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
Definition: newgrf_config.cpp:147
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:401
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:647
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
LoadCheckData::grfconfig
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:43
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SLE_ARR
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:638
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:152
OverrideManagerBase::ResetMapping
void ResetMapping()
Resets the mapping, which is used while initializing game.
Definition: newgrf_commons.cpp:82
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
Load_NewGRFMapping
void Load_NewGRFMapping(OverrideManagerBase &mapping)
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:42
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
EntityIDMapping
Maps an entity id stored on the map to a GRF file.
Definition: newgrf_commons.h:186
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
Definition: newgrf_config.cpp:471
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:177
_newgrf_mapping_desc
static const SaveLoad _newgrf_mapping_desc[]
Save and load the mapping between a spec and the NewGRF it came from.
Definition: newgrf_sl.cpp:19
OverrideManagerBase::mapping_ID
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
Definition: newgrf_commons.h:204
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:170
OverrideManagerBase
Definition: newgrf_commons.h:192
SLV_101
@ SLV_101
101 14233
Definition: saveload.h:164
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition: newgrf_config.cpp:496
SLV_151
@ SLV_151
151 20918
Definition: saveload.h:224
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
newgrf_sl.h
AppendToGRFConfigList
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
Definition: newgrf_config.cpp:485
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
GCF_STATIC
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:25
Save_NewGRFMapping
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:30