OpenTTD Source: src/saveload/vehicle_sl.cpp Source File

OpenTTD Source 15.0-RC2

vehicle_sl.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/vehicle_sl_compat.h"
14
15#include "../debug.h"
16#include "../vehicle_func.h"
17#include "../train.h"
18#include "../roadveh.h"
19#include "../ship.h"
20#include "../aircraft.h"
21#include "../timetable.h"
22#include "../station_base.h"
23#include "../effectvehicle_base.h"
24#include "../company_base.h"
25#include "../company_func.h"
26#include "../disaster_vehicle.h"
27#include "../economy_base.h"
28
29#include "../safeguards.h"
30
35 void ConnectMultiheadedTrains()
36{
37 for (Train *v : Train::Iterate()) {
38 v->other_multiheaded_part = nullptr;
39 }
40
41 for (Train *v : Train::Iterate()) {
42 if (v->IsFrontEngine() || v->IsFreeWagon()) {
43 /* Two ways to associate multiheaded parts to each other:
44 * sequential-matching: Trains shall be arranged to look like <..>..<..>..<..>..
45 * bracket-matching: Free vehicle chains shall be arranged to look like ..<..<..>..<..>..>..
46 *
47 * Note: Old savegames might contain chains which do not comply with these rules, e.g.
48 * - the front and read parts have invalid orders
49 * - different engine types might be combined
50 * - there might be different amounts of front and rear parts.
51 *
52 * Note: The multiheaded parts need to be matched exactly like they are matched on the server, else desyncs will occur.
53 * This is why two matching strategies are needed.
54 */
55
56 bool sequential_matching = v->IsFrontEngine();
57
58 for (Train *u = v; u != nullptr; u = u->GetNextVehicle()) {
59 if (u->other_multiheaded_part != nullptr) continue; // we already linked this one
60
61 if (u->IsMultiheaded()) {
62 if (!u->IsEngine()) {
63 /* we got a rear car without a front car. We will convert it to a front one */
64 u->SetEngine();
65 u->spritenum--;
66 }
67
68 /* Find a matching back part */
69 EngineID eid = u->engine_type;
70 Train *w;
71 if (sequential_matching) {
72 for (w = u->GetNextVehicle(); w != nullptr; w = w->GetNextVehicle()) {
73 if (w->engine_type != eid || w->other_multiheaded_part != nullptr || !w->IsMultiheaded()) continue;
74
75 /* we found a car to partner with this engine. Now we will make sure it face the right way */
76 if (w->IsEngine()) {
77 w->ClearEngine();
78 w->spritenum++;
79 }
80 break;
81 }
82 } else {
83 uint stack_pos = 0;
84 for (w = u->GetNextVehicle(); w != nullptr; w = w->GetNextVehicle()) {
85 if (w->engine_type != eid || w->other_multiheaded_part != nullptr || !w->IsMultiheaded()) continue;
86
87 if (w->IsEngine()) {
88 stack_pos++;
89 } else {
90 if (stack_pos == 0) break;
91 stack_pos--;
92 }
93 }
94 }
95
96 if (w != nullptr) {
97 w->other_multiheaded_part = u;
98 u->other_multiheaded_part = w;
99 } else {
100 /* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */
101 u->ClearMultiheaded();
102 }
103 }
104 }
105 }
106 }
107}
108
113 void ConvertOldMultiheadToNew()
114{
115 for (Train *t : Train::Iterate()) SetBit(t->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop
116
117 for (Train *t : Train::Iterate()) {
118 if (HasBit(t->subtype, 7) && ((t->subtype & ~0x80) == 0 || (t->subtype & ~0x80) == 4)) {
119 for (Train *u = t; u != nullptr; u = u->Next()) {
120 const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
121
122 ClrBit(u->subtype, 7);
123 switch (u->subtype) {
124 case 0: // TS_Front_Engine
125 if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
126 u->SetFrontEngine();
127 u->SetEngine();
128 break;
129
130 case 1: // TS_Artic_Part
131 u->subtype = 0;
132 u->SetArticulatedPart();
133 break;
134
135 case 2: // TS_Not_First
136 u->subtype = 0;
137 if (rvi->railveh_type == RAILVEH_WAGON) {
138 /* normal wagon */
139 u->SetWagon();
140 break;
141 }
142 if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) {
143 /* rear end of a multiheaded engine */
144 u->SetMultiheaded();
145 break;
146 }
147 if (rvi->railveh_type == RAILVEH_MULTIHEAD) u->SetMultiheaded();
148 u->SetEngine();
149 break;
150
151 case 4: // TS_Free_Car
152 u->subtype = 0;
153 u->SetWagon();
154 u->SetFreeWagon();
155 break;
156 default: SlErrorCorrupt("Invalid train subtype");
157 }
158 }
159 }
160 }
161}
162
163
165 void UpdateOldAircraft()
166{
167 /* set airport_flags to 0 for all airports just to be sure */
168 for (Station *st : Station::Iterate()) {
169 st->airport.blocks = {}; // reset airport
170 }
171
172 for (Aircraft *a : Aircraft::Iterate()) {
173 /* airplane has another vehicle with subtype 4 (shadow), helicopter also has 3 (rotor)
174 * skip those */
175 if (a->IsNormalAircraft()) {
176 /* airplane in terminal stopped doesn't hurt anyone, so goto next */
177 if (a->vehstatus.Test(VehState::Stopped) && a->state == 0) {
178 a->state = HANGAR;
179 continue;
180 }
181
182 AircraftLeaveHangar(a, a->direction); // make airplane visible if it was in a depot for example
183 a->vehstatus.Reset(VehState::Stopped); // make airplane moving
184 UpdateAircraftCache(a);
185 a->cur_speed = a->vcache.cached_max_speed; // so aircraft don't have zero speed while in air
186 if (!a->current_order.IsType(OT_GOTO_STATION) && !a->current_order.IsType(OT_GOTO_DEPOT)) {
187 /* reset current order so aircraft doesn't have invalid "station-only" order */
188 a->current_order.MakeDummy();
189 }
190 a->state = FLYING;
191 AircraftNextAirportPos_and_Order(a); // move it to the entry point of the airport
192 GetNewVehiclePosResult gp = GetNewVehiclePos(a);
193 a->tile = TileIndex{}; // aircraft in air is tile=0
194
195 /* correct speed of helicopter-rotors */
196 if (a->subtype == AIR_HELICOPTER) a->Next()->Next()->cur_speed = 32;
197
198 /* set new position x,y,z */
199 GetAircraftFlightLevelBounds(a, &a->z_pos, nullptr);
200 SetAircraftPosition(a, gp.x, gp.y, GetAircraftFlightLevel(a));
201 }
202 }
203
204 /* Clear aircraft from loading vehicles, if we bumped them into the air. */
205 for (Station *st : Station::Iterate()) {
206 for (auto iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); /* nothing */) {
207 Vehicle *v = *iter;
208 if (v->type == VEH_AIRCRAFT && !v->current_order.IsType(OT_LOADING)) {
209 iter = st->loading_vehicles.erase(iter);
210 delete v->cargo_payment;
211 } else {
212 ++iter;
213 }
214 }
215 }
216}
217
225 static void CheckValidVehicles()
226{
227 size_t total_engines = Engine::GetPoolSize();
228 EngineID first_engine[4] = { EngineID::Invalid(), EngineID::Invalid(), EngineID::Invalid(), EngineID::Invalid() };
229
230 for (const Engine *e : Engine::IterateType(VEH_TRAIN)) { first_engine[VEH_TRAIN] = e->index; break; }
231 for (const Engine *e : Engine::IterateType(VEH_ROAD)) { first_engine[VEH_ROAD] = e->index; break; }
232 for (const Engine *e : Engine::IterateType(VEH_SHIP)) { first_engine[VEH_SHIP] = e->index; break; }
233 for (const Engine *e : Engine::IterateType(VEH_AIRCRAFT)) { first_engine[VEH_AIRCRAFT] = e->index; break; }
234
235 for (Vehicle *v : Vehicle::Iterate()) {
236 /* Test if engine types match */
237 switch (v->type) {
238 case VEH_TRAIN:
239 case VEH_ROAD:
240 case VEH_SHIP:
241 case VEH_AIRCRAFT:
242 if (v->engine_type >= total_engines || v->type != v->GetEngine()->type) {
243 v->engine_type = first_engine[v->type];
244 }
245 break;
246
247 default:
248 break;
249 }
250 }
251}
252
253extern uint8_t _age_cargo_skip_counter; // From misc_sl.cpp
254
256 void AfterLoadVehiclesPhase1(bool part_of_load)
257{
258 for (Vehicle *v : Vehicle::Iterate()) {
259 /* Reinstate the previous pointer */
260 if (v->Next() != nullptr) v->Next()->previous = v;
261 if (v->NextShared() != nullptr) v->NextShared()->previous_shared = v;
262
263 if (part_of_load) v->fill_percent_te_id = INVALID_TE_ID;
264 v->first = nullptr;
265 if (v->IsGroundVehicle()) v->GetGroundVehicleCache()->first_engine = EngineID::Invalid();
266 }
267
268 /* AfterLoadVehicles may also be called in case of NewGRF reload, in this
269 * case we may not convert orders again. */
270 if (part_of_load) {
271 /* Create shared vehicle chain for very old games (pre 5,2) and create
272 * OrderList from shared vehicle chains. For this to work correctly, the
273 * following conditions must be fulfilled:
274 * a) both next_shared and previous_shared are not set for pre 5,2 games
275 * b) both next_shared and previous_shared are set for later games
276 */
277 std::map<uint32_t, OrderList *> mapping;
278
279 for (Vehicle *v : Vehicle::Iterate()) {
280 if (v->orders != nullptr) {
281 if (IsSavegameVersionBefore(SLV_105)) { // Pre-105 didn't save an OrderList
282 if (mapping[v->old_orders] == nullptr) {
283 /* This adds the whole shared vehicle chain for case b */
284
285 /* Creating an OrderList here is safe because the number of vehicles
286 * allowed in these savegames matches the number of OrderLists. As
287 * such each vehicle can get an OrderList and it will (still) fit. */
288 assert(OrderList::CanAllocateItem());
289 std::vector<Order> orders;
290 for (const OldOrderSaveLoadItem *old_order = GetOldOrder(v->old_orders); old_order != nullptr; old_order = GetOldOrder(old_order->next)) {
291 orders.push_back(std::move(old_order->order));
292 }
293 v->orders = mapping[v->old_orders] = new OrderList(std::move(orders), v);
294 } else {
295 v->orders = mapping[v->old_orders];
296 /* For old games (case a) we must create the shared vehicle chain */
297 if (IsSavegameVersionBefore(SLV_5, 2)) {
298 v->AddToShared(v->orders->GetFirstSharedVehicle());
299 }
300 }
301 } else { // OrderList was saved as such, only recalculate not saved values
302 if (v->PreviousShared() == nullptr) {
303 v->orders->Initialize(v);
304 }
305 }
306 }
307 }
308 }
309
310 for (Vehicle *v : Vehicle::Iterate()) {
311 /* Fill the first pointers */
312 if (v->Previous() == nullptr) {
313 for (Vehicle *u = v; u != nullptr; u = u->Next()) {
314 u->first = v;
315 }
316 }
317 }
318
319 if (part_of_load) {
320 if (IsSavegameVersionBefore(SLV_105)) {
321 /* Before 105 there was no order for shared orders, thus it messed up horribly */
322 for (Vehicle *v : Vehicle::Iterate()) {
323 if (v->First() != v || v->orders != nullptr || v->previous_shared != nullptr || v->next_shared == nullptr) continue;
324
325 /* As above, allocating OrderList here is safe. */
326 assert(OrderList::CanAllocateItem());
327 v->orders = new OrderList();
328 v->orders->first_shared = v;
329 for (Vehicle *u = v; u != nullptr; u = u->next_shared) {
330 u->orders = v->orders;
331 }
332 }
333 }
334
335 if (IsSavegameVersionBefore(SLV_157)) {
336 /* The road vehicle subtype was converted to a flag. */
337 for (RoadVehicle *rv : RoadVehicle::Iterate()) {
338 if (rv->subtype == 0) {
339 /* The road vehicle is at the front. */
340 rv->SetFrontEngine();
341 } else if (rv->subtype == 1) {
342 /* The road vehicle is an articulated part. */
343 rv->subtype = 0;
344 rv->SetArticulatedPart();
345 } else {
346 SlErrorCorrupt("Invalid road vehicle subtype");
347 }
348 }
349 }
350
351 if (IsSavegameVersionBefore(SLV_160)) {
352 /* In some old savegames there might be some "crap" stored. */
353 for (Vehicle *v : Vehicle::Iterate()) {
354 if (!v->IsPrimaryVehicle() && v->type != VEH_DISASTER) {
355 v->current_order.Free();
356 v->unitnumber = 0;
357 }
358 }
359 }
360
361 if (IsSavegameVersionBefore(SLV_162)) {
362 /* Set the vehicle-local cargo age counter from the old global counter. */
363 for (Vehicle *v : Vehicle::Iterate()) {
364 v->cargo_age_counter = _age_cargo_skip_counter;
365 }
366 }
367
368 if (IsSavegameVersionBefore(SLV_180)) {
369 /* Set service interval flags */
370 for (Vehicle *v : Vehicle::Iterate()) {
371 if (!v->IsPrimaryVehicle()) continue;
372
373 const Company *c = Company::Get(v->owner);
374 int interval = CompanyServiceInterval(c, v->type);
375
376 v->SetServiceIntervalIsCustom(v->GetServiceInterval() != interval);
377 v->SetServiceIntervalIsPercent(c->settings.vehicle.servint_ispercent);
378 }
379 }
380
381 if (IsSavegameVersionBefore(SLV_SHIP_ROTATION)) {
382 /* Ship rotation added */
383 for (Ship *s : Ship::Iterate()) {
384 s->rotation = s->direction;
385 }
386 } else {
387 for (Ship *s : Ship::Iterate()) {
388 if (s->rotation == s->direction) continue;
389 /* In case we are rotating on gameload, set the rotation position to
390 * the current position, otherwise the applied workaround offset would
391 * be with respect to 0,0.
392 */
393 s->rotation_x_pos = s->x_pos;
394 s->rotation_y_pos = s->y_pos;
395 }
396 }
397
398 if (IsSavegameVersionBefore(SLV_TIMETABLE_START_TICKS)) {
399 /* Convert timetable start from a date to an absolute tick in TimerGameTick::counter. */
400 for (Vehicle *v : Vehicle::Iterate()) {
401 /* If the start date is 0, the vehicle is not waiting to start and can be ignored. */
402 if (v->timetable_start == 0) continue;
403
404 v->timetable_start = GetStartTickFromDate(TimerGameEconomy::Date(v->timetable_start));
405 }
406 }
407
408 if (IsSavegameVersionBefore(SLV_VEHICLE_ECONOMY_AGE)) {
409 /* Set vehicle economy age based on calendar age. */
410 for (Vehicle *v : Vehicle::Iterate()) {
411 v->economy_age = TimerGameEconomy::Date{v->age.base()};
412 }
413 }
414 }
415
416 CheckValidVehicles();
417}
418
420 void AfterLoadVehiclesPhase2(bool part_of_load)
421{
422 for (Vehicle *v : Vehicle::Iterate()) {
423 assert(v->First() != nullptr);
424
425 v->trip_occupancy = CalcPercentVehicleFilled(v, nullptr);
426
427 switch (v->type) {
428 case VEH_TRAIN: {
429 Train *t = Train::From(v);
430 if (t->IsFrontEngine() || t->IsFreeWagon()) {
431 t->gcache.last_speed = t->cur_speed; // update displayed train speed
432 t->ConsistChanged(CCF_SAVELOAD);
433 }
434 break;
435 }
436
437 case VEH_ROAD: {
438 RoadVehicle *rv = RoadVehicle::From(v);
439 if (rv->IsFrontEngine()) {
440 rv->gcache.last_speed = rv->cur_speed; // update displayed road vehicle speed
441
442 rv->roadtype = Engine::Get(rv->engine_type)->VehInfo<RoadVehicleInfo>().roadtype;
443 rv->compatible_roadtypes = GetRoadTypeInfo(rv->roadtype)->powered_roadtypes;
444 RoadTramType rtt = GetRoadTramType(rv->roadtype);
445 for (RoadVehicle *u = rv; u != nullptr; u = u->Next()) {
446 u->roadtype = rv->roadtype;
447 u->compatible_roadtypes = rv->compatible_roadtypes;
448 if (GetRoadType(u->tile, rtt) == INVALID_ROADTYPE) SlErrorCorrupt("Road vehicle on invalid road type");
449 }
450
451 RoadVehUpdateCache(rv);
452 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) {
453 rv->CargoChanged();
454 }
455 }
456 break;
457 }
458
459 case VEH_SHIP:
460 Ship::From(v)->UpdateCache();
461 break;
462
463 default: break;
464 }
465 }
466
467 /* Stop non-front engines */
468 if (part_of_load && IsSavegameVersionBefore(SLV_112)) {
469 for (Vehicle *v : Vehicle::Iterate()) {
470 if (v->type == VEH_TRAIN) {
471 Train *t = Train::From(v);
472 if (!t->IsFrontEngine()) {
473 if (t->IsEngine()) t->vehstatus.Set(VehState::Stopped);
474 /* cur_speed is now relevant for non-front parts - nonzero breaks
475 * moving-wagons-inside-depot- and autoreplace- code */
476 t->cur_speed = 0;
477 }
478 }
479 /* trains weren't stopping gradually in old OTTD versions (and TTO/TTD)
480 * other vehicle types didn't have zero speed while stopped (even in 'recent' OTTD versions) */
481 if (v->vehstatus.Test(VehState::Stopped) && (v->type != VEH_TRAIN || IsSavegameVersionBefore(SLV_2, 1))) {
482 v->cur_speed = 0;
483 }
484 }
485 }
486
487 for (Vehicle *v : Vehicle::Iterate()) {
488 switch (v->type) {
489 case VEH_ROAD:
490 case VEH_TRAIN:
491 case VEH_SHIP:
492 v->GetImage(v->direction, EIT_ON_MAP, &v->sprite_cache.sprite_seq);
493 break;
494
495 case VEH_AIRCRAFT:
496 if (Aircraft::From(v)->IsNormalAircraft()) {
497 v->GetImage(v->direction, EIT_ON_MAP, &v->sprite_cache.sprite_seq);
498
499 /* The aircraft's shadow will have the same image as the aircraft, but no colour */
500 Vehicle *shadow = v->Next();
501 if (shadow == nullptr) SlErrorCorrupt("Missing shadow for aircraft");
502
503 shadow->sprite_cache.sprite_seq.CopyWithoutPalette(v->sprite_cache.sprite_seq);
504
505 /* In the case of a helicopter we will update the rotor sprites */
506 if (v->subtype == AIR_HELICOPTER) {
507 Vehicle *rotor = shadow->Next();
508 if (rotor == nullptr) SlErrorCorrupt("Missing rotor for helicopter");
509
510 GetRotorImage(Aircraft::From(v), EIT_ON_MAP, &rotor->sprite_cache.sprite_seq);
511 }
512
513 UpdateAircraftCache(Aircraft::From(v), true);
514 }
515 break;
516
517 case VEH_DISASTER: {
518 auto *dv = DisasterVehicle::From(v);
519 if (dv->subtype == ST_SMALL_UFO && dv->state != 0) {
520 RoadVehicle *u = RoadVehicle::GetIfValid(v->dest_tile.base());
521 if (u != nullptr && u->IsFrontEngine()) {
522 /* Delete UFO targetting a vehicle which is already a target. */
523 if (u->disaster_vehicle != VehicleID::Invalid() && u->disaster_vehicle != dv->index) {
524 delete v;
525 continue;
526 } else {
527 u->disaster_vehicle = dv->index;
528 }
529 }
530 }
531 break;
532 }
533
534 default: break;
535 }
536
537 if (part_of_load && v->unitnumber != 0) {
538 Company::Get(v->owner)->freeunits[v->type].UseID(v->unitnumber);
539 }
540
541 v->UpdateDeltaXY();
542 v->coord.left = INVALID_COORD;
543 v->sprite_cache.old_coord.left = INVALID_COORD;
544 if (v->type != VEH_EFFECT) v->UpdatePosition();
545 v->UpdateViewport(false);
546 }
547}
548
549bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // From train_cmd.cpp
550void ReverseTrainDirection(Train *v);
551void ReverseTrainSwapVeh(Train *v, int l, int r);
552
554 void FixupTrainLengths()
555{
556 /* Vehicle center was moved from 4 units behind the front to half the length
557 * behind the front. Move vehicles so they end up on the same spot. */
558 for (Vehicle *v : Vehicle::Iterate()) {
559 if (v->type == VEH_TRAIN && v->IsPrimaryVehicle()) {
560 /* The vehicle center is now more to the front depending on vehicle length,
561 * so we need to move all vehicles forward to cover the difference to the
562 * old center, otherwise wagon spacing in trains would be broken upon load. */
563 for (Train *u = Train::From(v); u != nullptr; u = u->Next()) {
564 if (u->track == TRACK_BIT_DEPOT || u->vehstatus.Test(VehState::Crashed)) continue;
565
566 Train *next = u->Next();
567
568 /* Try to pull the vehicle half its length forward. */
569 int diff = (VEHICLE_LENGTH - u->gcache.cached_veh_length) / 2;
570 int done;
571 for (done = 0; done < diff; done++) {
572 if (!TrainController(u, next, false)) break;
573 }
574
575 if (next != nullptr && done < diff && u->IsFrontEngine()) {
576 /* Pulling the front vehicle forwards failed, we either encountered a dead-end
577 * or a red signal. To fix this, we try to move the whole train the required
578 * space backwards and re-do the fix up of the front vehicle. */
579
580 /* Ignore any signals when backtracking. */
581 TrainForceProceeding old_tfp = u->force_proceed;
582 u->force_proceed = TFP_SIGNAL;
583
584 /* Swap start<>end, start+1<>end-1, ... */
585 int r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
586 int l = 0;
587 do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
588
589 /* We moved the first vehicle which is now the last. Move it back to the
590 * original position as we will fix up the last vehicle later in the loop. */
591 for (int i = 0; i < done; i++) TrainController(u->Last(), nullptr);
592
593 /* Move the train backwards to get space for the first vehicle. As the stopping
594 * distance from a line end is rounded up, move the train one unit more to cater
595 * for front vehicles with odd lengths. */
596 int moved;
597 for (moved = 0; moved < diff + 1; moved++) {
598 if (!TrainController(u, nullptr, false)) break;
599 }
600
601 /* Swap start<>end, start+1<>end-1, ... again. */
602 r = CountVehiclesInChain(u) - 1; // number of vehicles - 1
603 l = 0;
604 do ReverseTrainSwapVeh(u, l++, r--); while (l <= r);
605
606 u->force_proceed = old_tfp;
607
608 /* Tracks are too short to fix the train length. The player has to fix the
609 * train in a depot. Bail out so we don't damage the vehicle chain any more. */
610 if (moved < diff + 1) break;
611
612 /* Re-do the correction for the first vehicle. */
613 for (done = 0; done < diff; done++) TrainController(u, next, false);
614
615 /* We moved one unit more backwards than needed for even-length front vehicles,
616 * try to move that unit forward again. We don't care if this step fails. */
617 TrainController(u, nullptr, false);
618 }
619
620 /* If the next wagon is still in a depot, check if it shouldn't be outside already. */
621 if (next != nullptr && next->track == TRACK_BIT_DEPOT) {
622 int d = TicksToLeaveDepot(u);
623 if (d <= 0) {
624 /* Next vehicle should have left the depot already, show it and pull forward. */
625 next->vehstatus.Reset(VehState::Hidden);
626 next->track = TrackToTrackBits(GetRailDepotTrack(next->tile));
627 for (int i = 0; i >= d; i--) TrainController(next, nullptr);
628 }
629 }
630 }
631
632 /* Update all cached properties after moving the vehicle chain around. */
633 Train::From(v)->ConsistChanged(CCF_TRACK);
634 }
635 }
636}
637
638static uint8_t _cargo_periods;
639static StationID _cargo_source;
640static TileIndex _cargo_source_xy;
641static uint16_t _cargo_count;
642static uint16_t _cargo_paid_for;
643static Money _cargo_feeder_share;
644
645 class SlVehicleCommon : public DefaultSaveLoadHandler<SlVehicleCommon, Vehicle> {
646public:
647 static inline const SaveLoad description[] = {
648 SLE_VAR(Vehicle, subtype, SLE_UINT8),
649
650 SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
651 SLE_CONDVAR(Vehicle, name, SLE_NAME, SL_MIN_VERSION, SLV_84),
652 SLE_CONDSSTR(Vehicle, name, SLE_STR | SLF_ALLOW_CONTROL, SLV_84, SL_MAX_VERSION),
653 SLE_CONDVAR(Vehicle, unitnumber, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_8),
654 SLE_CONDVAR(Vehicle, unitnumber, SLE_UINT16, SLV_8, SL_MAX_VERSION),
655 SLE_VAR(Vehicle, owner, SLE_UINT8),
656 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
657 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
658 SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
659 SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
660
661 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
662 SLE_CONDVAR(Vehicle, x_pos, SLE_UINT32, SLV_6, SL_MAX_VERSION),
663 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
664 SLE_CONDVAR(Vehicle, y_pos, SLE_UINT32, SLV_6, SL_MAX_VERSION),
665 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_164),
666 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, SLV_164, SL_MAX_VERSION),
667 SLE_VAR(Vehicle, direction, SLE_UINT8),
668
669 SLE_VAR(Vehicle, spritenum, SLE_UINT8),
670 SLE_VAR(Vehicle, engine_type, SLE_UINT16),
671 SLE_VAR(Vehicle, cur_speed, SLE_UINT16),
672 SLE_VAR(Vehicle, subspeed, SLE_UINT8),
673 SLE_VAR(Vehicle, acceleration, SLE_UINT8),
674 SLE_CONDVAR(Vehicle, motion_counter, SLE_UINT32, SLV_VEH_MOTION_COUNTER, SL_MAX_VERSION),
675 SLE_VAR(Vehicle, progress, SLE_UINT8),
676
677 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
678 SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
679 SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16, SLV_5, SL_MAX_VERSION),
680 SLE_CONDVAR(Vehicle, last_loading_station, SLE_UINT16, SLV_182, SL_MAX_VERSION),
681
682 SLE_VAR(Vehicle, cargo_type, SLE_UINT8),
683 SLE_CONDVAR(Vehicle, cargo_subtype, SLE_UINT8, SLV_35, SL_MAX_VERSION),
684 SLEG_CONDVAR("cargo_days", _cargo_periods, SLE_UINT8, SL_MIN_VERSION, SLV_68),
685 SLEG_CONDVAR("cargo_source", _cargo_source, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_7),
686 SLEG_CONDVAR("cargo_source", _cargo_source, SLE_UINT16, SLV_7, SLV_68),
687 SLEG_CONDVAR("cargo_source_xy", _cargo_source_xy, SLE_UINT32, SLV_44, SLV_68),
688 SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
689 SLE_CONDVAR(Vehicle, refit_cap, SLE_UINT16, SLV_182, SL_MAX_VERSION),
690 SLEG_CONDVAR("cargo_count", _cargo_count, SLE_UINT16, SL_MIN_VERSION, SLV_68),
691 SLE_CONDREFLIST(Vehicle, cargo.packets, REF_CARGO_PACKET, SLV_68, SL_MAX_VERSION),
692 SLE_CONDARR(Vehicle, cargo.action_counts, SLE_UINT, VehicleCargoList::NUM_MOVE_TO_ACTION, SLV_181, SL_MAX_VERSION),
693 SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, SLV_162, SL_MAX_VERSION),
694
695 SLE_VAR(Vehicle, day_counter, SLE_UINT8),
696 SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
697 SLE_CONDVAR(Vehicle, running_ticks, SLE_UINT8, SLV_88, SL_MAX_VERSION),
698
699 SLE_VAR(Vehicle, cur_implicit_order_index, SLE_UINT8),
700 SLE_CONDVAR(Vehicle, cur_real_order_index, SLE_UINT8, SLV_158, SL_MAX_VERSION),
701
702 /* This next line is for version 4 and prior compatibility.. it temporarily reads
703 type and flags (which were both 4 bits) into type. Later on this is
704 converted correctly */
705 SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, SL_MIN_VERSION, SLV_5),
706 SLE_CONDVAR(Vehicle, current_order.dest, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
707
708 /* Orders for version 5 and on */
709 SLE_CONDVAR(Vehicle, current_order.type, SLE_UINT8, SLV_5, SL_MAX_VERSION),
710 SLE_CONDVAR(Vehicle, current_order.flags, SLE_UINT8, SLV_5, SL_MAX_VERSION),
711 SLE_CONDVAR(Vehicle, current_order.dest, SLE_UINT16, SLV_5, SL_MAX_VERSION),
712
713 /* Refit in current order */
714 SLE_CONDVAR(Vehicle, current_order.refit_cargo, SLE_UINT8, SLV_36, SL_MAX_VERSION),
715
716 /* Timetable in current order */
717 SLE_CONDVAR(Vehicle, current_order.wait_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
718 SLE_CONDVAR(Vehicle, current_order.travel_time, SLE_UINT16, SLV_67, SL_MAX_VERSION),
719 SLE_CONDVAR(Vehicle, current_order.max_speed, SLE_UINT16, SLV_174, SL_MAX_VERSION),
720 SLE_CONDVAR(Vehicle, timetable_start, SLE_FILE_I32 | SLE_VAR_U64, SLV_129, SLV_TIMETABLE_START_TICKS),
721 SLE_CONDVAR(Vehicle, timetable_start, SLE_UINT64, SLV_TIMETABLE_START_TICKS, SL_MAX_VERSION),
722
723 SLE_CONDVARNAME(Vehicle, old_orders, "orders", SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_69),
724 SLE_CONDVARNAME(Vehicle, old_orders, "orders", SLE_UINT32, SLV_69, SLV_105),
725 SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, SLV_105, SL_MAX_VERSION),
726
727 SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
728 SLE_CONDVAR(Vehicle, age, SLE_INT32, SLV_31, SL_MAX_VERSION),
729 SLE_CONDVAR(Vehicle, economy_age, SLE_INT32, SLV_VEHICLE_ECONOMY_AGE, SL_MAX_VERSION),
730 SLE_CONDVAR(Vehicle, max_age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
731 SLE_CONDVAR(Vehicle, max_age, SLE_INT32, SLV_31, SL_MAX_VERSION),
732 SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
733 SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32, SLV_31, SL_MAX_VERSION),
734 SLE_CONDVAR(Vehicle, date_of_last_service_newgrf, SLE_INT32, SLV_NEWGRF_LAST_SERVICE, SL_MAX_VERSION),
735 SLE_CONDVAR(Vehicle, service_interval, SLE_UINT16, SL_MIN_VERSION, SLV_31),
736 SLE_CONDVAR(Vehicle, service_interval, SLE_FILE_U32 | SLE_VAR_U16, SLV_31, SLV_180),
737 SLE_CONDVAR(Vehicle, service_interval, SLE_UINT16, SLV_180, SL_MAX_VERSION),
738 SLE_VAR(Vehicle, reliability, SLE_UINT16),
739 SLE_VAR(Vehicle, reliability_spd_dec, SLE_UINT16),
740 SLE_VAR(Vehicle, breakdown_ctr, SLE_UINT8),
741 SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8),
742 SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
743 SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8),
744 SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
745 SLE_CONDVAR(Vehicle, build_year, SLE_INT32, SLV_31, SL_MAX_VERSION),
746
747 SLE_VAR(Vehicle, load_unload_ticks, SLE_UINT16),
748 SLEG_CONDVAR("cargo_paid_for", _cargo_paid_for, SLE_UINT16, SLV_45, SL_MAX_VERSION),
749 SLE_CONDVAR(Vehicle, vehicle_flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_40, SLV_180),
750 SLE_CONDVAR(Vehicle, vehicle_flags, SLE_UINT16, SLV_180, SL_MAX_VERSION),
751
752 SLE_CONDVAR(Vehicle, profit_this_year, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_65),
753 SLE_CONDVAR(Vehicle, profit_this_year, SLE_INT64, SLV_65, SL_MAX_VERSION),
754 SLE_CONDVAR(Vehicle, profit_last_year, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_65),
755 SLE_CONDVAR(Vehicle, profit_last_year, SLE_INT64, SLV_65, SL_MAX_VERSION),
756 SLEG_CONDVAR("cargo_feeder_share", _cargo_feeder_share, SLE_FILE_I32 | SLE_VAR_I64, SLV_51, SLV_65),
757 SLEG_CONDVAR("cargo_feeder_share", _cargo_feeder_share, SLE_INT64, SLV_65, SLV_68),
758 SLE_CONDVAR(Vehicle, value, SLE_FILE_I32 | SLE_VAR_I64, SL_MIN_VERSION, SLV_65),
759 SLE_CONDVAR(Vehicle, value, SLE_INT64, SLV_65, SL_MAX_VERSION),
760
761 SLE_CONDVAR(Vehicle, random_bits, SLE_FILE_U8 | SLE_VAR_U16, SLV_2, SLV_EXTEND_VEHICLE_RANDOM),
762 SLE_CONDVAR(Vehicle, random_bits, SLE_UINT16, SLV_EXTEND_VEHICLE_RANDOM, SL_MAX_VERSION),
763 SLE_CONDVARNAME(Vehicle, waiting_random_triggers, "waiting_triggers", SLE_UINT8, SLV_2, SL_MAX_VERSION),
764
765 SLE_CONDREF(Vehicle, next_shared, REF_VEHICLE, SLV_2, SL_MAX_VERSION),
766 SLE_CONDVAR(Vehicle, group_id, SLE_UINT16, SLV_60, SL_MAX_VERSION),
767
768 SLE_CONDVAR(Vehicle, current_order_time, SLE_FILE_U32 | SLE_VAR_I32, SLV_67, SLV_TIMETABLE_TICKS_TYPE),
769 SLE_CONDVAR(Vehicle, current_order_time, SLE_INT32, SLV_TIMETABLE_TICKS_TYPE, SL_MAX_VERSION),
770 SLE_CONDVAR(Vehicle, last_loading_tick, SLE_UINT64, SLV_LAST_LOADING_TICK, SL_MAX_VERSION),
771 SLE_CONDVAR(Vehicle, lateness_counter, SLE_INT32, SLV_67, SL_MAX_VERSION),
772
773 SLE_CONDVAR(Vehicle, depot_unbunching_last_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
774 SLE_CONDVAR(Vehicle, depot_unbunching_next_departure, SLE_UINT64, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
775 SLE_CONDVAR(Vehicle, round_trip_time, SLE_INT32, SLV_DEPOT_UNBUNCHING, SL_MAX_VERSION),
776 };
777
778 static inline const SaveLoadCompatTable compat_description = _vehicle_common_sl_compat;
779
780 void Save(Vehicle *v) const override
781 {
782 SlObject(v, this->GetDescription());
783 }
784
785 void Load(Vehicle *v) const override
786 {
787 SlObject(v, this->GetLoadDescription());
788 }
789
790 void FixPointers(Vehicle *v) const override
791 {
792 SlObject(v, this->GetDescription());
793 }
794};
795
796 class SlVehicleTrain : public DefaultSaveLoadHandler<SlVehicleTrain, Vehicle> {
797public:
798 static inline const SaveLoad description[] = {
799 SLEG_STRUCT("common", SlVehicleCommon),
800 SLE_VAR(Train, crash_anim_pos, SLE_UINT16),
801 SLE_VAR(Train, force_proceed, SLE_UINT8),
802 SLE_VAR(Train, track, SLE_UINT8),
803
804 SLE_CONDVAR(Train, flags, SLE_FILE_U8 | SLE_VAR_U16, SLV_2, SLV_100),
805 SLE_CONDVAR(Train, flags, SLE_UINT16, SLV_100, SL_MAX_VERSION),
806 SLE_CONDVAR(Train, wait_counter, SLE_UINT16, SLV_136, SL_MAX_VERSION),
807 SLE_CONDVAR(Train, gv_flags, SLE_UINT16, SLV_139, SL_MAX_VERSION),
808 };
809 static inline const SaveLoadCompatTable compat_description = _vehicle_train_sl_compat;
810
811 void Save(Vehicle *v) const override
812 {
813 if (v->type != VEH_TRAIN) return;
814 SlObject(v, this->GetDescription());
815 }
816
817 void Load(Vehicle *v) const override
818 {
819 if (v->type != VEH_TRAIN) return;
820 SlObject(v, this->GetLoadDescription());
821 }
822
823 void FixPointers(Vehicle *v) const override
824 {
825 if (v->type != VEH_TRAIN) return;
826 SlObject(v, this->GetDescription());
827 }
828};
829
830 class SlVehicleRoadVehPath : public VectorSaveLoadHandler<SlVehicleRoadVehPath, RoadVehicle, RoadVehPathElement> {
831public:
832 static inline const SaveLoad description[] = {
833 SLE_VAR(RoadVehPathElement, trackdir, SLE_UINT8),
834 SLE_VAR(RoadVehPathElement, tile, SLE_UINT32),
835 };
836 static inline const SaveLoadCompatTable compat_description = {};
837
838 std::vector<RoadVehPathElement> &GetVector(RoadVehicle *rv) const override { return rv->path; }
839};
840
841 class SlVehicleRoadVeh : public DefaultSaveLoadHandler<SlVehicleRoadVeh, Vehicle> {
842public:
843 /* RoadVehicle path is stored in std::pair which cannot be directly saved. */
844 static inline std::vector<Trackdir> rv_path_td;
845 static inline std::vector<TileIndex> rv_path_tile;
846
847 static inline const SaveLoad description[] = {
848 SLEG_STRUCT("common", SlVehicleCommon),
849 SLE_VAR(RoadVehicle, state, SLE_UINT8),
850 SLE_VAR(RoadVehicle, frame, SLE_UINT8),
851 SLE_VAR(RoadVehicle, blocked_ctr, SLE_UINT16),
852 SLE_VAR(RoadVehicle, overtaking, SLE_UINT8),
853 SLE_VAR(RoadVehicle, overtaking_ctr, SLE_UINT8),
854 SLE_VAR(RoadVehicle, crashed_ctr, SLE_UINT16),
855 SLE_VAR(RoadVehicle, reverse_ctr, SLE_UINT8),
856 SLEG_CONDVECTOR("path.td", rv_path_td, SLE_UINT8, SLV_ROADVEH_PATH_CACHE, SLV_PATH_CACHE_FORMAT),
857 SLEG_CONDVECTOR("path.tile", rv_path_tile, SLE_UINT32, SLV_ROADVEH_PATH_CACHE, SLV_PATH_CACHE_FORMAT),
858 SLEG_CONDSTRUCTLIST("path", SlVehicleRoadVehPath, SLV_PATH_CACHE_FORMAT, SL_MAX_VERSION),
859 SLE_CONDVAR(RoadVehicle, gv_flags, SLE_UINT16, SLV_139, SL_MAX_VERSION),
860 };
861 static inline const SaveLoadCompatTable compat_description = _vehicle_roadveh_sl_compat;
862
863 static void ConvertPathCache(RoadVehicle &rv)
864 {
865 /* The two vectors should be the same size, but if not we can just ignore the cache and not cause more issues. */
866 if (rv_path_td.size() != rv_path_tile.size()) {
867 Debug(sl, 1, "Found RoadVehicle {} with invalid path cache, ignoring.", rv.index);
868 return;
869 }
870 size_t n = std::min(rv_path_td.size(), rv_path_tile.size());
871 if (n == 0) return;
872
873 rv.path.reserve(n);
874 for (size_t c = 0; c < n; ++c) {
875 rv.path.emplace_back(rv_path_td[c], rv_path_tile[c]);
876 }
877
878 /* Path cache is now taken from back instead of front, so needs reversing. */
879 std::reverse(std::begin(rv.path), std::end(rv.path));
880 }
881
882 void Save(Vehicle *v) const override
883 {
884 if (v->type != VEH_ROAD) return;
885 SlObject(v, this->GetDescription());
886 }
887
888 void Load(Vehicle *v) const override
889 {
890 if (v->type != VEH_ROAD) return;
891 SlObject(v, this->GetLoadDescription());
892 if (!IsSavegameVersionBefore(SLV_ROADVEH_PATH_CACHE) && IsSavegameVersionBefore(SLV_PATH_CACHE_FORMAT)) {
893 ConvertPathCache(*static_cast<RoadVehicle *>(v));
894 }
895 }
896
897 void FixPointers(Vehicle *v) const override
898 {
899 if (v->type != VEH_ROAD) return;
900 SlObject(v, this->GetDescription());
901 }
902};
903
904 class SlVehicleShipPath : public VectorSaveLoadHandler<SlVehicleShipPath, Ship, ShipPathElement> {
905public:
906 static inline const SaveLoad description[] = {
907 SLE_VAR(ShipPathElement, trackdir, SLE_UINT8),
908 };
909 static inline const SaveLoadCompatTable compat_description = {};
910
911 std::vector<ShipPathElement> &GetVector(Ship *s) const override { return s->path; }
912};
913
914 class SlVehicleShip : public DefaultSaveLoadHandler<SlVehicleShip, Vehicle> {
915public:
916 static inline std::vector<Trackdir> ship_path_td;
917
918 static inline const SaveLoad description[] = {
919 SLEG_STRUCT("common", SlVehicleCommon),
920 SLE_VAR(Ship, state, SLE_UINT8),
921 SLEG_CONDVECTOR("path", ship_path_td, SLE_UINT8, SLV_SHIP_PATH_CACHE, SLV_PATH_CACHE_FORMAT),
922 SLEG_CONDSTRUCTLIST("path", SlVehicleShipPath, SLV_PATH_CACHE_FORMAT, SL_MAX_VERSION),
923 SLE_CONDVAR(Ship, rotation, SLE_UINT8, SLV_SHIP_ROTATION, SL_MAX_VERSION),
924 };
925 static inline const SaveLoadCompatTable compat_description = _vehicle_ship_sl_compat;
926
927 void Save(Vehicle *v) const override
928 {
929 if (v->type != VEH_SHIP) return;
930 SlObject(v, this->GetDescription());
931 }
932
933 void Load(Vehicle *v) const override
934 {
935 if (v->type != VEH_SHIP) return;
936 SlObject(v, this->GetLoadDescription());
937
938 if (IsSavegameVersionBefore(SLV_PATH_CACHE_FORMAT)) {
939 /* Path cache is now taken from back instead of front, so needs reversing. */
940 Ship *s = static_cast<Ship *>(v);
941 std::transform(std::rbegin(ship_path_td), std::rend(ship_path_td), std::back_inserter(s->path), [](Trackdir trackdir) { return trackdir; });
942 }
943 }
944
945 void FixPointers(Vehicle *v) const override
946 {
947 if (v->type != VEH_SHIP) return;
948 SlObject(v, this->GetDescription());
949 }
950};
951
952 class SlVehicleAircraft : public DefaultSaveLoadHandler<SlVehicleAircraft, Vehicle> {
953public:
954 static inline const SaveLoad description[] = {
955 SLEG_STRUCT("common", SlVehicleCommon),
956 SLE_VAR(Aircraft, crashed_counter, SLE_UINT16),
957 SLE_VAR(Aircraft, pos, SLE_UINT8),
958
959 SLE_CONDVAR(Aircraft, targetairport, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
960 SLE_CONDVAR(Aircraft, targetairport, SLE_UINT16, SLV_5, SL_MAX_VERSION),
961
962 SLE_VAR(Aircraft, state, SLE_UINT8),
963
964 SLE_CONDVAR(Aircraft, previous_pos, SLE_UINT8, SLV_2, SL_MAX_VERSION),
965 SLE_CONDVAR(Aircraft, last_direction, SLE_UINT8, SLV_2, SL_MAX_VERSION),
966 SLE_CONDVAR(Aircraft, number_consecutive_turns, SLE_UINT8, SLV_2, SL_MAX_VERSION),
967
968 SLE_CONDVAR(Aircraft, turn_counter, SLE_UINT8, SLV_136, SL_MAX_VERSION),
969 SLE_CONDVAR(Aircraft, flags, SLE_UINT8, SLV_167, SL_MAX_VERSION),
970 };
971 static inline const SaveLoadCompatTable compat_description = _vehicle_aircraft_sl_compat;
972
973 void Save(Vehicle *v) const override
974 {
975 if (v->type != VEH_AIRCRAFT) return;
976 SlObject(v, this->GetDescription());
977 }
978
979 void Load(Vehicle *v) const override
980 {
981 if (v->type != VEH_AIRCRAFT) return;
982 SlObject(v, this->GetLoadDescription());
983 }
984
985 void FixPointers(Vehicle *v) const override
986 {
987 if (v->type != VEH_AIRCRAFT) return;
988 SlObject(v, this->GetDescription());
989 }
990};
991
992 class SlVehicleEffect : public DefaultSaveLoadHandler<SlVehicleEffect, Vehicle> {
993public:
994 static inline const SaveLoad description[] = {
995 SLE_VAR(Vehicle, subtype, SLE_UINT8),
996
997 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
998 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
999
1000 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
1001 SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, SLV_6, SL_MAX_VERSION),
1002 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
1003 SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, SLV_6, SL_MAX_VERSION),
1004 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_164),
1005 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, SLV_164, SL_MAX_VERSION),
1006
1007 SLE_VAR(Vehicle, sprite_cache.sprite_seq.seq[0].sprite, SLE_FILE_U16 | SLE_VAR_U32),
1008 SLE_VAR(Vehicle, progress, SLE_UINT8),
1009 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
1010
1011 SLE_VAR(EffectVehicle, animation_state, SLE_UINT16),
1012 SLE_VAR(EffectVehicle, animation_substate, SLE_UINT8),
1013
1014 SLE_CONDVAR(Vehicle, spritenum, SLE_UINT8, SLV_2, SL_MAX_VERSION),
1015 };
1016 static inline const SaveLoadCompatTable compat_description = _vehicle_effect_sl_compat;
1017
1018 void Save(Vehicle *v) const override
1019 {
1020 if (v->type != VEH_EFFECT) return;
1021 SlObject(v, this->GetDescription());
1022 }
1023
1024 void Load(Vehicle *v) const override
1025 {
1026 if (v->type != VEH_EFFECT) return;
1027 SlObject(v, this->GetLoadDescription());
1028 }
1029
1030 void FixPointers(Vehicle *v) const override
1031 {
1032 if (v->type != VEH_EFFECT) return;
1033 SlObject(v, this->GetDescription());
1034 }
1035};
1036
1037 class SlVehicleDisaster : public DefaultSaveLoadHandler<SlVehicleDisaster, Vehicle> {
1038public:
1039 static inline const SaveLoad description[] = {
1040 SLE_REF(Vehicle, next, REF_VEHICLE_OLD),
1041
1042 SLE_VAR(Vehicle, subtype, SLE_UINT8),
1043 SLE_CONDVAR(Vehicle, tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
1044 SLE_CONDVAR(Vehicle, tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
1045 SLE_CONDVAR(Vehicle, dest_tile, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_6),
1046 SLE_CONDVAR(Vehicle, dest_tile, SLE_UINT32, SLV_6, SL_MAX_VERSION),
1047
1048 SLE_CONDVAR(Vehicle, x_pos, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
1049 SLE_CONDVAR(Vehicle, x_pos, SLE_INT32, SLV_6, SL_MAX_VERSION),
1050 SLE_CONDVAR(Vehicle, y_pos, SLE_FILE_I16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_6),
1051 SLE_CONDVAR(Vehicle, y_pos, SLE_INT32, SLV_6, SL_MAX_VERSION),
1052 SLE_CONDVAR(Vehicle, z_pos, SLE_FILE_U8 | SLE_VAR_I32, SL_MIN_VERSION, SLV_164),
1053 SLE_CONDVAR(Vehicle, z_pos, SLE_INT32, SLV_164, SL_MAX_VERSION),
1054 SLE_VAR(Vehicle, direction, SLE_UINT8),
1055
1056 SLE_VAR(Vehicle, owner, SLE_UINT8),
1057 SLE_VAR(Vehicle, vehstatus, SLE_UINT8),
1058 SLE_CONDVARNAME(DisasterVehicle, state, "current_order.dest", SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_5),
1059 SLE_CONDVARNAME(DisasterVehicle, state, "current_order.dest", SLE_UINT16, SLV_5, SLV_DISASTER_VEH_STATE),
1060 SLE_CONDVAR(DisasterVehicle, state, SLE_UINT16, SLV_DISASTER_VEH_STATE, SL_MAX_VERSION),
1061
1062 SLE_VAR(Vehicle, sprite_cache.sprite_seq.seq[0].sprite, SLE_FILE_U16 | SLE_VAR_U32),
1063 SLE_CONDVAR(Vehicle, age, SLE_FILE_U16 | SLE_VAR_I32, SL_MIN_VERSION, SLV_31),
1064 SLE_CONDVAR(Vehicle, age, SLE_INT32, SLV_31, SL_MAX_VERSION),
1065 SLE_VAR(Vehicle, tick_counter, SLE_UINT8),
1066
1067 SLE_CONDVAR(DisasterVehicle, image_override, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_191),
1068 SLE_CONDVAR(DisasterVehicle, image_override, SLE_UINT32, SLV_191, SL_MAX_VERSION),
1069 SLE_CONDVAR(DisasterVehicle, big_ufo_destroyer_target, SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_191),
1070 SLE_CONDVAR(DisasterVehicle, big_ufo_destroyer_target, SLE_UINT32, SLV_191, SL_MAX_VERSION),
1071 SLE_CONDVAR(DisasterVehicle, flags, SLE_UINT8, SLV_194, SL_MAX_VERSION),
1072 };
1073
1074 static inline const SaveLoadCompatTable compat_description = _vehicle_disaster_sl_compat;
1075
1076 void Save(Vehicle *v) const override
1077 {
1078 if (v->type != VEH_DISASTER) return;
1079 SlObject(v, this->GetDescription());
1080 }
1081
1082 void Load(Vehicle *v) const override
1083 {
1084 if (v->type != VEH_DISASTER) return;
1085 SlObject(v, this->GetLoadDescription());
1086 }
1087
1088 void FixPointers(Vehicle *v) const override
1089 {
1090 if (v->type != VEH_DISASTER) return;
1091 SlObject(v, this->GetDescription());
1092 }
1093};
1094
1095static const SaveLoad _vehicle_desc[] = {
1096 SLE_SAVEBYTE(Vehicle, type),
1097 SLEG_STRUCT("train", SlVehicleTrain),
1098 SLEG_STRUCT("roadveh", SlVehicleRoadVeh),
1099 SLEG_STRUCT("ship", SlVehicleShip),
1100 SLEG_STRUCT("aircraft", SlVehicleAircraft),
1101 SLEG_STRUCT("effect", SlVehicleEffect),
1102 SLEG_STRUCT("disaster", SlVehicleDisaster),
1103};
1104
1105 struct VEHSChunkHandler : ChunkHandler {
1106 VEHSChunkHandler() : ChunkHandler('VEHS', CH_SPARSE_TABLE) {}
1107
1108 void Save() const override
1109 {
1110 SlTableHeader(_vehicle_desc);
1111
1112 /* Write the vehicles */
1113 for (Vehicle *v : Vehicle::Iterate()) {
1114 SlSetArrayIndex(v->index);
1115 SlObject(v, _vehicle_desc);
1116 }
1117 }
1118
1119 void Load() const override
1120 {
1121 const std::vector<SaveLoad> slt = SlCompatTableHeader(_vehicle_desc, _vehicle_sl_compat);
1122
1123 int index;
1124
1125 _cargo_count = 0;
1126
1127 while ((index = SlIterateArray()) != -1) {
1128 Vehicle *v;
1129 VehicleType vtype = (VehicleType)SlReadByte();
1130
1131 switch (vtype) {
1132 case VEH_TRAIN: v = new (VehicleID(index)) Train(); break;
1133 case VEH_ROAD: v = new (VehicleID(index)) RoadVehicle(); break;
1134 case VEH_SHIP: v = new (VehicleID(index)) Ship(); break;
1135 case VEH_AIRCRAFT: v = new (VehicleID(index)) Aircraft(); break;
1136 case VEH_EFFECT: v = new (VehicleID(index)) EffectVehicle(); break;
1137 case VEH_DISASTER: v = new (VehicleID(index)) DisasterVehicle(); break;
1138 case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
1139 default: SlErrorCorrupt("Invalid vehicle type");
1140 }
1141
1142 SlObject(v, slt);
1143
1144 if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
1145 /* Don't construct the packet with station here, because that'll fail with old savegames */
1146 CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_periods, _cargo_source, _cargo_source_xy, _cargo_feeder_share);
1147 v->cargo.Append(cp);
1148 }
1149
1150 /* Old savegames used 'last_station_visited = 0xFF' */
1151 if (IsSavegameVersionBefore(SLV_5) && v->last_station_visited == 0xFF) {
1152 v->last_station_visited = StationID::Invalid();
1153 }
1154
1155 if (IsSavegameVersionBefore(SLV_182)) v->last_loading_station = StationID::Invalid();
1156
1157 if (IsSavegameVersionBefore(SLV_5)) {
1158 /* Convert the current_order.type (which is a mix of type and flags, because
1159 * in those versions, they both were 4 bits big) to type and flags */
1160 v->current_order.flags = GB(v->current_order.type, 4, 4);
1161 v->current_order.type &= 0x0F;
1162 }
1163
1164 /* Advanced vehicle lists got added */
1165 if (IsSavegameVersionBefore(SLV_60)) v->group_id = DEFAULT_GROUP;
1166 }
1167 }
1168
1169 void FixPointers() const override
1170 {
1171 for (Vehicle *v : Vehicle::Iterate()) {
1172 SlObject(v, _vehicle_desc);
1173 }
1174 }
1175};
1176
1177static const VEHSChunkHandler VEHS;
1178static const ChunkHandlerRef veh_chunk_handlers[] = {
1179 VEHS,
1180};
1181
1182extern const ChunkHandlerTable _veh_chunk_handlers(veh_chunk_handlers);
GetAircraftFlightLevelBounds
void GetAircraftFlightLevelBounds(const Vehicle *v, int *min, int *max)
Get the 'flight level' bounds, in pixels from 'z_pos' 0 for a particular vehicle for normal flight si...
Definition aircraft_cmd.cpp:726
AircraftNextAirportPos_and_Order
void AircraftNextAirportPos_and_Order(Aircraft *v)
set the right pos when heading to other airports after takeoff
Definition aircraft_cmd.cpp:1460
AircraftLeaveHangar
void AircraftLeaveHangar(Aircraft *v, Direction exit_dir)
Aircraft is about to leave the hangar.
Definition aircraft_cmd.cpp:1480
SetAircraftPosition
void SetAircraftPosition(Aircraft *v, int x, int y, int z)
Set aircraft position.
Definition aircraft_cmd.cpp:532
AIR_HELICOPTER
@ AIR_HELICOPTER
an helicopter
Definition aircraft.h:29
UpdateAircraftCache
void UpdateAircraftCache(Aircraft *v, bool update_range=false)
Update cached values of an aircraft.
Definition aircraft_cmd.cpp:601
HANGAR
@ HANGAR
Heading for hangar.
Definition airport.h:64
FLYING
@ FLYING
Vehicle is flying in the air.
Definition airport.h:77
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
ClrBit
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
Definition bitmath_func.hpp:139
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
DefaultSaveLoadHandler
Default handler for saving/loading an object to/from disk.
Definition saveload.h:596
DefaultSaveLoadHandler< SlVehicleCommon, Vehicle >::GetDescription
SaveLoadTable GetDescription() const override
Definition saveload.h:598
Engine
Definition engine_base.h:38
Engine::IterateType
static Pool::IterateWrapperFiltered< Engine, EngineTypeFilter > IterateType(VehicleType vt, size_t from=0)
Returns an iterable ensemble of all valid engines of the given type.
Definition engine_base.h:197
OverflowSafeInt< int64_t >
RoadTypeInfo::powered_roadtypes
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition road.h:96
SaveLoadHandler::GetLoadDescription
SaveLoadTable GetLoadDescription() const
Get the description for how to load the chunk.
Definition saveload.cpp:3444
SlVehicleAircraft
Definition vehicle_sl.cpp:952
SlVehicleCommon
Definition vehicle_sl.cpp:645
SlVehicleDisaster
Definition vehicle_sl.cpp:1037
SlVehicleEffect
Definition vehicle_sl.cpp:992
SlVehicleRoadVehPath
Definition vehicle_sl.cpp:830
SlVehicleRoadVehPath::GetVector
std::vector< RoadVehPathElement > & GetVector(RoadVehicle *rv) const override
Get instance of vector to load/save.
Definition vehicle_sl.cpp:838
SlVehicleRoadVeh
Definition vehicle_sl.cpp:841
SlVehicleShipPath
Definition vehicle_sl.cpp:904
SlVehicleShipPath::GetVector
std::vector< ShipPathElement > & GetVector(Ship *s) const override
Get instance of vector to load/save.
Definition vehicle_sl.cpp:911
SlVehicleShip
Definition vehicle_sl.cpp:914
SlVehicleTrain
Definition vehicle_sl.cpp:796
VectorSaveLoadHandler
Default handler for saving/loading a vector to/from disk.
Definition saveload.h:1382
VehicleCargoList::Append
void Append(CargoPacket *cp, MoveToAction action=MTA_KEEP)
Appends the given cargo packet.
Definition cargopacket.cpp:255
CompanyServiceInterval
int CompanyServiceInterval(const Company *c, VehicleType type)
Get the service interval for the given company and vehicle type.
Definition company_cmd.cpp:1280
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
ST_SMALL_UFO
@ ST_SMALL_UFO
Small UFO, tries to find a road vehicle to destroy.
Definition disaster_vehicle.h:19
RAILVEH_WAGON
@ RAILVEH_WAGON
simple wagon, not motorized
Definition engine_type.h:34
RAILVEH_MULTIHEAD
@ RAILVEH_MULTIHEAD
indicates a combination of two locomotives
Definition engine_type.h:33
DEFAULT_GROUP
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
GetOldOrder
OldOrderSaveLoadItem * GetOldOrder(size_t pool_index)
Get a pointer to an old order with the given reference index.
Definition order_sl.cpp:125
TicksToLeaveDepot
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
Definition rail_cmd.cpp:2926
GetRailDepotTrack
Track GetRailDepotTrack(Tile t)
Returns the track of a depot, ignoring direction.
Definition rail_map.h:182
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:215
RoadTramType
RoadTramType
The different types of road type.
Definition road_type.h:37
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:28
RoadVehUpdateCache
void RoadVehUpdateCache(RoadVehicle *v, bool same_length=false)
Update the cache of a road vehicle.
Definition roadveh_cmd.cpp:219
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
SlReadByte
uint8_t SlReadByte()
Wrapper for reading a byte from the buffer.
Definition saveload.cpp:410
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
saveload.h
Functions/types related to saving and loading games.
SLEG_CONDVAR
#define SLEG_CONDVAR(name, variable, type, from, to)
Storage of a global variable in some savegame versions.
Definition saveload.h:1124
SLF_ALLOW_CONTROL
@ SLF_ALLOW_CONTROL
Allow control codes in the strings.
Definition saveload.h:702
SLEG_CONDVECTOR
#define SLEG_CONDVECTOR(name, variable, type, from, to)
Storage of a global vector of SL_VAR elements in some savegame versions.
Definition saveload.h:1184
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
REF_VEHICLE_OLD
@ REF_VEHICLE_OLD
Load/save an old-style reference to a vehicle (for pre-4.4 savegames).
Definition saveload.h:619
REF_CARGO_PACKET
@ REF_CARGO_PACKET
Load/save a reference to a cargo packet.
Definition saveload.h:622
REF_ORDERLIST
@ REF_ORDERLIST
Load/save a reference to an orderlist.
Definition saveload.h:623
REF_VEHICLE
@ REF_VEHICLE
Load/save a reference to a vehicle.
Definition saveload.h:616
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_SAVEBYTE
#define SLE_SAVEBYTE(base, variable)
Only write byte during saving; never read it during loading.
Definition saveload.h:1098
SLE_REF
#define SLE_REF(base, variable, type)
Storage of a reference in every version of a savegame.
Definition saveload.h:1034
SLE_CONDREFLIST
#define SLE_CONDREFLIST(base, variable, type, from, to)
Storage of a list of SL_REF elements in some savegame versions.
Definition saveload.h:969
SLE_CONDARR
#define SLE_CONDARR(base, variable, type, length, from, to)
Storage of a fixed-size array of SL_VAR elements in some savegame versions.
Definition saveload.h:915
SaveLoadCompatTable
std::span< const struct SaveLoadCompat > SaveLoadCompatTable
A table of SaveLoadCompat entries.
Definition saveload.h:533
SLEG_STRUCT
#define SLEG_STRUCT(name, handler)
Storage of a structs in every savegame version.
Definition saveload.h:1233
SLE_CONDSSTR
#define SLE_CONDSSTR(base, variable, type, from, to)
Storage of a std::string in some savegame versions.
Definition saveload.h:948
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLV_69
@ SLV_69
69 10319
Definition saveload.h:125
SLV_67
@ SLV_67
67 10236
Definition saveload.h:123
SLV_182
@ SLV_182
182 25115 FS#5492, r25259, r25296 Goal status
Definition saveload.h:261
SLV_40
@ SLV_40
40 7326
Definition saveload.h:91
SLV_129
@ SLV_129
129 18292
Definition saveload.h:197
SLV_84
@ SLV_84
84 11822
Definition saveload.h:143
SLV_36
@ SLV_36
36 6624
Definition saveload.h:86
SLV_180
@ SLV_180
180 24998 1.3.x
Definition saveload.h:259
SLV_174
@ SLV_174
174 23973 1.2.x
Definition saveload.h:251
SLV_100
@ SLV_100
100 13952
Definition saveload.h:163
SLV_44
@ SLV_44
44 8144
Definition saveload.h:95
SLV_VEHICLE_ECONOMY_AGE
@ SLV_VEHICLE_ECONOMY_AGE
334 PR#12141 v14.0 Add vehicle age in economy year, for profit stats minimum age
Definition saveload.h:380
SLV_ROADVEH_PATH_CACHE
@ SLV_ROADVEH_PATH_CACHE
211 PR#7261 Add path cache for road vehicles.
Definition saveload.h:297
SLV_6
@ SLV_6
6.0 1721 6.1 1768
Definition saveload.h:46
SLV_181
@ SLV_181
181 25012
Definition saveload.h:260
SLV_60
@ SLV_60
60 9874
Definition saveload.h:115
SLV_65
@ SLV_65
65 10210
Definition saveload.h:121
SLV_DISASTER_VEH_STATE
@ SLV_DISASTER_VEH_STATE
312 PR#10798 Explicit storage of disaster vehicle state.
Definition saveload.h:354
SLV_TIMETABLE_START_TICKS
@ SLV_TIMETABLE_START_TICKS
321 PR#11468 Convert timetable start from a date to ticks.
Definition saveload.h:365
SLV_SHIP_ROTATION
@ SLV_SHIP_ROTATION
204 PR#7065 Add extra rotation stages for ships.
Definition saveload.h:288
SLV_191
@ SLV_191
191 26636 FS#6026 Fix disaster vehicle storage (No bump) 191 26646 FS#6041 Linkgraph - store location...
Definition saveload.h:272
SLV_8
@ SLV_8
8.0 1786
Definition saveload.h:49
SLV_112
@ SLV_112
112 15290
Definition saveload.h:177
SLV_SHIP_PATH_CACHE
@ SLV_SHIP_PATH_CACHE
203 PR#7072 Add path cache for ships
Definition saveload.h:287
SLV_PATH_CACHE_FORMAT
@ SLV_PATH_CACHE_FORMAT
346 PR#12345 Vehicle path cache format changed.
Definition saveload.h:395
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SLV_164
@ SLV_164
164 23290
Definition saveload.h:239
SLV_160
@ SLV_160
160 21974 1.1.x
Definition saveload.h:235
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_VEH_MOTION_COUNTER
@ SLV_VEH_MOTION_COUNTER
288 PR#8591 Desync safe motion counter
Definition saveload.h:325
SLV_EXTEND_VEHICLE_RANDOM
@ SLV_EXTEND_VEHICLE_RANDOM
310 PR#10701 Extend vehicle random bits.
Definition saveload.h:352
SLV_51
@ SLV_51
51 8978
Definition saveload.h:104
SLV_35
@ SLV_35
35 6602
Definition saveload.h:85
SLV_68
@ SLV_68
68 10266
Definition saveload.h:124
SLV_2
@ SLV_2
2.0 0.3.0 2.1 0.3.1, 0.3.2
Definition saveload.h:34
SLV_157
@ SLV_157
157 21862
Definition saveload.h:231
SLV_LAST_LOADING_TICK
@ SLV_LAST_LOADING_TICK
301 PR#9693 Store tick of last loading for vehicles.
Definition saveload.h:341
SLV_105
@ SLV_105
105 14803
Definition saveload.h:169
SLV_DEPOT_UNBUNCHING
@ SLV_DEPOT_UNBUNCHING
331 PR#11945 Allow unbunching shared order vehicles at a depot.
Definition saveload.h:377
SLV_139
@ SLV_139
139 19346
Definition saveload.h:209
SLV_158
@ SLV_158
158 21933
Definition saveload.h:232
SLV_45
@ SLV_45
45 8501
Definition saveload.h:97
SLV_167
@ SLV_167
167 23504
Definition saveload.h:243
SLV_TIMETABLE_TICKS_TYPE
@ SLV_TIMETABLE_TICKS_TYPE
323 PR#11435 Convert timetable current order time to ticks.
Definition saveload.h:367
SLV_5
@ SLV_5
5.0 1429 5.1 1440 5.2 1525 0.3.6
Definition saveload.h:43
SLV_NEWGRF_LAST_SERVICE
@ SLV_NEWGRF_LAST_SERVICE
317 PR#11124 Added stable date_of_last_service to avoid NewGRF trouble.
Definition saveload.h:360
SLV_88
@ SLV_88
88 12134
Definition saveload.h:148
SLV_7
@ SLV_7
7.0 1770
Definition saveload.h:48
SLV_194
@ SLV_194
194 26881 v1.5
Definition saveload.h:276
SLV_162
@ SLV_162
162 22713
Definition saveload.h:237
SLV_31
@ SLV_31
31 5999
Definition saveload.h:80
SLV_136
@ SLV_136
136 18764
Definition saveload.h:206
SLE_CONDREF
#define SLE_CONDREF(base, variable, type, from, to)
Storage of a reference in some savegame versions.
Definition saveload.h:904
SLEG_CONDSTRUCTLIST
#define SLEG_CONDSTRUCTLIST(name, handler, from, to)
Storage of a list of structs in some savegame versions.
Definition saveload.h:1193
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
SLE_CONDVARNAME
#define SLE_CONDVARNAME(base, variable, name, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:894
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Aircraft
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:72
Aircraft::IsNormalAircraft
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow.
Definition aircraft.h:121
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition vehicle_type.h:51
CargoPacket
Container for cargo from the same location and time.
Definition cargopacket.h:41
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
CompanyProperties::settings
CompanySettings settings
settings specific for each company
Definition company_base.h:124
CompanySettings::vehicle
VehicleDefaultSettings vehicle
default settings for vehicles
Definition settings_type.h:615
Company
Definition company_base.h:127
DisasterVehicle
Disasters, like submarines, skyrangers and their shadows, belong to this class.
Definition disaster_vehicle.h:37
EffectVehicle
A special vehicle is one of the following:
Definition effectvehicle_base.h:24
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition settings_type.h:641
GetNewVehiclePosResult
Position information of a vehicle after it moved.
Definition vehicle_func.h:214
GetNewVehiclePosResult::y
int y
x and y position of the vehicle after moving
Definition vehicle_func.h:215
GroundVehicleCache::last_speed
uint16_t last_speed
The last speed we did display, so we only have to redraw when this changes.
Definition ground_vehicle.hpp:48
GroundVehicle::gcache
GroundVehicleCache gcache
Cache of often calculated values.
Definition ground_vehicle.hpp:83
GroundVehicle::CargoChanged
void CargoChanged()
Recalculates the cached weight of a vehicle and its parts.
Definition ground_vehicle.cpp:79
GroundVehicle::IsEngine
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
Definition ground_vehicle.hpp:318
GroundVehicle::IsMultiheaded
bool IsMultiheaded() const
Check if the vehicle is a multiheaded engine.
Definition ground_vehicle.hpp:330
GroundVehicle::IsFreeWagon
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Definition ground_vehicle.hpp:312
GroundVehicle::ClearEngine
void ClearEngine()
Clear engine status.
Definition ground_vehicle.hpp:286
GroundVehicle::ClearMultiheaded
void ClearMultiheaded()
Clear multiheaded engine property.
Definition ground_vehicle.hpp:306
MutableSpriteCache::sprite_seq
VehicleSpriteSeq sprite_seq
Vehicle appearance.
Definition vehicle_base.h:162
OldOrderSaveLoadItem
Compatibility struct to allow saveload of pool-based orders.
Definition order_base.h:258
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:274
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
Order::type
uint8_t type
The type of order + non-stop flags.
Definition order_base.h:47
Order::flags
uint8_t flags
Load/unload types, depot order/action types.
Definition order_base.h:48
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_vehicle_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition pool_type.hpp:436
Pool::PoolItem<&_engine_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition pool_type.hpp:408
Pool::PoolItem<&_company_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_orderlist_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition pool_type.hpp:357
RailVehicleInfo
Information about a rail vehicle.
Definition engine_type.h:74
RoadVehPathElement
Element of the RoadVehPathCache.
Definition roadveh.h:85
RoadVehicleInfo
Information about a road vehicle.
Definition engine_type.h:146
RoadVehicle
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
RoadVehicle::compatible_roadtypes
RoadTypes compatible_roadtypes
NOSAVE: Roadtypes this consist is powered on.
Definition roadveh.h:110
RoadVehicle::path
RoadVehPathCache path
Cached path.
Definition roadveh.h:99
RoadVehicle::roadtype
RoadType roadtype
NOSAVE: Roadtype of this vehicle.
Definition roadveh.h:108
RoadVehicle::disaster_vehicle
VehicleID disaster_vehicle
NOSAVE: Disaster vehicle targetting this vehicle.
Definition roadveh.h:109
SaveLoad
SaveLoad type struct.
Definition saveload.h:732
ShipPathElement
Element of the ShipPathCache.
Definition ship.h:20
Ship
All ships have this type.
Definition ship.h:32
Ship::path
ShipPathCache path
Cached path.
Definition ship.h:33
Ship::UpdateCache
void UpdateCache()
Update the caches of this ship.
Definition ship_cmd.cpp:231
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition base_station_base.h:299
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition vehicle_base.h:1049
SpecializedVehicle::Iterate
static Pool::IterateWrapper< T > Iterate(size_t from=0)
Returns an iterable ensemble of all valid vehicles of type T.
Definition vehicle_base.h:1203
SpecializedVehicle::From
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition vehicle_base.h:1134
SpecializedVehicle::GetIfValid
static T * GetIfValid(auto index)
Returns vehicle if the index is a valid index for this vehicle type.
Definition vehicle_base.h:1124
SpecializedVehicle::GetNextVehicle
T * GetNextVehicle() const
Get the next real (non-articulated part) vehicle in the consist.
Definition vehicle_base.h:1093
Station
Station data structure.
Definition station_base.h:500
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Train
'Train' is either a loco or a wagon.
Definition train.h:91
Train::ConsistChanged
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
Definition train_cmd.cpp:107
VEHSChunkHandler
Definition vehicle_sl.cpp:1105
VEHSChunkHandler::FixPointers
void FixPointers() const override
Fix the pointers.
Definition vehicle_sl.cpp:1169
VEHSChunkHandler::Load
void Load() const override
Load the chunk.
Definition vehicle_sl.cpp:1119
VEHSChunkHandler::Save
void Save() const override
Save the chunk.
Definition vehicle_sl.cpp:1108
VehicleDefaultSettings::servint_ispercent
bool servint_ispercent
service intervals are in percents
Definition settings_type.h:602
VehicleSettings::roadveh_acceleration_model
uint8_t roadveh_acceleration_model
realistic acceleration for road vehicles
Definition settings_type.h:521
VehicleSpriteSeq::CopyWithoutPalette
void CopyWithoutPalette(const VehicleSpriteSeq &src)
Copy data from another sprite sequence, while dropping all recolouring information.
Definition vehicle_base.h:140
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::cargo_payment
CargoPayment * cargo_payment
The cargo payment we're currently in.
Definition vehicle_base.h:238
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition vehicle_base.h:278
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition vehicle_base.h:296
Vehicle::last_loading_station
StationID last_loading_station
Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
Definition vehicle_base.h:293
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition vehicle_base.h:321
Vehicle::vehstatus
VehStates vehstatus
Status.
Definition vehicle_base.h:309
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition vehicle_base.h:311
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition vehicle_base.h:587
Vehicle::spritenum
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
Definition vehicle_base.h:276
Vehicle::cur_speed
uint16_t cur_speed
current speed
Definition vehicle_base.h:283
Vehicle::IsFrontEngine
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Definition vehicle_base.h:913
Vehicle::sprite_cache
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
Definition vehicle_base.h:323
Vehicle::tile
TileIndex tile
Current tile index.
Definition vehicle_base.h:225
Vehicle::last_station_visited
StationID last_station_visited
The last station we stopped at.
Definition vehicle_base.h:292
Vehicle::next_shared
Vehicle * next_shared
pointer to the next vehicle that shares the order
Definition vehicle_base.h:213
GetStartTickFromDate
TimerGameTick::TickCounter GetStartTickFromDate(TimerGameEconomy::Date start_date)
Get the TimerGameTick::TickCounter tick of a given date.
Definition timetable_cmd.cpp:29
TrackToTrackBits
TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition track_func.h:77
TRACK_BIT_DEPOT
@ TRACK_BIT_DEPOT
Bitflag for a depot.
Definition track_type.h:53
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
CCF_TRACK
static constexpr ConsistChangeFlags CCF_TRACK
Valid changes while vehicle is driving, and possibly changing tracks.
Definition train.h:52
CCF_SAVELOAD
static constexpr ConsistChangeFlags CCF_SAVELOAD
Valid changes when loading a savegame. (Everything that is not stored in the save....
Definition train.h:57
TrainForceProceeding
TrainForceProceeding
Modes for ignoring signals.
Definition train.h:38
TFP_SIGNAL
@ TFP_SIGNAL
Ignore next signal, after the signal ignore being stuck.
Definition train.h:41
GetNewVehiclePos
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1765
CalcPercentVehicleFilled
uint8_t CalcPercentVehicleFilled(const Vehicle *front, StringID *colour)
Calculates how full a vehicle is.
Definition vehicle.cpp:1461
VehState::Crashed
@ Crashed
Vehicle is crashed.
VehState::Hidden
@ Hidden
Vehicle is not visible.
VehState::Stopped
@ Stopped
Vehicle is stopped by the player.
INVALID_COORD
static const int32_t INVALID_COORD
Sentinel for an invalid coordinate.
Definition vehicle_base.h:1207
IsCompanyBuildableVehicleType
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition vehicle_func.h:228
AfterLoadVehiclesPhase1
void AfterLoadVehiclesPhase1(bool part_of_load)
Called after load for phase 1 of vehicle initialisation.
Definition vehicle_sl.cpp:256
FixupTrainLengths
void FixupTrainLengths()
Fixup old train spacing.
Definition vehicle_sl.cpp:554
UpdateOldAircraft
void UpdateOldAircraft()
need to be called to load aircraft from old version
Definition vehicle_sl.cpp:165
ReverseTrainDirection
void ReverseTrainDirection(Train *v)
Turn a train around.
Definition train_cmd.cpp:1982
ConvertOldMultiheadToNew
void ConvertOldMultiheadToNew()
Converts all trains to the new subtype format introduced in savegame 16.2 It also links multiheaded e...
Definition vehicle_sl.cpp:113
TrainController
bool TrainController(Train *v, Vehicle *nomove, bool reverse=true)
Move a vehicle chain one movement stop forwards.
Definition train_cmd.cpp:3284
ConnectMultiheadedTrains
void ConnectMultiheadedTrains()
Link front and rear multiheaded engines to each other This is done when loading a savegame.
Definition vehicle_sl.cpp:35
_age_cargo_skip_counter
uint8_t _age_cargo_skip_counter
Skip aging of cargo? Used before savegame version 162.
Definition misc_sl.cpp:79
AfterLoadVehiclesPhase2
void AfterLoadVehiclesPhase2(bool part_of_load)
Called after load for phase 2 of vehicle initialisation.
Definition vehicle_sl.cpp:420
ReverseTrainSwapVeh
void ReverseTrainSwapVeh(Train *v, int l, int r)
Swap vehicles l and r in consist v, and reverse their direction.
Definition train_cmd.cpp:1646
CheckValidVehicles
static void CheckValidVehicles()
Check all vehicles to ensure their engine type is valid for the currently loaded NewGRFs (that includ...
Definition vehicle_sl.cpp:225
vehicle_sl_compat.h
Loading of vehicle chunks before table headers were added.
_vehicle_train_sl_compat
const SaveLoadCompat _vehicle_train_sl_compat[]
Original field order for SlVehicleTrain.
Definition vehicle_sl_compat.h:99
_vehicle_disaster_sl_compat
const SaveLoadCompat _vehicle_disaster_sl_compat[]
Original field order for SlVehicleDisaster.
Definition vehicle_sl_compat.h:174
_vehicle_effect_sl_compat
const SaveLoadCompat _vehicle_effect_sl_compat[]
Original field order for SlVehicleEffect.
Definition vehicle_sl_compat.h:157
_vehicle_ship_sl_compat
const SaveLoadCompat _vehicle_ship_sl_compat[]
Original field order for SlVehicleShip.
Definition vehicle_sl_compat.h:133
_vehicle_sl_compat
const SaveLoadCompat _vehicle_sl_compat[]
Original field order for vehicle_desc.
Definition vehicle_sl_compat.h:197
_vehicle_roadveh_sl_compat
const SaveLoadCompat _vehicle_roadveh_sl_compat[]
Original field order for SlVehicleRoadVeh.
Definition vehicle_sl_compat.h:114
_vehicle_common_sl_compat
const SaveLoadCompat _vehicle_common_sl_compat[]
Original field order for SlVehicleCommon.
Definition vehicle_sl_compat.h:16
_vehicle_aircraft_sl_compat
const SaveLoadCompat _vehicle_aircraft_sl_compat[]
Original field order for SlVehicleAircraft.
Definition vehicle_sl_compat.h:142
EIT_ON_MAP
@ EIT_ON_MAP
Vehicle drawn in viewport.
Definition vehicle_type.h:75
VehicleID
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
Definition vehicle_type.h:17
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
VEH_INVALID
@ VEH_INVALID
Non-existing type of vehicle.
Definition vehicle_type.h:36
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:26
VEH_DISASTER
@ VEH_DISASTER
Disaster vehicle type.
Definition vehicle_type.h:33
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition vehicle_type.h:28
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition vehicle_type.h:27
VEH_EFFECT
@ VEH_EFFECT
Effect vehicle type (smoke, explosions, sparks, bubbles)
Definition vehicle_type.h:32
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:25
VEHICLE_LENGTH
static const uint VEHICLE_LENGTH
The length of a vehicle in tile units.
Definition vehicle_type.h:65