OpenTTD
industry_sl.cpp
Go to the documentation of this file.
1 /* $Id$ */
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, SL_MIN_VERSION, SLV_6),
24  SLE_CONDVAR(Industry, location.tile, SLE_UINT32, SLV_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),
30  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 2, SLV_78, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
31  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
32  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 3, SLV_70, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
33  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
34  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
35  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
36  SLE_CONDARR(Industry, production_rate, SLE_UINT8, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
37  SLE_CONDARR(Industry, production_rate, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
39  SLE_CONDARR(Industry, accepts_cargo, SLE_UINT8, 3, SLV_78, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
40  SLE_CONDARR(Industry, accepts_cargo, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
41  SLE_VAR(Industry, prod_level, SLE_UINT8),
42  SLE_CONDARR(Industry, this_month_production, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
43  SLE_CONDARR(Industry, this_month_production, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
44  SLE_CONDARR(Industry, this_month_transported, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
45  SLE_CONDARR(Industry, this_month_transported, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
46  SLE_CONDARR(Industry, last_month_pct_transported, SLE_UINT8, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
47  SLE_CONDARR(Industry, last_month_pct_transported, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
48  SLE_CONDARR(Industry, last_month_production, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
49  SLE_CONDARR(Industry, last_month_production, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
50  SLE_CONDARR(Industry, last_month_transported, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
51  SLE_CONDARR(Industry, last_month_transported, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
52 
53  SLE_VAR(Industry, counter, SLE_UINT16),
54 
55  SLE_VAR(Industry, type, SLE_UINT8),
56  SLE_VAR(Industry, owner, SLE_UINT8),
57  SLE_VAR(Industry, random_colour, SLE_UINT8),
58  SLE_CONDVAR(Industry, last_prod_year, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
59  SLE_CONDVAR(Industry, last_prod_year, SLE_INT32, SLV_31, SL_MAX_VERSION),
60  SLE_VAR(Industry, was_cargo_delivered, SLE_UINT8),
61 
62  SLE_CONDVAR(Industry, founder, SLE_UINT8, SLV_70, SL_MAX_VERSION),
63  SLE_CONDVAR(Industry, construction_date, SLE_INT32, SLV_70, SL_MAX_VERSION),
64  SLE_CONDVAR(Industry, construction_type, SLE_UINT8, SLV_70, SL_MAX_VERSION),
65  SLE_CONDVAR(Industry, last_cargo_accepted_at[0], SLE_INT32, SLV_70, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
66  SLE_CONDARR(Industry, last_cargo_accepted_at, SLE_INT32, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
67  SLE_CONDVAR(Industry, selected_layout, SLE_UINT8, SLV_73, SL_MAX_VERSION),
68 
69  SLEG_CONDARR(_old_ind_persistent_storage.storage, SLE_UINT32, 16, SLV_76, SLV_161),
71 
72  SLE_CONDNULL(1, SLV_82, SLV_197), // random_triggers
73  SLE_CONDVAR(Industry, random, SLE_UINT16, SLV_82, SL_MAX_VERSION),
74 
75  SLE_CONDNULL(32, SLV_2, SLV_144), // old reserved space
76 
77  SLE_END()
78 };
79 
80 static void Save_INDY()
81 {
82  Industry *ind;
83 
84  /* Write the industries */
85  FOR_ALL_INDUSTRIES(ind) {
86  SlSetArrayIndex(ind->index);
87  SlObject(ind, _industry_desc);
88  }
89 }
90 
91 static void Save_IIDS()
92 {
93  Save_NewGRFMapping(_industry_mngr);
94 }
95 
96 static void Save_TIDS()
97 {
98  Save_NewGRFMapping(_industile_mngr);
99 }
100 
101 static void Load_INDY()
102 {
103  int index;
104 
106 
107  while ((index = SlIterateArray()) != -1) {
108  Industry *i = new (index) Industry();
109  SlObject(i, _industry_desc);
110 
111  /* Before savegame version 161, persistent storages were not stored in a pool. */
113  /* Store the old persistent storage. The GRFID will be added later. */
115  i->psa = new PersistentStorage(0, 0, 0);
116  memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(_old_ind_persistent_storage.storage));
117  }
119  }
120 }
121 
122 static void Load_IIDS()
123 {
124  Load_NewGRFMapping(_industry_mngr);
125 }
126 
127 static void Load_TIDS()
128 {
129  Load_NewGRFMapping(_industile_mngr);
130 }
131 
132 static void Ptrs_INDY()
133 {
134  Industry *i;
135 
136  FOR_ALL_INDUSTRIES(i) {
137  SlObject(i, _industry_desc);
138  }
139 }
140 
144  SLEG_END()
145 };
146 
148 static void LoadSave_IBLD()
149 {
150  SlGlobList(_industry_builder_desc);
151 }
152 
155  SLE_VAR(IndustryTypeBuildData, probability, SLE_UINT32),
156  SLE_VAR(IndustryTypeBuildData, min_number, SLE_UINT8),
157  SLE_VAR(IndustryTypeBuildData, target_count, SLE_UINT16),
158  SLE_VAR(IndustryTypeBuildData, max_wait, SLE_UINT16),
159  SLE_VAR(IndustryTypeBuildData, wait_count, SLE_UINT16),
160  SLE_END()
161 };
162 
164 static void Save_ITBL()
165 {
166  for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
167  SlSetArrayIndex(i);
168  SlObject(_industry_builder.builddata + i, _industrytype_builder_desc);
169  }
170 }
171 
173 static void Load_ITBL()
174 {
175  for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
177  }
178  int index;
179  while ((index = SlIterateArray()) != -1) {
180  if ((uint)index >= NUM_INDUSTRYTYPES) SlErrorCorrupt("Too many industry builder datas");
181  SlObject(_industry_builder.builddata + index, _industrytype_builder_desc);
182  }
183 }
184 
185 extern const ChunkHandler _industry_chunk_handlers[] = {
186  { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, nullptr, CH_ARRAY},
187  { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY},
188  { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY},
189  { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, nullptr, nullptr, CH_RIFF},
190  { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY | CH_LAST},
191 };
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:157
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:644
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:765
Code handling saving and loading of NewGRF mappings.
70 10541
Definition: saveload.h:128
IndustryBuildData _industry_builder
In-game manager of industries.
76 11139
Definition: saveload.h:135
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:35
61 9892
Definition: saveload.h:117
#define SLEG_END()
End marker of global variables save or load.
Definition: saveload.h:757
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:546
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:356
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:604
210 PR#7234 Company stations can serve industries with attached neutral stations. ...
Definition: saveload.h:297
Defines the internal data of a functional industry.
Definition: industry.h:42
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
82 11410
Definition: saveload.h:142
Load/save a reference to a town.
Definition: saveload.h:376
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:718
#define SLEG_CONDARR(variable, type, length, from, to)
Storage of a global array in some savegame versions.
Definition: saveload.h:692
Class for persistent storage of data.
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:536
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:76
Highest possible saveload version.
Definition: saveload.h:307
First savegame version.
Definition: saveload.h:32
202 PR#6867 Increase industry cargo slots to 16 in, 16 out
Definition: saveload.h:287
IndustryType type
type of industry.
Definition: industry.h:59
Load/save a reference to a station.
Definition: saveload.h:375
TYPE storage[SIZE]
Memory to for the storage array.
73 10903
Definition: saveload.h:131
Class for pooled persistent storage of data.
static void LoadSave_IBLD()
Load/save industry builder.
Data for managing the number of industries of a single industry type.
Definition: industry.h:176
6.0 1721 6.1 1768
Definition: saveload.h:47
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:639
Handlers and description of chunk.
Definition: saveload.h:358
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:128
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:653
144 20334
Definition: saveload.h:216
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of an array in some savegame versions.
Definition: saveload.h:557
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:1548
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:193
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1566
SaveLoad type struct.
Definition: saveload.h:498
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:596
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:192
78 11176
Definition: saveload.h:137
161 22567
Definition: saveload.h:237
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD&#39;s id mapping.
Definition: newgrf_sl.cpp:32
31 5999
Definition: saveload.h:81
197 27978 v1.8
Definition: saveload.h:281
Last chunk in this array.
Definition: saveload.h:393
Load/save a reference to a persistent storage.
Definition: saveload.h:382