# industry_sl.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../industry.h"
    00014
    00015 #include "saveload.h"
    00016 #include "newgrf_sl.h"
    00017
    00018 static OldPersistentStorage _old_ind_persistent_storage;
    00019
    00020 static const SaveLoad _industry_desc[] = {
    00021   SLE_CONDVAR(Industry, location.tile,              SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
    00022   SLE_CONDVAR(Industry, location.tile,              SLE_UINT32,                  6, SL_MAX_VERSION),
    00023       SLE_VAR(Industry, location.w,                 SLE_FILE_U8 | SLE_VAR_U16),
    00024       SLE_VAR(Industry, location.h,                 SLE_FILE_U8 | SLE_VAR_U16),
    00025       SLE_REF(Industry, town,                       REF_TOWN),
    00026   SLE_CONDNULL( 2, 0, 60),
    00027   SLE_CONDARR(Industry, produced_cargo,             SLE_UINT8,  2,              78, SL_MAX_VERSION),
    00028   SLE_CONDARR(Industry, incoming_cargo_waiting,     SLE_UINT16, 3,              70, SL_MAX_VERSION),
    00029       SLE_ARR(Industry, produced_cargo_waiting,     SLE_UINT16, 2),
    00030       SLE_ARR(Industry, production_rate,            SLE_UINT8,  2),
    00031   SLE_CONDNULL( 3, 0, 60),
    00032   SLE_CONDARR(Industry, accepts_cargo,              SLE_UINT8,  3,              78, SL_MAX_VERSION),
    00033       SLE_VAR(Industry, prod_level,                 SLE_UINT8),
    00034       SLE_ARR(Industry, this_month_production,      SLE_UINT16, 2),
    00035       SLE_ARR(Industry, this_month_transported,     SLE_UINT16, 2),
    00036       SLE_ARR(Industry, last_month_pct_transported, SLE_UINT8,  2),
    00037       SLE_ARR(Industry, last_month_production,      SLE_UINT16, 2),
    00038       SLE_ARR(Industry, last_month_transported,     SLE_UINT16, 2),
    00039
    00040       SLE_VAR(Industry, counter,                    SLE_UINT16),
    00041
    00042       SLE_VAR(Industry, type,                       SLE_UINT8),
    00043       SLE_VAR(Industry, owner,                      SLE_UINT8),
    00044       SLE_VAR(Industry, random_colour,              SLE_UINT8),
    00045   SLE_CONDVAR(Industry, last_prod_year,             SLE_FILE_U8 | SLE_VAR_I32,  0, 30),
    00046   SLE_CONDVAR(Industry, last_prod_year,             SLE_INT32,                 31, SL_MAX_VERSION),
    00047       SLE_VAR(Industry, was_cargo_delivered,        SLE_UINT8),
    00048
    00049   SLE_CONDVAR(Industry, founder,                    SLE_UINT8,                 70, SL_MAX_VERSION),
    00050   SLE_CONDVAR(Industry, construction_date,          SLE_INT32,                 70, SL_MAX_VERSION),
    00051   SLE_CONDVAR(Industry, construction_type,          SLE_UINT8,                 70, SL_MAX_VERSION),
    00052   SLE_CONDVAR(Industry, last_cargo_accepted_at,     SLE_INT32,                 70, SL_MAX_VERSION),
    00053   SLE_CONDVAR(Industry, selected_layout,            SLE_UINT8,                 73, SL_MAX_VERSION),
    00054
    00055   SLEG_CONDARR(_old_ind_persistent_storage.storage, SLE_UINT32, 16,            76, 160),
    00056   SLE_CONDREF(Industry, psa,                        REF_STORAGE,              161, SL_MAX_VERSION),
    00057
    00058   SLE_CONDVAR(Industry, random_triggers,            SLE_UINT8,                 82, SL_MAX_VERSION),
    00059   SLE_CONDVAR(Industry, random,                     SLE_UINT16,                82, SL_MAX_VERSION),
    00060
    00061   SLE_CONDNULL(32, 2, 143), // old reserved space
    00062
    00063   SLE_END()
    00064 };
    00065
    00066 static void Save_INDY()
    00067 {
    00068   Industry *ind;
    00069
    00070   /* Write the industries */
    00071   FOR_ALL_INDUSTRIES(ind) {
    00072     SlSetArrayIndex(ind->index);
    00073     SlObject(ind, _industry_desc);
    00074   }
    00075 }
    00076
    00077 static void Save_IIDS()
    00078 {
    00079   Save_NewGRFMapping(_industry_mngr);
    00080 }
    00081
    00082 static void Save_TIDS()
    00083 {
    00084   Save_NewGRFMapping(_industile_mngr);
    00085 }
    00086
    00087 static void Load_INDY()
    00088 {
    00089   int index;
    00090
    00091   Industry::ResetIndustryCounts();
    00092
    00093   while ((index = SlIterateArray()) != -1) {
    00094     Industry *i = new (index) Industry();
    00095     SlObject(i, _industry_desc);
    00096
    00097     /* Before savegame version 161, persistent storages were not stored in a pool. */
    00098     if (IsSavegameVersionBefore(161) && !IsSavegameVersionBefore(76)) {
    00099       /* Store the old persistent storage. The GRFID will be added later. */
    00100       assert(PersistentStorage::CanAllocateItem());
    00101       i->psa = new PersistentStorage(0);
    00102       memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(i->psa->storage));
    00103     }
    00104     Industry::IncIndustryTypeCount(i->type);
    00105   }
    00106 }
    00107
    00108 static void Load_IIDS()
    00109 {
    00110   Load_NewGRFMapping(_industry_mngr);
    00111 }
    00112
    00113 static void Load_TIDS()
    00114 {
    00115   Load_NewGRFMapping(_industile_mngr);
    00116 }
    00117
    00118 static void Ptrs_INDY()
    00119 {
    00120   Industry *i;
    00121
    00122   FOR_ALL_INDUSTRIES(i) {
    00123     SlObject(i, _industry_desc);
    00124   }
    00125 }
    00126
    00128 static const SaveLoad _industry_builder_desc[] = {
    00129   SLEG_VAR(_industry_builder.wanted_inds, SLE_UINT32),
    00130   SLEG_END()
    00131 };
    00132
    00134 static void LoadSave_IBLD()
    00135 {
    00136   SlGlobList(_industry_builder_desc);
    00137 }
    00138
    00140 static const SaveLoad _industrytype_builder_desc[] = {
    00141   SLE_VAR(IndustryTypeBuildData, probability,  SLE_UINT32),
    00142   SLE_VAR(IndustryTypeBuildData, min_number,   SLE_UINT8),
    00143   SLE_VAR(IndustryTypeBuildData, target_count, SLE_UINT16),
    00144   SLE_VAR(IndustryTypeBuildData, max_wait,     SLE_UINT16),
    00145   SLE_VAR(IndustryTypeBuildData, wait_count,   SLE_UINT16),
    00146   SLE_END()
    00147 };
    00148
    00150 static void Save_ITBL()
    00151 {
    00152   for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00153     SlSetArrayIndex(i);
    00154     SlObject(_industry_builder.builddata + i, _industrytype_builder_desc);
    00155   }
    00156 }
    00157
    00159 static void Load_ITBL()
    00160 {
    00161   int index;
    00162   for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00163     index = SlIterateArray();
    00164     assert(index == i);
    00165     SlObject(_industry_builder.builddata + i, _industrytype_builder_desc);
    00166   }
    00167   index = SlIterateArray();
    00168   assert(index == -1);
    00169 }
    00170
    00171 extern const ChunkHandler _industry_chunk_handlers[] = {
    00172   { 'INDY', Save_INDY,     Load_INDY,     Ptrs_INDY, NULL, CH_ARRAY},
    00173   { 'IIDS', Save_IIDS,     Load_IIDS,     NULL,      NULL, CH_ARRAY},
    00174   { 'TIDS', Save_TIDS,     Load_TIDS,     NULL,      NULL, CH_ARRAY},
    00175   { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, NULL,      NULL, CH_RIFF},
    00176   { 'ITBL', Save_ITBL,     Load_ITBL,     NULL,      NULL, CH_ARRAY | CH_LAST},
    00177 };