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 
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 
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);
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 */
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 };
Code handling saving and loading of NewGRF mappings.
GRFConfig * _grfconfig
First item in list of current GRF set up.
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:26
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:38
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:313
struct GRFConfig * next
NOSAVE: Next item in the linked list.
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:39
Functions/types related to saving and loading games.
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
Information about GRF, used in the game and (part of it) in savegames.
Maps an entity id stored on the map to a GRF file.
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
Handlers and description of chunk.
Definition: saveload.h:66
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
void Load_NewGRFMapping(OverrideManagerBase &mapping)
Load a GRF ID + local id -> OpenTTD&#39;s id mapping.
Definition: newgrf_sl.cpp:44
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
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
void ResetMapping()
Resets the mapping, which is used while initializing game.
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
SaveLoad type struct.
Definition: saveload.h:208
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:322
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD&#39;s id mapping.
Definition: newgrf_sl.cpp:32
Last chunk in this array.
Definition: saveload.h:104