1 /* $Id: cargopacket_sl.cpp 25361 2013-06-09 13:03:48Z fonsinchen $ */
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/>.
10 /** @file cargopacket_sl.cpp Code handling saving and loading of cargo packets */
12 #include "../stdafx.h"
13 #include "../vehicle_base.h"
14 #include "../station_base.h"
18 #include "../safeguards.h"
21 * Savegame conversion for cargopackets.
23 /* static */ void CargoPacket::AfterLoad()
25 if (IsSavegameVersionBefore(44)) {
27 /* If we remove a station while cargo from it is still en route, payment calculation will assume
28 * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
29 * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
30 * where this situation exists, the cargo-source information is lost. in this case, we set the source
31 * to the current tile of the vehicle to prevent excessive profits
34 const CargoPacketList
*packets
= v
->cargo
.Packets();
35 for (VehicleCargoList::ConstIterator
it(packets
->begin()); it
!= packets
->end(); it
++) {
36 CargoPacket
*cp
= *it
;
37 cp
->source_xy
= Station::IsValidID(cp
->source
) ? Station::Get(cp
->source
)->xy
: v
->tile
;
38 cp
->loaded_at_xy
= cp
->source_xy
;
42 /* Store position of the station where the goods come from, so there
43 * are no very high payments when stations get removed. However, if the
44 * station where the goods came from is already removed, the source
45 * information is lost. In that case we set it to the position of this
48 FOR_ALL_STATIONS(st
) {
49 for (CargoID c
= 0; c
< NUM_CARGO
; c
++) {
50 GoodsEntry
*ge
= &st
->goods
[c
];
52 const StationCargoPacketMap
*packets
= ge
->cargo
.Packets();
53 for (StationCargoList::ConstIterator
it(packets
->begin()); it
!= packets
->end(); it
++) {
54 CargoPacket
*cp
= *it
;
55 cp
->source_xy
= Station::IsValidID(cp
->source
) ? Station::Get(cp
->source
)->xy
: st
->xy
;
56 cp
->loaded_at_xy
= cp
->source_xy
;
62 if (IsSavegameVersionBefore(120)) {
63 /* CargoPacket's source should be either INVALID_STATION or a valid station */
65 FOR_ALL_CARGOPACKETS(cp
) {
66 if (!Station::IsValidID(cp
->source
)) cp
->source
= INVALID_STATION
;
70 if (!IsSavegameVersionBefore(68)) {
71 /* Only since version 68 we have cargo packets. Savegames from before used
72 * 'new CargoPacket' + cargolist.Append so their caches are already
73 * correct and do not need rebuilding. */
75 FOR_ALL_VEHICLES(v
) v
->cargo
.InvalidateCache();
78 FOR_ALL_STATIONS(st
) {
79 for (CargoID c
= 0; c
< NUM_CARGO
; c
++) st
->goods
[c
].cargo
.InvalidateCache();
83 if (IsSavegameVersionBefore(181)) {
85 FOR_ALL_VEHICLES(v
) v
->cargo
.KeepAll();
90 * Wrapper function to get the CargoPacket's internal structure while
91 * some of the variables itself are private.
92 * @return the saveload description for CargoPackets.
94 const SaveLoad
*GetCargoPacketDesc()
96 static const SaveLoad _cargopacket_desc
[] = {
97 SLE_VAR(CargoPacket
, source
, SLE_UINT16
),
98 SLE_VAR(CargoPacket
, source_xy
, SLE_UINT32
),
99 SLE_VAR(CargoPacket
, loaded_at_xy
, SLE_UINT32
),
100 SLE_VAR(CargoPacket
, count
, SLE_UINT16
),
101 SLE_VAR(CargoPacket
, days_in_transit
, SLE_UINT8
),
102 SLE_VAR(CargoPacket
, feeder_share
, SLE_INT64
),
103 SLE_CONDVAR(CargoPacket
, source_type
, SLE_UINT8
, 125, SL_MAX_VERSION
),
104 SLE_CONDVAR(CargoPacket
, source_id
, SLE_UINT16
, 125, SL_MAX_VERSION
),
106 /* Used to be paid_for, but that got changed. */
107 SLE_CONDNULL(1, 0, 120),
111 return _cargopacket_desc
;
115 * Save the cargo packets.
117 static void Save_CAPA()
121 FOR_ALL_CARGOPACKETS(cp
) {
122 SlSetArrayIndex(cp
->index
);
123 SlObject(cp
, GetCargoPacketDesc());
128 * Load the cargo packets.
130 static void Load_CAPA()
134 while ((index
= SlIterateArray()) != -1) {
135 CargoPacket
*cp
= new (index
) CargoPacket();
136 SlObject(cp
, GetCargoPacketDesc());
140 /** Chunk handlers related to cargo packets. */
141 extern const ChunkHandler _cargopacket_chunk_handlers
[] = {
142 { 'CAPA', Save_CAPA
, Load_CAPA
, NULL
, NULL
, CH_ARRAY
| CH_LAST
},