1
/* $Id: vehicle_sl.cpp 15903 2009-03-30 23:15:05Z rubidium $ */
3
/** @file vehicle_sl.cpp Code handling saving and loading of vehicles */
6
#include "../vehicle_func.h"
8
#include "../roadveh.h"
10
#include "../aircraft.h"
11
#include "../effectvehicle_base.h"
18
* Link front and rear multiheaded engines to each other
19
* This is done when loading a savegame
21
void ConnectMultiheadedTrains()
26
if (v->type == VEH_TRAIN) {
27
v->u.rail.other_multiheaded_part = NULL;
32
if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) {
33
/* Two ways to associate multiheaded parts to each other:
34
* sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
35
* bracket-matching: Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
37
* Note: Old savegames might contain chains which do not comply with these rules, e.g.
38
* - the front and read parts have invalid orders
39
* - different engine types might be combined
40
* - there might be different amounts of front and rear parts.
42
* Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
43
* This is why two matching strategies are needed.
46
bool sequential_matching = IsFrontEngine(v);
48
for (Vehicle *u = v; u != NULL; u = GetNextVehicle(u)) {
49
if (u->u.rail.other_multiheaded_part != NULL) continue; // we already linked this one
51
if (IsMultiheaded(u)) {
52
if (!IsTrainEngine(u)) {
53
/* we got a rear car without a front car. We will convert it to a front one */
58
/* Find a matching back part */
59
EngineID eid = u->engine_type;
61
if (sequential_matching) {
62
for (w = GetNextVehicle(u); w != NULL; w = GetNextVehicle(w)) {
63
if (w->engine_type != eid || w->u.rail.other_multiheaded_part != NULL || !IsMultiheaded(w)) continue;
65
/* we found a car to partner with this engine. Now we will make sure it face the right way */
66
if (IsTrainEngine(w)) {
74
for (w = GetNextVehicle(u); w != NULL; w = GetNextVehicle(w)) {
75
if (w->engine_type != eid || w->u.rail.other_multiheaded_part != NULL || !IsMultiheaded(w)) continue;
77
if (IsTrainEngine(w)) {
80
if (stack_pos == 0) break;
87
w->u.rail.other_multiheaded_part = u;
88
u->u.rail.other_multiheaded_part = w;
90
/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
100
* Converts all trains to the new subtype format introduced in savegame 16.2
101
* It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found
103
void ConvertOldMultiheadToNew()
106
FOR_ALL_VEHICLES(v) {
107
if (v->type == VEH_TRAIN) {
108
SetBit(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
112
FOR_ALL_VEHICLES(v) {
113
if (v->type == VEH_TRAIN) {
114
if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) {
115
for (Vehicle *u = v; u != NULL; u = u->Next()) {
116
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
118
ClrBit(u->subtype, 7);
119
switch (u->subtype) {
120
case 0: // TS_Front_Engine
121
if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
126
case 1: // TS_Artic_Part
128
SetArticulatedPart(u);
131
case 2: // TS_Not_First
133
if (rvi->railveh_type == RAILVEH_WAGON) {
138
if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
139
/* rear end of a multiheaded engine */
143
if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
147
case 4: // TS_Free_Car
152
default: NOT_REACHED(); break;
161
/** need to be called to load aircraft from old version */
162
void UpdateOldAircraft()
164
/* set airport_flags to 0 for all airports just to be sure */
166
FOR_ALL_STATIONS(st) {
167
st->airport_flags = 0; // reset airport
171
FOR_ALL_VEHICLES(v_oldstyle) {
172
/* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
174
if (v_oldstyle->type == VEH_AIRCRAFT && IsNormalAircraft(v_oldstyle)) {
175
/* airplane in terminal stopped doesn't hurt anyone, so goto next */
176
if (v_oldstyle->vehstatus & VS_STOPPED && v_oldstyle->u.air.state == 0) {
177
v_oldstyle->u.air.state = HANGAR;
181
AircraftLeaveHangar(v_oldstyle); // make airplane visible if it was in a depot for example
182
v_oldstyle->vehstatus &= ~VS_STOPPED; // make airplane moving
183
v_oldstyle->cur_speed = v_oldstyle->max_speed; // so aircraft don't have zero speed while in air
184
if (!v_oldstyle->current_order.IsType(OT_GOTO_STATION) && !v_oldstyle->current_order.IsType(OT_GOTO_DEPOT)) {
185
/* reset current order so aircraft doesn't have invalid "station-only" order */
186
v_oldstyle->current_order.MakeDummy();
188
v_oldstyle->u.air.state = FLYING;
189
AircraftNextAirportPos_and_Order(v_oldstyle); // move it to the entry point of the airport
190
GetNewVehiclePosResult gp = GetNewVehiclePos(v_oldstyle);
191
v_oldstyle->tile = 0; // aircraft in air is tile=0
193
/* correct speed of helicopter-rotors */
194
if (v_oldstyle->subtype == AIR_HELICOPTER) v_oldstyle->Next()->Next()->cur_speed = 32;
196
/* set new position x,y,z */
197
SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
203
* Check all vehicles to ensure their engine type is valid
204
* for the currently loaded NewGRFs (that includes none...)
205
* This only makes a difference if NewGRFs are missing, otherwise
206
* all vehicles will be valid. This does not make such a game
207
* playable, it only prevents crash.
209
static void CheckValidVehicles()
211
uint total_engines = GetEnginePoolSize();
212
EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
215
FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; }
216
FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; }
217
FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; }
218
FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; }
221
FOR_ALL_VEHICLES(v) {
222
/* Test if engine types match */
228
if (v->engine_type >= total_engines || v->type != GetEngine(v->engine_type)->type) {
229
v->engine_type = first_engine[v->type];
239
/** Called after load to update coordinates */
240
void AfterLoadVehicles(bool part_of_load)
244
FOR_ALL_VEHICLES(v) {
245
/* Reinstate the previous pointer */
246
if (v->Next() != NULL) v->Next()->previous = v;
247
if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
249
v->UpdateDeltaXY(v->direction);
251
if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
253
if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
254
if (v->type == VEH_ROAD) v->u.road.first_engine = INVALID_ENGINE;
256
v->cargo.InvalidateCache();
259
/* AfterLoadVehicles may also be called in case of NewGRF reload, in this
260
* case we may not convert orders again. */
262
/* Create shared vehicle chain for very old games (pre 5,2) and create
263
* OrderList from shared vehicle chains. For this to work correctly, the
264
* following conditions must be fulfilled:
265
* a) both next_shared and previous_shared are not set for pre 5,2 games
266
* b) both next_shared and previous_shared are set for later games
268
std::map<Order*, OrderList*> mapping;
270
FOR_ALL_VEHICLES(v) {
271
if (v->orders.old != NULL) {
272
if (CheckSavegameVersion(105)) { // Pre-105 didn't save an OrderList
273
if (mapping[v->orders.old] == NULL) {
274
/* This adds the whole shared vehicle chain for case b */
275
v->orders.list = mapping[v->orders.old] = new OrderList(v->orders.old, v);
277
v->orders.list = mapping[v->orders.old];
278
/* For old games (case a) we must create the shared vehicle chain */
279
if (CheckSavegameVersionOldStyle(5, 2)) {
280
v->AddToShared(v->orders.list->GetFirstSharedVehicle());
283
} else { // OrderList was saved as such, only recalculate not saved values
284
if (v->PreviousShared() == NULL) {
285
new (v->orders.list) OrderList(v->orders.list->GetFirstOrder(), v);
292
FOR_ALL_VEHICLES(v) {
293
/* Fill the first pointers */
294
if (v->Previous() == NULL) {
295
for (Vehicle *u = v; u != NULL; u = u->Next()) {
301
CheckValidVehicles();
303
FOR_ALL_VEHICLES(v) {
304
assert(v->first != NULL);
306
if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) {
307
if (IsFrontEngine(v)) v->u.rail.last_speed = v->cur_speed; // update displayed train speed
308
TrainConsistChanged(v, false);
309
} else if (v->type == VEH_ROAD && IsRoadVehFront(v)) {
310
RoadVehUpdateCache(v);
314
/* Stop non-front engines */
315
if (CheckSavegameVersion(112)) {
316
FOR_ALL_VEHICLES(v) {
317
if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
318
if (IsTrainEngine(v)) v->vehstatus |= VS_STOPPED;
319
/* cur_speed is now relevant for non-front parts - nonzero breaks
320
* moving-wagons-inside-depot- and autoreplace- code */
323
/* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
324
* other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
325
if ((v->vehstatus & VS_STOPPED) && (v->type != VEH_TRAIN || CheckSavegameVersionOldStyle(2, 1))) {
331
FOR_ALL_VEHICLES(v) {
334
v->u.road.roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
335
v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
339
v->cur_image = v->GetImage(v->direction);
343
if (IsNormalAircraft(v)) {
344
v->cur_image = v->GetImage(v->direction);
346
/* The plane's shadow will have the same image as the plane */
347
Vehicle *shadow = v->Next();
348
shadow->cur_image = v->cur_image;
350
/* In the case of a helicopter we will update the rotor sprites */
351
if (v->subtype == AIR_HELICOPTER) {
352
Vehicle *rotor = shadow->Next();
353
rotor->cur_image = GetRotorImage(v);
356
UpdateAircraftCache(v);
362
v->coord.left = INVALID_COORD;
363
VehicleMove(v, false);
367
static uint8 _cargo_days;
368
static uint16 _cargo_source;
369
static uint32 _cargo_source_xy;
370
static uint16 _cargo_count;
371
static uint16 _cargo_paid_for;
372
static Money _cargo_feeder_share;
373
static uint32 _cargo_loaded_at_xy;
376
* Make it possible to make the saveload tables "friends" of other classes.
377
* @param vt the vehicle type. Can be VEH_END for the common vehicle description data
378
* @return the saveload description
380
const SaveLoad *GetVehicleDescription(VehicleType vt)
382
/** Save and load of vehicles */
383
static const SaveLoad _common_veh_desc[] = {
384
SLE_VAR(Vehicle, subtype, SLE_UINT8),
386
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
387
SLE_CONDVAR(Vehicle, name, SLE_NAME, 0, 83),
388
SLE_CONDSTR(Vehicle, name, SLE_STR, 0, 84, SL_MAX_VERSION),
389
SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, 0, 7),
390
SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, 8, SL_MAX_VERSION),
391
SLE_VAR(Vehicle, owner, SLE_UINT8),
392
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
393
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
394
SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
395
SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
397
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
398
SLE_CONDVAR(Vehicle, x_pos, SLE_UINT32, 6, SL_MAX_VERSION),
399
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
400
SLE_CONDVAR(Vehicle, y_pos, SLE_UINT32, 6, SL_MAX_VERSION),
401
SLE_VAR(Vehicle, z_pos, SLE_UINT8),
402
SLE_VAR(Vehicle, direction, SLE_UINT8),
404
SLE_CONDNULL(2, 0, 57),
405
SLE_VAR(Vehicle, spritenum, SLE_UINT8),
406
SLE_CONDNULL(5, 0, 57),
407
SLE_VAR(Vehicle, engine_type, SLE_UINT16),
409
SLE_VAR(Vehicle, max_speed, SLE_UINT16),
410
SLE_VAR(Vehicle, cur_speed, SLE_UINT16),
411
SLE_VAR(Vehicle, subspeed, SLE_UINT8),
412
SLE_VAR(Vehicle, acceleration, SLE_UINT8),
413
SLE_VAR(Vehicle, progress, SLE_UINT8),
415
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
416
SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
417
SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16, 5, SL_MAX_VERSION),
419
SLE_VAR(Vehicle, cargo_type, SLE_UINT8),
420
SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, 35, SL_MAX_VERSION),
421
SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67),
422
SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
423
SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67),
424
SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67),
425
SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
426
SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67),
427
SLE_CONDLST(Vehicle, cargo, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
429
SLE_VAR(Vehicle, day_counter, SLE_UINT8),
430
SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
431
SLE_CONDVAR(Vehicle, running_ticks, SLE_UINT8, 88, SL_MAX_VERSION),
433
SLE_VAR(Vehicle, cur_order_index, SLE_UINT8),
434
/* num_orders is now part of OrderList and is not saved but counted */
435
SLE_CONDNULL(1, 0, 104),
437
/* This next line is for version 4 and prior compatibility.. it temporarily reads
438
type and flags (which were both 4 bits) into type. Later on this is
439
converted correctly */
440
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 0, 4),
441
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
443
/* Orders for version 5 and on */
444
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8, 5, SL_MAX_VERSION),
445
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, flags), SLE_UINT8, 5, SL_MAX_VERSION),
446
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16, 5, SL_MAX_VERSION),
448
/* Refit in current order */
449
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_cargo), SLE_UINT8, 36, SL_MAX_VERSION),
450
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_subtype), SLE_UINT8, 36, SL_MAX_VERSION),
452
/* Timetable in current order */
453
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, wait_time), SLE_UINT16, 67, SL_MAX_VERSION),
454
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, travel_time), SLE_UINT16, 67, SL_MAX_VERSION),
456
SLE_CONDREF(Vehicle, orders, REF_ORDER, 0, 104),
457
SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION),
459
SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
460
SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
461
SLE_CONDVAR(Vehicle, max_age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
462
SLE_CONDVAR(Vehicle, max_age, SLE_INT32, 31, SL_MAX_VERSION),
463
SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
464
SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32, 31, SL_MAX_VERSION),
465
SLE_CONDVAR(Vehicle, service_interval, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
466
SLE_CONDVAR(Vehicle, service_interval, SLE_INT32, 31, SL_MAX_VERSION),
467
SLE_VAR(Vehicle, reliability, SLE_UINT16),
468
SLE_VAR(Vehicle, reliability_spd_dec, SLE_UINT16),
469
SLE_VAR(Vehicle, breakdown_ctr, SLE_UINT8),
470
SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8),
471
SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
472
SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8),
473
SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
474
SLE_CONDVAR(Vehicle, build_year, SLE_INT32, 31, SL_MAX_VERSION),
476
SLE_VAR(Vehicle, load_unload_time_rem, SLE_UINT16),
477
SLEG_CONDVAR( _cargo_paid_for, SLE_UINT16, 45, SL_MAX_VERSION),
478
SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT8, 40, SL_MAX_VERSION),
480
SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
481
SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, 65, SL_MAX_VERSION),
482
SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
483
SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, 65, SL_MAX_VERSION),
484
SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64, 51, 64),
485
SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
486
SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67),
487
SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
488
SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION),
490
SLE_CONDVAR(Vehicle, random_bits, SLE_UINT8, 2, SL_MAX_VERSION),
491
SLE_CONDVAR(Vehicle, waiting_triggers, SLE_UINT8, 2, SL_MAX_VERSION),
493
SLE_CONDREF(Vehicle, next_shared, REF_VEHICLE, 2, SL_MAX_VERSION),
494
SLE_CONDNULL(2, 2, 68),
495
SLE_CONDNULL(4, 69, 100),
497
SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
499
SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, 67, SL_MAX_VERSION),
500
SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, 67, SL_MAX_VERSION),
502
/* reserve extra space in savegame here. (currently 10 bytes) */
503
SLE_CONDNULL(10, 2, SL_MAX_VERSION),
509
static const SaveLoad _train_desc[] = {
510
SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
512
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos), SLE_UINT16),
513
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed), SLE_UINT8),
514
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype), SLE_UINT8),
515
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, track), SLE_UINT8),
517
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags), SLE_FILE_U8 | SLE_VAR_U16, 2, 99),
518
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags), SLE_UINT16, 100, SL_MAX_VERSION),
519
SLE_CONDNULL(2, 2, 59),
521
SLE_CONDNULL(2, 2, 19),
522
/* reserve extra space in savegame here. (currently 11 bytes) */
523
SLE_CONDNULL(11, 2, SL_MAX_VERSION),
528
static const SaveLoad _roadveh_desc[] = {
529
SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
531
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state), SLE_UINT8),
532
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame), SLE_UINT8),
533
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr), SLE_UINT16),
534
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking), SLE_UINT8),
535
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8),
536
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, crashed_ctr), SLE_UINT16),
537
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, reverse_ctr), SLE_UINT8),
539
SLE_CONDREFX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot), REF_ROADSTOPS, 6, SL_MAX_VERSION),
540
SLE_CONDNULL(1, 6, SL_MAX_VERSION),
541
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot_age), SLE_UINT8, 6, SL_MAX_VERSION),
542
/* reserve extra space in savegame here. (currently 16 bytes) */
543
SLE_CONDNULL(16, 2, SL_MAX_VERSION),
548
static const SaveLoad _ship_desc[] = {
549
SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
551
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
553
/* reserve extra space in savegame here. (currently 16 bytes) */
554
SLE_CONDNULL(16, 2, SL_MAX_VERSION),
559
static const SaveLoad _aircraft_desc[] = {
560
SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
562
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16),
563
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos), SLE_UINT8),
565
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
566
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport), SLE_UINT16, 5, SL_MAX_VERSION),
568
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state), SLE_UINT8),
570
SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos), SLE_UINT8, 2, SL_MAX_VERSION),
572
/* reserve extra space in savegame here. (currently 15 bytes) */
573
SLE_CONDNULL(15, 2, SL_MAX_VERSION),
578
static const SaveLoad _special_desc[] = {
579
SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
581
SLE_VAR(Vehicle, subtype, SLE_UINT8),
583
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
584
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
586
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
587
SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
588
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
589
SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
590
SLE_VAR(Vehicle, z_pos, SLE_UINT8),
592
SLE_VAR(Vehicle, cur_image, SLE_UINT16),
593
SLE_CONDNULL(5, 0, 57),
594
SLE_VAR(Vehicle, progress, SLE_UINT8),
595
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
597
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_state), SLE_UINT16),
598
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_substate), SLE_UINT8),
600
SLE_CONDVAR(Vehicle, spritenum, SLE_UINT8, 2, SL_MAX_VERSION),
602
/* reserve extra space in savegame here. (currently 15 bytes) */
603
SLE_CONDNULL(15, 2, SL_MAX_VERSION),
608
static const SaveLoad _disaster_desc[] = {
609
SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
611
SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
613
SLE_VAR(Vehicle, subtype, SLE_UINT8),
614
SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
615
SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
616
SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
617
SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
619
SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
620
SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
621
SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
622
SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
623
SLE_VAR(Vehicle, z_pos, SLE_UINT8),
624
SLE_VAR(Vehicle, direction, SLE_UINT8),
626
SLE_CONDNULL(5, 0, 57),
627
SLE_VAR(Vehicle, owner, SLE_UINT8),
628
SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
629
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
630
SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16, 5, SL_MAX_VERSION),
632
SLE_VAR(Vehicle, cur_image, SLE_UINT16),
633
SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
634
SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
635
SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
637
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, image_override), SLE_UINT16),
638
SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, big_ufo_destroyer_target), SLE_UINT16),
640
/* reserve extra space in savegame here. (currently 16 bytes) */
641
SLE_CONDNULL(16, 2, SL_MAX_VERSION),
647
static const SaveLoad *_veh_descs[] = {
657
return _veh_descs[vt];
660
/** Will be called when the vehicles need to be saved. */
661
static void Save_VEHS()
664
/* Write the vehicles */
665
FOR_ALL_VEHICLES(v) {
666
SlSetArrayIndex(v->index);
667
SlObject(v, GetVehicleDescription(v->type));
671
/** Will be called when vehicles need to be loaded. */
678
while ((index = SlIterateArray()) != -1) {
680
VehicleType vtype = (VehicleType)SlReadByte();
683
case VEH_TRAIN: v = new (index) Train(); break;
684
case VEH_ROAD: v = new (index) RoadVehicle(); break;
685
case VEH_SHIP: v = new (index) Ship(); break;
686
case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
687
case VEH_EFFECT: v = new (index) EffectVehicle(); break;
688
case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
689
case VEH_INVALID: v = new (index) InvalidVehicle(); break;
690
default: NOT_REACHED();
693
SlObject(v, GetVehicleDescription(vtype));
695
if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v)) {
696
/* Don't construct the packet with station here, because that'll fail with old savegames */
697
CargoPacket *cp = new CargoPacket();
698
cp->source = _cargo_source;
699
cp->source_xy = _cargo_source_xy;
700
cp->count = _cargo_count;
701
cp->days_in_transit = _cargo_days;
702
cp->feeder_share = _cargo_feeder_share;
703
cp->loaded_at_xy = _cargo_loaded_at_xy;
707
/* Old savegames used 'last_station_visited = 0xFF' */
708
if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
709
v->last_station_visited = INVALID_STATION;
711
if (CheckSavegameVersion(5)) {
712
/* Convert the current_order.type (which is a mix of type and flags, because
713
* in those versions, they both were 4 bits big) to type and flags */
714
v->current_order.flags = GB(v->current_order.type, 4, 4);
715
v->current_order.type &= 0x0F;
718
/* Advanced vehicle lists got added */
719
if (CheckSavegameVersion(60)) v->group_id = DEFAULT_GROUP;
723
extern const ChunkHandler _veh_chunk_handlers[] = {
724
{ 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST},