# company_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 "../company_func.h"
    00014 #include "../company_manager_face.h"
    00015 #include "../fios.h"
    00016
    00017 #include "saveload.h"
    00018
    00019 #include "table/strings.h"
    00020
    00039 CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
    00040 {
    00041   CompanyManagerFace cmf = 0;
    00042   GenderEthnicity ge = GE_WM;
    00043
    00044   if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
    00045   if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
    00046
    00047   SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN,    ge, ge);
    00048   SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
    00049   SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR,  ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
    00050   SetCompanyManagerFaceBits(cmf, CMFV_CHIN,        ge, ScaleCompanyManagerFaceValue(CMFV_CHIN,     ge, GB(face,  4, 2)));
    00051   SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS,    ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face,  6, 4)));
    00052   SetCompanyManagerFaceBits(cmf, CMFV_HAIR,        ge, ScaleCompanyManagerFaceValue(CMFV_HAIR,     ge, GB(face, 16, 4)));
    00053   SetCompanyManagerFaceBits(cmf, CMFV_JACKET,      ge, ScaleCompanyManagerFaceValue(CMFV_JACKET,   ge, GB(face, 20, 2)));
    00054   SetCompanyManagerFaceBits(cmf, CMFV_COLLAR,      ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR,   ge, GB(face, 22, 2)));
    00055   SetCompanyManagerFaceBits(cmf, CMFV_GLASSES,     ge, GB(face, 28, 1));
    00056
    00057   uint lips = GB(face, 10, 4);
    00058   if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
    00059     SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
    00060     SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
    00061   } else {
    00062     if (!HasBit(ge, GENDER_FEMALE)) {
    00063       lips = lips * 15 / 16;
    00064       lips -= 3;
    00065       if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
    00066     } else {
    00067       lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
    00068     }
    00069     SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
    00070
    00071     uint nose = GB(face, 13, 3);
    00072     if (ge == GE_WF) {
    00073       nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
    00074     } else {
    00075       nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
    00076     }
    00077     SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
    00078   }
    00079
    00080   uint tie_earring = GB(face, 24, 4);
    00081   if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
    00082     if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
    00083     SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
    00084   }
    00085
    00086   return cmf;
    00087 }
    00088
    00089
    00090
    00091 /* Save/load of companies */
    00092 static const SaveLoad _company_desc[] = {
    00093       SLE_VAR(CompanyProperties, name_2,          SLE_UINT32),
    00094       SLE_VAR(CompanyProperties, name_1,          SLE_STRINGID),
    00095   SLE_CONDSTR(CompanyProperties, name,            SLE_STR, 0,                       84, SL_MAX_VERSION),
    00096
    00097       SLE_VAR(CompanyProperties, president_name_1, SLE_UINT16),
    00098       SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32),
    00099   SLE_CONDSTR(CompanyProperties, president_name,  SLE_STR, 0,                       84, SL_MAX_VERSION),
    00100
    00101       SLE_VAR(CompanyProperties, face,            SLE_UINT32),
    00102
    00103   /* money was changed to a 64 bit field in savegame version 1. */
    00104   SLE_CONDVAR(CompanyProperties, money,                 SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
    00105   SLE_CONDVAR(CompanyProperties, money,                 SLE_INT64,                   1, SL_MAX_VERSION),
    00106
    00107   SLE_CONDVAR(CompanyProperties, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00108   SLE_CONDVAR(CompanyProperties, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
    00109
    00110       SLE_VAR(CompanyProperties, colour,                SLE_UINT8),
    00111       SLE_VAR(CompanyProperties, money_fraction,        SLE_UINT8),
    00112   SLE_CONDVAR(CompanyProperties, avail_railtypes,       SLE_UINT8,                   0, 57),
    00113       SLE_VAR(CompanyProperties, block_preview,         SLE_UINT8),
    00114
    00115   SLE_CONDVAR(CompanyProperties, cargo_types,           SLE_FILE_U16 | SLE_VAR_U32,  0, 93),
    00116   SLE_CONDVAR(CompanyProperties, cargo_types,           SLE_UINT32,                 94, SL_MAX_VERSION),
    00117   SLE_CONDVAR(CompanyProperties, location_of_HQ,        SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00118   SLE_CONDVAR(CompanyProperties, location_of_HQ,        SLE_UINT32,                  6, SL_MAX_VERSION),
    00119   SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00120   SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_UINT32,                  6, SL_MAX_VERSION),
    00121   SLE_CONDVAR(CompanyProperties, inaugurated_year,      SLE_FILE_U8  | SLE_VAR_I32,  0, 30),
    00122   SLE_CONDVAR(CompanyProperties, inaugurated_year,      SLE_INT32,                  31, SL_MAX_VERSION),
    00123
    00124       SLE_ARR(CompanyProperties, share_owners,          SLE_UINT8, 4),
    00125
    00126       SLE_VAR(CompanyProperties, num_valid_stat_ent,    SLE_UINT8),
    00127
    00128       SLE_VAR(CompanyProperties, quarters_of_bankruptcy,SLE_UINT8),
    00129   SLE_CONDVAR(CompanyProperties, bankrupt_asked,        SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
    00130   SLE_CONDVAR(CompanyProperties, bankrupt_asked,        SLE_UINT16,                104, SL_MAX_VERSION),
    00131       SLE_VAR(CompanyProperties, bankrupt_timeout,      SLE_INT16),
    00132   SLE_CONDVAR(CompanyProperties, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00133   SLE_CONDVAR(CompanyProperties, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
    00134
    00135   /* yearly expenses was changed to 64-bit in savegame version 2. */
    00136   SLE_CONDARR(CompanyProperties, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
    00137   SLE_CONDARR(CompanyProperties, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
    00138
    00139   SLE_CONDVAR(CompanyProperties, is_ai,                 SLE_BOOL,                    2, SL_MAX_VERSION),
    00140   SLE_CONDNULL(1, 107, 111),
    00141   SLE_CONDNULL(1, 4, 99),
    00142
    00143   SLE_CONDVAR(CompanyProperties, terraform_limit,       SLE_UINT32,                156, SL_MAX_VERSION),
    00144   SLE_CONDVAR(CompanyProperties, clear_limit,           SLE_UINT32,                156, SL_MAX_VERSION),
    00145
    00146   SLE_END()
    00147 };
    00148
    00149 static const SaveLoad _company_settings_desc[] = {
    00150   /* Engine renewal settings */
    00151   SLE_CONDNULL(512, 16, 18),
    00152   SLE_CONDREF(Company, engine_renew_list,            REF_ENGINE_RENEWS,   19, SL_MAX_VERSION),
    00153   SLE_CONDVAR(Company, settings.engine_renew,        SLE_BOOL,            16, SL_MAX_VERSION),
    00154   SLE_CONDVAR(Company, settings.engine_renew_months, SLE_INT16,           16, SL_MAX_VERSION),
    00155   SLE_CONDVAR(Company, settings.engine_renew_money,  SLE_UINT32,          16, SL_MAX_VERSION),
    00156   SLE_CONDVAR(Company, settings.renew_keep_length,   SLE_BOOL,             2, SL_MAX_VERSION),
    00157
    00158   /* Default vehicle settings */
    00159   SLE_CONDVAR(Company, settings.vehicle.servint_ispercent,   SLE_BOOL,     120, SL_MAX_VERSION),
    00160   SLE_CONDVAR(Company, settings.vehicle.servint_trains,    SLE_UINT16,     120, SL_MAX_VERSION),
    00161   SLE_CONDVAR(Company, settings.vehicle.servint_roadveh,   SLE_UINT16,     120, SL_MAX_VERSION),
    00162   SLE_CONDVAR(Company, settings.vehicle.servint_aircraft,  SLE_UINT16,     120, SL_MAX_VERSION),
    00163   SLE_CONDVAR(Company, settings.vehicle.servint_ships,     SLE_UINT16,     120, SL_MAX_VERSION),
    00164
    00165   SLE_CONDNULL(63, 2, 143), // old reserved space
    00166
    00167   SLE_END()
    00168 };
    00169
    00170 static const SaveLoad _company_settings_skip_desc[] = {
    00171   /* Engine renewal settings */
    00172   SLE_CONDNULL(512, 16, 18),
    00173   SLE_CONDNULL(2, 19, 68),                 // engine_renew_list
    00174   SLE_CONDNULL(4, 69, SL_MAX_VERSION),     // engine_renew_list
    00175   SLE_CONDNULL(1, 16, SL_MAX_VERSION),     // settings.engine_renew
    00176   SLE_CONDNULL(2, 16, SL_MAX_VERSION),     // settings.engine_renew_months
    00177   SLE_CONDNULL(4, 16, SL_MAX_VERSION),     // settings.engine_renew_money
    00178   SLE_CONDNULL(1,  2, SL_MAX_VERSION),     // settings.renew_keep_length
    00179
    00180   /* Default vehicle settings */
    00181   SLE_CONDNULL(1, 120, SL_MAX_VERSION),    // settings.vehicle.servint_ispercent
    00182   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_trains
    00183   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_roadveh
    00184   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_aircraft
    00185   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_ships
    00186
    00187   SLE_CONDNULL(63, 2, 143), // old reserved space
    00188
    00189   SLE_END()
    00190 };
    00191
    00192 static const SaveLoad _company_economy_desc[] = {
    00193   /* these were changed to 64-bit in savegame format 2 */
    00194   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00195   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
    00196   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00197   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
    00198   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00199   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
    00200
    00201       SLE_VAR(CompanyEconomyEntry, delivered_cargo,     SLE_INT32),
    00202       SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
    00203
    00204   SLE_END()
    00205 };
    00206
    00207 /* We do need to read this single value, as the bigger it gets, the more data is stored */
    00208 struct CompanyOldAI {
    00209   uint8 num_build_rec;
    00210 };
    00211
    00212 static const SaveLoad _company_ai_desc[] = {
    00213   SLE_CONDNULL(2,  0, 106),
    00214   SLE_CONDNULL(2,  0, 12),
    00215   SLE_CONDNULL(4, 13, 106),
    00216   SLE_CONDNULL(8,  0, 106),
    00217    SLE_CONDVAR(CompanyOldAI, num_build_rec, SLE_UINT8, 0, 106),
    00218   SLE_CONDNULL(3,  0, 106),
    00219
    00220   SLE_CONDNULL(2,  0,  5),
    00221   SLE_CONDNULL(4,  6, 106),
    00222   SLE_CONDNULL(2,  0,  5),
    00223   SLE_CONDNULL(4,  6, 106),
    00224   SLE_CONDNULL(2,  0, 106),
    00225
    00226   SLE_CONDNULL(2,  0,  5),
    00227   SLE_CONDNULL(4,  6, 106),
    00228   SLE_CONDNULL(2,  0,  5),
    00229   SLE_CONDNULL(4,  6, 106),
    00230   SLE_CONDNULL(2,  0, 106),
    00231
    00232   SLE_CONDNULL(2,  0, 68),
    00233   SLE_CONDNULL(4,  69, 106),
    00234
    00235   SLE_CONDNULL(18, 0, 106),
    00236   SLE_CONDNULL(20, 0, 106),
    00237   SLE_CONDNULL(32, 0, 106),
    00238
    00239   SLE_CONDNULL(64, 2, 106),
    00240   SLE_END()
    00241 };
    00242
    00243 static const SaveLoad _company_ai_build_rec_desc[] = {
    00244   SLE_CONDNULL(2, 0, 5),
    00245   SLE_CONDNULL(4, 6, 106),
    00246   SLE_CONDNULL(2, 0, 5),
    00247   SLE_CONDNULL(4, 6, 106),
    00248   SLE_CONDNULL(8, 0, 106),
    00249   SLE_END()
    00250 };
    00251
    00252 static const SaveLoad _company_livery_desc[] = {
    00253   SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
    00254   SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
    00255   SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
    00256   SLE_END()
    00257 };
    00258
    00259 static void SaveLoad_PLYR_common(Company *c, CompanyProperties *cprops)
    00260 {
    00261   int i;
    00262
    00263   SlObject(cprops, _company_desc);
    00264   if (c != NULL) {
    00265     SlObject(c, _company_settings_desc);
    00266   } else {
    00267     char nothing;
    00268     SlObject(&nothing, _company_settings_skip_desc);
    00269   }
    00270
    00271   /* Keep backwards compatible for savegames, so load the old AI block */
    00272   if (IsSavegameVersionBefore(107) && cprops->is_ai) {
    00273     CompanyOldAI old_ai;
    00274     char nothing;
    00275
    00276     SlObject(&old_ai, _company_ai_desc);
    00277     for (i = 0; i != old_ai.num_build_rec; i++) {
    00278       SlObject(&nothing, _company_ai_build_rec_desc);
    00279     }
    00280   }
    00281
    00282   /* Write economy */
    00283   SlObject(&cprops->cur_economy, _company_economy_desc);
    00284
    00285   /* Write old economy entries. */
    00286   for (i = 0; i < cprops->num_valid_stat_ent; i++) {
    00287     SlObject(&cprops->old_economy[i], _company_economy_desc);
    00288   }
    00289
    00290   /* Write each livery entry. */
    00291   int num_liveries = IsSavegameVersionBefore(63) ? LS_END - 4 : (IsSavegameVersionBefore(85) ? LS_END - 2: LS_END);
    00292   if (c != NULL) {
    00293     for (i = 0; i < num_liveries; i++) {
    00294       SlObject(&c->livery[i], _company_livery_desc);
    00295     }
    00296
    00297     if (num_liveries < LS_END) {
    00298       /* We want to insert some liveries somewhere in between. This means some have to be moved. */
    00299       memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
    00300       c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
    00301       c->livery[LS_PASSENGER_WAGON_MAGLEV]   = c->livery[LS_MAGLEV];
    00302     }
    00303
    00304     if (num_liveries == LS_END - 4) {
    00305       /* Copy bus/truck liveries over to trams */
    00306       c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
    00307       c->livery[LS_FREIGHT_TRAM]   = c->livery[LS_TRUCK];
    00308     }
    00309   } else {
    00310     /* Skip liveries */
    00311     Livery dummy_livery;
    00312     for (i = 0; i < num_liveries; i++) {
    00313       SlObject(&dummy_livery, _company_livery_desc);
    00314     }
    00315   }
    00316 }
    00317
    00318 static void SaveLoad_PLYR(Company *c)
    00319 {
    00320   SaveLoad_PLYR_common(c, c);
    00321 }
    00322
    00323 static void Save_PLYR()
    00324 {
    00325   Company *c;
    00326   FOR_ALL_COMPANIES(c) {
    00327     SlSetArrayIndex(c->index);
    00328     SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
    00329   }
    00330 }
    00331
    00332 static void Load_PLYR()
    00333 {
    00334   int index;
    00335   while ((index = SlIterateArray()) != -1) {
    00336     Company *c = new (index) Company();
    00337     SaveLoad_PLYR(c);
    00338     _company_colours[index] = (Colours)c->colour;
    00339   }
    00340 }
    00341
    00342 static void Check_PLYR()
    00343 {
    00344   int index;
    00345   while ((index = SlIterateArray()) != -1) {
    00346     CompanyProperties *cprops = new CompanyProperties();
    00347     memset(cprops, 0, sizeof(*cprops));
    00348     SaveLoad_PLYR_common(NULL, cprops);
    00349
    00350     /* We do not load old custom names */
    00351     if (IsSavegameVersionBefore(84))
    00352     {
    00353       if (GB(cprops->name_1, 11, 5) == 15) {
    00354         cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
    00355       }
    00356
    00357       if (GB(cprops->president_name_1, 11, 5) == 15) {
    00358         cprops->president_name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
    00359       }
    00360     }
    00361
    00362     if (!_load_check_data.companies.Insert(index, cprops)) delete cprops;
    00363   }
    00364 }
    00365
    00366 static void Ptrs_PLYR()
    00367 {
    00368   Company *c;
    00369   FOR_ALL_COMPANIES(c) {
    00370     SlObject(c, _company_settings_desc);
    00371   }
    00372 }
    00373
    00374
    00375 extern const ChunkHandler _company_chunk_handlers[] = {
    00376   { 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY | CH_LAST},
    00377 };