# 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 #include "../rail_map.h"
    00017 #include "../road_map.h"
    00018 #include "../station_map.h"
    00019 #include "../tunnelbridge_map.h"
    00020 #include "../tunnelbridge.h"
    00021 #include "../station_base.h"
    00022
    00023 #include "saveload.h"
    00024
    00025 #include "table/strings.h"
    00026
    00045 CompanyManagerFace ConvertFromOldCompanyManagerFace(uint32 face)
    00046 {
    00047   CompanyManagerFace cmf = 0;
    00048   GenderEthnicity ge = GE_WM;
    00049
    00050   if (HasBit(face, 31)) SetBit(ge, GENDER_FEMALE);
    00051   if (HasBit(face, 27) && (HasBit(face, 26) == HasBit(face, 19))) SetBit(ge, ETHNICITY_BLACK);
    00052
    00053   SetCompanyManagerFaceBits(cmf, CMFV_GEN_ETHN,    ge, ge);
    00054   SetCompanyManagerFaceBits(cmf, CMFV_HAS_GLASSES, ge, GB(face, 28, 3) <= 1);
    00055   SetCompanyManagerFaceBits(cmf, CMFV_EYE_COLOUR,  ge, HasBit(ge, ETHNICITY_BLACK) ? 0 : ClampU(GB(face, 20, 3), 5, 7) - 5);
    00056   SetCompanyManagerFaceBits(cmf, CMFV_CHIN,        ge, ScaleCompanyManagerFaceValue(CMFV_CHIN,     ge, GB(face,  4, 2)));
    00057   SetCompanyManagerFaceBits(cmf, CMFV_EYEBROWS,    ge, ScaleCompanyManagerFaceValue(CMFV_EYEBROWS, ge, GB(face,  6, 4)));
    00058   SetCompanyManagerFaceBits(cmf, CMFV_HAIR,        ge, ScaleCompanyManagerFaceValue(CMFV_HAIR,     ge, GB(face, 16, 4)));
    00059   SetCompanyManagerFaceBits(cmf, CMFV_JACKET,      ge, ScaleCompanyManagerFaceValue(CMFV_JACKET,   ge, GB(face, 20, 2)));
    00060   SetCompanyManagerFaceBits(cmf, CMFV_COLLAR,      ge, ScaleCompanyManagerFaceValue(CMFV_COLLAR,   ge, GB(face, 22, 2)));
    00061   SetCompanyManagerFaceBits(cmf, CMFV_GLASSES,     ge, GB(face, 28, 1));
    00062
    00063   uint lips = GB(face, 10, 4);
    00064   if (!HasBit(ge, GENDER_FEMALE) && lips < 4) {
    00065     SetCompanyManagerFaceBits(cmf, CMFV_HAS_MOUSTACHE, ge, true);
    00066     SetCompanyManagerFaceBits(cmf, CMFV_MOUSTACHE,     ge, max(lips, 1U) - 1);
    00067   } else {
    00068     if (!HasBit(ge, GENDER_FEMALE)) {
    00069       lips = lips * 15 / 16;
    00070       lips -= 3;
    00071       if (HasBit(ge, ETHNICITY_BLACK) && lips > 8) lips = 0;
    00072     } else {
    00073       lips = ScaleCompanyManagerFaceValue(CMFV_LIPS, ge, lips);
    00074     }
    00075     SetCompanyManagerFaceBits(cmf, CMFV_LIPS, ge, lips);
    00076
    00077     uint nose = GB(face, 13, 3);
    00078     if (ge == GE_WF) {
    00079       nose = (nose * 3 >> 3) * 3 >> 2; // There is 'hole' in the nose sprites for females
    00080     } else {
    00081       nose = ScaleCompanyManagerFaceValue(CMFV_NOSE, ge, nose);
    00082     }
    00083     SetCompanyManagerFaceBits(cmf, CMFV_NOSE, ge, nose);
    00084   }
    00085
    00086   uint tie_earring = GB(face, 24, 4);
    00087   if (!HasBit(ge, GENDER_FEMALE) || tie_earring < 3) { // Not all females have an earring
    00088     if (HasBit(ge, GENDER_FEMALE)) SetCompanyManagerFaceBits(cmf, CMFV_HAS_TIE_EARRING, ge, true);
    00089     SetCompanyManagerFaceBits(cmf, CMFV_TIE_EARRING, ge, HasBit(ge, GENDER_FEMALE) ? tie_earring : ScaleCompanyManagerFaceValue(CMFV_TIE_EARRING, ge, tie_earring / 2));
    00090   }
    00091
    00092   return cmf;
    00093 }
    00094
    00096 void AfterLoadCompanyStats()
    00097 {
    00098   /* Reset infrastructure statistics to zero. */
    00099   Company *c;
    00100   FOR_ALL_COMPANIES(c) {
    00101     MemSetT(c->road_infrastructure, 0, lengthof(c->road_infrastructure));
    00102     MemSetT(c->rail_infrastructure, 0, lengthof(c->rail_infrastructure));
    00103     c->signal_infrastructure = 0;
    00104     c->water_infrastructure = 0;
    00105     c->station_infrastructure = 0;
    00106     c->airport_infrastructure = 0;
    00107   }
    00108
    00109   /* Collect airport count. */
    00110   Station *st;
    00111   FOR_ALL_STATIONS(st) {
    00112     if ((st->facilities & FACIL_AIRPORT) && Company::IsValidID(st->owner)) {
    00113       Company::Get(st->owner)->airport_infrastructure++;
    00114     }
    00115   }
    00116
    00117   for (TileIndex tile = 0; tile < MapSize(); tile++) {
    00118     switch (GetTileType(tile)) {
    00119       case MP_RAILWAY:
    00120         c = Company::GetIfValid(GetTileOwner(tile));
    00121         if (c != NULL) {
    00122           uint pieces = 1;
    00123           if (IsPlainRail(tile)) {
    00124             TrackBits bits = GetTrackBits(tile);
    00125             pieces = CountBits(bits);
    00126             if (TracksOverlap(bits)) pieces *= pieces;
    00127           }
    00128           c->rail_infrastructure[GetRailType(tile)] += pieces;
    00129
    00130           if (HasSignals(tile)) c->signal_infrastructure += CountBits(GetPresentSignals(tile));
    00131         }
    00132         break;
    00133
    00134       case MP_ROAD: {
    00135         if (IsLevelCrossing(tile)) {
    00136           c = Company::GetIfValid(GetTileOwner(tile));
    00137           if (c != NULL) c->rail_infrastructure[GetRailType(tile)] += LEVELCROSSING_TRACKBIT_FACTOR;
    00138         }
    00139
    00140         /* Iterate all present road types as each can have a different owner. */
    00141         RoadType rt;
    00142         FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
    00143           c = Company::GetIfValid(IsRoadDepot(tile) ? GetTileOwner(tile) : GetRoadOwner(tile, rt));
    00144           /* A level crossings and depots have two road bits. */
    00145           if (c != NULL) c->road_infrastructure[rt] += IsNormalRoad(tile) ? CountBits(GetRoadBits(tile, rt)) : 2;
    00146         }
    00147         break;
    00148       }
    00149
    00150       case MP_STATION:
    00151         c = Company::GetIfValid(GetTileOwner(tile));
    00152         if (c != NULL && GetStationType(tile) != STATION_AIRPORT && !IsBuoy(tile)) c->station_infrastructure++;
    00153
    00154         switch (GetStationType(tile)) {
    00155           case STATION_RAIL:
    00156           case STATION_WAYPOINT:
    00157             if (c != NULL && !IsStationTileBlocked(tile)) c->rail_infrastructure[GetRailType(tile)]++;
    00158             break;
    00159
    00160           case STATION_BUS:
    00161           case STATION_TRUCK: {
    00162             /* Iterate all present road types as each can have a different owner. */
    00163             RoadType rt;
    00164             FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
    00165               c = Company::GetIfValid(GetRoadOwner(tile, rt));
    00166               if (c != NULL) c->road_infrastructure[rt] += 2; // A road stop has two road bits.
    00167             }
    00168             break;
    00169           }
    00170
    00171           case STATION_DOCK:
    00172           case STATION_BUOY:
    00173             if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
    00174               if (c != NULL) c->water_infrastructure++;
    00175             }
    00176             break;
    00177
    00178           default:
    00179             break;
    00180         }
    00181         break;
    00182
    00183       case MP_WATER:
    00184         if (IsShipDepot(tile) || IsLock(tile)) {
    00185           c = Company::GetIfValid(GetTileOwner(tile));
    00186           if (c != NULL) c->water_infrastructure += LOCK_DEPOT_TILE_FACTOR;
    00187         }
    00188         /* FALL THROUGH */
    00189
    00190       case MP_OBJECT:
    00191         if (GetWaterClass(tile) == WATER_CLASS_CANAL) {
    00192           c = Company::GetIfValid(GetTileOwner(tile));
    00193           if (c != NULL) c->water_infrastructure++;
    00194         }
    00195         break;
    00196
    00197       case MP_TUNNELBRIDGE: {
    00198         /* Only count the tunnel/bridge if we're on the northern end tile. */
    00199         TileIndex other_end = GetOtherTunnelBridgeEnd(tile);
    00200         if (tile < other_end) {
    00201           /* Count each tunnel/bridge TUNNELBRIDGE_TRACKBIT_FACTOR times to simulate
    00202            * the higher structural maintenance needs, and don't forget the end tiles. */
    00203           uint len = (GetTunnelBridgeLength(tile, other_end) + 2) * TUNNELBRIDGE_TRACKBIT_FACTOR;
    00204
    00205           switch (GetTunnelBridgeTransportType(tile)) {
    00206             case TRANSPORT_RAIL:
    00207               c = Company::GetIfValid(GetTileOwner(tile));
    00208               if (c != NULL) c->rail_infrastructure[GetRailType(tile)] += len;
    00209               break;
    00210
    00211             case TRANSPORT_ROAD: {
    00212               /* Iterate all present road types as each can have a different owner. */
    00213               RoadType rt;
    00214               FOR_EACH_SET_ROADTYPE(rt, GetRoadTypes(tile)) {
    00215                 c = Company::GetIfValid(GetRoadOwner(tile, rt));
    00216                 if (c != NULL) c->road_infrastructure[rt] += len * 2; // A full diagonal road has two road bits.
    00217               }
    00218               break;
    00219             }
    00220
    00221             case TRANSPORT_WATER:
    00222               c = Company::GetIfValid(GetTileOwner(tile));
    00223               if (c != NULL) c->water_infrastructure += len;
    00224               break;
    00225
    00226             default:
    00227               break;
    00228           }
    00229         }
    00230         break;
    00231       }
    00232
    00233       default:
    00234         break;
    00235     }
    00236   }
    00237 }
    00238
    00239 /* Save/load of companies */
    00240 static const SaveLoad _company_desc[] = {
    00241       SLE_VAR(CompanyProperties, name_2,          SLE_UINT32),
    00242       SLE_VAR(CompanyProperties, name_1,          SLE_STRINGID),
    00243   SLE_CONDSTR(CompanyProperties, name,            SLE_STR, 0,                       84, SL_MAX_VERSION),
    00244
    00245       SLE_VAR(CompanyProperties, president_name_1, SLE_UINT16),
    00246       SLE_VAR(CompanyProperties, president_name_2, SLE_UINT32),
    00247   SLE_CONDSTR(CompanyProperties, president_name,  SLE_STR, 0,                       84, SL_MAX_VERSION),
    00248
    00249       SLE_VAR(CompanyProperties, face,            SLE_UINT32),
    00250
    00251   /* money was changed to a 64 bit field in savegame version 1. */
    00252   SLE_CONDVAR(CompanyProperties, money,                 SLE_VAR_I64 | SLE_FILE_I32,  0, 0),
    00253   SLE_CONDVAR(CompanyProperties, money,                 SLE_INT64,                   1, SL_MAX_VERSION),
    00254
    00255   SLE_CONDVAR(CompanyProperties, current_loan,          SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00256   SLE_CONDVAR(CompanyProperties, current_loan,          SLE_INT64,                  65, SL_MAX_VERSION),
    00257
    00258       SLE_VAR(CompanyProperties, colour,                SLE_UINT8),
    00259       SLE_VAR(CompanyProperties, money_fraction,        SLE_UINT8),
    00260   SLE_CONDVAR(CompanyProperties, avail_railtypes,       SLE_UINT8,                   0, 57),
    00261       SLE_VAR(CompanyProperties, block_preview,         SLE_UINT8),
    00262
    00263   SLE_CONDVAR(CompanyProperties, cargo_types,           SLE_FILE_U16 | SLE_VAR_U32,  0, 93),
    00264   SLE_CONDVAR(CompanyProperties, cargo_types,           SLE_UINT32,                 94, SL_MAX_VERSION),
    00265   SLE_CONDVAR(CompanyProperties, location_of_HQ,        SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00266   SLE_CONDVAR(CompanyProperties, location_of_HQ,        SLE_UINT32,                  6, SL_MAX_VERSION),
    00267   SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
    00268   SLE_CONDVAR(CompanyProperties, last_build_coordinate, SLE_UINT32,                  6, SL_MAX_VERSION),
    00269   SLE_CONDVAR(CompanyProperties, inaugurated_year,      SLE_FILE_U8  | SLE_VAR_I32,  0, 30),
    00270   SLE_CONDVAR(CompanyProperties, inaugurated_year,      SLE_INT32,                  31, SL_MAX_VERSION),
    00271
    00272       SLE_ARR(CompanyProperties, share_owners,          SLE_UINT8, 4),
    00273
    00274       SLE_VAR(CompanyProperties, num_valid_stat_ent,    SLE_UINT8),
    00275
    00276       SLE_VAR(CompanyProperties, quarters_of_bankruptcy,SLE_UINT8),
    00277   SLE_CONDVAR(CompanyProperties, bankrupt_asked,        SLE_FILE_U8  | SLE_VAR_U16,  0, 103),
    00278   SLE_CONDVAR(CompanyProperties, bankrupt_asked,        SLE_UINT16,                104, SL_MAX_VERSION),
    00279       SLE_VAR(CompanyProperties, bankrupt_timeout,      SLE_INT16),
    00280   SLE_CONDVAR(CompanyProperties, bankrupt_value,        SLE_VAR_I64 | SLE_FILE_I32,  0, 64),
    00281   SLE_CONDVAR(CompanyProperties, bankrupt_value,        SLE_INT64,                  65, SL_MAX_VERSION),
    00282
    00283   /* yearly expenses was changed to 64-bit in savegame version 2. */
    00284   SLE_CONDARR(CompanyProperties, yearly_expenses,       SLE_FILE_I32 | SLE_VAR_I64, 3 * 13, 0, 1),
    00285   SLE_CONDARR(CompanyProperties, yearly_expenses,       SLE_INT64, 3 * 13,                  2, IS_SL - 1),
    00286   SLE_CONDARR(CompanyProperties, yearly_expenses,       SLE_INT64, 3 * 15,              IS_SL, SL_MAX_VERSION),
    00287
    00288   SLE_CONDVAR(CompanyProperties, is_ai,                 SLE_BOOL,                    2, SL_MAX_VERSION),
    00289   SLE_CONDNULL(1, 107, 111),
    00290   SLE_CONDNULL(1, 4, 99),
    00291
    00292   SLE_CONDVAR(CompanyProperties, terraform_limit,       SLE_UINT32,                156, BEFORE_ADDING_PATCHES_SL),
    00293   SLE_CONDVAR(CompanyProperties, terraform_limit,       SLE_UINT32,                KEEP_COMPATIBLE_14_SV, SL_MAX_VERSION),
    00294   SLE_CONDVAR(CompanyProperties, clear_limit,           SLE_UINT32,                156, BEFORE_ADDING_PATCHES_SL),
    00295   SLE_CONDVAR(CompanyProperties, clear_limit,           SLE_UINT32,                KEEP_COMPATIBLE_14_SV, SL_MAX_VERSION),
    00296
    00297   SLE_END()
    00298 };
    00299
    00300 static const SaveLoad _company_settings_desc[] = {
    00301   /* Engine renewal settings */
    00302   SLE_CONDNULL(512, 16, 18),
    00303   SLE_CONDREF(Company, engine_renew_list,            REF_ENGINE_RENEWS,   19, SL_MAX_VERSION),
    00304   SLE_CONDVAR(Company, settings.engine_renew,        SLE_BOOL,            16, SL_MAX_VERSION),
    00305   SLE_CONDVAR(Company, settings.engine_renew_months, SLE_INT16,           16, SL_MAX_VERSION),
    00306   SLE_CONDVAR(Company, settings.engine_renew_money,  SLE_UINT32,          16, SL_MAX_VERSION),
    00307   SLE_CONDVAR(Company, settings.renew_keep_length,   SLE_BOOL,             2, SL_MAX_VERSION),
    00308
    00309   /* Default vehicle settings */
    00310   SLE_CONDVAR(Company, settings.vehicle.servint_ispercent,   SLE_BOOL,     120, SL_MAX_VERSION),
    00311   SLE_CONDVAR(Company, settings.vehicle.servint_trains,    SLE_UINT16,     120, SL_MAX_VERSION),
    00312   SLE_CONDVAR(Company, settings.vehicle.servint_roadveh,   SLE_UINT16,     120, SL_MAX_VERSION),
    00313   SLE_CONDVAR(Company, settings.vehicle.servint_aircraft,  SLE_UINT16,     120, SL_MAX_VERSION),
    00314   SLE_CONDVAR(Company, settings.vehicle.servint_ships,     SLE_UINT16,     120, SL_MAX_VERSION),
    00315
    00316   SLE_CONDNULL(63, 2, 143), // old reserved space
    00317
    00318   SLE_END()
    00319 };
    00320
    00321 static const SaveLoad _company_settings_skip_desc[] = {
    00322   /* Engine renewal settings */
    00323   SLE_CONDNULL(512, 16, 18),
    00324   SLE_CONDNULL(2, 19, 68),                 // engine_renew_list
    00325   SLE_CONDNULL(4, 69, SL_MAX_VERSION),     // engine_renew_list
    00326   SLE_CONDNULL(1, 16, SL_MAX_VERSION),     // settings.engine_renew
    00327   SLE_CONDNULL(2, 16, SL_MAX_VERSION),     // settings.engine_renew_months
    00328   SLE_CONDNULL(4, 16, SL_MAX_VERSION),     // settings.engine_renew_money
    00329   SLE_CONDNULL(1,  2, SL_MAX_VERSION),     // settings.renew_keep_length
    00330
    00331   /* Default vehicle settings */
    00332   SLE_CONDNULL(1, 120, SL_MAX_VERSION),    // settings.vehicle.servint_ispercent
    00333   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_trains
    00334   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_roadveh
    00335   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_aircraft
    00336   SLE_CONDNULL(2, 120, SL_MAX_VERSION),    // settings.vehicle.servint_ships
    00337
    00338   SLE_CONDNULL(63, 2, 143), // old reserved space
    00339
    00340   SLE_END()
    00341 };
    00342
    00343 static const SaveLoad _company_economy_desc[] = {
    00344   /* these were changed to 64-bit in savegame format 2 */
    00345   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00346   SLE_CONDVAR(CompanyEconomyEntry, income,              SLE_INT64,                  2, SL_MAX_VERSION),
    00347   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00348   SLE_CONDVAR(CompanyEconomyEntry, expenses,            SLE_INT64,                  2, SL_MAX_VERSION),
    00349   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_FILE_I32 | SLE_VAR_I64, 0, 1),
    00350   SLE_CONDVAR(CompanyEconomyEntry, company_value,       SLE_INT64,                  2, SL_MAX_VERSION),
    00351
    00352       SLE_VAR(CompanyEconomyEntry, delivered_cargo,     SLE_INT32),
    00353       SLE_VAR(CompanyEconomyEntry, performance_history, SLE_INT32),
    00354
    00355   SLE_END()
    00356 };
    00357
    00358 /* We do need to read this single value, as the bigger it gets, the more data is stored */
    00359 struct CompanyOldAI {
    00360   uint8 num_build_rec;
    00361 };
    00362
    00363 static const SaveLoad _company_ai_desc[] = {
    00364   SLE_CONDNULL(2,  0, 106),
    00365   SLE_CONDNULL(2,  0, 12),
    00366   SLE_CONDNULL(4, 13, 106),
    00367   SLE_CONDNULL(8,  0, 106),
    00368    SLE_CONDVAR(CompanyOldAI, num_build_rec, SLE_UINT8, 0, 106),
    00369   SLE_CONDNULL(3,  0, 106),
    00370
    00371   SLE_CONDNULL(2,  0,  5),
    00372   SLE_CONDNULL(4,  6, 106),
    00373   SLE_CONDNULL(2,  0,  5),
    00374   SLE_CONDNULL(4,  6, 106),
    00375   SLE_CONDNULL(2,  0, 106),
    00376
    00377   SLE_CONDNULL(2,  0,  5),
    00378   SLE_CONDNULL(4,  6, 106),
    00379   SLE_CONDNULL(2,  0,  5),
    00380   SLE_CONDNULL(4,  6, 106),
    00381   SLE_CONDNULL(2,  0, 106),
    00382
    00383   SLE_CONDNULL(2,  0, 68),
    00384   SLE_CONDNULL(4,  69, 106),
    00385
    00386   SLE_CONDNULL(18, 0, 106),
    00387   SLE_CONDNULL(20, 0, 106),
    00388   SLE_CONDNULL(32, 0, 106),
    00389
    00390   SLE_CONDNULL(64, 2, 106),
    00391   SLE_END()
    00392 };
    00393
    00394 static const SaveLoad _company_ai_build_rec_desc[] = {
    00395   SLE_CONDNULL(2, 0, 5),
    00396   SLE_CONDNULL(4, 6, 106),
    00397   SLE_CONDNULL(2, 0, 5),
    00398   SLE_CONDNULL(4, 6, 106),
    00399   SLE_CONDNULL(8, 0, 106),
    00400   SLE_END()
    00401 };
    00402
    00403 static const SaveLoad _company_livery_desc[] = {
    00404   SLE_CONDVAR(Livery, in_use,  SLE_BOOL,  34, SL_MAX_VERSION),
    00405   SLE_CONDVAR(Livery, colour1, SLE_UINT8, 34, SL_MAX_VERSION),
    00406   SLE_CONDVAR(Livery, colour2, SLE_UINT8, 34, SL_MAX_VERSION),
    00407   SLE_END()
    00408 };
    00409
    00410 static void SaveLoad_PLYR_common(Company *c, CompanyProperties *cprops)
    00411 {
    00412   int i;
    00413
    00414   SlObject(cprops, _company_desc);
    00415   if (c != NULL) {
    00416     SlObject(c, _company_settings_desc);
    00417   } else {
    00418     char nothing;
    00419     SlObject(&nothing, _company_settings_skip_desc);
    00420   }
    00421
    00422   /* Keep backwards compatible for savegames, so load the old AI block */
    00423   if (IsSavegameVersionBefore(107) && cprops->is_ai) {
    00424     CompanyOldAI old_ai;
    00425     char nothing;
    00426
    00427     SlObject(&old_ai, _company_ai_desc);
    00428     for (i = 0; i != old_ai.num_build_rec; i++) {
    00429       SlObject(&nothing, _company_ai_build_rec_desc);
    00430     }
    00431   }
    00432
    00433   /* Write economy */
    00434   SlObject(&cprops->cur_economy, _company_economy_desc);
    00435
    00436   /* Write old economy entries. */
    00437   for (i = 0; i < cprops->num_valid_stat_ent; i++) {
    00438     SlObject(&cprops->old_economy[i], _company_economy_desc);
    00439   }
    00440
    00441   /* Write each livery entry. */
    00442   int num_liveries = IsSavegameVersionBefore(63) ? LS_END - 4 : (IsSavegameVersionBefore(85) ? LS_END - 2: LS_END);
    00443   if (c != NULL) {
    00444     for (i = 0; i < num_liveries; i++) {
    00445       SlObject(&c->livery[i], _company_livery_desc);
    00446     }
    00447
    00448     if (num_liveries < LS_END) {
    00449       /* We want to insert some liveries somewhere in between. This means some have to be moved. */
    00450       memmove(&c->livery[LS_FREIGHT_WAGON], &c->livery[LS_PASSENGER_WAGON_MONORAIL], (LS_END - LS_FREIGHT_WAGON) * sizeof(c->livery[0]));
    00451       c->livery[LS_PASSENGER_WAGON_MONORAIL] = c->livery[LS_MONORAIL];
    00452       c->livery[LS_PASSENGER_WAGON_MAGLEV]   = c->livery[LS_MAGLEV];
    00453     }
    00454
    00455     if (num_liveries == LS_END - 4) {
    00456       /* Copy bus/truck liveries over to trams */
    00457       c->livery[LS_PASSENGER_TRAM] = c->livery[LS_BUS];
    00458       c->livery[LS_FREIGHT_TRAM]   = c->livery[LS_TRUCK];
    00459     }
    00460   } else {
    00461     /* Skip liveries */
    00462     Livery dummy_livery;
    00463     for (i = 0; i < num_liveries; i++) {
    00464       SlObject(&dummy_livery, _company_livery_desc);
    00465     }
    00466   }
    00467 }
    00468
    00469 static void SaveLoad_PLYR(Company *c)
    00470 {
    00471   SaveLoad_PLYR_common(c, c);
    00472 }
    00473
    00474 static void Save_PLYR()
    00475 {
    00476   Company *c;
    00477   FOR_ALL_COMPANIES(c) {
    00478     SlSetArrayIndex(c->index);
    00479     SlAutolength((AutolengthProc*)SaveLoad_PLYR, c);
    00480   }
    00481 }
    00482
    00483 static void Load_PLYR()
    00484 {
    00485   int index;
    00486   while ((index = SlIterateArray()) != -1) {
    00487     Company *c = new (index) Company();
    00488     SaveLoad_PLYR(c);
    00489     _company_colours[index] = (Colours)c->colour;
    00490   }
    00491 }
    00492
    00493 static void Check_PLYR()
    00494 {
    00495   int index;
    00496   while ((index = SlIterateArray()) != -1) {
    00497     CompanyProperties *cprops = new CompanyProperties();
    00498     memset(cprops, 0, sizeof(*cprops));
    00499     SaveLoad_PLYR_common(NULL, cprops);
    00500
    00501     /* We do not load old custom names */
    00502     if (IsSavegameVersionBefore(84))
    00503     {
    00504       if (GB(cprops->name_1, 11, 5) == 15) {
    00505         cprops->name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
    00506       }
    00507
    00508       if (GB(cprops->president_name_1, 11, 5) == 15) {
    00509         cprops->president_name_1 = STR_GAME_SAVELOAD_NOT_AVAILABLE;
    00510       }
    00511     }
    00512
    00513     if (!_load_check_data.companies.Insert(index, cprops)) delete cprops;
    00514   }
    00515 }
    00516
    00517 static void Ptrs_PLYR()
    00518 {
    00519   Company *c;
    00520   FOR_ALL_COMPANIES(c) {
    00521     SlObject(c, _company_settings_desc);
    00522   }
    00523 }
    00524
    00525
    00526 extern const ChunkHandler _company_chunk_handlers[] = {
    00527   { 'PLYR', Save_PLYR, Load_PLYR, Ptrs_PLYR, Check_PLYR, CH_ARRAY | CH_LAST},
    00528 };