# company_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: company_sl.cpp 15428 2009-02-09 02:57:15Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../company_base.h"
    00007 #include "../company_func.h"
    00008 #include "../company_manager_face.h"
    00009
    00010 #include "saveload.h"
    00011
    00030 CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
    00031 {
    00032   CompanyManagerFace cmf = 0;
    00033   GenderEthnicity ge = GE_WM;
    00034
    00035   if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
    00036   if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
    00037
    00038   SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN,    ge, ge);
    00039   SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
    00040   SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR,  ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
    00041   SetCompanyManagerFaceBits(cmf, CMFV_CHIN,        ge, ScaleCompanyManagerFaceValue(CMFV_CHIN,     ge, GB(face,  4, 2)));
    00042   SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS,    ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face,  6, 4)));
    00043   SetCompanyManagerFaceBits(cmf, CMFV_HAIR,        ge, ScaleCompanyManagerFaceValue(CMFV_HAIR,     ge, GB(face, 16, 4)));
    00044   SetCompanyManagerFaceBits(cmf, CMFV_JACKET,      ge, ScaleCompanyManagerFaceValue(CMFV_JACKET,   ge, GB(face, 20, 2)));
    00045   SetCompanyManagerFaceBits(cmf, CMFV_COLLAR,      ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR,   ge, GB(face, 22, 2)));
    00046   SetCompanyManagerFaceBits(cmf, CMFV_GLASSES,     ge, GB(face, 28, 1));
    00047
    00048   uint lips = GB(face, 10, 4);
    00049   if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
    00050     SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
    00051     SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
    00052   } else {
    00053     if (!HasBit(ge, GENDER_FEMALE)) {
    00054       lips = lips * 15 / 16;
    00055       lips -= 3;
    00056       if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
    00057     } else {
    00058       lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
    00059     }
    00060     SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
    00061
    00062     uint nose = GB(face, 13, 3);
    00063     if (ge == GE_WF) {
    00064       nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
    00065     } else {
    00066       nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
    00067     }
    00068     SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
    00069   }
    00070
    00071   uint tie_earring = GB(face, 24, 4);
    00072   if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
    00073     if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
    00074     SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
    00075   }
    00076
    00077   return cmf;
    00078 }
    00079
    00080
    00081
    00082 /* Save/load of companies */
    00083 static const SaveLoad _company_desc[] = {
    00084       SLE_VAR(Company, name_2,          SLE_UINT32),
    00085       SLE_VAR(Company, name_1,          SLE_STRINGID),
    00086   SLE_CONDSTR(Company, name,            SLE_STR, 0,                       84, SL_MAX_VERSION),
    00087
    00088       SLE_VAR(Company, president_name_1, SLE_UINT16),
    00089       SLE_VAR(Company, president_name_2, SLE_UINT32),
    00090   SLE_CONDSTR(Company, president_name,  SLE_STR, 0,                       84, SL_MAX_VERSION),
    00091
    00092       SLE_VAR(Company, face,            SLE_UINT32),
    00093
    00094   /* money was changed to a 64 bit field in savegame version 1. */
    00095   SLE_CONDVAR(Company, money,                 SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
    00096   SLE_CONDVAR(Company, money,                 SLE_INT64,                   1, SL_MAX_VERSION),
    00097
    00098   SLE_CONDVAR(Company, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00099   SLE_CONDVAR(Company, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
    00100
    00101       SLE_VAR(Company, colour,                SLE_UINT8),
    00102       SLE_VAR(Company, money_fraction,        SLE_UINT8),
    00103   SLE_CONDVAR(Company, avail_railtypes,       SLE_UINT8,                   0, 57),
    00104       SLE_VAR(Company, block_preview,         SLE_UINT8),
    00105
    00106   SLE_CONDVAR(Company, cargo_types,           SLE_FILE_U16 | SLE_VAR_U32,  0, 93),
    00107   SLE_CONDVAR(Company, cargo_types,           SLE_UINT32,                 94, SL_MAX_VERSION),
    00108   SLE_CONDVAR(Company, location_of_HQ,        SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00109   SLE_CONDVAR(Company, location_of_HQ,        SLE_UINT32,                  6, SL_MAX_VERSION),
    00110   SLE_CONDVAR(Company, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00111   SLE_CONDVAR(Company, last_build_coordinate, SLE_UINT32,                  6, SL_MAX_VERSION),
    00112   SLE_CONDVAR(Company, inaugurated_year,      SLE_FILE_U8  | SLE_VAR_I32,  0, 30),
    00113   SLE_CONDVAR(Company, inaugurated_year,      SLE_INT32,                  31, SL_MAX_VERSION),
    00114
    00115       SLE_ARR(Company, share_owners,          SLE_UINT8, 4),
    00116
    00117       SLE_VAR(Company, num_valid_stat_ent,    SLE_UINT8),
    00118
    00119       SLE_VAR(Company, quarters_of_bankrupcy, SLE_UINT8),
    00120   SLE_CONDVAR(Company, bankrupt_asked,        SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
    00121   SLE_CONDVAR(Company, bankrupt_asked,        SLE_UINT16,                104, SL_MAX_VERSION),
    00122       SLE_VAR(Company, bankrupt_timeout,      SLE_INT16),
    00123   SLE_CONDVAR(Company, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00124   SLE_CONDVAR(Company, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
    00125
    00126   /* yearly expenses was changed to 64-bit in savegame version 2. */
    00127   SLE_CONDARR(Company, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
    00128   SLE_CONDARR(Company, yearly_expenses,       SLE_INT64, 3 * 13,                  2, SL_MAX_VERSION),
    00129
    00130   SLE_CONDVAR(Company, is_ai,                 SLE_BOOL,                    2, SL_MAX_VERSION),
    00131   SLE_CONDNULL(1, 107, 111),
    00132   SLE_CONDNULL(1, 4, 99),
    00133
    00134   /* Engine renewal settings */
    00135   SLE_CONDNULL(512, 16, 18),
    00136   SLE_CONDREF(Company, engine_renew_list,     REF_ENGINE_RENEWS,          19, SL_MAX_VERSION),
    00137   SLE_CONDVAR(Company, engine_renew,          SLE_BOOL,                   16, SL_MAX_VERSION),
    00138   SLE_CONDVAR(Company, engine_renew_months,   SLE_INT16,                  16, SL_MAX_VERSION),
    00139   SLE_CONDVAR(Company, engine_renew_money,    SLE_UINT32,                 16, SL_MAX_VERSION),
    00140   SLE_CONDVAR(Company, renew_keep_length,     SLE_BOOL,                    2, SL_MAX_VERSION), // added with 16.1, but was blank since 2
    00141
    00142   /* Reserve extra space in savegame here. (currently 63 bytes) */
    00143   SLE_CONDNULL(63, 2, SL_MAX_VERSION),
    00144
    00145   SLE_END()
    00146 };
    00147
    00148 static const SaveLoad _company_economy_desc[] = {
    00149   /* these were changed to 64-bit in savegame format 2 */
    00150   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00151   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
    00152   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00153   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
    00154   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00155   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
    00156
    00157       SLE_VAR(CompanyEconomyEntry, delivered_cargo,     SLE_INT32),
    00158       SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
    00159
    00160   SLE_END()
    00161 };
    00162
    00163 /* We do need to read this single value, as the bigger it gets, the more data is stored */
    00164 struct CompanyOldAI {
    00165   uint8 num_build_rec;
    00166 };
    00167
    00168 static const SaveLoad _company_ai_desc[] = {
    00169   SLE_CONDNULL(2,  0, 106),
    00170   SLE_CONDNULL(2,  0, 12),
    00171   SLE_CONDNULL(4, 13, 106),
    00172   SLE_CONDNULL(8,  0, 106),
    00173    SLE_CONDVAR(CompanyOldAI, num_build_rec, SLE_UINT8, 0, 106),
    00174   SLE_CONDNULL(3,  0, 106),
    00175
    00176   SLE_CONDNULL(2,  0,  5),
    00177   SLE_CONDNULL(4,  6, 106),
    00178   SLE_CONDNULL(2,  0,  5),
    00179   SLE_CONDNULL(4,  6, 106),
    00180   SLE_CONDNULL(2,  0, 106),
    00181
    00182   SLE_CONDNULL(2,  0,  5),
    00183   SLE_CONDNULL(4,  6, 106),
    00184   SLE_CONDNULL(2,  0,  5),
    00185   SLE_CONDNULL(4,  6, 106),
    00186   SLE_CONDNULL(2,  0, 106),
    00187
    00188   SLE_CONDNULL(2,  0, 68),
    00189   SLE_CONDNULL(4,  69, 106),
    00190
    00191   SLE_CONDNULL(18, 0, 106),
    00192   SLE_CONDNULL(20, 0, 106),
    00193   SLE_CONDNULL(32, 0, 106),
    00194
    00195   SLE_CONDNULL(64, 2, 106),
    00196   SLE_END()
    00197 };
    00198
    00199 static const SaveLoad _company_ai_build_rec_desc[] = {
    00200   SLE_CONDNULL(2, 0, 5),
    00201   SLE_CONDNULL(4, 6, 106),
    00202   SLE_CONDNULL(2, 0, 5),
    00203   SLE_CONDNULL(4, 6, 106),
    00204   SLE_CONDNULL(8, 0, 106),
    00205   SLE_END()
    00206 };
    00207
    00208 static const SaveLoad _company_livery_desc[] = {
    00209   SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
    00210   SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
    00211   SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
    00212   SLE_END()
    00213 };
    00214
    00215 static void SaveLoad_PLYR(Company *c)
    00216 {
    00217   int i;
    00218
    00219   SlObject(c, _company_desc);
    00220
    00221   /* Keep backwards compatible for savegames, so load the old AI block */
    00222   if (CheckSavegameVersion(107) && !IsHumanCompany(c->index)) {
    00223     CompanyOldAI old_ai;
    00224     char nothing;
    00225
    00226     SlObject(&old_ai, _company_ai_desc);
    00227     for (i = 0; i != old_ai.num_build_rec; i++) {
    00228       SlObject(&nothing, _company_ai_build_rec_desc);
    00229     }
    00230   }
    00231
    00232   /* Write economy */
    00233   SlObject(&c->cur_economy, _company_economy_desc);
    00234
    00235   /* Write old economy entries. */
    00236   for (i = 0; i < c->num_valid_stat_ent; i++) {
    00237     SlObject(&c->old_economy[i], _company_economy_desc);
    00238   }
    00239
    00240   /* Write each livery entry. */
    00241   int num_liveries = CheckSavegameVersion(63) ? LS_END - 4 : (CheckSavegameVersion(85) ? LS_END - 2: LS_END);
    00242   for (i = 0; i < num_liveries; i++) {
    00243     SlObject(&c->livery[i], _company_livery_desc);
    00244   }
    00245
    00246   if (num_liveries < LS_END) {
    00247     /* We want to insert some liveries somewhere in between. This means some have to be moved. */
    00248     memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
    00249     c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
    00250     c->livery[LS_PASSENGER_WAGON_MAGLEV]   = c->livery[LS_MAGLEV];
    00251   }
    00252
    00253   if (num_liveries == LS_END - 4) {
    00254     /* Copy bus/truck liveries over to trams */
    00255     c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
    00256     c->livery[LS_FREIGHT_TRAM]   = c->livery[LS_TRUCK];
    00257   }
    00258 }
    00259
    00260 static void Save_PLYR()
    00261 {
    00262   Company *c;
    00263   FOR_ALL_COMPANIES(c) {
    00264     SlSetArrayIndex(c->index);
    00265     SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
    00266   }
    00267 }
    00268
    00269 static void Load_PLYR()
    00270 {
    00271   int index;
    00272   while ((index = SlIterateArray()) != -1) {
    00273     Company *c = new (index) Company();
    00274     SaveLoad_PLYR(c);
    00275     _company_colours[index] = (Colours)c->colour;
    00276   }
    00277 }
    00278
    00279 extern const ChunkHandler _company_chunk_handlers[] = {
    00280   { 'PLYR', Save_PLYR, Load_PLYR, CH_ARRAY | CH_LAST},
    00281 };