engine_sl.cpp

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

Generated on Sun May 15 19:20:14 2011 for OpenTTD by  doxygen 1.6.1