1 /* $Id: industry_sl.cpp 26175 2013-12-23 18:09:29Z frosch $ */
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/>.
10 /** @file industry_sl.cpp Code handling saving and loading of industries */
12 #include "../stdafx.h"
13 #include "../industry.h"
16 #include "newgrf_sl.h"
18 #include "../safeguards.h"
20 static OldPersistentStorage _old_ind_persistent_storage
;
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), ///< used to be industry's produced_cargo
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), ///< used to be industry's accepts_cargo
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),
42 SLE_VAR(Industry
, counter
, SLE_UINT16
),
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
),
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
),
57 SLEG_CONDARR(_old_ind_persistent_storage
.storage
, SLE_UINT32
, 16, 76, 160),
58 SLE_CONDREF(Industry
, psa
, REF_STORAGE
, 161, SL_MAX_VERSION
),
60 SLE_CONDNULL(1, 82, 196), // random_triggers
61 SLE_CONDNULL(1, SL_PATCH_PACK
, SL_PATCH_PACK_1_18_6
), // random_triggers
63 SLE_CONDVAR(Industry
, random
, SLE_UINT16
, 82, SL_MAX_VERSION
),
65 SLE_CONDNULL(32, 2, 143), // old reserved space
70 static void Save_INDY()
74 /* Write the industries */
75 FOR_ALL_INDUSTRIES(ind
) {
76 SlSetArrayIndex(ind
->index
);
77 SlObject(ind
, _industry_desc
);
81 static void Save_IIDS()
83 Save_NewGRFMapping(_industry_mngr
);
86 static void Save_TIDS()
88 Save_NewGRFMapping(_industile_mngr
);
91 static void Load_INDY()
95 Industry::ResetIndustryCounts();
97 while ((index
= SlIterateArray()) != -1) {
98 Industry
*i
= new (index
) Industry();
99 SlObject(i
, _industry_desc
);
101 /* Before savegame version 161, persistent storages were not stored in a pool. */
102 if (IsSavegameVersionBefore(161) && !IsSavegameVersionBefore(76)) {
103 /* Store the old persistent storage. The GRFID will be added later. */
104 assert(PersistentStorage::CanAllocateItem());
105 i
->psa
= new PersistentStorage(0, 0, 0);
106 memcpy(i
->psa
->storage
, _old_ind_persistent_storage
.storage
, sizeof(i
->psa
->storage
));
108 Industry::IncIndustryTypeCount(i
->type
);
112 static void Load_IIDS()
114 Load_NewGRFMapping(_industry_mngr
);
117 static void Load_TIDS()
119 Load_NewGRFMapping(_industile_mngr
);
122 static void Ptrs_INDY()
126 FOR_ALL_INDUSTRIES(i
) {
127 SlObject(i
, _industry_desc
);
131 /** Description of the data to save and load in #IndustryBuildData. */
132 static const SaveLoad _industry_builder_desc
[] = {
133 SLEG_VAR(_industry_builder
.wanted_inds
, SLE_UINT32
),
137 /** Load/save industry builder. */
138 static void LoadSave_IBLD()
140 SlGlobList(_industry_builder_desc
);
143 /** Description of the data to save and load in #IndustryTypeBuildData. */
144 static const SaveLoad _industrytype_builder_desc
[] = {
145 SLE_VAR(IndustryTypeBuildData
, probability
, SLE_UINT32
),
146 SLE_VAR(IndustryTypeBuildData
, min_number
, SLE_UINT8
),
147 SLE_VAR(IndustryTypeBuildData
, target_count
, SLE_UINT16
),
148 SLE_VAR(IndustryTypeBuildData
, max_wait
, SLE_UINT16
),
149 SLE_VAR(IndustryTypeBuildData
, wait_count
, SLE_UINT16
),
153 /** Save industry-type build data. */
154 static void Save_ITBL()
156 for (int i
= 0; i
< NUM_INDUSTRYTYPES
; i
++) {
158 SlObject(_industry_builder
.builddata
+ i
, _industrytype_builder_desc
);
162 /** Load industry-type build data. */
163 static void Load_ITBL()
165 for (IndustryType it
= 0; it
< NUM_INDUSTRYTYPES
; it
++) {
166 _industry_builder
.builddata
[it
].Reset();
169 while ((index
= SlIterateArray()) != -1) {
170 if ((uint
)index
>= NUM_INDUSTRYTYPES
) SlErrorCorrupt("Too many industry builder datas");
171 SlObject(_industry_builder
.builddata
+ index
, _industrytype_builder_desc
);
175 extern const ChunkHandler _industry_chunk_handlers
[] = {
176 { 'INDY', Save_INDY
, Load_INDY
, Ptrs_INDY
, NULL
, CH_ARRAY
},
177 { 'IIDS', Save_IIDS
, Load_IIDS
, NULL
, NULL
, CH_ARRAY
},
178 { 'TIDS', Save_TIDS
, Load_TIDS
, NULL
, NULL
, CH_ARRAY
},
179 { 'IBLD', LoadSave_IBLD
, LoadSave_IBLD
, NULL
, NULL
, CH_RIFF
},
180 { 'ITBL', Save_ITBL
, Load_ITBL
, NULL
, NULL
, CH_ARRAY
| CH_LAST
},