engine_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 "saveload_internal.h"
00014 #include "../engine_base.h"
00015 #include <map>
00016 
00017 static const SaveLoad _engine_desc[] = {
00018    SLE_CONDVAR(Engine, intro_date,          SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
00019    SLE_CONDVAR(Engine, intro_date,          SLE_INT32,                  31, SL_MAX_VERSION),
00020    SLE_CONDVAR(Engine, age,                 SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
00021    SLE_CONDVAR(Engine, age,                 SLE_INT32,                  31, SL_MAX_VERSION),
00022        SLE_VAR(Engine, reliability,         SLE_UINT16),
00023        SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
00024        SLE_VAR(Engine, reliability_start,   SLE_UINT16),
00025        SLE_VAR(Engine, reliability_max,     SLE_UINT16),
00026        SLE_VAR(Engine, reliability_final,   SLE_UINT16),
00027        SLE_VAR(Engine, duration_phase_1,    SLE_UINT16),
00028        SLE_VAR(Engine, duration_phase_2,    SLE_UINT16),
00029        SLE_VAR(Engine, duration_phase_3,    SLE_UINT16),
00030 
00031   SLE_CONDNULL(1,                                                        0, 120),
00032        SLE_VAR(Engine, flags,               SLE_UINT8),
00033   SLE_CONDNULL(1,                                                        0, 178), // old preview_company_rank
00034    SLE_CONDVAR(Engine, preview_asked,       SLE_UINT16,                179, SL_MAX_VERSION),
00035    SLE_CONDVAR(Engine, preview_company,     SLE_UINT8,                 179, SL_MAX_VERSION),
00036        SLE_VAR(Engine, preview_wait,        SLE_UINT8),
00037   SLE_CONDNULL(1,                                                        0,  44),
00038    SLE_CONDVAR(Engine, company_avail,       SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
00039    SLE_CONDVAR(Engine, company_avail,       SLE_UINT16,                104, SL_MAX_VERSION),
00040    SLE_CONDSTR(Engine, name,                SLE_STR, 0,                 84, SL_MAX_VERSION),
00041 
00042   SLE_CONDNULL(16,                                                       2, 143), // old reserved space
00043 
00044   SLE_END()
00045 };
00046 
00047 static std::map<EngineID, Engine> _temp_engine;
00048 
00049 Engine *GetTempDataEngine(EngineID index)
00050 {
00051   return &_temp_engine[index];
00052 }
00053 
00054 static void Save_ENGN()
00055 {
00056   Engine *e;
00057   FOR_ALL_ENGINES(e) {
00058     SlSetArrayIndex(e->index);
00059     SlObject(e, _engine_desc);
00060   }
00061 }
00062 
00063 static void Load_ENGN()
00064 {
00065   /* As engine data is loaded before engines are initialized we need to load
00066    * this information into a temporary array. This is then copied into the
00067    * engine pool after processing NewGRFs by CopyTempEngineData(). */
00068   int index;
00069   while ((index = SlIterateArray()) != -1) {
00070     Engine *e = GetTempDataEngine(index);
00071     SlObject(e, _engine_desc);
00072 
00073     if (IsSavegameVersionBefore(179)) {
00074       /* preview_company_rank was replaced with preview_company and preview_asked.
00075        * Just cancel any previews. */
00076       e->flags &= ~4; // ENGINE_OFFER_WINDOW_OPEN
00077       e->preview_company = INVALID_COMPANY;
00078       e->preview_asked = (CompanyMask)-1;
00079     }
00080   }
00081 }
00082 
00086 void CopyTempEngineData()
00087 {
00088   Engine *e;
00089   FOR_ALL_ENGINES(e) {
00090     if (e->index >= _temp_engine.size()) break;
00091 
00092     const Engine *se = GetTempDataEngine(e->index);
00093     e->intro_date          = se->intro_date;
00094     e->age                 = se->age;
00095     e->reliability         = se->reliability;
00096     e->reliability_spd_dec = se->reliability_spd_dec;
00097     e->reliability_start   = se->reliability_start;
00098     e->reliability_max     = se->reliability_max;
00099     e->reliability_final   = se->reliability_final;
00100     e->duration_phase_1    = se->duration_phase_1;
00101     e->duration_phase_2    = se->duration_phase_2;
00102     e->duration_phase_3    = se->duration_phase_3;
00103     e->flags               = se->flags;
00104     e->preview_asked       = se->preview_asked;
00105     e->preview_company     = se->preview_company;
00106     e->preview_wait        = se->preview_wait;
00107     e->company_avail       = se->company_avail;
00108     if (se->name != NULL) e->name = strdup(se->name);
00109   }
00110 
00111   /* Get rid of temporary data */
00112   _temp_engine.clear();
00113 }
00114 
00115 static void Load_ENGS()
00116 {
00117   /* Load old separate String ID list into a temporary array. This
00118    * was always 256 entries. */
00119   StringID names[256];
00120 
00121   SlArray(names, lengthof(names), SLE_STRINGID);
00122 
00123   /* Copy each string into the temporary engine array. */
00124   for (EngineID engine = 0; engine < lengthof(names); engine++) {
00125     Engine *e = GetTempDataEngine(engine);
00126     e->name = CopyFromOldName(names[engine]);
00127   }
00128 }
00129 
00131 static const SaveLoad _engine_id_mapping_desc[] = {
00132   SLE_VAR(EngineIDMapping, grfid,         SLE_UINT32),
00133   SLE_VAR(EngineIDMapping, internal_id,   SLE_UINT16),
00134   SLE_VAR(EngineIDMapping, type,          SLE_UINT8),
00135   SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
00136   SLE_END()
00137 };
00138 
00139 static void Save_EIDS()
00140 {
00141   const EngineIDMapping *end = _engine_mngr.End();
00142   uint index = 0;
00143   for (EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
00144     SlSetArrayIndex(index);
00145     SlObject(eid, _engine_id_mapping_desc);
00146   }
00147 }
00148 
00149 static void Load_EIDS()
00150 {
00151   _engine_mngr.Clear();
00152 
00153   while (SlIterateArray() != -1) {
00154     EngineIDMapping *eid = _engine_mngr.Append();
00155     SlObject(eid, _engine_id_mapping_desc);
00156   }
00157 }
00158 
00159 extern const ChunkHandler _engine_chunk_handlers[] = {
00160   { 'EIDS', Save_EIDS, Load_EIDS, NULL, NULL, CH_ARRAY          },
00161   { 'ENGN', Save_ENGN, Load_ENGN, NULL, NULL, CH_ARRAY          },
00162   { 'ENGS', NULL,      Load_ENGS, NULL, NULL, CH_RIFF | CH_LAST },
00163 };