company_sl.cpp

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