vehicle_sl.cpp

Go to the documentation of this file.
00001 /* $Id: vehicle_sl.cpp 18385 2009-12-02 18:18:56Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../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 
00025 /*
00026  * Link front and rear multiheaded engines to each other
00027  * This is done when loading a savegame
00028  */
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: NOT_REACHED();
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       a->cur_speed = a->max_speed; // so aircraft don't have zero speed while in air
00184       if (!a->current_order.IsType(OT_GOTO_STATION) && !a->current_order.IsType(OT_GOTO_DEPOT)) {
00185         /* reset current order so aircraft doesn't have invalid "station-only" order */
00186         a->current_order.MakeDummy();
00187       }
00188       a->state = FLYING;
00189       AircraftNextAirportPos_and_Order(a); // move it to the entry point of the airport
00190       GetNewVehiclePosResult gp = GetNewVehiclePos(a);
00191       a->tile = 0; // aircraft in air is tile=0
00192 
00193       /* correct speed of helicopter-rotors */
00194       if (a->subtype == AIR_HELICOPTER) a->Next()->Next()->cur_speed = 32;
00195 
00196       /* set new position x,y,z */
00197       SetAircraftPosition(a, gp.x, gp.y, GetAircraftFlyingAltitude(a));
00198     }
00199   }
00200 }
00201 
00209 static void CheckValidVehicles()
00210 {
00211   size_t total_engines = Engine::GetPoolSize();
00212   EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
00213 
00214   Engine *e;
00215   FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; }
00216   FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; }
00217   FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; }
00218   FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; }
00219 
00220   Vehicle *v;
00221   FOR_ALL_VEHICLES(v) {
00222     /* Test if engine types match */
00223     switch (v->type) {
00224       case VEH_TRAIN:
00225       case VEH_ROAD:
00226       case VEH_SHIP:
00227       case VEH_AIRCRAFT:
00228         if (v->engine_type >= total_engines || v->type != Engine::Get(v->engine_type)->type) {
00229           v->engine_type = first_engine[v->type];
00230         }
00231         break;
00232 
00233       default:
00234         break;
00235     }
00236   }
00237 }
00238 
00240 void AfterLoadVehicles(bool part_of_load)
00241 {
00242   Vehicle *v;
00243 
00244   FOR_ALL_VEHICLES(v) {
00245     /* Reinstate the previous pointer */
00246     if (v->Next() != NULL) v->Next()->previous = v;
00247     if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
00248 
00249     v->UpdateDeltaXY(v->direction);
00250 
00251     if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
00252     v->first = NULL;
00253     if (v->type == VEH_TRAIN) Train::From(v)->tcache.first_engine = INVALID_ENGINE;
00254     if (v->type == VEH_ROAD)  RoadVehicle::From(v)->rcache.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 (CheckSavegameVersion(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             v->orders.list = mapping[v->orders.old] = new OrderList(v->orders.old, v);
00274           } else {
00275             v->orders.list = mapping[v->orders.old];
00276             /* For old games (case a) we must create the shared vehicle chain */
00277             if (CheckSavegameVersionOldStyle(5, 2)) {
00278               v->AddToShared(v->orders.list->GetFirstSharedVehicle());
00279             }
00280           }
00281         } else { // OrderList was saved as such, only recalculate not saved values
00282           if (v->PreviousShared() == NULL) {
00283             v->orders.list->Initialize(v->orders.list->first, v);
00284           }
00285         }
00286       }
00287     }
00288   }
00289 
00290   FOR_ALL_VEHICLES(v) {
00291     /* Fill the first pointers */
00292     if (v->Previous() == NULL) {
00293       for (Vehicle *u = v; u != NULL; u = u->Next()) {
00294         u->first = v;
00295       }
00296     }
00297   }
00298 
00299   if (CheckSavegameVersion(105)) {
00300     /* Before 105 there was no order for shared orders, thus it messed up horribly */
00301     FOR_ALL_VEHICLES(v) {
00302       if (v->First() != v || v->orders.list != NULL || v->previous_shared != NULL || v->next_shared == NULL) continue;
00303 
00304       v->orders.list = new OrderList(NULL, v);
00305       for (Vehicle *u = v; u != NULL; u = u->next_shared) {
00306         u->orders.list = v->orders.list;
00307       }
00308     }
00309   }
00310 
00311   CheckValidVehicles();
00312 
00313   FOR_ALL_VEHICLES(v) {
00314     assert(v->first != NULL);
00315 
00316     if (v->type == VEH_TRAIN) {
00317       Train *t = Train::From(v);
00318       if (t->IsFrontEngine() || t->IsFreeWagon()) {
00319         t->tcache.last_speed = t->cur_speed; // update displayed train speed
00320         TrainConsistChanged(t, false);
00321       }
00322     } else if (v->type == VEH_ROAD) {
00323       RoadVehicle *rv = RoadVehicle::From(v);
00324       if (rv->IsRoadVehFront()) {
00325         RoadVehUpdateCache(rv);
00326       }
00327     }
00328   }
00329 
00330   /* Stop non-front engines */
00331   if (CheckSavegameVersion(112)) {
00332     FOR_ALL_VEHICLES(v) {
00333       if (v->type == VEH_TRAIN) {
00334         Train *t = Train::From(v);
00335         if (!t->IsFrontEngine()) {
00336           if (t->IsEngine()) t->vehstatus |= VS_STOPPED;
00337           /* cur_speed is now relevant for non-front parts - nonzero breaks
00338            * moving-wagons-inside-depot- and autoreplace- code */
00339           t->cur_speed = 0;
00340         }
00341       }
00342       /* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
00343        * other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
00344       if ((v->vehstatus & VS_STOPPED) && (v->type != VEH_TRAIN || CheckSavegameVersionOldStyle(2, 1))) {
00345         v->cur_speed = 0;
00346       }
00347     }
00348   }
00349 
00350   FOR_ALL_VEHICLES(v) {
00351     switch (v->type) {
00352       case VEH_ROAD: {
00353         RoadVehicle *rv = RoadVehicle::From(v);
00354         rv->roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
00355         rv->compatible_roadtypes = RoadTypeToRoadTypes(rv->roadtype);
00356       }
00357         /* FALL THROUGH */
00358       case VEH_TRAIN:
00359       case VEH_SHIP:
00360         v->cur_image = v->GetImage(v->direction);
00361         break;
00362 
00363       case VEH_AIRCRAFT:
00364         if (Aircraft::From(v)->IsNormalAircraft()) {
00365           v->cur_image = v->GetImage(v->direction);
00366 
00367           /* The plane's shadow will have the same image as the plane */
00368           Vehicle *shadow = v->Next();
00369           shadow->cur_image = v->cur_image;
00370 
00371           /* In the case of a helicopter we will update the rotor sprites */
00372           if (v->subtype == AIR_HELICOPTER) {
00373             Vehicle *rotor = shadow->Next();
00374             rotor->cur_image = GetRotorImage(Aircraft::From(v));
00375           }
00376 
00377           UpdateAircraftCache(Aircraft::From(v));
00378         }
00379         break;
00380       default: break;
00381     }
00382 
00383     v->coord.left = INVALID_COORD;
00384     VehicleMove(v, false);
00385   }
00386 }
00387 
00388 static uint8  _cargo_days;
00389 static uint16 _cargo_source;
00390 static uint32 _cargo_source_xy;
00391 static uint16 _cargo_count;
00392 static uint16 _cargo_paid_for;
00393 static Money  _cargo_feeder_share;
00394 static uint32 _cargo_loaded_at_xy;
00395 
00401 const SaveLoad *GetVehicleDescription(VehicleType vt)
00402 {
00404   static const SaveLoad _common_veh_desc[] = {
00405          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00406 
00407          SLE_REF(Vehicle, next,                  REF_VEHICLE_OLD),
00408      SLE_CONDVAR(Vehicle, name,                  SLE_NAME,                     0,  83),
00409      SLE_CONDSTR(Vehicle, name,                  SLE_STR, 0,                  84, SL_MAX_VERSION),
00410      SLE_CONDVAR(Vehicle, unitnumber,            SLE_FILE_U8  | SLE_VAR_U16,   0,   7),
00411      SLE_CONDVAR(Vehicle, unitnumber,            SLE_UINT16,                   8, SL_MAX_VERSION),
00412          SLE_VAR(Vehicle, owner,                 SLE_UINT8),
00413      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00414      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00415      SLE_CONDVAR(Vehicle, dest_tile,             SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00416      SLE_CONDVAR(Vehicle, dest_tile,             SLE_UINT32,                   6, SL_MAX_VERSION),
00417 
00418      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00419      SLE_CONDVAR(Vehicle, x_pos,                 SLE_UINT32,                   6, SL_MAX_VERSION),
00420      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00421      SLE_CONDVAR(Vehicle, y_pos,                 SLE_UINT32,                   6, SL_MAX_VERSION),
00422          SLE_VAR(Vehicle, z_pos,                 SLE_UINT8),
00423          SLE_VAR(Vehicle, direction,             SLE_UINT8),
00424 
00425     SLE_CONDNULL(2,                                                            0,  57),
00426          SLE_VAR(Vehicle, spritenum,             SLE_UINT8),
00427     SLE_CONDNULL(5,                                                            0,  57),
00428          SLE_VAR(Vehicle, engine_type,           SLE_UINT16),
00429 
00430          SLE_VAR(Vehicle, max_speed,             SLE_UINT16),
00431          SLE_VAR(Vehicle, cur_speed,             SLE_UINT16),
00432          SLE_VAR(Vehicle, subspeed,              SLE_UINT8),
00433          SLE_VAR(Vehicle, acceleration,          SLE_UINT8),
00434          SLE_VAR(Vehicle, progress,              SLE_UINT8),
00435 
00436          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00437      SLE_CONDVAR(Vehicle, last_station_visited,  SLE_FILE_U8  | SLE_VAR_U16,   0,   4),
00438      SLE_CONDVAR(Vehicle, last_station_visited,  SLE_UINT16,                   5, SL_MAX_VERSION),
00439 
00440          SLE_VAR(Vehicle, cargo_type,            SLE_UINT8),
00441      SLE_CONDVAR(Vehicle, cargo_subtype,         SLE_UINT8,                   35, SL_MAX_VERSION),
00442     SLEG_CONDVAR(         _cargo_days,           SLE_UINT8,                    0,  67),
00443     SLEG_CONDVAR(         _cargo_source,         SLE_FILE_U8  | SLE_VAR_U16,   0,   6),
00444     SLEG_CONDVAR(         _cargo_source,         SLE_UINT16,                   7,  67),
00445     SLEG_CONDVAR(         _cargo_source_xy,      SLE_UINT32,                  44,  67),
00446          SLE_VAR(Vehicle, cargo_cap,             SLE_UINT16),
00447     SLEG_CONDVAR(         _cargo_count,          SLE_UINT16,                   0,  67),
00448      SLE_CONDLST(Vehicle, cargo.packets,         REF_CARGO_PACKET,            68, SL_MAX_VERSION),
00449 
00450          SLE_VAR(Vehicle, day_counter,           SLE_UINT8),
00451          SLE_VAR(Vehicle, tick_counter,          SLE_UINT8),
00452      SLE_CONDVAR(Vehicle, running_ticks,         SLE_UINT8,                   88, SL_MAX_VERSION),
00453 
00454          SLE_VAR(Vehicle, cur_order_index,       SLE_UINT8),
00455     /* num_orders is now part of OrderList and is not saved but counted */
00456     SLE_CONDNULL(1,                                                            0, 104),
00457 
00458     /* This next line is for version 4 and prior compatibility.. it temporarily reads
00459      type and flags (which were both 4 bits) into type. Later on this is
00460      converted correctly */
00461      SLE_CONDVAR(Vehicle, current_order.type,    SLE_UINT8,                    0,   4),
00462      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_FILE_U8  | SLE_VAR_U16,   0,   4),
00463 
00464     /* Orders for version 5 and on */
00465      SLE_CONDVAR(Vehicle, current_order.type,    SLE_UINT8,                    5, SL_MAX_VERSION),
00466      SLE_CONDVAR(Vehicle, current_order.flags,   SLE_UINT8,                    5, SL_MAX_VERSION),
00467      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_UINT16,                   5, SL_MAX_VERSION),
00468 
00469     /* Refit in current order */
00470      SLE_CONDVAR(Vehicle, current_order.refit_cargo,   SLE_UINT8,             36, SL_MAX_VERSION),
00471      SLE_CONDVAR(Vehicle, current_order.refit_subtype, SLE_UINT8,             36, SL_MAX_VERSION),
00472 
00473     /* Timetable in current order */
00474      SLE_CONDVAR(Vehicle, current_order.wait_time,     SLE_UINT16,            67, SL_MAX_VERSION),
00475      SLE_CONDVAR(Vehicle, current_order.travel_time,   SLE_UINT16,            67, SL_MAX_VERSION),
00476      SLE_CONDVAR(Vehicle, timetable_start,       SLE_INT32,                  129, SL_MAX_VERSION),
00477 
00478      SLE_CONDREF(Vehicle, orders,                REF_ORDER,                    0, 104),
00479      SLE_CONDREF(Vehicle, orders,                REF_ORDERLIST,              105, SL_MAX_VERSION),
00480 
00481      SLE_CONDVAR(Vehicle, age,                   SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00482      SLE_CONDVAR(Vehicle, age,                   SLE_INT32,                   31, SL_MAX_VERSION),
00483      SLE_CONDVAR(Vehicle, max_age,               SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00484      SLE_CONDVAR(Vehicle, max_age,               SLE_INT32,                   31, SL_MAX_VERSION),
00485      SLE_CONDVAR(Vehicle, date_of_last_service,  SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00486      SLE_CONDVAR(Vehicle, date_of_last_service,  SLE_INT32,                   31, SL_MAX_VERSION),
00487      SLE_CONDVAR(Vehicle, service_interval,      SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00488      SLE_CONDVAR(Vehicle, service_interval,      SLE_INT32,                   31, SL_MAX_VERSION),
00489          SLE_VAR(Vehicle, reliability,           SLE_UINT16),
00490          SLE_VAR(Vehicle, reliability_spd_dec,   SLE_UINT16),
00491          SLE_VAR(Vehicle, breakdown_ctr,         SLE_UINT8),
00492          SLE_VAR(Vehicle, breakdown_delay,       SLE_UINT8),
00493          SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
00494          SLE_VAR(Vehicle, breakdown_chance,      SLE_UINT8),
00495      SLE_CONDVAR(Vehicle, build_year,            SLE_FILE_U8 | SLE_VAR_I32,    0,  30),
00496      SLE_CONDVAR(Vehicle, build_year,            SLE_INT32,                   31, SL_MAX_VERSION),
00497 
00498          SLE_VAR(Vehicle, time_counter,          SLE_UINT16),
00499     SLEG_CONDVAR(         _cargo_paid_for,       SLE_UINT16,                  45, SL_MAX_VERSION),
00500      SLE_CONDVAR(Vehicle, vehicle_flags,         SLE_UINT8,                   40, SL_MAX_VERSION),
00501 
00502      SLE_CONDVAR(Vehicle, profit_this_year,      SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00503      SLE_CONDVAR(Vehicle, profit_this_year,      SLE_INT64,                   65, SL_MAX_VERSION),
00504      SLE_CONDVAR(Vehicle, profit_last_year,      SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00505      SLE_CONDVAR(Vehicle, profit_last_year,      SLE_INT64,                   65, SL_MAX_VERSION),
00506     SLEG_CONDVAR(         _cargo_feeder_share,   SLE_FILE_I32 | SLE_VAR_I64,  51,  64),
00507     SLEG_CONDVAR(         _cargo_feeder_share,   SLE_INT64,                   65,  67),
00508     SLEG_CONDVAR(         _cargo_loaded_at_xy,   SLE_UINT32,                  51,  67),
00509      SLE_CONDVAR(Vehicle, value,                 SLE_FILE_I32 | SLE_VAR_I64,   0,  64),
00510      SLE_CONDVAR(Vehicle, value,                 SLE_INT64,                   65, SL_MAX_VERSION),
00511 
00512      SLE_CONDVAR(Vehicle, random_bits,           SLE_UINT8,                    2, SL_MAX_VERSION),
00513      SLE_CONDVAR(Vehicle, waiting_triggers,      SLE_UINT8,                    2, SL_MAX_VERSION),
00514 
00515      SLE_CONDREF(Vehicle, next_shared,           REF_VEHICLE,                  2, SL_MAX_VERSION),
00516     SLE_CONDNULL(2,                                                            2,  68),
00517     SLE_CONDNULL(4,                                                           69, 100),
00518 
00519      SLE_CONDVAR(Vehicle, group_id,              SLE_UINT16,                  60, SL_MAX_VERSION),
00520 
00521      SLE_CONDVAR(Vehicle, current_order_time,    SLE_UINT32,                  67, SL_MAX_VERSION),
00522      SLE_CONDVAR(Vehicle, lateness_counter,      SLE_INT32,                   67, SL_MAX_VERSION),
00523 
00524     /* reserve extra space in savegame here. (currently 10 bytes) */
00525     SLE_CONDNULL(10,                                                           2, SL_MAX_VERSION),
00526 
00527          SLE_END()
00528   };
00529 
00530 
00531   static const SaveLoad _train_desc[] = {
00532     SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
00533     SLE_VEH_INCLUDE(),
00534          SLE_VAR(Train, crash_anim_pos,      SLE_UINT16),
00535          SLE_VAR(Train, force_proceed,       SLE_UINT8),
00536          SLE_VAR(Train, railtype,            SLE_UINT8),
00537          SLE_VAR(Train, track,               SLE_UINT8),
00538 
00539      SLE_CONDVAR(Train, flags,               SLE_FILE_U8  | SLE_VAR_U16,   2,  99),
00540      SLE_CONDVAR(Train, flags,               SLE_UINT16,                 100, SL_MAX_VERSION),
00541     SLE_CONDNULL(2, 2, 59),
00542 
00543     SLE_CONDNULL(2, 2, 19),
00544     /* reserve extra space in savegame here. (currently 11 bytes) */
00545     SLE_CONDNULL(11, 2, SL_MAX_VERSION),
00546 
00547          SLE_END()
00548   };
00549 
00550   static const SaveLoad _roadveh_desc[] = {
00551     SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
00552     SLE_VEH_INCLUDE(),
00553          SLE_VAR(RoadVehicle, state,                SLE_UINT8),
00554          SLE_VAR(RoadVehicle, frame,                SLE_UINT8),
00555          SLE_VAR(RoadVehicle, blocked_ctr,          SLE_UINT16),
00556          SLE_VAR(RoadVehicle, overtaking,           SLE_UINT8),
00557          SLE_VAR(RoadVehicle, overtaking_ctr,       SLE_UINT8),
00558          SLE_VAR(RoadVehicle, crashed_ctr,          SLE_UINT16),
00559          SLE_VAR(RoadVehicle, reverse_ctr,          SLE_UINT8),
00560 
00561     SLE_CONDNULL(2,                                                               6,  68),
00562     SLE_CONDNULL(4,                                                              69, 130),
00563     SLE_CONDNULL(2,                                                               6, 130),
00564     /* reserve extra space in savegame here. (currently 16 bytes) */
00565     SLE_CONDNULL(16,                                                              2, SL_MAX_VERSION),
00566 
00567          SLE_END()
00568   };
00569 
00570   static const SaveLoad _ship_desc[] = {
00571     SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
00572     SLE_VEH_INCLUDE(),
00573          SLE_VAR(Ship, state, SLE_UINT8),
00574 
00575     /* reserve extra space in savegame here. (currently 16 bytes) */
00576     SLE_CONDNULL(16, 2, SL_MAX_VERSION),
00577 
00578          SLE_END()
00579   };
00580 
00581   static const SaveLoad _aircraft_desc[] = {
00582     SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
00583     SLE_VEH_INCLUDE(),
00584          SLE_VAR(Aircraft, crashed_counter,       SLE_UINT16),
00585          SLE_VAR(Aircraft, pos,                   SLE_UINT8),
00586 
00587      SLE_CONDVAR(Aircraft, targetairport,         SLE_FILE_U8  | SLE_VAR_U16,   0, 4),
00588      SLE_CONDVAR(Aircraft, targetairport,         SLE_UINT16,                   5, SL_MAX_VERSION),
00589 
00590          SLE_VAR(Aircraft, state,                 SLE_UINT8),
00591 
00592      SLE_CONDVAR(Aircraft, previous_pos,          SLE_UINT8,                    2, SL_MAX_VERSION),
00593      SLE_CONDVAR(Aircraft, last_direction,        SLE_UINT8,                    2, SL_MAX_VERSION),
00594      SLE_CONDVAR(Aircraft, number_consecutive_turns, SLE_UINT8,                 2, SL_MAX_VERSION),
00595 
00596     /* reserve extra space in savegame here. (currently 13 bytes) */
00597     SLE_CONDNULL(13,                                                           2, SL_MAX_VERSION),
00598 
00599          SLE_END()
00600   };
00601 
00602   static const SaveLoad _special_desc[] = {
00603     SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
00604 
00605          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00606 
00607      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00608      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00609 
00610      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00611      SLE_CONDVAR(Vehicle, x_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00612      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00613      SLE_CONDVAR(Vehicle, y_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00614          SLE_VAR(Vehicle, z_pos,                 SLE_UINT8),
00615 
00616          SLE_VAR(Vehicle, cur_image,             SLE_UINT16),
00617     SLE_CONDNULL(5,                                                            0,  57),
00618          SLE_VAR(Vehicle, progress,              SLE_UINT8),
00619          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00620 
00621          SLE_VAR(EffectVehicle, animation_state,    SLE_UINT16),
00622          SLE_VAR(EffectVehicle, animation_substate, SLE_UINT8),
00623 
00624      SLE_CONDVAR(Vehicle, spritenum,             SLE_UINT8,                    2, SL_MAX_VERSION),
00625 
00626     /* reserve extra space in savegame here. (currently 15 bytes) */
00627     SLE_CONDNULL(15,                                                           2, SL_MAX_VERSION),
00628 
00629          SLE_END()
00630   };
00631 
00632   static const SaveLoad _disaster_desc[] = {
00633     SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
00634 
00635          SLE_REF(Vehicle, next,                  REF_VEHICLE_OLD),
00636 
00637          SLE_VAR(Vehicle, subtype,               SLE_UINT8),
00638      SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00639      SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
00640      SLE_CONDVAR(Vehicle, dest_tile,             SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
00641      SLE_CONDVAR(Vehicle, dest_tile,             SLE_UINT32,                   6, SL_MAX_VERSION),
00642 
00643      SLE_CONDVAR(Vehicle, x_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00644      SLE_CONDVAR(Vehicle, x_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00645      SLE_CONDVAR(Vehicle, y_pos,                 SLE_FILE_I16 | SLE_VAR_I32,   0,   5),
00646      SLE_CONDVAR(Vehicle, y_pos,                 SLE_INT32,                    6, SL_MAX_VERSION),
00647          SLE_VAR(Vehicle, z_pos,                 SLE_UINT8),
00648          SLE_VAR(Vehicle, direction,             SLE_UINT8),
00649 
00650     SLE_CONDNULL(5,                                                            0,  57),
00651          SLE_VAR(Vehicle, owner,                 SLE_UINT8),
00652          SLE_VAR(Vehicle, vehstatus,             SLE_UINT8),
00653      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_FILE_U8 | SLE_VAR_U16,    0,   4),
00654      SLE_CONDVAR(Vehicle, current_order.dest,    SLE_UINT16,                   5, SL_MAX_VERSION),
00655 
00656          SLE_VAR(Vehicle, cur_image,             SLE_UINT16),
00657      SLE_CONDVAR(Vehicle, age,                   SLE_FILE_U16 | SLE_VAR_I32,   0,  30),
00658      SLE_CONDVAR(Vehicle, age,                   SLE_INT32,                   31, SL_MAX_VERSION),
00659          SLE_VAR(Vehicle, tick_counter,          SLE_UINT8),
00660 
00661          SLE_VAR(DisasterVehicle, image_override,            SLE_UINT16),
00662          SLE_VAR(DisasterVehicle, big_ufo_destroyer_target,  SLE_UINT16),
00663 
00664     /* reserve extra space in savegame here. (currently 16 bytes) */
00665     SLE_CONDNULL(16,                                                           2, SL_MAX_VERSION),
00666 
00667          SLE_END()
00668   };
00669 
00670 
00671   static const SaveLoad * const _veh_descs[] = {
00672     _train_desc,
00673     _roadveh_desc,
00674     _ship_desc,
00675     _aircraft_desc,
00676     _special_desc,
00677     _disaster_desc,
00678     _common_veh_desc,
00679   };
00680 
00681   return _veh_descs[vt];
00682 }
00683 
00685 static void Save_VEHS()
00686 {
00687   Vehicle *v;
00688   /* Write the vehicles */
00689   FOR_ALL_VEHICLES(v) {
00690     SlSetArrayIndex(v->index);
00691     SlObject(v, GetVehicleDescription(v->type));
00692   }
00693 }
00694 
00696 void Load_VEHS()
00697 {
00698   int index;
00699 
00700   _cargo_count = 0;
00701 
00702   while ((index = SlIterateArray()) != -1) {
00703     Vehicle *v;
00704     VehicleType vtype = (VehicleType)SlReadByte();
00705 
00706     switch (vtype) {
00707       case VEH_TRAIN:    v = new (index) Train();           break;
00708       case VEH_ROAD:     v = new (index) RoadVehicle();     break;
00709       case VEH_SHIP:     v = new (index) Ship();            break;
00710       case VEH_AIRCRAFT: v = new (index) Aircraft();        break;
00711       case VEH_EFFECT:   v = new (index) EffectVehicle();   break;
00712       case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
00713       case VEH_INVALID: /* Savegame shouldn't contain invalid vehicles */
00714       default: NOT_REACHED();
00715     }
00716 
00717     SlObject(v, GetVehicleDescription(vtype));
00718 
00719     if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v)) {
00720       /* Don't construct the packet with station here, because that'll fail with old savegames */
00721       CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
00722       v->cargo.Append(cp);
00723     }
00724 
00725     /* Old savegames used 'last_station_visited = 0xFF' */
00726     if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
00727       v->last_station_visited = INVALID_STATION;
00728 
00729     if (CheckSavegameVersion(5)) {
00730       /* Convert the current_order.type (which is a mix of type and flags, because
00731        *  in those versions, they both were 4 bits big) to type and flags */
00732       v->current_order.flags = GB(v->current_order.type, 4, 4);
00733       v->current_order.type &= 0x0F;
00734     }
00735 
00736     /* Advanced vehicle lists got added */
00737     if (CheckSavegameVersion(60)) v->group_id = DEFAULT_GROUP;
00738   }
00739 }
00740 
00741 static void Ptrs_VEHS()
00742 {
00743   Vehicle *v;
00744   FOR_ALL_VEHICLES(v) {
00745     SlObject(v, GetVehicleDescription(v->type));
00746   }
00747 }
00748 
00749 extern const ChunkHandler _veh_chunk_handlers[] = {
00750   { 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, CH_SPARSE_ARRAY | CH_LAST},
00751 };

Generated on Tue Jan 5 21:02:58 2010 for OpenTTD by  doxygen 1.5.6