OpenTTD Source  12.0-beta2
industry_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 
12 #include "saveload.h"
14 
15 #include "../industry.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),
29  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 2, SLV_78, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
30  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
31  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 3, SLV_70, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
32  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
33  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
34  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
35  SLE_CONDARR(Industry, production_rate, SLE_UINT8, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
36  SLE_CONDARR(Industry, production_rate, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
37  SLE_CONDARR(Industry, accepts_cargo, SLE_UINT8, 3, SLV_78, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
38  SLE_CONDARR(Industry, accepts_cargo, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
39  SLE_VAR(Industry, prod_level, SLE_UINT8),
40  SLE_CONDARR(Industry, this_month_production, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
41  SLE_CONDARR(Industry, this_month_production, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
42  SLE_CONDARR(Industry, this_month_transported, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
43  SLE_CONDARR(Industry, this_month_transported, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
44  SLE_CONDARR(Industry, last_month_pct_transported, SLE_UINT8, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
45  SLE_CONDARR(Industry, last_month_pct_transported, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
46  SLE_CONDARR(Industry, last_month_production, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
47  SLE_CONDARR(Industry, last_month_production, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
48  SLE_CONDARR(Industry, last_month_transported, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
49  SLE_CONDARR(Industry, last_month_transported, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
50 
51  SLE_VAR(Industry, counter, SLE_UINT16),
52 
53  SLE_VAR(Industry, type, SLE_UINT8),
54  SLE_VAR(Industry, owner, SLE_UINT8),
55  SLE_VAR(Industry, random_colour, SLE_UINT8),
56  SLE_CONDVAR(Industry, last_prod_year, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
57  SLE_CONDVAR(Industry, last_prod_year, SLE_INT32, SLV_31, SL_MAX_VERSION),
58  SLE_VAR(Industry, was_cargo_delivered, SLE_UINT8),
60 
61  SLE_CONDVAR(Industry, founder, SLE_UINT8, SLV_70, SL_MAX_VERSION),
62  SLE_CONDVAR(Industry, construction_date, SLE_INT32, SLV_70, SL_MAX_VERSION),
63  SLE_CONDVAR(Industry, construction_type, SLE_UINT8, SLV_70, SL_MAX_VERSION),
64  SLE_CONDVAR(Industry, last_cargo_accepted_at[0], SLE_INT32, SLV_70, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
65  SLE_CONDARR(Industry, last_cargo_accepted_at, SLE_INT32, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
66  SLE_CONDVAR(Industry, selected_layout, SLE_UINT8, SLV_73, SL_MAX_VERSION),
67  SLE_CONDVAR(Industry, exclusive_supplier, SLE_UINT8, SLV_GS_INDUSTRY_CONTROL, SL_MAX_VERSION),
68  SLE_CONDVAR(Industry, exclusive_consumer, SLE_UINT8, SLV_GS_INDUSTRY_CONTROL, SL_MAX_VERSION),
69 
70  SLEG_CONDARR("storage", _old_ind_persistent_storage.storage, SLE_UINT32, 16, SLV_76, SLV_161),
72 
73  SLE_CONDVAR(Industry, random, SLE_UINT16, SLV_82, SL_MAX_VERSION),
75 };
76 
78  INDYChunkHandler() : ChunkHandler('INDY', CH_TABLE) {}
79 
80  void Save() const override
81  {
82  SlTableHeader(_industry_desc);
83 
84  /* Write the industries */
85  for (Industry *ind : Industry::Iterate()) {
86  SlSetArrayIndex(ind->index);
87  SlObject(ind, _industry_desc);
88  }
89  }
90 
91  void Load() const override
92  {
93  const std::vector<SaveLoad> slt = SlCompatTableHeader(_industry_desc, _industry_sl_compat);
94 
95  int index;
96 
98 
99  while ((index = SlIterateArray()) != -1) {
100  Industry *i = new (index) Industry();
101  SlObject(i, slt);
102 
103  /* Before savegame version 161, persistent storages were not stored in a pool. */
105  /* Store the old persistent storage. The GRFID will be added later. */
107  i->psa = new PersistentStorage(0, 0, 0);
108  memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(_old_ind_persistent_storage.storage));
109  }
111  }
112  }
113 
114  void FixPointers() const override
115  {
116  for (Industry *i : Industry::Iterate()) {
117  SlObject(i, _industry_desc);
118  }
119  }
120 };
121 
123  IIDSChunkHandler() : NewGRFMappingChunkHandler('IIDS', _industry_mngr) {}
124 };
125 
127  TIDSChunkHandler() : NewGRFMappingChunkHandler('TIDS', _industile_mngr) {}
128 };
129 
132  SLEG_VAR("wanted_inds", _industry_builder.wanted_inds, SLE_UINT32),
133 };
134 
137  IBLDChunkHandler() : ChunkHandler('IBLD', CH_TABLE) {}
138 
139  void Save() const override
140  {
142 
143  SlSetArrayIndex(0);
145  }
146 
147  void Load() const override
148  {
149  const std::vector<SaveLoad> slt = SlCompatTableHeader(_industry_builder_desc, _industry_builder_sl_compat);
150 
152  SlGlobList(slt);
153  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many IBLD entries");
154  }
155 };
156 
159  SLE_VAR(IndustryTypeBuildData, probability, SLE_UINT32),
160  SLE_VAR(IndustryTypeBuildData, min_number, SLE_UINT8),
161  SLE_VAR(IndustryTypeBuildData, target_count, SLE_UINT16),
162  SLE_VAR(IndustryTypeBuildData, max_wait, SLE_UINT16),
163  SLE_VAR(IndustryTypeBuildData, wait_count, SLE_UINT16),
164 };
165 
168  ITBLChunkHandler() : ChunkHandler('ITBL', CH_TABLE) {}
169 
170  void Save() const override
171  {
173 
174  for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
175  SlSetArrayIndex(i);
177  }
178  }
179 
180  void Load() const override
181  {
183 
184  for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
186  }
187  int index;
188  while ((index = SlIterateArray()) != -1) {
189  if ((uint)index >= NUM_INDUSTRYTYPES) SlErrorCorrupt("Too many industry builder datas");
190  SlObject(_industry_builder.builddata + index, slt);
191  }
192  }
193 };
194 
195 static const INDYChunkHandler INDY;
196 static const IIDSChunkHandler IIDS;
197 static const TIDSChunkHandler TIDS;
198 static const IBLDChunkHandler IBLD;
199 static const ITBLChunkHandler ITBL;
200 static const ChunkHandlerRef industry_chunk_handlers[] = {
201  INDY,
202  IIDS,
203  TIDS,
204  IBLD,
205  ITBL,
206 };
207 
208 extern const ChunkHandlerTable _industry_chunk_handlers(industry_chunk_handlers);
TIDSChunkHandler
Definition: industry_sl.cpp:126
NUM_INDUSTRYTYPES
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:26
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
NewGRFMappingChunkHandler
Definition: newgrf_sl.h:15
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:35
_industry_builder_sl_compat
const SaveLoadCompat _industry_builder_sl_compat[]
Original field order for _industry_builder_desc.
Definition: industry_sl_compat.h:59
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:538
IndustryBuildData::builddata
IndustryTypeBuildData builddata[NUM_INDUSTRYTYPES]
Industry build data for every industry type.
Definition: industry.h:229
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition: saveload.h:744
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
IBLDChunkHandler
Industry builder.
Definition: industry_sl.cpp:136
SLE_CONDARR
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of a fixed-size array of SL_VAR elements in some savegame versions.
Definition: saveload.h:723
_industrytype_builder_desc
static const SaveLoad _industrytype_builder_desc[]
Description of the data to save and load in IndustryTypeBuildData.
Definition: industry_sl.cpp:158
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:702
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:798
saveload.h
SLV_78
@ SLV_78
78 11176
Definition: saveload.h:140
industry_sl_compat.h
SLV_SERVE_NEUTRAL_INDUSTRIES
@ SLV_SERVE_NEUTRAL_INDUSTRIES
210 PR#7234 Company stations can serve industries with attached neutral stations.
Definition: saveload.h:300
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
SLV_76
@ SLV_76
76 11139
Definition: saveload.h:138
INDYChunkHandler::Load
void Load() const override
Load the chunk.
Definition: industry_sl.cpp:91
_industrytype_builder_sl_compat
const SaveLoadCompat _industrytype_builder_sl_compat[]
Original field order for _industrytype_builder_desc.
Definition: industry_sl_compat.h:64
INDYChunkHandler
Definition: industry_sl.cpp:77
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:780
SlGlobList
void SlGlobList(const SaveLoadTable &slt)
Save or Load (a list of) global variables.
Definition: saveload.cpp:2083
REF_STATION
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:537
SLV_EXTEND_INDUSTRY_CARGO_SLOTS
@ SLV_EXTEND_INDUSTRY_CARGO_SLOTS
202 PR#6867 Increase industry cargo slots to 16 in, 16 out
Definition: saveload.h:290
SLV_161
@ SLV_161
161 22567
Definition: saveload.h:240
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition: saveload.h:625
IIDSChunkHandler
Definition: industry_sl.cpp:122
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition: industry_cmd.cpp:2266
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
ITBLChunkHandler
Industry-type build data.
Definition: industry_sl.cpp:167
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:84
Industry::type
IndustryType type
type of industry.
Definition: industry.h:83
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:937
_industry_sl_compat
const SaveLoadCompat _industry_sl_compat[]
Original field order for _industry_desc.
Definition: industry_sl_compat.h:16
_industry_builder_desc
static const SaveLoad _industry_builder_desc[]
Description of the data to save and load in IndustryBuildData.
Definition: industry_sl.cpp:131
SLV_73
@ SLV_73
73 10903
Definition: saveload.h:134
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:544
INDYChunkHandler::Save
void Save() const override
Save the chunk.
Definition: industry_sl.cpp:80
Industry::ResetIndustryCounts
static void ResetIndustryCounts()
Resets industry counts.
Definition: industry.h:186
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition: saveload.h:712
SLV_82
@ SLV_82
82 11410
Definition: saveload.h:145
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
PersistentStorageArray::storage
TYPE storage[SIZE]
Memory to for the storage array.
Definition: newgrf_storage.h:67
ITBLChunkHandler::Load
void Load() const override
Load the chunk.
Definition: industry_sl.cpp:180
SLV_INDUSTRY_TEXT
@ SLV_INDUSTRY_TEXT
289 PR#8576 v1.11.0-RC1 Additional GS text for industries.
Definition: saveload.h:330
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:342
ITBLChunkHandler::Save
void Save() const override
Save the chunk.
Definition: industry_sl.cpp:170
IndustryBuildData::wanted_inds
uint32 wanted_inds
Number of wanted industries (bits 31-16), and a fraction (bits 15-0).
Definition: industry.h:230
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:772
Pool::PoolItem<&_industry_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
SLV_GS_INDUSTRY_CONTROL
@ SLV_GS_INDUSTRY_CONTROL
287 PR#7912 and PR#8115 GS industry control.
Definition: saveload.h:328
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:50
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
Industry::IncIndustryTypeCount
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:157
SLEG_CONDARR
#define SLEG_CONDARR(name, variable, type, length, from, to)
Storage of a global fixed-size array of SL_VAR elements in some savegame versions.
Definition: saveload.h:870
Pool::PoolItem<&_persistent_storage_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition: industry.h:213
IBLDChunkHandler::Save
void Save() const override
Save the chunk.
Definition: industry_sl.cpp:139
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition: industry_cmd.cpp:65
INDYChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition: industry_sl.cpp:114
SLV_70
@ SLV_70
70 10541
Definition: saveload.h:131
IBLDChunkHandler::Load
void Load() const override
Load the chunk.
Definition: industry_sl.cpp:147
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:105
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
newgrf_sl.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
PersistentStorageArray
Class for persistent storage of data.
Definition: newgrf_storage.h:66