~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/saveload/vehicle_sl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: vehicle_sl.cpp 15903 2009-03-30 23:15:05Z rubidium $ */
 
2
 
 
3
/** @file vehicle_sl.cpp Code handling saving and loading of vehicles */
 
4
 
 
5
#include "../stdafx.h"
 
6
#include "../vehicle_func.h"
 
7
#include "../train.h"
 
8
#include "../roadveh.h"
 
9
#include "../ship.h"
 
10
#include "../aircraft.h"
 
11
#include "../effectvehicle_base.h"
 
12
 
 
13
#include "saveload.h"
 
14
 
 
15
#include <map>
 
16
 
 
17
/*
 
18
 * Link front and rear multiheaded engines to each other
 
19
 * This is done when loading a savegame
 
20
 */
 
21
void ConnectMultiheadedTrains()
 
22
{
 
23
        Vehicle *v;
 
24
 
 
25
        FOR_ALL_VEHICLES(v) {
 
26
                if (v->type == VEH_TRAIN) {
 
27
                        v->u.rail.other_multiheaded_part = NULL;
 
28
                }
 
29
        }
 
30
 
 
31
        FOR_ALL_VEHICLES(v) {
 
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 ..<..<..>..<..>..>..
 
36
                         *
 
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.
 
41
                         *
 
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.
 
44
                         */
 
45
 
 
46
                        bool sequential_matching = IsFrontEngine(v);
 
47
 
 
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
 
50
 
 
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 */
 
54
                                                SetTrainEngine(u);
 
55
                                                u->spritenum--;
 
56
                                        }
 
57
 
 
58
                                        /* Find a matching back part */
 
59
                                        EngineID eid = u->engine_type;
 
60
                                        Vehicle *w;
 
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;
 
64
 
 
65
                                                        /* we found a car to partner with this engine. Now we will make sure it face the right way */
 
66
                                                        if (IsTrainEngine(w)) {
 
67
                                                                ClearTrainEngine(w);
 
68
                                                                w->spritenum++;
 
69
                                                        }
 
70
                                                        break;
 
71
                                                }
 
72
                                        } else {
 
73
                                                uint stack_pos = 0;
 
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;
 
76
 
 
77
                                                        if (IsTrainEngine(w)) {
 
78
                                                                stack_pos++;
 
79
                                                        } else {
 
80
                                                                if (stack_pos == 0) break;
 
81
                                                                stack_pos--;
 
82
                                                        }
 
83
                                                }
 
84
                                        }
 
85
 
 
86
                                        if (w != NULL) {
 
87
                                                w->u.rail.other_multiheaded_part = u;
 
88
                                                u->u.rail.other_multiheaded_part = w;
 
89
                                        } else {
 
90
                                                /* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
 
91
                                                ClearMultiheaded(u);
 
92
                                        }
 
93
                                }
 
94
                        }
 
95
                }
 
96
        }
 
97
}
 
98
 
 
99
/**
 
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
 
102
 */
 
103
void ConvertOldMultiheadToNew()
 
104
{
 
105
        Vehicle *v;
 
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
 
109
                }
 
110
        }
 
111
 
 
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);
 
117
 
 
118
                                        ClrBit(u->subtype, 7);
 
119
                                        switch (u->subtype) {
 
120
                                                case 0: // TS_Front_Engine
 
121
                                                        if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
122
                                                        SetFrontEngine(u);
 
123
                                                        SetTrainEngine(u);
 
124
                                                        break;
 
125
 
 
126
                                                case 1: // TS_Artic_Part
 
127
                                                        u->subtype = 0;
 
128
                                                        SetArticulatedPart(u);
 
129
                                                        break;
 
130
 
 
131
                                                case 2: // TS_Not_First
 
132
                                                        u->subtype = 0;
 
133
                                                        if (rvi->railveh_type == RAILVEH_WAGON) {
 
134
                                                                /* normal wagon */
 
135
                                                                SetTrainWagon(u);
 
136
                                                                break;
 
137
                                                        }
 
138
                                                        if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
 
139
                                                                /* rear end of a multiheaded engine */
 
140
                                                                SetMultiheaded(u);
 
141
                                                                break;
 
142
                                                        }
 
143
                                                        if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u);
 
144
                                                        SetTrainEngine(u);
 
145
                                                        break;
 
146
 
 
147
                                                case 4: // TS_Free_Car
 
148
                                                        u->subtype = 0;
 
149
                                                        SetTrainWagon(u);
 
150
                                                        SetFreeWagon(u);
 
151
                                                        break;
 
152
                                                default: NOT_REACHED(); break;
 
153
                                        }
 
154
                                }
 
155
                        }
 
156
                }
 
157
        }
 
158
}
 
159
 
 
160
 
 
161
/** need to be called to load aircraft from old version */
 
162
void UpdateOldAircraft()
 
163
{
 
164
        /* set airport_flags to 0 for all airports just to be sure */
 
165
        Station *st;
 
166
        FOR_ALL_STATIONS(st) {
 
167
                st->airport_flags = 0; // reset airport
 
168
        }
 
169
 
 
170
        Vehicle *v_oldstyle;
 
171
        FOR_ALL_VEHICLES(v_oldstyle) {
 
172
                /* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
 
173
                 * skip those */
 
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;
 
178
                                continue;
 
179
                        }
 
180
 
 
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();
 
187
                        }
 
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
 
192
 
 
193
                        /* correct speed of helicopter-rotors */
 
194
                        if (v_oldstyle->subtype == AIR_HELICOPTER) v_oldstyle->Next()->Next()->cur_speed = 32;
 
195
 
 
196
                        /* set new position x,y,z */
 
197
                        SetAircraftPosition(v_oldstyle, gp.x, gp.y, GetAircraftFlyingAltitude(v_oldstyle));
 
198
                }
 
199
        }
 
200
}
 
201
 
 
202
/**
 
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.
 
208
 */
 
209
static void CheckValidVehicles()
 
210
{
 
211
        uint total_engines = GetEnginePoolSize();
 
212
        EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
 
213
 
 
214
        Engine *e;
 
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; }
 
219
 
 
220
        Vehicle *v;
 
221
        FOR_ALL_VEHICLES(v) {
 
222
                /* Test if engine types match */
 
223
                switch (v->type) {
 
224
                        case VEH_TRAIN:
 
225
                        case VEH_ROAD:
 
226
                        case VEH_SHIP:
 
227
                        case VEH_AIRCRAFT:
 
228
                                if (v->engine_type >= total_engines || v->type != GetEngine(v->engine_type)->type) {
 
229
                                        v->engine_type = first_engine[v->type];
 
230
                                }
 
231
                                break;
 
232
 
 
233
                        default:
 
234
                                break;
 
235
                }
 
236
        }
 
237
}
 
238
 
 
239
/** Called after load to update coordinates */
 
240
void AfterLoadVehicles(bool part_of_load)
 
241
{
 
242
        Vehicle *v;
 
243
 
 
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;
 
248
 
 
249
                v->UpdateDeltaXY(v->direction);
 
250
 
 
251
                if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
 
252
                v->first = NULL;
 
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;
 
255
 
 
256
                v->cargo.InvalidateCache();
 
257
        }
 
258
 
 
259
        /* AfterLoadVehicles may also be called in case of NewGRF reload, in this
 
260
         * case we may not convert orders again. */
 
261
        if (part_of_load) {
 
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
 
267
                 */
 
268
                std::map<Order*, OrderList*> mapping;
 
269
 
 
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);
 
276
                                        } else {
 
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());
 
281
                                                }
 
282
                                        }
 
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);
 
286
                                        }
 
287
                                }
 
288
                        }
 
289
                }
 
290
        }
 
291
 
 
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()) {
 
296
                                u->first = v;
 
297
                        }
 
298
                }
 
299
        }
 
300
 
 
301
        CheckValidVehicles();
 
302
 
 
303
        FOR_ALL_VEHICLES(v) {
 
304
                assert(v->first != NULL);
 
305
 
 
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);
 
311
                }
 
312
        }
 
313
 
 
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 */
 
321
                                v->cur_speed = 0;
 
322
                        }
 
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))) {
 
326
                                v->cur_speed = 0;
 
327
                        }
 
328
                }
 
329
        }
 
330
 
 
331
        FOR_ALL_VEHICLES(v) {
 
332
                switch (v->type) {
 
333
                        case VEH_ROAD:
 
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);
 
336
                                /* FALL THROUGH */
 
337
                        case VEH_TRAIN:
 
338
                        case VEH_SHIP:
 
339
                                v->cur_image = v->GetImage(v->direction);
 
340
                                break;
 
341
 
 
342
                        case VEH_AIRCRAFT:
 
343
                                if (IsNormalAircraft(v)) {
 
344
                                        v->cur_image = v->GetImage(v->direction);
 
345
 
 
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;
 
349
 
 
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);
 
354
                                        }
 
355
 
 
356
                                        UpdateAircraftCache(v);
 
357
                                }
 
358
                                break;
 
359
                        default: break;
 
360
                }
 
361
 
 
362
                v->coord.left = INVALID_COORD;
 
363
                VehicleMove(v, false);
 
364
        }
 
365
}
 
366
 
 
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;
 
374
 
 
375
/**
 
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
 
379
 */
 
380
const SaveLoad *GetVehicleDescription(VehicleType vt)
 
381
{
 
382
        /** Save and load of vehicles */
 
383
        static const SaveLoad _common_veh_desc[] = {
 
384
                     SLE_VAR(Vehicle, subtype,               SLE_UINT8),
 
385
 
 
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),
 
396
 
 
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),
 
403
 
 
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),
 
408
 
 
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),
 
414
 
 
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),
 
418
 
 
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),
 
428
 
 
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),
 
432
 
 
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),
 
436
 
 
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),
 
442
 
 
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),
 
447
 
 
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),
 
451
 
 
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),
 
455
 
 
456
                 SLE_CONDREF(Vehicle, orders,                REF_ORDER,                    0, 104),
 
457
                 SLE_CONDREF(Vehicle, orders,                REF_ORDERLIST,              105, SL_MAX_VERSION),
 
458
 
 
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),
 
475
 
 
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),
 
479
 
 
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),
 
489
 
 
490
                 SLE_CONDVAR(Vehicle, random_bits,           SLE_UINT8,                    2, SL_MAX_VERSION),
 
491
                 SLE_CONDVAR(Vehicle, waiting_triggers,      SLE_UINT8,                    2, SL_MAX_VERSION),
 
492
 
 
493
                 SLE_CONDREF(Vehicle, next_shared,           REF_VEHICLE,                  2, SL_MAX_VERSION),
 
494
                SLE_CONDNULL(2,                                                            2,  68),
 
495
                SLE_CONDNULL(4,                                                           69, 100),
 
496
 
 
497
                 SLE_CONDVAR(Vehicle, group_id,              SLE_UINT16,                  60, SL_MAX_VERSION),
 
498
 
 
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),
 
501
 
 
502
                /* reserve extra space in savegame here. (currently 10 bytes) */
 
503
                SLE_CONDNULL(10,                                                           2, SL_MAX_VERSION),
 
504
 
 
505
                     SLE_END()
 
506
        };
 
507
 
 
508
 
 
509
        static const SaveLoad _train_desc[] = {
 
510
                SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
 
511
                SLE_VEH_INCLUDEX(),
 
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),
 
516
 
 
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),
 
520
 
 
521
                SLE_CONDNULL(2, 2, 19),
 
522
                /* reserve extra space in savegame here. (currently 11 bytes) */
 
523
                SLE_CONDNULL(11, 2, SL_MAX_VERSION),
 
524
 
 
525
                     SLE_END()
 
526
        };
 
527
 
 
528
        static const SaveLoad _roadveh_desc[] = {
 
529
                SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
 
530
                SLE_VEH_INCLUDEX(),
 
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),
 
538
 
 
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),
 
544
 
 
545
                     SLE_END()
 
546
        };
 
547
 
 
548
        static const SaveLoad _ship_desc[] = {
 
549
                SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
 
550
                SLE_VEH_INCLUDEX(),
 
551
                    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state),  SLE_UINT8),
 
552
 
 
553
                /* reserve extra space in savegame here. (currently 16 bytes) */
 
554
                SLE_CONDNULL(16, 2, SL_MAX_VERSION),
 
555
 
 
556
                     SLE_END()
 
557
        };
 
558
 
 
559
        static const SaveLoad _aircraft_desc[] = {
 
560
                SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
 
561
                SLE_VEH_INCLUDEX(),
 
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),
 
564
 
 
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),
 
567
 
 
568
                    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state),                 SLE_UINT8),
 
569
 
 
570
                SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos),          SLE_UINT8,                    2, SL_MAX_VERSION),
 
571
 
 
572
                /* reserve extra space in savegame here. (currently 15 bytes) */
 
573
                SLE_CONDNULL(15,                                                           2, SL_MAX_VERSION),
 
574
 
 
575
                     SLE_END()
 
576
        };
 
577
 
 
578
        static const SaveLoad _special_desc[] = {
 
579
                SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
 
580
 
 
581
                     SLE_VAR(Vehicle, subtype,               SLE_UINT8),
 
582
 
 
583
                 SLE_CONDVAR(Vehicle, tile,                  SLE_FILE_U16 | SLE_VAR_U32,   0,   5),
 
584
                 SLE_CONDVAR(Vehicle, tile,                  SLE_UINT32,                   6, SL_MAX_VERSION),
 
585
 
 
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),
 
591
 
 
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),
 
596
 
 
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),
 
599
 
 
600
                 SLE_CONDVAR(Vehicle, spritenum,             SLE_UINT8,                    2, SL_MAX_VERSION),
 
601
 
 
602
                /* reserve extra space in savegame here. (currently 15 bytes) */
 
603
                SLE_CONDNULL(15,                                                           2, SL_MAX_VERSION),
 
604
 
 
605
                     SLE_END()
 
606
        };
 
607
 
 
608
        static const SaveLoad _disaster_desc[] = {
 
609
                SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
 
610
 
 
611
                     SLE_REF(Vehicle, next,                  REF_VEHICLE_OLD),
 
612
 
 
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),
 
618
 
 
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),
 
625
 
 
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),
 
631
 
 
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),
 
636
 
 
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),
 
639
 
 
640
                /* reserve extra space in savegame here. (currently 16 bytes) */
 
641
                SLE_CONDNULL(16,                                                           2, SL_MAX_VERSION),
 
642
 
 
643
                     SLE_END()
 
644
        };
 
645
 
 
646
 
 
647
        static const SaveLoad *_veh_descs[] = {
 
648
                _train_desc,
 
649
                _roadveh_desc,
 
650
                _ship_desc,
 
651
                _aircraft_desc,
 
652
                _special_desc,
 
653
                _disaster_desc,
 
654
                _common_veh_desc,
 
655
        };
 
656
 
 
657
        return _veh_descs[vt];
 
658
}
 
659
 
 
660
/** Will be called when the vehicles need to be saved. */
 
661
static void Save_VEHS()
 
662
{
 
663
        Vehicle *v;
 
664
        /* Write the vehicles */
 
665
        FOR_ALL_VEHICLES(v) {
 
666
                SlSetArrayIndex(v->index);
 
667
                SlObject(v, GetVehicleDescription(v->type));
 
668
        }
 
669
}
 
670
 
 
671
/** Will be called when vehicles need to be loaded. */
 
672
void Load_VEHS()
 
673
{
 
674
        int index;
 
675
 
 
676
        _cargo_count = 0;
 
677
 
 
678
        while ((index = SlIterateArray()) != -1) {
 
679
                Vehicle *v;
 
680
                VehicleType vtype = (VehicleType)SlReadByte();
 
681
 
 
682
                switch (vtype) {
 
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();
 
691
                }
 
692
 
 
693
                SlObject(v, GetVehicleDescription(vtype));
 
694
 
 
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;
 
704
                        v->cargo.Append(cp);
 
705
                }
 
706
 
 
707
                /* Old savegames used 'last_station_visited = 0xFF' */
 
708
                if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
 
709
                        v->last_station_visited = INVALID_STATION;
 
710
 
 
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;
 
716
                }
 
717
 
 
718
                /* Advanced vehicle lists got added */
 
719
                if (CheckSavegameVersion(60)) v->group_id = DEFAULT_GROUP;
 
720
        }
 
721
}
 
722
 
 
723
extern const ChunkHandler _veh_chunk_handlers[] = {
 
724
        { 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST},
 
725
};