# company_sl.cpp

Go to the documentation of this file.

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