OpenTTD
industry_sl.cpp
Go to the documentation of this file.
1 /* $Id: industry_sl.cpp 27929 2017-10-25 15:38:19Z 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 "../industry.h"
14 
15 #include "saveload.h"
16 #include "newgrf_sl.h"
17 
18 #include "../safeguards.h"
19 
20 static OldPersistentStorage _old_ind_persistent_storage;
21 
22 static const SaveLoad _industry_desc[] = {
23  SLE_CONDVAR(Industry, location.tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
24  SLE_CONDVAR(Industry, location.tile, SLE_UINT32, 6, SL_MAX_VERSION),
25  SLE_VAR(Industry, location.w, SLE_FILE_U8 | SLE_VAR_U16),
26  SLE_VAR(Industry, location.h, SLE_FILE_U8 | SLE_VAR_U16),
27  SLE_REF(Industry, town, REF_TOWN),
28  SLE_CONDNULL( 2, 0, 60),
29  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 2, 78, SL_MAX_VERSION),
30  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 3, 70, SL_MAX_VERSION),
31  SLE_ARR(Industry, produced_cargo_waiting, SLE_UINT16, 2),
32  SLE_ARR(Industry, production_rate, SLE_UINT8, 2),
33  SLE_CONDNULL( 3, 0, 60),
34  SLE_CONDARR(Industry, accepts_cargo, SLE_UINT8, 3, 78, SL_MAX_VERSION),
35  SLE_VAR(Industry, prod_level, SLE_UINT8),
36  SLE_ARR(Industry, this_month_production, SLE_UINT16, 2),
37  SLE_ARR(Industry, this_month_transported, SLE_UINT16, 2),
38  SLE_ARR(Industry, last_month_pct_transported, SLE_UINT8, 2),
39  SLE_ARR(Industry, last_month_production, SLE_UINT16, 2),
40  SLE_ARR(Industry, last_month_transported, SLE_UINT16, 2),
41 
42  SLE_VAR(Industry, counter, SLE_UINT16),
43 
44  SLE_VAR(Industry, type, SLE_UINT8),
45  SLE_VAR(Industry, owner, SLE_UINT8),
46  SLE_VAR(Industry, random_colour, SLE_UINT8),
47  SLE_CONDVAR(Industry, last_prod_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
48  SLE_CONDVAR(Industry, last_prod_year, SLE_INT32, 31, SL_MAX_VERSION),
49  SLE_VAR(Industry, was_cargo_delivered, SLE_UINT8),
50 
51  SLE_CONDVAR(Industry, founder, SLE_UINT8, 70, SL_MAX_VERSION),
52  SLE_CONDVAR(Industry, construction_date, SLE_INT32, 70, SL_MAX_VERSION),
53  SLE_CONDVAR(Industry, construction_type, SLE_UINT8, 70, SL_MAX_VERSION),
54  SLE_CONDVAR(Industry, last_cargo_accepted_at, SLE_INT32, 70, SL_MAX_VERSION),
55  SLE_CONDVAR(Industry, selected_layout, SLE_UINT8, 73, SL_MAX_VERSION),
56 
57  SLEG_CONDARR(_old_ind_persistent_storage.storage, SLE_UINT32, 16, 76, 160),
59 
60  SLE_CONDNULL(1, 82, 196), // random_triggers
61  SLE_CONDVAR(Industry, random, SLE_UINT16, 82, SL_MAX_VERSION),
62 
63  SLE_CONDNULL(32, 2, 143), // old reserved space
64 
65  SLE_END()
66 };
67 
68 static void Save_INDY()
69 {
70  Industry *ind;
71 
72  /* Write the industries */
73  FOR_ALL_INDUSTRIES(ind) {
74  SlSetArrayIndex(ind->index);
75  SlObject(ind, _industry_desc);
76  }
77 }
78 
79 static void Save_IIDS()
80 {
81  Save_NewGRFMapping(_industry_mngr);
82 }
83 
84 static void Save_TIDS()
85 {
86  Save_NewGRFMapping(_industile_mngr);
87 }
88 
89 static void Load_INDY()
90 {
91  int index;
92 
94 
95  while ((index = SlIterateArray()) != -1) {
96  Industry *i = new (index) Industry();
97  SlObject(i, _industry_desc);
98 
99  /* Before savegame version 161, persistent storages were not stored in a pool. */
101  /* Store the old persistent storage. The GRFID will be added later. */
103  i->psa = new PersistentStorage(0, 0, 0);
104  memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(i->psa->storage));
105  }
107  }
108 }
109 
110 static void Load_IIDS()
111 {
112  Load_NewGRFMapping(_industry_mngr);
113 }
114 
115 static void Load_TIDS()
116 {
117  Load_NewGRFMapping(_industile_mngr);
118 }
119 
120 static void Ptrs_INDY()
121 {
122  Industry *i;
123 
124  FOR_ALL_INDUSTRIES(i) {
125  SlObject(i, _industry_desc);
126  }
127 }
128 
132  SLEG_END()
133 };
134 
136 static void LoadSave_IBLD()
137 {
138  SlGlobList(_industry_builder_desc);
139 }
140 
143  SLE_VAR(IndustryTypeBuildData, probability, SLE_UINT32),
144  SLE_VAR(IndustryTypeBuildData, min_number, SLE_UINT8),
145  SLE_VAR(IndustryTypeBuildData, target_count, SLE_UINT16),
146  SLE_VAR(IndustryTypeBuildData, max_wait, SLE_UINT16),
147  SLE_VAR(IndustryTypeBuildData, wait_count, SLE_UINT16),
148  SLE_END()
149 };
150 
152 static void Save_ITBL()
153 {
154  for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
155  SlSetArrayIndex(i);
156  SlObject(_industry_builder.builddata + i, _industrytype_builder_desc);
157  }
158 }
159 
161 static void Load_ITBL()
162 {
163  for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
165  }
166  int index;
167  while ((index = SlIterateArray()) != -1) {
168  if ((uint)index >= NUM_INDUSTRYTYPES) SlErrorCorrupt("Too many industry builder datas");
169  SlObject(_industry_builder.builddata + index, _industrytype_builder_desc);
170  }
171 }
172 
173 extern const ChunkHandler _industry_chunk_handlers[] = {
174  { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, NULL, CH_ARRAY},
175  { 'IIDS', Save_IIDS, Load_IIDS, NULL, NULL, CH_ARRAY},
176  { 'TIDS', Save_TIDS, Load_TIDS, NULL, NULL, CH_ARRAY},
177  { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, NULL, NULL, CH_RIFF},
178  { 'ITBL', Save_ITBL, Load_ITBL, NULL, NULL, CH_ARRAY | CH_LAST},
179 };
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:136
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
Code handling saving and loading of NewGRF mappings.
IndustryBuildData _industry_builder
In-game manager of industries.
#define SLEG_END()
End marker of global variables save or load.
Definition: saveload.h:457
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:256
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:561
static void Load_ITBL()
Load industry-type build data.
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:304
#define SLE_ARR(base, variable, type, length)
Storage of an array in every version of a savegame.
Definition: saveload.h:313
Defines the internal data of a functional industry.
Definition: industry.h:39
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Load/save a reference to a town.
Definition: saveload.h:84
static void Save_ITBL()
Save industry-type build data.
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:418
#define SLEG_CONDARR(variable, type, length, from, to)
Storage of a global array in some savegame versions.
Definition: saveload.h:392
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
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:71
IndustryType type
type of industry.
Definition: industry.h:55
TYPE storage[SIZE]
Memory to for the storage array.
Class for pooled persistent storage of data.
static void LoadSave_IBLD()
Load/save industry builder.
#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
Data for managing the number of industries of a single industry type.
Definition: industry.h:155
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
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:107
void Reset()
Reset the entry.
static const SaveLoad _industrytype_builder_desc[]
Description of the data to save and load in IndustryTypeBuildData.
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
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of an array in some savegame versions.
Definition: saveload.h:267
static const IndustryType NUM_INDUSTRYTYPES
total number of industry types, new and old; limited to 240 because we need some special ids like INV...
Definition: industry_type.h:28
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
static const SaveLoad _industry_builder_desc[]
Description of the data to save and load in IndustryBuildData.
uint32 wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:172
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1630
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
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:171
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
Load/save a reference to a persistent storage.
Definition: saveload.h:90