vehicle_sl.cpp

Go to the documentation of this file.
00001 /* $Id: vehicle_sl.cpp 26317 2014-02-07 23:48:56Z 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 "../vehicle_func.h"
00014 #include "../train.h"
00015 #include "../roadveh.h"
00016 #include "../ship.h"
00017 #include "../aircraft.h"
00018 #include "../station_base.h"
00019 #include "../effectvehicle_base.h"
00020 #include "../company_base.h"
00021 #include "../company_func.h"
00022 
00023 #include "saveload.h"
00024 
00025 #include <map>
00026 
00031 void ConnectMultiheadedTrains()
00032 {
00033   Train *v;
00034 
00035   FOR_ALL_TRAINS(v) {
00036     v->other_multiheaded_part = NULL;
00037   }
00038 
00039   FOR_ALL_TRAINS(v) {
00040     if (v->IsFrontEngine() || v->IsFreeWagon()) {
00041       /* Two ways to associate multiheaded parts to each other:
00042        * sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
00043        * bracket-matching:    Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
00044        *
00045        * Note: Old savegames might contain chains which do not comply with these rules, e.g.
00046        *   - the front and read parts have invalid orders
00047        *   - different engine types might be combined
00048        *   - there might be different amounts of front and rear parts.
00049        *
00050        * Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
00051        *   This is why two matching strategies are needed.
00052        */
00053 
00054       bool sequential_matching = v->IsFrontEngine();
00055 
00056       for (Train *u = v; u != NULL; u = u->GetNextVehicle()) {
00057         if (u->other_multiheaded_part != NULL) continue; // we already linked this one
00058 
00059         if (u->IsMultiheaded()) {
00060           if (!u->IsEngine()) {
00061             /* we got a rear car without a front car. We will convert it to a front one */
00062             u->SetEngine();
00063             u->spritenum--;
00064           }
00065 
00066           /* Find a matching back part */
00067           EngineID eid = u->engine_type;
00068           Train *w;
00069           if (sequential_matching) {
00070             for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
00071               if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
00072 
00073               /* we found a car to partner with this engine. Now we will make sure it face the right way */
00074               if (w->IsEngine()) {
00075                 w->ClearEngine();
00076                 w->spritenum++;
00077               }
00078               break;
00079             }
00080           } else {
00081             uint stack_pos = 0;
00082             for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
00083               if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
00084 
00085               if (w->IsEngine()) {
00086                 stack_pos++;
00087               } else {
00088                 if (stack_pos == 0) break;
00089                 stack_pos--;
00090               }
00091             }
00092           }
00093 
00094           if (w != NULL) {
00095             w->other_multiheaded_part = u;
00096             u->other_multiheaded_part = w;
00097           } else {
00098             /* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
00099             u->ClearMultiheaded();
00100           }
00101         }
00102       }
00103     }
00104   }
00105 }
00106 
00111 void ConvertOldMultiheadToNew()
00112 {
00113   Train *t;
00114   FOR_ALL_TRAINS(t) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
00115 
00116   FOR_ALL_TRAINS(t) {
00117     if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) {
00118       for (Train *u = t; u != NULL; u = u->Next()) {
00119         const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
00120 
00121         ClrBit(u->subtype, 7);
00122         switch (u->subtype) {
00123           case 0: // TS_Front_Engine
00124             if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
00125             u->SetFrontEngine();
00126             u->SetEngine();
00127             break;
00128 
00129           case 1: // TS_Artic_Part
00130             u->subtype = 0;
00131             u->SetArticulatedPart();
00132             break;
00133 
00134           case 2: // TS_Not_First
00135             u->subtype = 0;
00136             if (rvi->railveh_type == RAILVEH_WAGON) {
00137               /* normal wagon */
00138               u->SetWagon();
00139               break;
00140             }
00141             if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
00142               /* rear end of a multiheaded engine */
00143               u->SetMultiheaded();
00144               break;
00145             }
00146             if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
00147             u->SetEngine();
00148             break;
00149 
00150           case 4: // TS_Free_Car
00151             u->subtype = 0;
00152             u->SetWagon();
00153             u->SetFreeWagon();
00154             break;
00155           default: SlErrorCorrupt("Invalid train subtype");
00156         }
00157       }
00158     }
00159   }
00160 }
00161 
00162 
00164 void UpdateOldAircraft()
00165 {
00166   /* set airport_flags to 0 for all airports just to be sure */
00167   Station *st;
00168   FOR_ALL_STATIONS(st) {
00169     st->airport.flags = 0; // reset airport
00170   }
00171 
00172   Aircraft *a;
00173   FOR_ALL_AIRCRAFT(a) {
00174     /* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
00175      * skip those */
00176     if (a->IsNormalAircraft()) {
00177       /* airplane in terminal stopped doesn't hurt anyone, so goto next */
00178       if ((a->vehstatus & VS_STOPPED) && a->state == 0) {
00179         a->state = HANGAR;
00180         continue;
00181       }
00182 
00183       AircraftLeaveHangar(a, a->direction); // make airplane visible if it was in a depot for example
00184       a->vehstatus &= ~VS_STOPPED; // make airplane moving
00185       UpdateAircraftCache(a);
00186       a->cur_speed = a->vcache.cached_max_speed; // so aircraft don't have zero speed while in air
00187       if (!a->current_order.IsType(OT_GOTO_STATION) && !a->current_order.IsType(OT_GOTO_DEPOT)) {
00188         /* reset current order so aircraft doesn't have invalid "station-only" order */
00189         a->current_order.MakeDummy();
00190       }
00191       a->state = FLYING;
00192       AircraftNextAirportPos_and_Order(a); // move it to the entry point of the airport
00193       GetNewVehiclePosResult gp = GetNewVehiclePos(a);
00194       a->tile = 0; // aircraft in air is tile=0
00195 
00196       /* correct speed of helicopter-rotors */
00197       if (a->subtype == AIR_HELICOPTER) a->Next()->Next()->cur_speed = 32;
00198 
00199       /* set new position x,y,z */
00200       SetAircraftPosition(a, gp.x, gp.y, GetAircraftFlyingAltitude(a));
00201     }
00202   }
00203 }
00204 
00212 static void CheckValidVehicles()
00213 {
00214   size_t total_engines = Engine::GetPoolSize();
00215   EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
00216 
00217   Engine *e;
00218   FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; }
00219   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; }
00220   FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; }
00221   FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; }
00222 
00223   Vehicle *v;
00224   FOR_ALL_VEHICLES(v) {
00225     /* Test if engine types match */
00226     switch (v->type) {
00227       case VEH_TRAIN:
00228       case VEH_ROAD:
00229       case VEH_SHIP:
00230       case VEH_AIRCRAFT:
00231         if (v->engine_type >= total_engines || v->type != v->GetEngine()->type) {
00232           v->engine_type = first_engine[v->type];
00233         }
00234         break;
00235 
00236       default:
00237         break;
00238     }
00239   }
00240 }
00241 
00242 extern byte _age_cargo_skip_counter; // From misc_sl.cpp
00243 
00245 void AfterLoadVehicles(bool part_of_load)
00246 {
00247   Vehicle *v;
00248 
00249   FOR_ALL_VEHICLES(v) {
00250     /* Reinstate the previous pointer */
00251     if (v->Next() != NULL) v->Next()->previous = v;
00252     if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
00253 
00254     if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
00255     v->first = NULL;
00256     if (v->IsGroundVehicle()) v->GetGroundVehicleCache()->first_engine = INVALID_ENGINE;
00257   }
00258 
00259   /* AfterLoadVehicles may also be called in case of NewGRF reload, in this
00260    * case we may not convert orders again. */
00261   if (part_of_load) {
00262     /* Create shared vehicle chain for very old games (pre 5,2) and create
00263      * OrderList from shared vehicle chains. For this to work correctly, the
00264      * following conditions must be fulfilled:
00265      * a) both next_shared and previous_shared are not set for pre 5,2 games
00266      * b) both next_shared and previous_shared are set for later games
00267      */
00268     std::map<Order*, OrderList*> mapping;
00269 
00270     FOR_ALL_VEHICLES(v) {
00271       if (v->orders.old != NULL) {
00272         if (IsSavegameVersionBefore(105)) { // Pre-105 didn't save an OrderList
00273           if (mapping[v->orders.old] == NULL) {
00274             /* This adds the whole shared vehicle chain for case b */
00275 
00276             /* Creating an OrderList here is safe because the number of vehicles
00277              * allowed in these savegames matches the number of OrderLists. As
00278              * such each vehicle can get an OrderList and it will (still) fit. */
00279             assert(OrderList::CanAllocateItem());
00280             v->orders.list = mapping[v->orders.old] = new OrderList(v->orders.old, v);
00281           } else {
00282             v->orders.list = mapping[v->orders.old];
00283             /* For old games (case a) we must create the shared vehicle chain */
00284             if (IsSavegameVersionBefore(5, 2)) {
00285               v->AddToShared(v->orders.list->GetFirstSharedVehicle());
00286             }
00287           }
00288         } else { // OrderList was saved as such, only recalculate not saved values
00289           if (v->PreviousShared() == NULL) {
00290             v->orders.list->Initialize(v->orders.list->first, v);
00291           }
00292         }
00293       }
00294     }
00295   }
00296 
00297   FOR_ALL_VEHICLES(v) {
00298     /* Fill the first pointers */
00299     if (v->Previous() == NULL) {
00300       for (Vehicle *u = v; u != NULL; u = u->Next()) {
00301         u->first = v;
00302       }
00303     }
00304   }
00305 
00306   if (part_of_load) {
00307     if (IsSavegameVersionBefore(105)) {
00308       /* Before 105 there was no order for shared orders, thus it messed up horribly */
00309       FOR_ALL_VEHICLES(v) {
00310         if (v->First() != v || v->orders.list != NULL || v->previous_shared != NULL || v->next_shared == NULL) continue;
00311 
00312         /* As above, allocating OrderList here is safe. */
00313         assert(OrderList::CanAllocateItem());
00314         v->orders.list = new OrderList(NULL, v);
00315         for (Vehicle *u = v; u != NULL; u = u->next_shared) {
00316           u->orders.list = v->orders.list;
00317         }
00318       }
00319     }
00320 
00321     if (IsSavegameVersionBefore(157)) {
00322       /* The road vehicle subtype was converted to a flag. */
00323       RoadVehicle *rv;
00324       FOR_ALL_ROADVEHICLES(rv) {
00325         if (rv->subtype == 0) {
00326           /* The road vehicle is at the front. */
00327           rv->SetFrontEngine();
00328         } else if (rv->subtype == 1) {
00329           /* The road vehicle is an articulated part. */
00330           rv->subtype = 0;
00331           rv->SetArticulatedPart();
00332         } else {
00333           SlErrorCorrupt("Invalid road vehicle subtype");
00334         }
00335       }
00336     }
00337 
00338     if (IsSavegameVersionBefore(160)) {
00339       /* In some old savegames there might be some "crap" stored. */
00340       FOR_ALL_VEHICLES(v) {
00341         if (!v->IsPrimaryVehicle() && v->type != VEH_DISASTER) {
00342           v->current_order.Free();
00343           v->unitnumber = 0;
00344         }
00345       }
00346     }
00347 
00348     if (IsSavegameVersionBefore(162)) {
00349       /* Set the vehicle-local cargo age counter from the old global counter. */
00350       FOR_ALL_VEHICLES(v) {
00351         v->cargo_age_counter = _age_cargo_skip_counter;
00352       }
00353     }
00354 
00355     if (IsSavegameVersionBefore(180)) {
00356       /* Set service interval flags */
00357       FOR_ALL_VEHICLES(v) {
00358         if (!v->IsPrimaryVehicle()) continue;
00359 
00360         const Company *c = Company::Get(v->owner);
00361         int interval = CompanyServiceInterval(c, v->type);
00362 
00363         v->SetServiceIntervalIsCustom(v->GetServiceInterval() != interval);
00364         v->SetServiceIntervalIsPercent(c->settings.vehicle.servint_ispercent);
00365       }
00366     }
00367   }
00368 
00369   CheckValidVehicles();
00370 
00371   FOR_ALL_VEHICLES(v) {
00372     assert(v->first != NULL);
00373 
00374     switch (v->type) {
00375       case VEH_TRAIN: {
00376         Train *t = Train::From(v);
00377         if (t->IsFrontEngine() || t->IsFreeWagon()) {
00378           t->gcache.last_speed = t->cur_speed; // update displayed train speed
00379           t->ConsistChanged(CCF_SAVELOAD);
00380         }
00381         break;
00382       }
00383 
00384       case VEH_ROAD: {
00385         RoadVehicle *rv = RoadVehicle::From(v);
00386         if (rv->IsFrontEngine()) {
00387           rv->gcache.last_speed = rv->cur_speed; // update displayed road vehicle speed
00388           RoadVehUpdateCache(rv);
00389           if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
00390             rv->CargoChanged();
00391           }
00392         }
00393         break;
00394       }
00395 
00396       case VEH_SHIP:
00397         Ship::From(v)->UpdateCache();
00398         break;
00399 
00400       default: break;
00401     }
00402   }
00403 
00404   /* Stop non-front engines */
00405   if (part_of_load && IsSavegameVersionBefore(112)) {
00406     FOR_ALL_VEHICLES(v) {
00407       if (v->type == VEH_TRAIN) {
00408         Train *t = Train::From(v);
00409         if (!t->IsFrontEngine()) {
00410           if (t->IsEngine()) t->vehstatus |= VS_STOPPED;
00411           /* cur_speed is now relevant for non-front parts - nonzero breaks
00412            * moving-wagons-inside-depot- and autoreplace- code */
00413           t->cur_speed = 0;
00414         }
00415       }
00416       /* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
00417        * other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
00418       if ((v->vehstatus & VS_STOPPED) && (v->type != VEH_TRAIN || IsSavegameVersionBefore(2, 1))) {
00419         v->cur_speed = 0;
00420       }
00421     }
00422   }
00423 
00424   FOR_ALL_VEHICLES(v) {
00425     switch (v->type) {
00426       case VEH_ROAD: {
00427         RoadVehicle *rv = RoadVehicle::From(v);
00428         rv->roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
00429         rv->compatible_roadtypes = RoadTypeToRoadTypes(rv->roadtype);
00430         /* FALL THROUGH */
00431       }
00432 
00433       case VEH_TRAIN:
00434       case VEH_SHIP:
00435         v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
00436         break;
00437 
00438       case VEH_AIRCRAFT:
00439         if (Aircraft::From(v)->IsNormalAircraft()) {
00440           v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
00441 
00442           /* The plane's shadow will have the same image as the plane */
00443           Vehicle *shadow = v->Next();
00444           shadow->cur_image = v->cur_image;
00445 
00446           /* In the case of a helicopter we will update the rotor sprites */
00447           if (v->subtype == AIR_HELICOPTER) {
00448             Vehicle *rotor = shadow->Next();
00449             rotor->cur_image = GetRotorImage(Aircraft::From(v), EIT_ON_MAP);
00450           }
00451 
00452           UpdateAircraftCache(Aircraft::From(v), true);
00453         }
00454         break;
00455       default: break;
00456     }
00457 
00458     v->UpdateDeltaXY(v->direction);
00459     v->coord.left = INVALID_COORD;
00460     VehicleUpdatePosition(v);
00461     VehicleUpdateViewport(v, false);
00462   }
00463 }
00464 
00465 bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // From train_cmd.cpp
00466 void ReverseTrainDirection(Train *v);
00467 void ReverseTrainSwapVeh(Train *v, int l, int r);
00468 
00470 void FixupTrainLengths()
00471 {
00472   /* Vehicle center was moved from 4 units behind the front to half the length
00473    * behind the front. Move vehicles so they end up on the same spot. */
00474   Vehicle *v;
00475   FOR_ALL_VEHICLES(v) {
00476     if (v->type == VEH_TRAIN && v->IsPrimaryVehicle()) {
00477       /* The vehicle center is now more to the front depending on vehicle length,
00478        * so we need to move all vehicles forward to cover the difference to the
00479        * old center, otherwise wagon spacing in trains would be broken upon load. */
00480       for (Train *u = Train::From(v); u != NULL; u = u->Next()) {
00481         if (u->track == TRACK_BIT_DEPOT || (u->vehstatus & VS_CRASHED)) continue;
00482 
00483         Train *next = u->Next();
00484 
00485         /* Try to pull the vehicle half its length forward. */
00486         int diff = (VEHICLE_LENGTH - u->gcache.cached_veh_length) / 2;
00487         int done;
00488         for (done = 0; done < diff; done++) {
00489           if (!TrainController(u, next, false)) break;
00490         }
00491 
00492         if (next != NULL && done < diff && u->IsFrontEngine()) {
00493           /* Pulling the front vehicle forwards failed, we either encountered a dead-end
00494            * or a red signal. To fix this, we try to move the whole train the required
00495            * space backwards and re-do the fix up of the front vehicle. */
00496 
00497           /* Ignore any signals when backtracking. */
00498           TrainForceProceeding old_tfp = u->force_proceed;
00499           u->force_proceed = TFP_SIGNAL;
00500 
00501           /* Swap start<>end, start+1<>end-1, ... */
00502           int r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
00503           int l = 0;
00504           do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
00505 
00506           /* We moved the first vehicle which is now the last. Move it back to the
00507            * original position as we will fix up the last vehicle later in the loop. */
00508           for (int i = 0; i < done; i++) TrainController(u->Last(), NULL);
00509 
00510           /* Move the train backwards to get space for the first vehicle. As the stopping
00511            * distance from a line end is rounded up, move the train one unit more to cater
00512            * for front vehicles with odd lengths. */
00513           int moved;
00514           for (moved = 0; moved < diff + 1; moved++) {
00515             if (!TrainController(u, NULL, false)) break;
00516           }
00517 
00518           /* Swap start<>end, start+1<>end-1, ... again. */
00519           r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
00520           l = 0;
00521           do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
00522 
00523           u->force_proceed = old_tfp;
00524 
00525           /* Tracks are too short to fix the train length. The player has to fix the
00526            * train in a depot. Bail out so we don't damage the vehicle chain any more. */
00527           if (moved < diff + 1) break;
00528 
00529           /* Re-do the correction for the first vehicle. */
00530           for (done = 0; done < diff; done++) TrainController(u, next, false);
00531 
00532           /* We moved one unit more backwards than needed for even-length front vehicles,
00533            * try to move that unit forward again. We don't care if this step fails. */
00534           TrainController(u, NULL, false);
00535         }
00536 
00537         /* If the next wagon is still in a depot, check if it shouldn't be outside already. */
00538         if (next != NULL && next->track == TRACK_BIT_DEPOT) {
00539           int d = TicksToLeaveDepot(u);
00540           if (d <= 0) {
00541             /* Next vehicle should have left the depot already, show it and pull forward. */
00542             next->vehstatus &= ~VS_HIDDEN;
00543             next->track = TrackToTrackBits(GetRailDepotTrack(next->tile));
00544             for (int i = 0; i >= d; i--) TrainController(next, NULL);
00545           }
00546         }
00547       }
00548 
00549       /* Update all cached properties after moving the vehicle chain around. */
00550       Train::From(v)->ConsistChanged(CCF_TRACK);
00551     }
00552   }
00553 }
00554 
00555 static uint8  _cargo_days;
00556 static uint16 _cargo_source;
00557 static uint32 _cargo_source_xy;
00558 static uint16 _cargo_count;
00559 static uint16 _cargo_paid_for;
00560 static Money  _cargo_feeder_share;
00561 static uint32 _cargo_loaded_at_xy;
00562 
00568 const SaveLoad *GetVehicleDescription(VehicleType vt)
00569 {
00571   static const SaveLoad _common_veh_desc[] = {
00572          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00573 
00574          SLE_REF(Vehicle, next,                  REF_VEHICLE_OLD),
00575      SLE_CONDVAR(Vehicle, name,                  SLE_NAME,                     0,  83),
00576      SLE_CONDSTR(Vehicle, name,                  SLE_STR | SLF_ALLOW_CONTROL, 0, 84, SL_MAX_VERSION),
00577      SLE_CONDVAR(Vehicle, unitnumber,            SLE_FILE_U8  | SLE_VAR_U16,   0,   7),
00578      SLE_CONDVAR(Vehicle, unitnumber,            SLE_UINT16,                   8, SL_MAX_VERSION),
00579          SLE_VAR(Vehicle, owner,                 SLE_UINT8),
00580      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00581      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00582      SLE_CONDVAR(Vehicle, dest_tile,             SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00583      SLE_CONDVAR(Vehicle, dest_tile,             SLE_UINT32,                   6, SL_MAX_VERSION),
00584 
00585      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00586      SLE_CONDVAR(Vehicle, x_pos,                 SLE_UINT32,                   6, SL_MAX_VERSION),
00587      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00588      SLE_CONDVAR(Vehicle, y_pos,                 SLE_UINT32,                   6, SL_MAX_VERSION),
00589      SLE_CONDVAR(Vehicle, z_pos,                 SLE_FILE_U8  | SLE_VAR_I32,   0, 163),
00590      SLE_CONDVAR(Vehicle, z_pos,                 SLE_INT32,                  164, SL_MAX_VERSION),
00591          SLE_VAR(Vehicle, direction,             SLE_UINT8),
00592 
00593     SLE_CONDNULL(2,                                                            0,  57),
00594          SLE_VAR(Vehicle, spritenum,             SLE_UINT8),
00595     SLE_CONDNULL(5,                                                            0,  57),
00596          SLE_VAR(Vehicle, engine_type,           SLE_UINT16),
00597 
00598     SLE_CONDNULL(2,                                                            0,  151),
00599          SLE_VAR(Vehicle, cur_speed,             SLE_UINT16),
00600          SLE_VAR(Vehicle, subspeed,              SLE_UINT8),
00601          SLE_VAR(Vehicle, acceleration,          SLE_UINT8),
00602          SLE_VAR(Vehicle, progress,              SLE_UINT8),
00603 
00604          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00605      SLE_CONDVAR(Vehicle, last_station_visited,  SLE_FILE_U8  | SLE_VAR_U16,   0,   4),
00606      SLE_CONDVAR(Vehicle, last_station_visited,  SLE_UINT16,                   5, SL_MAX_VERSION),
00607      SLE_CONDVAR(Vehicle, last_loading_station,  SLE_UINT16,                 182, SL_MAX_VERSION),
00608 
00609          SLE_VAR(Vehicle, cargo_type,            SLE_UINT8),
00610      SLE_CONDVAR(Vehicle, cargo_subtype,         SLE_UINT8,                   35, SL_MAX_VERSION),
00611     SLEG_CONDVAR(         _cargo_days,           SLE_UINT8,                    0,  67),
00612     SLEG_CONDVAR(         _cargo_source,         SLE_FILE_U8  | SLE_VAR_U16,   0,   6),
00613     SLEG_CONDVAR(         _cargo_source,         SLE_UINT16,                   7,  67),
00614     SLEG_CONDVAR(         _cargo_source_xy,      SLE_UINT32,                  44,  67),
00615          SLE_VAR(Vehicle, cargo_cap,             SLE_UINT16),
00616      SLE_CONDVAR(Vehicle, refit_cap,             SLE_UINT16,                 182, SL_MAX_VERSION),
00617     SLEG_CONDVAR(         _cargo_count,          SLE_UINT16,                   0,  67),
00618      SLE_CONDLST(Vehicle, cargo.packets,         REF_CARGO_PACKET,            68, SL_MAX_VERSION),
00619      SLE_CONDARR(Vehicle, cargo.action_counts,   SLE_UINT, VehicleCargoList::NUM_MOVE_TO_ACTION, 181, SL_MAX_VERSION),
00620      SLE_CONDVAR(Vehicle, cargo_age_counter,     SLE_UINT16,                 162, SL_MAX_VERSION),
00621 
00622          SLE_VAR(Vehicle, day_counter,           SLE_UINT8),
00623          SLE_VAR(Vehicle, tick_counter,          SLE_UINT8),
00624      SLE_CONDVAR(Vehicle, running_ticks,         SLE_UINT8,                   88, SL_MAX_VERSION),
00625 
00626          SLE_VAR(Vehicle, cur_implicit_order_index,  SLE_UINT8),
00627      SLE_CONDVAR(Vehicle, cur_real_order_index,  SLE_UINT8,                  158, SL_MAX_VERSION),
00628     /* num_orders is now part of OrderList and is not saved but counted */
00629     SLE_CONDNULL(1,                                                            0, 104),
00630 
00631     /* This next line is for version 4 and prior compatibility.. it temporarily reads
00632      type and flags (which were both 4 bits) into type. Later on this is
00633      converted correctly */
00634      SLE_CONDVAR(Vehicle, current_order.type,    SLE_UINT8,                    0,   4),
00635      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_FILE_U8  | SLE_VAR_U16,   0,   4),
00636 
00637     /* Orders for version 5 and on */
00638      SLE_CONDVAR(Vehicle, current_order.type,    SLE_UINT8,                    5, SL_MAX_VERSION),
00639      SLE_CONDVAR(Vehicle, current_order.flags,   SLE_UINT8,                    5, SL_MAX_VERSION),
00640      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_UINT16,                   5, SL_MAX_VERSION),
00641 
00642     /* Refit in current order */
00643      SLE_CONDVAR(Vehicle, current_order.refit_cargo,   SLE_UINT8,             36, SL_MAX_VERSION),
00644     SLE_CONDNULL(1,                                                           36, 181), // refit_subtype
00645 
00646     /* Timetable in current order */
00647      SLE_CONDVAR(Vehicle, current_order.wait_time,     SLE_UINT16,            67, SL_MAX_VERSION),
00648      SLE_CONDVAR(Vehicle, current_order.travel_time,   SLE_UINT16,            67, SL_MAX_VERSION),
00649      SLE_CONDVAR(Vehicle, current_order.max_speed,     SLE_UINT16,           174, SL_MAX_VERSION),
00650      SLE_CONDVAR(Vehicle, timetable_start,       SLE_INT32,                  129, SL_MAX_VERSION),
00651 
00652      SLE_CONDREF(Vehicle, orders,                REF_ORDER,                    0, 104),
00653      SLE_CONDREF(Vehicle, orders,                REF_ORDERLIST,              105, SL_MAX_VERSION),
00654 
00655      SLE_CONDVAR(Vehicle, age,                   SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00656      SLE_CONDVAR(Vehicle, age,                   SLE_INT32,                   31, SL_MAX_VERSION),
00657      SLE_CONDVAR(Vehicle, max_age,               SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00658      SLE_CONDVAR(Vehicle, max_age,               SLE_INT32,                   31, SL_MAX_VERSION),
00659      SLE_CONDVAR(Vehicle, date_of_last_service,  SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00660      SLE_CONDVAR(Vehicle, date_of_last_service,  SLE_INT32,                   31, SL_MAX_VERSION),
00661      SLE_CONDVAR(Vehicle, service_interval,      SLE_UINT16,                   0,  30),
00662      SLE_CONDVAR(Vehicle, service_interval,      SLE_FILE_U32 | SLE_VAR_U16,  31, 179),
00663      SLE_CONDVAR(Vehicle, service_interval,      SLE_UINT16,                 180, SL_MAX_VERSION),
00664          SLE_VAR(Vehicle, reliability,           SLE_UINT16),
00665          SLE_VAR(Vehicle, reliability_spd_dec,   SLE_UINT16),
00666          SLE_VAR(Vehicle, breakdown_ctr,         SLE_UINT8),
00667          SLE_VAR(Vehicle, breakdown_delay,       SLE_UINT8),
00668          SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
00669          SLE_VAR(Vehicle, breakdown_chance,      SLE_UINT8),
00670      SLE_CONDVAR(Vehicle, build_year,            SLE_FILE_U8 | SLE_VAR_I32,    0,  30),
00671      SLE_CONDVAR(Vehicle, build_year,            SLE_INT32,                   31, SL_MAX_VERSION),
00672 
00673          SLE_VAR(Vehicle, load_unload_ticks,     SLE_UINT16),
00674     SLEG_CONDVAR(         _cargo_paid_for,       SLE_UINT16,                  45, SL_MAX_VERSION),
00675      SLE_CONDVAR(Vehicle, vehicle_flags,         SLE_FILE_U8 | SLE_VAR_U16,   40, 179),
00676      SLE_CONDVAR(Vehicle, vehicle_flags,         SLE_UINT16,                 180, SL_MAX_VERSION),
00677 
00678      SLE_CONDVAR(Vehicle, profit_this_year,      SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00679      SLE_CONDVAR(Vehicle, profit_this_year,      SLE_INT64,                   65, SL_MAX_VERSION),
00680      SLE_CONDVAR(Vehicle, profit_last_year,      SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00681      SLE_CONDVAR(Vehicle, profit_last_year,      SLE_INT64,                   65, SL_MAX_VERSION),
00682     SLEG_CONDVAR(         _cargo_feeder_share,   SLE_FILE_I32 | SLE_VAR_I64,  51,  64),
00683     SLEG_CONDVAR(         _cargo_feeder_share,   SLE_INT64,                   65,  67),
00684     SLEG_CONDVAR(         _cargo_loaded_at_xy,   SLE_UINT32,                  51,  67),
00685      SLE_CONDVAR(Vehicle, value,                 SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00686      SLE_CONDVAR(Vehicle, value,                 SLE_INT64,                   65, SL_MAX_VERSION),
00687 
00688      SLE_CONDVAR(Vehicle, random_bits,           SLE_UINT8,                    2, SL_MAX_VERSION),
00689      SLE_CONDVAR(Vehicle, waiting_triggers,      SLE_UINT8,                    2, SL_MAX_VERSION),
00690 
00691      SLE_CONDREF(Vehicle, next_shared,           REF_VEHICLE,                  2, SL_MAX_VERSION),
00692     SLE_CONDNULL(2,                                                            2,  68),
00693     SLE_CONDNULL(4,                                                           69, 100),
00694 
00695      SLE_CONDVAR(Vehicle, group_id,              SLE_UINT16,                  60, SL_MAX_VERSION),
00696 
00697      SLE_CONDVAR(Vehicle, current_order_time,    SLE_UINT32,                  67, SL_MAX_VERSION),
00698      SLE_CONDVAR(Vehicle, lateness_counter,      SLE_INT32,                   67, SL_MAX_VERSION),
00699 
00700     SLE_CONDNULL(10,                                                           2, 143), // old reserved space
00701 
00702          SLE_END()
00703   };
00704 
00705 
00706   static const SaveLoad _train_desc[] = {
00707     SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
00708     SLE_VEH_INCLUDE(),
00709          SLE_VAR(Train, crash_anim_pos,      SLE_UINT16),
00710          SLE_VAR(Train, force_proceed,       SLE_UINT8),
00711          SLE_VAR(Train, railtype,            SLE_UINT8),
00712          SLE_VAR(Train, track,               SLE_UINT8),
00713 
00714      SLE_CONDVAR(Train, flags,               SLE_FILE_U8  | SLE_VAR_U16,   2,  99),
00715      SLE_CONDVAR(Train, flags,               SLE_UINT16,                 100, SL_MAX_VERSION),
00716     SLE_CONDNULL(2, 2, 59),
00717 
00718      SLE_CONDVAR(Train, wait_counter,        SLE_UINT16,                 136, SL_MAX_VERSION),
00719 
00720     SLE_CONDNULL(2, 2, 19),
00721      SLE_CONDVAR(Train, gv_flags,            SLE_UINT16,                 139, SL_MAX_VERSION),
00722     SLE_CONDNULL(11, 2, 143), // old reserved space
00723 
00724          SLE_END()
00725   };
00726 
00727   static const SaveLoad _roadveh_desc[] = {
00728     SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
00729     SLE_VEH_INCLUDE(),
00730          SLE_VAR(RoadVehicle, state,                SLE_UINT8),
00731          SLE_VAR(RoadVehicle, frame,                SLE_UINT8),
00732          SLE_VAR(RoadVehicle, blocked_ctr,          SLE_UINT16),
00733          SLE_VAR(RoadVehicle, overtaking,           SLE_UINT8),
00734          SLE_VAR(RoadVehicle, overtaking_ctr,       SLE_UINT8),
00735          SLE_VAR(RoadVehicle, crashed_ctr,          SLE_UINT16),
00736          SLE_VAR(RoadVehicle, reverse_ctr,          SLE_UINT8),
00737 
00738     SLE_CONDNULL(2,                                                               6,  68),
00739      SLE_CONDVAR(RoadVehicle, gv_flags,             SLE_UINT16,                 139, SL_MAX_VERSION),
00740     SLE_CONDNULL(4,                                                              69, 130),
00741     SLE_CONDNULL(2,                                                               6, 130),
00742     SLE_CONDNULL(16,                                                              2, 143), // old reserved space
00743 
00744          SLE_END()
00745   };
00746 
00747   static const SaveLoad _ship_desc[] = {
00748     SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
00749     SLE_VEH_INCLUDE(),
00750          SLE_VAR(Ship, state, SLE_UINT8),
00751 
00752     SLE_CONDNULL(16, 2, 143), // old reserved space
00753 
00754          SLE_END()
00755   };
00756 
00757   static const SaveLoad _aircraft_desc[] = {
00758     SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
00759     SLE_VEH_INCLUDE(),
00760          SLE_VAR(Aircraft, crashed_counter,       SLE_UINT16),
00761          SLE_VAR(Aircraft, pos,                   SLE_UINT8),
00762 
00763      SLE_CONDVAR(Aircraft, targetairport,         SLE_FILE_U8  | SLE_VAR_U16,   0, 4),
00764      SLE_CONDVAR(Aircraft, targetairport,         SLE_UINT16,                   5, SL_MAX_VERSION),
00765 
00766          SLE_VAR(Aircraft, state,                 SLE_UINT8),
00767 
00768      SLE_CONDVAR(Aircraft, previous_pos,          SLE_UINT8,                    2, SL_MAX_VERSION),
00769      SLE_CONDVAR(Aircraft, last_direction,        SLE_UINT8,                    2, SL_MAX_VERSION),
00770      SLE_CONDVAR(Aircraft, number_consecutive_turns, SLE_UINT8,                 2, SL_MAX_VERSION),
00771 
00772      SLE_CONDVAR(Aircraft, turn_counter,          SLE_UINT8,                  136, SL_MAX_VERSION),
00773      SLE_CONDVAR(Aircraft, flags,                 SLE_UINT8,                  167, SL_MAX_VERSION),
00774 
00775     SLE_CONDNULL(13,                                                           2, 143), // old reserved space
00776 
00777          SLE_END()
00778   };
00779 
00780   static const SaveLoad _special_desc[] = {
00781     SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
00782 
00783          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00784 
00785      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00786      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00787 
00788      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00789      SLE_CONDVAR(Vehicle, x_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00790      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00791      SLE_CONDVAR(Vehicle, y_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00792      SLE_CONDVAR(Vehicle, z_pos,                 SLE_FILE_U8  | SLE_VAR_I32,   0, 163),
00793      SLE_CONDVAR(Vehicle, z_pos,                 SLE_INT32,                  164, SL_MAX_VERSION),
00794 
00795          SLE_VAR(Vehicle, cur_image,             SLE_FILE_U16 | SLE_VAR_U32),
00796     SLE_CONDNULL(5,                                                            0,  57),
00797          SLE_VAR(Vehicle, progress,              SLE_UINT8),
00798          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00799 
00800          SLE_VAR(EffectVehicle, animation_state,    SLE_UINT16),
00801          SLE_VAR(EffectVehicle, animation_substate, SLE_UINT8),
00802 
00803      SLE_CONDVAR(Vehicle, spritenum,             SLE_UINT8,                    2, SL_MAX_VERSION),
00804 
00805     SLE_CONDNULL(15,                                                           2, 143), // old reserved space
00806 
00807          SLE_END()
00808   };
00809 
00810   static const SaveLoad _disaster_desc[] = {
00811     SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
00812 
00813          SLE_REF(Vehicle, next,                  REF_VEHICLE_OLD),
00814 
00815          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00816      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00817      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00818      SLE_CONDVAR(Vehicle, dest_tile,             SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00819      SLE_CONDVAR(Vehicle, dest_tile,             SLE_UINT32,                   6, SL_MAX_VERSION),
00820 
00821      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00822      SLE_CONDVAR(Vehicle, x_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00823      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00824      SLE_CONDVAR(Vehicle, y_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00825      SLE_CONDVAR(Vehicle, z_pos,                 SLE_FILE_U8  | SLE_VAR_I32,   0, 163),
00826      SLE_CONDVAR(Vehicle, z_pos,                 SLE_INT32,                  164, SL_MAX_VERSION),
00827          SLE_VAR(Vehicle, direction,             SLE_UINT8),
00828 
00829     SLE_CONDNULL(5,                                                            0,  57),
00830          SLE_VAR(Vehicle, owner,                 SLE_UINT8),
00831          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00832      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_FILE_U8 | SLE_VAR_U16,    0,   4),
00833      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_UINT16,                   5, SL_MAX_VERSION),
00834 
00835          SLE_VAR(Vehicle, cur_image,             SLE_FILE_U16 | SLE_VAR_U32),
00836      SLE_CONDVAR(Vehicle, age,                   SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00837      SLE_CONDVAR(Vehicle, age,                   SLE_INT32,                   31, SL_MAX_VERSION),
00838          SLE_VAR(Vehicle, tick_counter,          SLE_UINT8),
00839 
00840          SLE_VAR(DisasterVehicle, image_override,            SLE_UINT16),
00841          SLE_VAR(DisasterVehicle, big_ufo_destroyer_target,  SLE_UINT16),
00842 
00843     SLE_CONDNULL(16,                                                           2, 143), // old reserved space
00844 
00845          SLE_END()
00846   };
00847 
00848 
00849   static const SaveLoad * const _veh_descs[] = {
00850     _train_desc,
00851     _roadveh_desc,
00852     _ship_desc,
00853     _aircraft_desc,
00854     _special_desc,
00855     _disaster_desc,
00856     _common_veh_desc,
00857   };
00858 
00859   return _veh_descs[vt];
00860 }
00861 
00863 static void Save_VEHS()
00864 {
00865   Vehicle *v;
00866   /* Write the vehicles */
00867   FOR_ALL_VEHICLES(v) {
00868     SlSetArrayIndex(v->index);
00869     SlObject(v, GetVehicleDescription(v->type));
00870   }
00871 }
00872 
00874 void Load_VEHS()
00875 {
00876   int index;
00877 
00878   _cargo_count = 0;
00879 
00880   while ((index = SlIterateArray()) != -1) {
00881     Vehicle *v;
00882     VehicleType vtype = (VehicleType)SlReadByte();
00883 
00884     switch (vtype) {
00885       case VEH_TRAIN:    v = new (index) Train();           break;
00886       case VEH_ROAD:     v = new (index) RoadVehicle();     break;
00887       case VEH_SHIP:     v = new (index) Ship();            break;
00888       case VEH_AIRCRAFT: v = new (index) Aircraft();        break;
00889       case VEH_EFFECT:   v = new (index) EffectVehicle();   break;
00890       case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
00891       case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
00892       default: SlErrorCorrupt("Invalid vehicle type");
00893     }
00894 
00895     SlObject(v, GetVehicleDescription(vtype));
00896 
00897     if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
00898       /* Don't construct the packet with station here, because that'll fail with old savegames */
00899       CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
00900       v->cargo.Append(cp);
00901     }
00902 
00903     /* Old savegames used 'last_station_visited = 0xFF' */
00904     if (IsSavegameVersionBefore(5) && v->last_station_visited == 0xFF) {
00905       v->last_station_visited = INVALID_STATION;
00906     }
00907 
00908     if (IsSavegameVersionBefore(182)) v->last_loading_station = INVALID_STATION;
00909 
00910     if (IsSavegameVersionBefore(5)) {
00911       /* Convert the current_order.type (which is a mix of type and flags, because
00912        *  in those versions, they both were 4 bits big) to type and flags */
00913       v->current_order.flags = GB(v->current_order.type, 4, 4);
00914       v->current_order.type &= 0x0F;
00915     }
00916 
00917     /* Advanced vehicle lists got added */
00918     if (IsSavegameVersionBefore(60)) v->group_id = DEFAULT_GROUP;
00919   }
00920 }
00921 
00922 static void Ptrs_VEHS()
00923 {
00924   Vehicle *v;
00925   FOR_ALL_VEHICLES(v) {
00926     SlObject(v, GetVehicleDescription(v->type));
00927   }
00928 }
00929 
00930 extern const ChunkHandler _veh_chunk_handlers[] = {
00931   { 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, NULL, CH_SPARSE_ARRAY | CH_LAST},
00932 };