OpenTTD Source  1.11.2
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 #include "../industry.h"
12 
13 #include "saveload.h"
14 #include "newgrf_sl.h"
15 
16 #include "../safeguards.h"
17 
18 static OldPersistentStorage _old_ind_persistent_storage;
19 
20 static const SaveLoad _industry_desc[] = {
21  SLE_CONDVAR(Industry, location.tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
22  SLE_CONDVAR(Industry, location.tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
23  SLE_VAR(Industry, location.w, SLE_FILE_U8 | SLE_VAR_U16),
24  SLE_VAR(Industry, location.h, SLE_FILE_U8 | SLE_VAR_U16),
25  SLE_REF(Industry, town, REF_TOWN),
28  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 2, SLV_78, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
29  SLE_CONDARR(Industry, produced_cargo, SLE_UINT8, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
30  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 3, SLV_70, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
31  SLE_CONDARR(Industry, incoming_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
32  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
33  SLE_CONDARR(Industry, produced_cargo_waiting, SLE_UINT16, 16, SLV_EXTEND_INDUSTRY_CARGO_SLOTS, SL_MAX_VERSION),
34  SLE_CONDARR(Industry, production_rate, SLE_UINT8, 2, SL_MIN_VERSION, SLV_EXTEND_INDUSTRY_CARGO_SLOTS),
35  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(_old_ind_persistent_storage.storage, SLE_UINT32, 16, SLV_76, SLV_161),
72 
73  SLE_CONDNULL(1, SLV_82, SLV_197), // random_triggers
74  SLE_CONDVAR(Industry, random, SLE_UINT16, SLV_82, SL_MAX_VERSION),
76 
77  SLE_CONDNULL(32, SLV_2, SLV_144), // old reserved space
78 
79  SLE_END()
80 };
81 
82 static void Save_INDY()
83 {
84  /* Write the industries */
85  for (Industry *ind : Industry::Iterate()) {
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  for (Industry *i : Industry::Iterate()) {
135  SlObject(i, _industry_desc);
136  }
137 }
138 
142  SLEG_END()
143 };
144 
146 static void LoadSave_IBLD()
147 {
149 }
150 
153  SLE_VAR(IndustryTypeBuildData, probability, SLE_UINT32),
154  SLE_VAR(IndustryTypeBuildData, min_number, SLE_UINT8),
155  SLE_VAR(IndustryTypeBuildData, target_count, SLE_UINT16),
156  SLE_VAR(IndustryTypeBuildData, max_wait, SLE_UINT16),
157  SLE_VAR(IndustryTypeBuildData, wait_count, SLE_UINT16),
158  SLE_END()
159 };
160 
162 static void Save_ITBL()
163 {
164  for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
165  SlSetArrayIndex(i);
167  }
168 }
169 
171 static void Load_ITBL()
172 {
173  for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
175  }
176  int index;
177  while ((index = SlIterateArray()) != -1) {
178  if ((uint)index >= NUM_INDUSTRYTYPES) SlErrorCorrupt("Too many industry builder datas");
180  }
181 }
182 
183 extern const ChunkHandler _industry_chunk_handlers[] = {
184  { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, nullptr, CH_ARRAY},
185  { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY},
186  { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY},
187  { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, nullptr, nullptr, CH_RIFF},
188  { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY | CH_LAST},
189 };
SLV_144
@ SLV_144
144 20334
Definition: saveload.h:215
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
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
REF_TOWN
@ REF_TOWN
Load/save a reference to a town.
Definition: saveload.h:394
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:594
SLE_CONDARR
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of an array in some savegame versions.
Definition: saveload.h:573
_industrytype_builder_desc
static const SaveLoad _industrytype_builder_desc[]
Description of the data to save and load in IndustryTypeBuildData.
Definition: industry_sl.cpp:152
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:552
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:648
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:411
saveload.h
SLV_78
@ SLV_78
78 11176
Definition: saveload.h:136
SLV_SERVE_NEUTRAL_INDUSTRIES
@ SLV_SERVE_NEUTRAL_INDUSTRIES
210 PR#7234 Company stations can serve industries with attached neutral stations.
Definition: saveload.h:296
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:380
Industry
Defines the internal data of a functional industry.
Definition: industry.h:66
SLV_76
@ SLV_76
76 11139
Definition: saveload.h:134
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:678
LoadSave_IBLD
static void LoadSave_IBLD()
Load/save industry builder.
Definition: industry_sl.cpp:146
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition: saveload.h:630
SLV_61
@ SLV_61
61 9892
Definition: saveload.h:116
REF_STATION
@ REF_STATION
Load/save a reference to a station.
Definition: saveload.h:393
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:286
SLV_161
@ SLV_161
161 22567
Definition: saveload.h:236
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
allow control codes in the strings
Definition: saveload.h:490
IndustryTypeBuildData::Reset
void Reset()
Reset the entry.
Definition: industry_cmd.cpp:2267
SLV_31
@ SLV_31
31 5999
Definition: saveload.h:80
Industry::type
IndustryType type
type of industry.
Definition: industry.h:83
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:687
_industry_builder_desc
static const SaveLoad _industry_builder_desc[]
Description of the data to save and load in IndustryBuildData.
Definition: industry_sl.cpp:140
SLV_73
@ SLV_73
73 10903
Definition: saveload.h:130
Load_NewGRFMapping
void Load_NewGRFMapping(OverrideManagerBase &mapping)
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:42
REF_STORAGE
@ REF_STORAGE
Load/save a reference to a persistent storage.
Definition: saveload.h:400
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:562
SLV_82
@ SLV_82
82 11410
Definition: saveload.h:141
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:816
PersistentStorageArray::storage
TYPE storage[SIZE]
Memory to for the storage array.
Definition: newgrf_storage.h:67
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
SLV_INDUSTRY_TEXT
@ SLV_INDUSTRY_TEXT
289 PR#8576 v1.11.0-RC1 Additional GS text for industries.
Definition: saveload.h:326
SLV_2
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition: saveload.h:34
SlGlobList
void SlGlobList(const SaveLoadGlobVarList *sldg)
Save or Load (a list of) global variables.
Definition: saveload.cpp:1630
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:329
SLEG_VAR
#define SLEG_VAR(variable, type)
Storage of a global variable in every savegame version.
Definition: saveload.h:762
Save_ITBL
static void Save_ITBL()
Save industry-type build data.
Definition: industry_sl.cpp:162
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:622
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:378
SLEG_END
#define SLEG_END()
End marker of global variables save or load.
Definition: saveload.h:808
SLV_GS_INDUSTRY_CONTROL
@ SLV_GS_INDUSTRY_CONTROL
287 PR#7912 and PR#8115 GS industry control.
Definition: saveload.h:324
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition: saveload.h:46
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
Industry::IncIndustryTypeCount
static void IncIndustryTypeCount(IndustryType type)
Increment the count of industries for this type.
Definition: industry.h:157
SLV_197
@ SLV_197
197 27978 v1.8
Definition: saveload.h:280
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:299
IndustryTypeBuildData
Data for managing the number of industries of a single industry type.
Definition: industry.h:213
SLEG_CONDARR
#define SLEG_CONDARR(variable, type, length, from, to)
Storage of a global array in some savegame versions.
Definition: saveload.h:727
Load_ITBL
static void Load_ITBL()
Load industry-type build data.
Definition: industry_sl.cpp:171
_industry_builder
IndustryBuildData _industry_builder
In-game manager of industries.
Definition: industry_cmd.cpp:65
SLV_70
@ SLV_70
70 10541
Definition: saveload.h:127
Industry::psa
PersistentStorage * psa
Persistent storage for NewGRF industries.
Definition: industry.h:105
SaveLoad
SaveLoad type struct.
Definition: saveload.h:517
newgrf_sl.h
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631
Save_NewGRFMapping
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:30
PersistentStorageArray
Class for persistent storage of data.
Definition: newgrf_storage.h:66