OpenTTD

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

- File List
- Globals

Functions | Variables
vehicle_sl.cpp File Reference

Code handling saving and loading of vehicles. More...

`#include "../stdafx.h"`
`#include "../vehicle_func.h"`
`#include "../train.h"`
`#include "../roadveh.h"`
`#include "../ship.h"`
`#include "../aircraft.h"`
`#include "../station_base.h"`
`#include "../effectvehicle_base.h"`
`#include "../company_base.h"`
`#include "../company_func.h"`
`#include "../disaster_vehicle.h"`
`#include "saveload.h"`
`#include <map>`
`#include "../safeguards.h"`

Go to the source code of this file.

## Functions
void | ConnectMultiheadedTrains ()
| Link front and rear multiheaded engines to each other This is done when loading a savegame.
void | ConvertOldMultiheadToNew ()
| Converts all trains to the new subtype format introduced in savegame 16.2 It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found.
void | UpdateOldAircraft ()
| need to be called to load aircraft from old version
static void | CheckValidVehicles ()
| Check all vehicles to ensure their engine type is valid for the currently loaded NewGRFs (that includes none...) This only makes a difference if NewGRFs are missing, otherwise all vehicles will be valid.
void | AfterLoadVehicles (bool part_of_load)
| Called after load to update coordinates.
bool | TrainController (Train *v, Vehicle *nomove, bool reverse=true)
| Move a vehicle chain one movement stop forwards.
void | ReverseTrainDirection (Train *v)
| Turn a train around.
void | ReverseTrainSwapVeh (Train *v, int l, int r)
| Swap vehicles l and r in consist v, and reverse their direction.
void | FixupTrainLengths ()
| Fixup old train spacing.
const SaveLoad * | GetVehicleDescription (VehicleType vt)
| Make it possible to make the saveload tables "friends" of other classes.
static void | Save_VEHS ()
| Will be called when the vehicles need to be saved.
void | Load_VEHS ()
| Will be called when vehicles need to be loaded.
static void | Ptrs_VEHS ()

## Variables
byte | _age_cargo_skip_counter
| Skip aging of cargo? Used before savegame version 162.
static uint8 | _cargo_days
static uint16 | _cargo_source
static uint32 | _cargo_source_xy
static uint16 | _cargo_count
static uint16 | _cargo_paid_for
static Money | _cargo_feeder_share
static uint32 | _cargo_loaded_at_xy
const ChunkHandler | _veh_chunk_handlers []

## Detailed Description

Code handling saving and loading of vehicles.

Definition in file vehicle_sl.cpp.

## Function Documentation

void AfterLoadVehicles | ( | bool | part_of_load | ) |

Called after load to update coordinates.

So we can set the #previous and #first pointers while loading.

For instantiating the shared vehicle chain.

Definition at line 249 of file vehicle_sl.cpp.

References _age_cargo_skip_counter, _settings_game, Vehicle::AddToShared(), AIR_HELICOPTER, CalcPercentVehicleFilled(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_orderlist_pool >::CanAllocateItem(), Vehicle::cargo_age_counter, GroundVehicle< T, Type >::CargoChanged(), CCF_SAVELOAD, CheckValidVehicles(), CompanyServiceInterval(), Train::ConsistChanged(), Vehicle::coord, VehicleSpriteSeq::CopyWithoutPalette(), Vehicle::cur_speed, Vehicle::current_order, Vehicle::direction, EF_ROAD_TRAM, EIT_ON_MAP, Vehicle::engine_type, Vehicle::fill_percent_te_id, Vehicle::first, OrderList::first, Vehicle::First(), GroundVehicleCache::first_engine, FOR_ALL_VEHICLES, Order::Free(), SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From(), SpecializedVehicle< Train, Type >::From(), SpecializedVehicle< RoadVehicle, Type >::From(), SpecializedVehicle< Ship, VEH_SHIP >::From(), GroundVehicle< T, Type >::gcache, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), OrderList::GetFirstSharedVehicle(), Vehicle::GetGroundVehicleCache(), Vehicle::GetImage(), HasBit(), OrderList::Initialize(), INVALID_COORD, INVALID_ENGINE, GroundVehicle< T, Type >::IsEngine(), GroundVehicle< T, Type >::IsFreeWagon(), Vehicle::IsFrontEngine(), Vehicle::IsGroundVehicle(), Vehicle::IsPrimaryVehicle(), IsSavegameVersionBefore(), GroundVehicleCache::last_speed, Vehicle::list, Vehicle::Next(), Vehicle::next_shared, Vehicle::NextShared(), Vehicle::old, Vehicle::orders, Vehicle::owner, Vehicle::previous, Vehicle::Previous(), Vehicle::previous_shared, Vehicle::PreviousShared(), ROADTYPE_ROAD, ROADTYPE_TRAM, RoadTypeToRoadTypes(), VehicleSettings::roadveh_acceleration_model, RoadVehUpdateCache(), VehicleDefaultSettings::servint_ispercent, GroundVehicle< T, Type >::SetArticulatedPart(), GroundVehicle< T, Type >::SetFrontEngine(), Company::settings, SlErrorCorrupt(), Vehicle::sprite_seq, Vehicle::subtype, Vehicle::trip_occupancy, BaseVehicle::type, Vehicle::unitnumber, UpdateAircraftCache(), Ship::UpdateCache(), Vehicle::UpdateDeltaXY(), Vehicle::UpdatePosition(), Vehicle::UpdateViewport(), VEH_AIRCRAFT, VEH_DISASTER, VEH_ROAD, VEH_SHIP, VEH_TRAIN, CompanySettings::vehicle, GameSettings::vehicle, Vehicle::vehstatus, and VS_STOPPED.

Referenced by AfterLoadGame(), and ReloadNewGRFData().

static void CheckValidVehicles | ( |  | ) |
| static

Check all vehicles to ensure their engine type is valid for the currently loaded NewGRFs (that includes none...) This only makes a difference if NewGRFs are missing, otherwise all vehicles will be valid.

This does not make such a game playable, it only prevents crash.

Definition at line 216 of file vehicle_sl.cpp.

References Vehicle::engine_type, FOR_ALL_VEHICLES, Vehicle::GetEngine(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::GetPoolSize(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_ENGINE, Engine::type, BaseVehicle::type, VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by AfterLoadVehicles().

void FixupTrainLengths | ( |  | ) |

Fixup old train spacing.

Definition at line 476 of file vehicle_sl.cpp.

References CCF_TRACK, Train::ConsistChanged(), FOR_ALL_VEHICLES, SpecializedVehicle< Train, Type >::From(), GetRailDepotTrack(), Vehicle::IsPrimaryVehicle(), SpecializedVehicle< T, Type >::Next(), ReverseTrainSwapVeh(), TFP_SIGNAL, TicksToLeaveDepot(), Vehicle::tile, TRACK_BIT_DEPOT, TrackToTrackBits(), TrainController(), BaseVehicle::type, VEH_TRAIN, VEHICLE_LENGTH, Vehicle::vehstatus, VS_CRASHED, and VS_HIDDEN.

Referenced by AfterLoadGame().

const SaveLoad* GetVehicleDescription | ( | VehicleType | vt | ) |

Make it possible to make the saveload tables "friends" of other classes.

So we can use private/protected variables in the saveload code.

Saving and loading the current order of vehicles.

The vehicles have a cargo list (and we want that saved).

Parameters

  vt | the vehicle type. Can be VEH_END for the common vehicle description data

Returns
  the saveload description

Save and load of vehicles

Definition at line 574 of file vehicle_sl.cpp.

References REF_CARGO_PACKET, REF_ORDER, REF_ORDERLIST, REF_VEHICLE, REF_VEHICLE_OLD, SL_MAX_VERSION, SLE_CONDARR, SLE_CONDLST, SLE_CONDNULL, SLE_CONDREF, SLE_CONDSTR, SLE_CONDVAR, SLE_END, SLE_REF, SLE_STR, SLE_VAR, SLE_WRITEBYTE, SLEG_CONDVAR, SLF_ALLOW_CONTROL, BaseVehicle::type, VEH_AIRCRAFT, VEH_DISASTER, VEH_EFFECT, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

Referenced by Load_VEHS(), and Save_VEHS().

void Load_VEHS | ( |  | ) |

Will be called when vehicles need to be loaded.

Loading of ancient vehicles.

Definition at line 883 of file vehicle_sl.cpp.

References VehicleCargoList::Append(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_cargopacket_pool >::CanAllocateItem(), Vehicle::cargo, Vehicle::current_order, DEFAULT_GROUP, Order::flags, GB(), GetVehicleDescription(), Vehicle::group_id, IsCompanyBuildableVehicleType(), IsSavegameVersionBefore(), Vehicle::last_loading_station, Vehicle::last_station_visited, SlErrorCorrupt(), SlIterateArray(), SlObject(), SlReadByte(), Order::type, VEH_AIRCRAFT, VEH_DISASTER, VEH_EFFECT, VEH_INVALID, VEH_ROAD, VEH_SHIP, and VEH_TRAIN.

void ReverseTrainDirection | ( | Train * | v | ) |

Turn a train around.

Parameters

  v | Train to turn around.

Definition at line 1821 of file train_cmd.cpp.

References _settings_game, AdvanceWagonsAfterSwap(), AdvanceWagonsBeforeSwap(), CCF_TRACK, CheckNextTrainTile(), ClrBit(), Train::ConsistChanged(), Vehicle::current_order, Vehicle::direction, FindFirstTrack(), FreeTrainTrackReservation(), GetRailDepotDirection(), Order::GetType(), Train::GetVehicleTrackdir(), HasBit(), HasSignalOnTrackdir(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_DIAGDIR, INVALID_TILE, InvalidateWindowData(), IsRailDepotTile(), IsRailStationTile(), IsTileType(), MarkTrainAsStuck(), MaybeBarCrossingWithSound(), MP_RAILWAY, MP_TUNNELBRIDGE, SpecializedVehicle< T, Type >::Next(), Vehicle::owner, GameSettings::pf, PathfinderSettings::reserve_paths, ReverseTrainSwapVeh(), SetRailStationPlatformReservation(), SetWindowWidgetDirty(), SIGSEG_PBS, Vehicle::tile, ToggleBit(), TRACK_BIT_DEPOT, TrackdirToExitdir(), TrainApproachingCrossingTile(), TrainExitDir(), TryPathReserve(), UpdateLevelCrossing(), UpdateSignalsOnSegment(), SpecializedVehicle< T, Type >::UpdateViewport(), VRF_TOGGLE_REVERSE, VRF_TRAIN_STUCK, Train::wait_counter, WC_VEHICLE_DEPOT, WC_VEHICLE_VIEW, and WID_VV_START_STOP.

Referenced by CmdReverseTrainDirection(), TrainApproachingLineEnd(), and TrainController().

void ReverseTrainSwapVeh | ( | Train * | v,
|  | int | l,
|  | int | r
| ) |  |

Swap vehicles l and r in consist v, and reverse their direction.

Parameters

  v | Consist to change.
  l | Vehicle index in the consist of the first vehicle.
  r | Vehicle index in the consist of the second vehicle.

Definition at line 1602 of file train_cmd.cpp.

References Vehicle::direction, GroundVehicle< T, Type >::gv_flags, SpecializedVehicle< T, Type >::Next(), Swap(), SwapTrainFlags(), Vehicle::tile, UpdateStatusAfterSwap(), Vehicle::vehstatus, VS_HIDDEN, Vehicle::x_pos, Vehicle::y_pos, and Vehicle::z_pos.

Referenced by FixupTrainLengths(), and ReverseTrainDirection().

static void Save_VEHS | ( |  | ) |
| static

Will be called when the vehicles need to be saved.

Definition at line 872 of file vehicle_sl.cpp.

References FOR_ALL_VEHICLES, GetVehicleDescription(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, SlObject(), and BaseVehicle::type.

bool TrainController | ( | Train * | v,
|  | Vehicle * | nomove,
|  | bool | reverse
| ) |  |

Move a vehicle chain one movement stop forwards.

Parameters

  v | First vehicle to move.
  nomove | Stop moving this and all following vehicles.
  reverse | Set to false to not execute the vehicle reversing. This does not change any other logic.

Returns
  True if the vehicle could be moved forward, false otherwise.

Definition at line 3113 of file train_cmd.cpp.

References _settings_client, _settings_game, RailtypeInfo::acceleration_type, AffectSpeedByZChange(), SoundSettings::ambient, TrainCache::cached_max_curve_speed, CCF_TRACK, CheckNextTrainTile(), ClearPathReservation(), DIAGDIR_BEGIN, DIAGDIR_END, DiagdirBetweenTiles(), DiagdirReachesTrackdirs(), DiagDirToDiagTrack(), DIRDIFF_45LEFT, DIRDIFF_45RIGHT, DirDifference(), error(), FIND_FIRST_BIT, FindFirstTrack(), FindFirstTrackdir(), SpecializedVehicle< T, Type >::First(), PathfinderSettings::forbid_90_deg, Train::GetCurveSpeedLimit(), GetNewVehiclePos(), GetOtherTunnelBridgeEnd(), GetRailTypeInfo(), GetReservedTrackbits(), GetTileRailType(), GetTileTrackStatus(), GetTunnelBridgeDirection(), HasBit(), HasCrossingReservation(), HasOnewaySignalBlockingTrackdir(), HasPbsSignalOnTrackdir(), HasReservedTracks(), HasSignalOnTrackdir(), HasSignals(), HasVehicleOnPos(), INVALID_TILE, IsLevelCrossingTile(), IsPlainRailTile(), IsTileType(), IsTunnel(), IsValidDiagDirection(), AccelerationSlowdownParams::large_turn, MarkTileDirtyByTile(), MarkTrainAsStuck(), MP_TUNNELBRIDGE, GetNewVehiclePosResult::new_tile, SpecializedVehicle< T, Type >::Next(), GetNewVehiclePosResult::old_tile, PathfinderSettings::path_backoff_interval, GameSettings::pf, SpecializedVehicle< T, Type >::Previous(), PathfinderSettings::reverse_at_signals, ReverseDiagDir(), ReverseTrackdir(), ReverseTrainDirection(), SetSignalStateByTrackdir(), SetWindowDirty(), SIGNAL_STATE_RED, SIGSEG_PBS, SIGTYPE_PBS, AccelerationSlowdownParams::small_turn, ClientSettings::sound, TFP_NONE, TFP_SIGNAL, TFP_STUCK, Vehicle::tile, TileAddByDiagDir(), TRACK_BIT_DEPOT, TRACK_BIT_LEFT, TRACK_BIT_LOWER, TRACK_BIT_NONE, TRACK_BIT_RIGHT, TRACK_BIT_UPPER, TRACK_BIT_WORMHOLE, TRACK_BIT_X, TRACK_BIT_Y, TrackBitsToTrack(), TrackCrossesTracks(), TrackdirBitsToTrackBits(), TrackDirectionToTrackdir(), TrackdirToExitdir(), TrackdirToTrack(), TrackStatusToRedSignals(), TrackStatusToTrackdirBits(), TrackToTrackBits(), VehicleSettings::train_acceleration_model, TrainApproachingCrossingTile(), TrainCheckIfLineEnds(), TrainEnterStation(), TRANSPORT_RAIL, TryPathReserve(), TryReserveRailTrack(), UpdateLevelCrossing(), UpdateSignalsOnSegment(), GameSettings::vehicle, VehicleEnterTile(), VETS_CANNOT_ENTER, VETS_ENTERED_STATION, VETS_ENTERED_WORMHOLE, VETS_STATION_ID_OFFSET, VRF_TRAIN_STUCK, VS_HIDDEN, PathfinderSettings::wait_oneway_signal, PathfinderSettings::wait_twoway_signal, WC_VEHICLE_VIEW, and GetNewVehiclePosResult::y.

Referenced by AdvanceWagonsAfterSwap(), AdvanceWagonsBeforeSwap(), and FixupTrainLengths().

## Variable Documentation

const ChunkHandler _veh_chunk_handlers[]

Initial value:
{
{ 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, NULL, CH_SPARSE_ARRAY | CH_LAST},
}