# engine_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: engine_sl.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
    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_VAR(Engine, preview_company_rank,SLE_UINT8),
    00034        SLE_VAR(Engine, preview_wait,        SLE_UINT8),
    00035   SLE_CONDNULL(1,                                                        0,  44),
    00036    SLE_CONDVAR(Engine, company_avail,       SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
    00037    SLE_CONDVAR(Engine, company_avail,       SLE_UINT16,                104, SL_MAX_VERSION),
    00038    SLE_CONDSTR(Engine, name,                SLE_STR, 0,                 84, SL_MAX_VERSION),
    00039
    00040   /* reserve extra space in savegame here. (currently 16 bytes) */
    00041   SLE_CONDNULL(16,                                                       2, SL_MAX_VERSION),
    00042
    00043   SLE_END()
    00044 };
    00045
    00046 static std::map<EngineID, Engine> _temp_engine;
    00047
    00048 Engine *GetTempDataEngine(EngineID index)
    00049 {
    00050   return &_temp_engine[index];
    00051 }
    00052
    00053 static void Save_ENGN()
    00054 {
    00055   Engine *e;
    00056   FOR_ALL_ENGINES(e) {
    00057     SlSetArrayIndex(e->index);
    00058     SlObject(e, _engine_desc);
    00059   }
    00060 }
    00061
    00062 static void Load_ENGN()
    00063 {
    00064   /* As engine data is loaded before engines are initialized we need to load
    00065    * this information into a temporary array. This is then copied into the
    00066    * engine pool after processing NewGRFs by CopyTempEngineData(). */
    00067   int index;
    00068   while ((index = SlIterateArray()) != -1) {
    00069     Engine *e = GetTempDataEngine(index);
    00070     SlObject(e, _engine_desc);
    00071   }
    00072 }
    00073
    00077 void CopyTempEngineData()
    00078 {
    00079   Engine *e;
    00080   FOR_ALL_ENGINES(e) {
    00081     if (e->index >= _temp_engine.size()) break;
    00082
    00083     const Engine *se = GetTempDataEngine(e->index);
    00084     e->intro_date          = se->intro_date;
    00085     e->age                 = se->age;
    00086     e->reliability         = se->reliability;
    00087     e->reliability_spd_dec = se->reliability_spd_dec;
    00088     e->reliability_start   = se->reliability_start;
    00089     e->reliability_max     = se->reliability_max;
    00090     e->reliability_final   = se->reliability_final;
    00091     e->duration_phase_1    = se->duration_phase_1;
    00092     e->duration_phase_2    = se->duration_phase_2;
    00093     e->duration_phase_3    = se->duration_phase_3;
    00094     e->flags               = se->flags;
    00095     e->preview_company_rank= se->preview_company_rank;
    00096     e->preview_wait        = se->preview_wait;
    00097     e->company_avail       = se->company_avail;
    00098     if (se->name != NULL) e->name = strdup(se->name);
    00099   }
    00100
    00101   /* Get rid of temporary data */
    00102   _temp_engine.clear();
    00103 }
    00104
    00105 static void Load_ENGS()
    00106 {
    00107   /* Load old separate String ID list into a temporary array. This
    00108    * was always 256 entries. */
    00109   StringID names[256];
    00110
    00111   SlArray(names, lengthof(names), SLE_STRINGID);
    00112
    00113   /* Copy each string into the temporary engine array. */
    00114   for (EngineID engine = 0; engine < lengthof(names); engine++) {
    00115     Engine *e = GetTempDataEngine(engine);
    00116     e->name = CopyFromOldName(names[engine]);
    00117   }
    00118 }
    00119
    00121 static const SaveLoad _engine_id_mapping_desc[] = {
    00122   SLE_VAR(EngineIDMapping, grfid,         SLE_UINT32),
    00123   SLE_VAR(EngineIDMapping, internal_id,   SLE_UINT16),
    00124   SLE_VAR(EngineIDMapping, type,          SLE_UINT8),
    00125   SLE_VAR(EngineIDMapping, substitute_id, SLE_UINT8),
    00126   SLE_END()
    00127 };
    00128
    00129 static void Save_EIDS()
    00130 {
    00131   const EngineIDMapping *end = _engine_mngr.End();
    00132   uint index = 0;
    00133   for (EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
    00134     SlSetArrayIndex(index);
    00135     SlObject(eid, _engine_id_mapping_desc);
    00136   }
    00137 }
    00138
    00139 static void Load_EIDS()
    00140 {
    00141   int index;
    00142
    00143   _engine_mngr.Clear();
    00144
    00145   while ((index = SlIterateArray()) != -1) {
    00146     EngineIDMapping *eid = _engine_mngr.Append();
    00147     SlObject(eid, _engine_id_mapping_desc);
    00148   }
    00149 }
    00150
    00151 extern const ChunkHandler _engine_chunk_handlers[] = {
    00152   { 'EIDS', Save_EIDS, Load_EIDS, NULL, CH_ARRAY          },
    00153   { 'ENGN', Save_ENGN, Load_ENGN, NULL, CH_ARRAY          },
    00154   { 'ENGS', NULL,      Load_ENGS, NULL, CH_RIFF | CH_LAST },
    00155 };