OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

vehicle_sl.cpp
Go to the documentation of this file.
1 /* $Id: vehicle_sl.cpp 27822 2017-03-24 07:33:31Z peter1138 $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../vehicle_func.h"
14 #include "../train.h"
15 #include "../roadveh.h"
16 #include "../ship.h"
17 #include "../aircraft.h"
18 #include "../station_base.h"
19 #include "../effectvehicle_base.h"
20 #include "../company_base.h"
21 #include "../company_func.h"
22 #include "../disaster_vehicle.h"
23
24 #include "saveload.h"
25
26 #include <map>
27
28 #include "../safeguards.h"
29
34 void ConnectMultiheadedTrains()
35 {
36 Train *v;
37
38 FOR_ALL_TRAINS(v) {
39 v->other_multiheaded_part = NULL;
40 }
41
42 FOR_ALL_TRAINS(v) {
43 if (v->IsFrontEngine() || v->IsFreeWagon()) {
44 /* Two ways to associate multiheaded parts to each other:
45 * sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
46 * bracket-matching: Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
47 *
48 * Note: Old savegames might contain chains which do not comply with these rules, e.g.
49 * - the front and read parts have invalid orders
50 * - different engine types might be combined
51 * - there might be different amounts of front and rear parts.
52 *
53 * Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
54 * This is why two matching strategies are needed.
55 */
56
57 bool sequential_matching = v->IsFrontEngine();
58
59 for (Train *u = v; u != NULL; u = u->GetNextVehicle()) {
60 if (u->other_multiheaded_part != NULL) continue; // we already linked this one
61
62 if (u->IsMultiheaded()) {
63 if (!u->IsEngine()) {
64 /* we got a rear car without a front car. We will convert it to a front one */
65 u->SetEngine();
66 u->spritenum--;
67 }
68
69 /* Find a matching back part */
70 EngineID eid = u->engine_type;
71 Train *w;
72 if (sequential_matching) {
73 for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
74 if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
75
76 /* we found a car to partner with this engine. Now we will make sure it face the right way */
77 if (w->IsEngine()) {
78 w->ClearEngine();
79 w->spritenum++;
80 }
81 break;
82 }
83 } else {
84 uint stack_pos = 0;
85 for (w = u->GetNextVehicle(); w != NULL; w = w->GetNextVehicle()) {
86 if (w->engine_type != eid || w->other_multiheaded_part != NULL || !w->IsMultiheaded()) continue;
87
88 if (w->IsEngine()) {
89 stack_pos++;
90 } else {
91 if (stack_pos == 0) break;
92 stack_pos--;
93 }
94 }
95 }
96
97 if (w != NULL) {
98 w->other_multiheaded_part = u;
99 u->other_multiheaded_part = w;
100 } else {
101 /* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
102 u->ClearMultiheaded();
103 }
104 }
105 }
106 }
107 }
108 }
109
114 void ConvertOldMultiheadToNew()
115 {
116 Train *t;
117 FOR_ALL_TRAINS(t) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
118
119 FOR_ALL_TRAINS(t) {
120 if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) {
121 for (Train *u = t; u != NULL; u = u->Next()) {
122 const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
123
124 ClrBit(u->subtype, 7);
125 switch (u->subtype) {
126 case 0: // TS_Front_Engine
127 if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
128 u->SetFrontEngine();
129 u->SetEngine();
130 break;
131
132 case 1: // TS_Artic_Part
133 u->subtype = 0;
134 u->SetArticulatedPart();
135 break;
136
137 case 2: // TS_Not_First
138 u->subtype = 0;
139 if (rvi->railveh_type == RAILVEH_WAGON) {
140 /* normal wagon */
141 u->SetWagon();
142 break;
143 }
144 if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
145 /* rear end of a multiheaded engine */
146 u->SetMultiheaded();
147 break;
148 }
149 if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
150 u->SetEngine();
151 break;
152
153 case 4: // TS_Free_Car
154 u->subtype = 0;
155 u->SetWagon();
156 u->SetFreeWagon();
157 break;
158 default: SlErrorCorrupt("Invalid train subtype");
159 }
160 }
161 }
162 }
163 }
164
165
167 void UpdateOldAircraft()
168 {
169 /* set airport_flags to 0 for all airports just to be sure */
170 Station *st;
171 FOR_ALL_STATIONS(st) {
172 st->airport.flags = 0; // reset airport
173 }
174
175 Aircraft *a;
176 FOR_ALL_AIRCRAFT(a) {
177 /* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
178 * skip those */
179 if (a->IsNormalAircraft()) {
180 /* airplane in terminal stopped doesn't hurt anyone, so goto next */
181 if ((a->vehstatus & VS_STOPPED) && a->state == 0) {
182 a->state = HANGAR;
183 continue;
184 }
185
186 AircraftLeaveHangar(a, a->direction); // make airplane visible if it was in a depot for example
187 a->vehstatus &= ~VS_STOPPED; // make airplane moving
188 UpdateAircraftCache(a);
189 a->cur_speed = a->vcache.cached_max_speed; // so aircraft don't have zero speed while in air
190 if (!a->current_order.IsType(OT_GOTO_STATION) && !a->current_order.IsType(OT_GOTO_DEPOT)) {
191 /* reset current order so aircraft doesn't have invalid "station-only" order */
192 a->current_order.MakeDummy();
193 }
194 a->state = FLYING;
195 AircraftNextAirportPos_and_Order(a); // move it to the entry point of the airport
196 GetNewVehiclePosResult gp = GetNewVehiclePos(a);
197 a->tile = 0; // aircraft in air is tile=0
198
199 /* correct speed of helicopter-rotors */
200 if (a->subtype == AIR_HELICOPTER) a->Next()->Next()->cur_speed = 32;
201
202 /* set new position x,y,z */
203 GetAircraftFlightLevelBounds(a, &a->z_pos, NULL);
204 SetAircraftPosition(a, gp.x, gp.y, GetAircraftFlightLevel(a));
205 }
206 }
207 }
208
216 static void CheckValidVehicles()
217 {
218 size_t total_engines = Engine::GetPoolSize();
219 EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE };
220
221 Engine *e;
222 FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; }
223 FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; }
224 FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; }
225 FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; }
226
227 Vehicle *v;
228 FOR_ALL_VEHICLES(v) {
229 /* Test if engine types match */
230 switch (v->type) {
231 case VEH_TRAIN:
232 case VEH_ROAD:
233 case VEH_SHIP:
234 case VEH_AIRCRAFT:
235 if (v->engine_type >= total_engines || v->type != v->GetEngine()->type) {
236 v->engine_type = first_engine[v->type];
237 }
238 break;
239
240 default:
241 break;
242 }
243 }
244 }
245
246 extern byte _age_cargo_skip_counter; // From misc_sl.cpp
247
249 void AfterLoadVehicles(bool part_of_load)
250 {
251 Vehicle *v;
252
253 FOR_ALL_VEHICLES(v) {
254 /* Reinstate the previous pointer */
255 if (v->Next() != NULL) v->Next()->previous = v;
256 if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
257
258 if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
259 v->first = NULL;
260 if (v->IsGroundVehicle()) v->GetGroundVehicleCache()->first_engine = INVALID_ENGINE;
261 }
262
263 /* AfterLoadVehicles may also be called in case of NewGRF reload, in this
264 * case we may not convert orders again. */
265 if (part_of_load) {
266 /* Create shared vehicle chain for very old games (pre 5,2) and create
267 * OrderList from shared vehicle chains. For this to work correctly, the
268 * following conditions must be fulfilled:
269 * a) both next_shared and previous_shared are not set for pre 5,2 games
270 * b) both next_shared and previous_shared are set for later games
271 */
272 std::map<Order*, OrderList*> mapping;
273
274 FOR_ALL_VEHICLES(v) {
275 if (v->orders.old != NULL) {
276 if (IsSavegameVersionBefore(105)) { // Pre-105 didn't save an OrderList
277 if (mapping[v->orders.old] == NULL) {
278 /* This adds the whole shared vehicle chain for case b */
279
280 /* Creating an OrderList here is safe because the number of vehicles
281 * allowed in these savegames matches the number of OrderLists. As
282 * such each vehicle can get an OrderList and it will (still) fit. */
283 assert(OrderList::CanAllocateItem());
284 v->orders.list = mapping[v->orders.old] = new OrderList(v->orders.old, v);
285 } else {
286 v->orders.list = mapping[v->orders.old];
287 /* For old games (case a) we must create the shared vehicle chain */
288 if (IsSavegameVersionBefore(5, 2)) {
289 v->AddToShared(v->orders.list->GetFirstSharedVehicle());
290 }
291 }
292 } else { // OrderList was saved as such, only recalculate not saved values
293 if (v->PreviousShared() == NULL) {
294 v->orders.list->Initialize(v->orders.list->first, v);
295 }
296 }
297 }
298 }
299 }
300
301 FOR_ALL_VEHICLES(v) {
302 /* Fill the first pointers */
303 if (v->Previous() == NULL) {
304 for (Vehicle *u = v; u != NULL; u = u->Next()) {
305 u->first = v;
306 }
307 }
308 }
309
310 if (part_of_load) {
311 if (IsSavegameVersionBefore(105)) {
312 /* Before 105 there was no order for shared orders, thus it messed up horribly */
313 FOR_ALL_VEHICLES(v) {
314 if (v->First() != v || v->orders.list != NULL || v->previous_shared != NULL || v->next_shared == NULL) continue;
315
316 /* As above, allocating OrderList here is safe. */
317 assert(OrderList::CanAllocateItem());
318 v->orders.list = new OrderList(NULL, v);
319 for (Vehicle *u = v; u != NULL; u = u->next_shared) {
320 u->orders.list = v->orders.list;
321 }
322 }
323 }
324
325 if (IsSavegameVersionBefore(157)) {
326 /* The road vehicle subtype was converted to a flag. */
327 RoadVehicle *rv;
328 FOR_ALL_ROADVEHICLES(rv) {
329 if (rv->subtype == 0) {
330 /* The road vehicle is at the front. */
331 rv->SetFrontEngine();
332 } else if (rv->subtype == 1) {
333 /* The road vehicle is an articulated part. */
334 rv->subtype = 0;
335 rv->SetArticulatedPart();
336 } else {
337 SlErrorCorrupt("Invalid road vehicle subtype");
338 }
339 }
340 }
341
342 if (IsSavegameVersionBefore(160)) {
343 /* In some old savegames there might be some "crap" stored. */
344 FOR_ALL_VEHICLES(v) {
345 if (!v->IsPrimaryVehicle() && v->type != VEH_DISASTER) {
346 v->current_order.Free();
347 v->unitnumber = 0;
348 }
349 }
350 }
351
352 if (IsSavegameVersionBefore(162)) {
353 /* Set the vehicle-local cargo age counter from the old global counter. */
354 FOR_ALL_VEHICLES(v) {
355 v->cargo_age_counter = _age_cargo_skip_counter;
356 }
357 }
358
359 if (IsSavegameVersionBefore(180)) {
360 /* Set service interval flags */
361 FOR_ALL_VEHICLES(v) {
362 if (!v->IsPrimaryVehicle()) continue;
363
364 const Company *c = Company::Get(v->owner);
365 int interval = CompanyServiceInterval(c, v->type);
366
367 v->SetServiceIntervalIsCustom(v->GetServiceInterval() != interval);
368 v->SetServiceIntervalIsPercent(c->settings.vehicle.servint_ispercent);
369 }
370 }
371 }
372
373 CheckValidVehicles();
374
375 FOR_ALL_VEHICLES(v) {
376 assert(v->first != NULL);
377
378 v->trip_occupancy = CalcPercentVehicleFilled(v, NULL);
379
380 switch (v->type) {
381 case VEH_TRAIN: {
382 Train *t = Train::From(v);
383 if (t->IsFrontEngine() || t->IsFreeWagon()) {
384 t->gcache.last_speed = t->cur_speed; // update displayed train speed
385 t->ConsistChanged(CCF_SAVELOAD);
386 }
387 break;
388 }
389
390 case VEH_ROAD: {
391 RoadVehicle *rv = RoadVehicle::From(v);
392 if (rv->IsFrontEngine()) {
393 rv->gcache.last_speed = rv->cur_speed; // update displayed road vehicle speed
394 RoadVehUpdateCache(rv);
395 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
396 rv->CargoChanged();
397 }
398 }
399 break;
400 }
401
402 case VEH_SHIP:
403 Ship::From(v)->UpdateCache();
404 break;
405
406 default: break;
407 }
408 }
409
410 /* Stop non-front engines */
411 if (part_of_load && IsSavegameVersionBefore(112)) {
412 FOR_ALL_VEHICLES(v) {
413 if (v->type == VEH_TRAIN) {
414 Train *t = Train::From(v);
415 if (!t->IsFrontEngine()) {
416 if (t->IsEngine()) t->vehstatus |= VS_STOPPED;
417 /* cur_speed is now relevant for non-front parts - nonzero breaks
418 * moving-wagons-inside-depot- and autoreplace- code */
419 t->cur_speed = 0;
420 }
421 }
422 /* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
423 * other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
424 if ((v->vehstatus & VS_STOPPED) && (v->type != VEH_TRAIN || IsSavegameVersionBefore(2, 1))) {
425 v->cur_speed = 0;
426 }
427 }
428 }
429
430 FOR_ALL_VEHICLES(v) {
431 switch (v->type) {
432 case VEH_ROAD: {
433 RoadVehicle *rv = RoadVehicle::From(v);
434 rv->roadtype = HasBit(EngInfo(v->First()->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
435 rv->compatible_roadtypes = RoadTypeToRoadTypes(rv->roadtype);
436 /* FALL THROUGH */
437 }
438
439 case VEH_TRAIN:
440 case VEH_SHIP:
441 v->GetImage(v->direction, EIT_ON_MAP, &v->sprite_seq);
442 break;
443
444 case VEH_AIRCRAFT:
445 if (Aircraft::From(v)->IsNormalAircraft()) {
446 v->GetImage(v->direction, EIT_ON_MAP, &v->sprite_seq);
447
448 /* The plane's shadow will have the same image as the plane, but no colour */
449 Vehicle *shadow = v->Next();
450 shadow->sprite_seq.CopyWithoutPalette(v->sprite_seq);
451
452 /* In the case of a helicopter we will update the rotor sprites */
453 if (v->subtype == AIR_HELICOPTER) {
454 Vehicle *rotor = shadow->Next();
455 GetRotorImage(Aircraft::From(v), EIT_ON_MAP, &rotor->sprite_seq);
456 }
457
458 UpdateAircraftCache(Aircraft::From(v), true);
459 }
460 break;
461 default: break;
462 }
463
464 v->UpdateDeltaXY(v->direction);
465 v->coord.left = INVALID_COORD;
466 v->UpdatePosition();
467 v->UpdateViewport(false);
468 }
469 }
470
471 bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // From train_cmd.cpp
472 void ReverseTrainDirection(Train *v);
473 void ReverseTrainSwapVeh(Train *v, int l, int r);
474
476 void FixupTrainLengths()
477 {
478 /* Vehicle center was moved from 4 units behind the front to half the length
479 * behind the front. Move vehicles so they end up on the same spot. */
480 Vehicle *v;
481 FOR_ALL_VEHICLES(v) {
482 if (v->type == VEH_TRAIN && v->IsPrimaryVehicle()) {
483 /* The vehicle center is now more to the front depending on vehicle length,
484 * so we need to move all vehicles forward to cover the difference to the
485 * old center, otherwise wagon spacing in trains would be broken upon load. */
486 for (Train *u = Train::From(v); u != NULL; u = u->Next()) {
487 if (u->track == TRACK_BIT_DEPOT || (u->vehstatus & VS_CRASHED)) continue;
488
489 Train *next = u->Next();
490
491 /* Try to pull the vehicle half its length forward. */
492 int diff = (VEHICLE_LENGTH - u->gcache.cached_veh_length) / 2;
493 int done;
494 for (done = 0; done < diff; done++) {
495 if (!TrainController(u, next, false)) break;
496 }
497
498 if (next != NULL && done < diff && u->IsFrontEngine()) {
499 /* Pulling the front vehicle forwards failed, we either encountered a dead-end
500 * or a red signal. To fix this, we try to move the whole train the required
501 * space backwards and re-do the fix up of the front vehicle. */
502
503 /* Ignore any signals when backtracking. */
504 TrainForceProceeding old_tfp = u->force_proceed;
505 u->force_proceed = TFP_SIGNAL;
506
507 /* Swap start<>end, start+1<>end-1, ... */
508 int r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
509 int l = 0;
510 do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
511
512 /* We moved the first vehicle which is now the last. Move it back to the
513 * original position as we will fix up the last vehicle later in the loop. */
514 for (int i = 0; i < done; i++) TrainController(u->Last(), NULL);
515
516 /* Move the train backwards to get space for the first vehicle. As the stopping
517 * distance from a line end is rounded up, move the train one unit more to cater
518 * for front vehicles with odd lengths. */
519 int moved;
520 for (moved = 0; moved < diff + 1; moved++) {
521 if (!TrainController(u, NULL, false)) break;
522 }
523
524 /* Swap start<>end, start+1<>end-1, ... again. */
525 r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
526 l = 0;
527 do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
528
529 u->force_proceed = old_tfp;
530
531 /* Tracks are too short to fix the train length. The player has to fix the
532 * train in a depot. Bail out so we don't damage the vehicle chain any more. */
533 if (moved < diff + 1) break;
534
535 /* Re-do the correction for the first vehicle. */
536 for (done = 0; done < diff; done++) TrainController(u, next, false);
537
538 /* We moved one unit more backwards than needed for even-length front vehicles,
539 * try to move that unit forward again. We don't care if this step fails. */
540 TrainController(u, NULL, false);
541 }
542
543 /* If the next wagon is still in a depot, check if it shouldn't be outside already. */
544 if (next != NULL && next->track == TRACK_BIT_DEPOT) {
545 int d = TicksToLeaveDepot(u);
546 if (d <= 0) {
547 /* Next vehicle should have left the depot already, show it and pull forward. */
548 next->vehstatus &= ~VS_HIDDEN;
549 next->track = TrackToTrackBits(GetRailDepotTrack(next->tile));
550 for (int i = 0; i >= d; i--) TrainController(next, NULL);
551 }
552 }
553 }
554
555 /* Update all cached properties after moving the vehicle chain around. */
556 Train::From(v)->ConsistChanged(CCF_TRACK);
557 }
558 }
559 }
560
561 static uint8 _cargo_days;
562 static uint16 _cargo_source;
563 static uint32 _cargo_source_xy;
564 static uint16 _cargo_count;
565 static uint16 _cargo_paid_for;
566 static Money _cargo_feeder_share;
567 static uint32 _cargo_loaded_at_xy;
568
574 const SaveLoad *GetVehicleDescription(VehicleType vt)
575 {
577 static const SaveLoad _common_veh_desc[] = {
578 SLE_VAR(Vehicle, subtype, SLE_UINT8),
579
580 SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
581 SLE_CONDVAR(Vehicle, name, SLE_NAME, 0, 83),
582 SLE_CONDSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, 0, 84, SL_MAX_VERSION),
583 SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, 0, 7),
584 SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, 8, SL_MAX_VERSION),
585 SLE_VAR(Vehicle, owner, SLE_UINT8),
586 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
587 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
588 SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
589 SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
590
591 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
592 SLE_CONDVAR(Vehicle, x_pos, SLE_UINT32, 6, SL_MAX_VERSION),
593 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
594 SLE_CONDVAR(Vehicle, y_pos, SLE_UINT32, 6, SL_MAX_VERSION),
595 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
596 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
597 SLE_VAR(Vehicle, direction, SLE_UINT8),
598
599 SLE_CONDNULL(2, 0, 57),
600 SLE_VAR(Vehicle, spritenum, SLE_UINT8),
601 SLE_CONDNULL(5, 0, 57),
602 SLE_VAR(Vehicle, engine_type, SLE_UINT16),
603
604 SLE_CONDNULL(2, 0, 151),
605 SLE_VAR(Vehicle, cur_speed, SLE_UINT16),
606 SLE_VAR(Vehicle, subspeed, SLE_UINT8),
607 SLE_VAR(Vehicle, acceleration, SLE_UINT8),
608 SLE_VAR(Vehicle, progress, SLE_UINT8),
609
610 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
611 SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
612 SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16, 5, SL_MAX_VERSION),
613 SLE_CONDVAR(Vehicle, last_loading_station, SLE_UINT16, 182, SL_MAX_VERSION),
614
615 SLE_VAR(Vehicle, cargo_type, SLE_UINT8),
616 SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, 35, SL_MAX_VERSION),
617 SLEG_CONDVAR( _cargo_days, SLE_UINT8, 0, 67),
618 SLEG_CONDVAR( _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, 0, 6),
619 SLEG_CONDVAR( _cargo_source, SLE_UINT16, 7, 67),
620 SLEG_CONDVAR( _cargo_source_xy, SLE_UINT32, 44, 67),
621 SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
622 SLE_CONDVAR(Vehicle, refit_cap, SLE_UINT16, 182, SL_MAX_VERSION),
623 SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67),
624 SLE_CONDLST(Vehicle, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
625 SLE_CONDARR(Vehicle, cargo.action_counts, SLE_UINT, VehicleCargoList::NUM_MOVE_TO_ACTION, 181, SL_MAX_VERSION),
626 SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION),
627
628 SLE_VAR(Vehicle, day_counter, SLE_UINT8),
629 SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
630 SLE_CONDVAR(Vehicle, running_ticks, SLE_UINT8, 88, SL_MAX_VERSION),
631
632 SLE_VAR(Vehicle, cur_implicit_order_index, SLE_UINT8),
633 SLE_CONDVAR(Vehicle, cur_real_order_index, SLE_UINT8, 158, SL_MAX_VERSION),
634 /* num_orders is now part of OrderList and is not saved but counted */
635 SLE_CONDNULL(1, 0, 104),
636
637 /* This next line is for version 4 and prior compatibility.. it temporarily reads
638 type and flags (which were both 4 bits) into type. Later on this is
639 converted correctly */
640 SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, 0, 4),
641 SLE_CONDVAR(Vehicle, current_order.dest, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
642
643 /* Orders for version 5 and on */
644 SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, 5, SL_MAX_VERSION),
645 SLE_CONDVAR(Vehicle, current_order.flags, SLE_UINT8, 5, SL_MAX_VERSION),
646 SLE_CONDVAR(Vehicle, current_order.dest, SLE_UINT16, 5, SL_MAX_VERSION),
647
648 /* Refit in current order */
649 SLE_CONDVAR(Vehicle, current_order.refit_cargo, SLE_UINT8, 36, SL_MAX_VERSION),
650 SLE_CONDNULL(1, 36, 181), // refit_subtype
651
652 /* Timetable in current order */
653 SLE_CONDVAR(Vehicle, current_order.wait_time, SLE_UINT16, 67, SL_MAX_VERSION),
654 SLE_CONDVAR(Vehicle, current_order.travel_time, SLE_UINT16, 67, SL_MAX_VERSION),
655 SLE_CONDVAR(Vehicle, current_order.max_speed, SLE_UINT16, 174, SL_MAX_VERSION),
656 SLE_CONDVAR(Vehicle, timetable_start, SLE_INT32, 129, SL_MAX_VERSION),
657
658 SLE_CONDREF(Vehicle, orders, REF_ORDER, 0, 104),
659 SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION),
660
661 SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
662 SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
663 SLE_CONDVAR(Vehicle, max_age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
664 SLE_CONDVAR(Vehicle, max_age, SLE_INT32, 31, SL_MAX_VERSION),
665 SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
666 SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32, 31, SL_MAX_VERSION),
667 SLE_CONDVAR(Vehicle, service_interval, SLE_UINT16, 0, 30),
668 SLE_CONDVAR(Vehicle, service_interval, SLE_FILE_U32 | SLE_VAR_U16, 31, 179),
669 SLE_CONDVAR(Vehicle, service_interval, SLE_UINT16, 180, SL_MAX_VERSION),
670 SLE_VAR(Vehicle, reliability, SLE_UINT16),
671 SLE_VAR(Vehicle, reliability_spd_dec, SLE_UINT16),
672 SLE_VAR(Vehicle, breakdown_ctr, SLE_UINT8),
673 SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8),
674 SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
675 SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8),
676 SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30),
677 SLE_CONDVAR(Vehicle, build_year, SLE_INT32, 31, SL_MAX_VERSION),
678
679 SLE_VAR(Vehicle, load_unload_ticks, SLE_UINT16),
680 SLEG_CONDVAR( _cargo_paid_for, SLE_UINT16, 45, SL_MAX_VERSION),
681 SLE_CONDVAR(Vehicle, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, 40, 179),
682 SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT16, 180, SL_MAX_VERSION),
683
684 SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
685 SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, 65, SL_MAX_VERSION),
686 SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
687 SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, 65, SL_MAX_VERSION),
688 SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64, 51, 64),
689 SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
690 SLEG_CONDVAR( _cargo_loaded_at_xy, SLE_UINT32, 51, 67),
691 SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
692 SLE_CONDVAR(Vehicle, value, SLE_INT64, 65, SL_MAX_VERSION),
693
694 SLE_CONDVAR(Vehicle, random_bits, SLE_UINT8, 2, SL_MAX_VERSION),
695 SLE_CONDVAR(Vehicle, waiting_triggers, SLE_UINT8, 2, SL_MAX_VERSION),
696
697 SLE_CONDREF(Vehicle, next_shared, REF_VEHICLE, 2, SL_MAX_VERSION),
698 SLE_CONDNULL(2, 2, 68),
699 SLE_CONDNULL(4, 69, 100),
700
701 SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
702
703 SLE_CONDVAR(Vehicle, current_order_time, SLE_UINT32, 67, SL_MAX_VERSION),
704 SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, 67, SL_MAX_VERSION),
705
706 SLE_CONDNULL(10, 2, 143), // old reserved space
707
708 SLE_END()
709 };
710
711
712 static const SaveLoad _train_desc[] = {
713 SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
714 SLE_VEH_INCLUDE(),
715 SLE_VAR(Train, crash_anim_pos, SLE_UINT16),
716 SLE_VAR(Train, force_proceed, SLE_UINT8),
717 SLE_VAR(Train, railtype, SLE_UINT8),
718 SLE_VAR(Train, track, SLE_UINT8),
719
720 SLE_CONDVAR(Train, flags, SLE_FILE_U8 | SLE_VAR_U16, 2, 99),
721 SLE_CONDVAR(Train, flags, SLE_UINT16, 100, SL_MAX_VERSION),
722 SLE_CONDNULL(2, 2, 59),
723
724 SLE_CONDVAR(Train, wait_counter, SLE_UINT16, 136, SL_MAX_VERSION),
725
726 SLE_CONDNULL(2, 2, 19),
727 SLE_CONDVAR(Train, gv_flags, SLE_UINT16, 139, SL_MAX_VERSION),
728 SLE_CONDNULL(11, 2, 143), // old reserved space
729
730 SLE_END()
731 };
732
733 static const SaveLoad _roadveh_desc[] = {
734 SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
735 SLE_VEH_INCLUDE(),
736 SLE_VAR(RoadVehicle, state, SLE_UINT8),
737 SLE_VAR(RoadVehicle, frame, SLE_UINT8),
738 SLE_VAR(RoadVehicle, blocked_ctr, SLE_UINT16),
739 SLE_VAR(RoadVehicle, overtaking, SLE_UINT8),
740 SLE_VAR(RoadVehicle, overtaking_ctr, SLE_UINT8),
741 SLE_VAR(RoadVehicle, crashed_ctr, SLE_UINT16),
742 SLE_VAR(RoadVehicle, reverse_ctr, SLE_UINT8),
743
744 SLE_CONDNULL(2, 6, 68),
745 SLE_CONDVAR(RoadVehicle, gv_flags, SLE_UINT16, 139, SL_MAX_VERSION),
746 SLE_CONDNULL(4, 69, 130),
747 SLE_CONDNULL(2, 6, 130),
748 SLE_CONDNULL(16, 2, 143), // old reserved space
749
750 SLE_END()
751 };
752
753 static const SaveLoad _ship_desc[] = {
754 SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
755 SLE_VEH_INCLUDE(),
756 SLE_VAR(Ship, state, SLE_UINT8),
757
758 SLE_CONDNULL(16, 2, 143), // old reserved space
759
760 SLE_END()
761 };
762
763 static const SaveLoad _aircraft_desc[] = {
764 SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
765 SLE_VEH_INCLUDE(),
766 SLE_VAR(Aircraft, crashed_counter, SLE_UINT16),
767 SLE_VAR(Aircraft, pos, SLE_UINT8),
768
769 SLE_CONDVAR(Aircraft, targetairport, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
770 SLE_CONDVAR(Aircraft, targetairport, SLE_UINT16, 5, SL_MAX_VERSION),
771
772 SLE_VAR(Aircraft, state, SLE_UINT8),
773
774 SLE_CONDVAR(Aircraft, previous_pos, SLE_UINT8, 2, SL_MAX_VERSION),
775 SLE_CONDVAR(Aircraft, last_direction, SLE_UINT8, 2, SL_MAX_VERSION),
776 SLE_CONDVAR(Aircraft, number_consecutive_turns, SLE_UINT8, 2, SL_MAX_VERSION),
777
778 SLE_CONDVAR(Aircraft, turn_counter, SLE_UINT8, 136, SL_MAX_VERSION),
779 SLE_CONDVAR(Aircraft, flags, SLE_UINT8, 167, SL_MAX_VERSION),
780
781 SLE_CONDNULL(13, 2, 143), // old reserved space
782
783 SLE_END()
784 };
785
786 static const SaveLoad _special_desc[] = {
787 SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
788
789 SLE_VAR(Vehicle, subtype, SLE_UINT8),
790
791 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
792 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
793
794 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
795 SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
796 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
797 SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
798 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
799 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
800
801 SLE_VAR(Vehicle, sprite_seq.seq[0].sprite, SLE_FILE_U16 | SLE_VAR_U32),
802 SLE_CONDNULL(5, 0, 57),
803 SLE_VAR(Vehicle, progress, SLE_UINT8),
804 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
805
806 SLE_VAR(EffectVehicle, animation_state, SLE_UINT16),
807 SLE_VAR(EffectVehicle, animation_substate, SLE_UINT8),
808
809 SLE_CONDVAR(Vehicle, spritenum, SLE_UINT8, 2, SL_MAX_VERSION),
810
811 SLE_CONDNULL(15, 2, 143), // old reserved space
812
813 SLE_END()
814 };
815
816 static const SaveLoad _disaster_desc[] = {
817 SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
818
819 SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
820
821 SLE_VAR(Vehicle, subtype, SLE_UINT8),
822 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
823 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, 6, SL_MAX_VERSION),
824 SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
825 SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, 6, SL_MAX_VERSION),
826
827 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
828 SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, 6, SL_MAX_VERSION),
829 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
830 SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, 6, SL_MAX_VERSION),
831 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, 0, 163),
832 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, 164, SL_MAX_VERSION),
833 SLE_VAR(Vehicle, direction, SLE_UINT8),
834
835 SLE_CONDNULL(5, 0, 57),
836 SLE_VAR(Vehicle, owner, SLE_UINT8),
837 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
838 SLE_CONDVAR(Vehicle, current_order.dest, SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
839 SLE_CONDVAR(Vehicle, current_order.dest, SLE_UINT16, 5, SL_MAX_VERSION),
840
841 SLE_VAR(Vehicle, sprite_seq.seq[0].sprite, SLE_FILE_U16 | SLE_VAR_U32),
842 SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
843 SLE_CONDVAR(Vehicle, age, SLE_INT32, 31, SL_MAX_VERSION),
844 SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
845
846 SLE_CONDVAR(DisasterVehicle, image_override, SLE_FILE_U16 | SLE_VAR_U32, 0, 190),
847 SLE_CONDVAR(DisasterVehicle, image_override, SLE_UINT32, 191, SL_MAX_VERSION),
848 SLE_CONDVAR(DisasterVehicle, big_ufo_destroyer_target, SLE_FILE_U16 | SLE_VAR_U32, 0, 190),
849 SLE_CONDVAR(DisasterVehicle, big_ufo_destroyer_target, SLE_UINT32, 191, SL_MAX_VERSION),
850 SLE_CONDVAR(DisasterVehicle, flags, SLE_UINT8, 194, SL_MAX_VERSION),
851
852 SLE_CONDNULL(16, 2, 143), // old reserved space
853
854 SLE_END()
855 };
856
857
858 static const SaveLoad * const _veh_descs[] = {
859 _train_desc,
860 _roadveh_desc,
861 _ship_desc,
862 _aircraft_desc,
863 _special_desc,
864 _disaster_desc,
865 _common_veh_desc,
866 };
867
868 return _veh_descs[vt];
869 }
870
872 static void Save_VEHS()
873 {
874 Vehicle *v;
875 /* Write the vehicles */
876 FOR_ALL_VEHICLES(v) {
877 SlSetArrayIndex(v->index);
878 SlObject(v, GetVehicleDescription(v->type));
879 }
880 }
881
883 void Load_VEHS()
884 {
885 int index;
886
887 _cargo_count = 0;
888
889 while ((index = SlIterateArray()) != -1) {
890 Vehicle *v;
891 VehicleType vtype = (VehicleType)SlReadByte();
892
893 switch (vtype) {
894 case VEH_TRAIN: v = new (index) Train(); break;
895 case VEH_ROAD: v = new (index) RoadVehicle(); break;
896 case VEH_SHIP: v = new (index) Ship(); break;
897 case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
898 case VEH_EFFECT: v = new (index) EffectVehicle(); break;
899 case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
900 case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
901 default: SlErrorCorrupt("Invalid vehicle type");
902 }
903
904 SlObject(v, GetVehicleDescription(vtype));
905
906 if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
907 /* Don't construct the packet with station here, because that'll fail with old savegames */
908 CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
909 v->cargo.Append(cp);
910 }
911
912 /* Old savegames used 'last_station_visited = 0xFF' */
913 if (IsSavegameVersionBefore(5) && v->last_station_visited == 0xFF) {
914 v->last_station_visited = INVALID_STATION;
915 }
916
917 if (IsSavegameVersionBefore(182)) v->last_loading_station = INVALID_STATION;
918
919 if (IsSavegameVersionBefore(5)) {
920 /* Convert the current_order.type (which is a mix of type and flags, because
921 * in those versions, they both were 4 bits big) to type and flags */
922 v->current_order.flags = GB(v->current_order.type, 4, 4);
923 v->current_order.type &= 0x0F;
924 }
925
926 /* Advanced vehicle lists got added */
927 if (IsSavegameVersionBefore(60)) v->group_id = DEFAULT_GROUP;
928 }
929 }
930
931 static void Ptrs_VEHS()
932 {
933 Vehicle *v;
934 FOR_ALL_VEHICLES(v) {
935 SlObject(v, GetVehicleDescription(v->type));
936 }
937 }
938
939 extern const ChunkHandler _veh_chunk_handlers[] = {
940 { 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, NULL, CH_SPARSE_ARRAY | CH_LAST},
941 };