# engine_sl.cpp

Go to the documentation of this file.

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