OpenTTD

newgrf_sl.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_sl.cpp 27772 2017-03-07 20:18:54Z frosch $ */
2
3 /*
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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../fios.h"
14
15 #include "saveload.h"
16 #include "newgrf_sl.h"
17
18 #include "../safeguards.h"
19
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),
25 SLE_END()
26 };
27
32 void Save_NewGRFMapping(const OverrideManagerBase &mapping)
33 {
34 for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
35 SlSetArrayIndex(i);
36 SlObject(&mapping.mapping_ID[i], _newgrf_mapping_desc);
37 }
38 }
39
44 void Load_NewGRFMapping(OverrideManagerBase &mapping)
45 {
46 /* Clear the current mapping stored.
47 * This will create the manager if ever it is not yet done */
48 mapping.ResetMapping();
49
50 uint max_id = mapping.GetMaxMapping();
51
52 int index;
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);
56 }
57 }
58
59
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),
68 SLE_END()
69 };
70
71
72 static void Save_NGRF()
73 {
74 int index = 0;
75
76 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
77 if (HasBit(c->flags, GCF_STATIC)) continue;
78 SlSetArrayIndex(index++);
79 SlObject(c, _grfconfig_desc);
80 }
81 }
82
83
84 static void Load_NGRF_common(GRFConfig *&grfconfig)
85 {
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);
92 }
93 }
94
95 static void Load_NGRF()
96 {
97 Load_NGRF_common(_grfconfig);
98
99 if (_game_mode == GM_MENU) {
100 /* Intro game must not have NewGRF. */
101 if (_grfconfig != NULL) SlErrorCorrupt("The intro game must not use NewGRF");
102
103 /* Activate intro NewGRFs (townnames) */
104 ResetGRFConfig(false);
105 } else {
106 /* Append static NewGRF configuration */
107 AppendStaticGRFConfigs(&_grfconfig);
108 }
109 }
110
111 static void Check_NGRF()
112 {
113 Load_NGRF_common(_load_check_data.grfconfig);
114 }
115
116 extern const ChunkHandler _newgrf_chunk_handlers[] = {
117 { 'NGRF', Save_NGRF, Load_NGRF, NULL, Check_NGRF, CH_ARRAY | CH_LAST }
118 };
newgrf_sl.h
Code handling saving and loading of NewGRF mappings.
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:192
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:444
GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:26
LoadCheckData::grfconfig
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:38
SLE_ARR
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:313
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:178
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:39
saveload.h
Functions/types related to saving and loading games.
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:153
EntityIDMapping
Maps an entity id stored on the map to a GRF file.
Definition: newgrf_commons.h:188
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
Definition: newgrf_config.cpp:514
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
AppendToGRFConfigList
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
Definition: newgrf_config.cpp:528
Load_NewGRFMapping
void Load_NewGRFMapping(OverrideManagerBase &mapping)
Load a GRF ID + local id -> OpenTTD&#39;s id mapping.
Definition: newgrf_sl.cpp:44
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
OverrideManagerBase::mapping_ID
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
Definition: newgrf_commons.h:206
_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:21
OverrideManagerBase::ResetMapping
void ResetMapping()
Resets the mapping, which is used while initializing game.
Definition: newgrf_commons.cpp:84
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition: newgrf_config.cpp:539
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
GRFConfig::SetSuitablePalette
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
Definition: newgrf_config.cpp:169
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:105
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
OverrideManagerBase
Definition: newgrf_commons.h:194
Save_NewGRFMapping
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD&#39;s id mapping.
Definition: newgrf_sl.cpp:32
CH_LAST
Last chunk in this array.
Definition: saveload.h:104