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

Generated on Fri May 27 04:19:48 2011 for OpenTTD by  doxygen 1.6.1