vehicle_sl.cpp

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

Generated on Sat Jun 5 21:52:09 2010 for OpenTTD by  doxygen 1.6.1