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

OpenTTD Source 1.11.0-RC1

cargopacket_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 <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../vehicle_base.h"
12 #include "../station_base.h"
13
14 #include "saveload.h"
15
16 #include "../safeguards.h"
17
21 /* static */ void CargoPacket::AfterLoad()
22 {
23 if (IsSavegameVersionBefore(SLV_44)) {
24 /* If we remove a station while cargo from it is still en route, payment calculation will assume
25 * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
26 * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
27 * where this situation exists, the cargo-source information is lost. in this case, we set the source
28 * to the current tile of the vehicle to prevent excessive profits
29 */
30 for (const Vehicle *v : Vehicle::Iterate()) {
31 const CargoPacketList *packets = v->cargo.Packets();
32 for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
33 CargoPacket *cp = *it;
34 cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : v->tile;
35 cp->loaded_at_xy = cp->source_xy;
36 }
37 }
38
39 /* Store position of the station where the goods come from, so there
40 * are no very high payments when stations get removed. However, if the
41 * station where the goods came from is already removed, the source
42 * information is lost. In that case we set it to the position of this
43 * station */
44 for (Station *st : Station::Iterate()) {
45 for (CargoID c = 0; c < NUM_CARGO; c++) {
46 GoodsEntry *ge = &st->goods[c];
47
48 const StationCargoPacketMap *packets = ge->cargo.Packets();
49 for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
50 CargoPacket *cp = *it;
51 cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
52 cp->loaded_at_xy = cp->source_xy;
53 }
54 }
55 }
56 }
57
58 if (IsSavegameVersionBefore(SLV_120)) {
59 /* CargoPacket's source should be either INVALID_STATION or a valid station */
60 for (CargoPacket *cp : CargoPacket::Iterate()) {
61 if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
62 }
63 }
64
65 if (!IsSavegameVersionBefore(SLV_68)) {
66 /* Only since version 68 we have cargo packets. Savegames from before used
67 * 'new CargoPacket' + cargolist.Append so their caches are already
68 * correct and do not need rebuilding. */
69 for (Vehicle *v : Vehicle::Iterate()) v->cargo.InvalidateCache();
70
71 for (Station *st : Station::Iterate()) {
72 for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
73 }
74 }
75
76 if (IsSavegameVersionBefore(SLV_181)) {
77 for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
78 }
79 }
80
86 const SaveLoad *GetCargoPacketDesc()
87 {
88 static const SaveLoad _cargopacket_desc[] = {
89 SLE_VAR(CargoPacket, source, SLE_UINT16),
90 SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
91 SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
92 SLE_VAR(CargoPacket, count, SLE_UINT16),
93 SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
94 SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
95 SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
96 SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION),
97
98 /* Used to be paid_for, but that got changed. */
99 SLE_CONDNULL(1, SL_MIN_VERSION, SLV_121),
100
101 SLE_END()
102 };
103 return _cargopacket_desc;
104 }
105
109 static void Save_CAPA()
110 {
111 for (CargoPacket *cp : CargoPacket::Iterate()) {
112 SlSetArrayIndex(cp->index);
113 SlObject(cp, GetCargoPacketDesc());
114 }
115 }
116
120 static void Load_CAPA()
121 {
122 int index;
123
124 while ((index = SlIterateArray()) != -1) {
125 CargoPacket *cp = new (index) CargoPacket();
126 SlObject(cp, GetCargoPacketDesc());
127 }
128 }
129
131 extern const ChunkHandler _cargopacket_chunk_handlers[] = {
132 { 'CAPA', Save_CAPA, Load_CAPA, nullptr, nullptr, CH_ARRAY | CH_LAST},
133 };
CargoList< VehicleCargoList, CargoPacketList >::ConstIterator
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:207
GetCargoPacketDesc
const SaveLoad * GetCargoPacketDesc()
Wrapper function to get the CargoPacket's internal structure while some of the variables itself are p...
Definition: cargopacket_sl.cpp:86
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
CargoList::Packets
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:246
Station
Station data structure.
Definition: station_base.h:450
CargoPacket::source
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:49
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:551
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
SpecializedStation< Station, false >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
Definition: base_station_base.h:210
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
SLV_181
@ SLV_181
181 25012
Definition: saveload.h:260
CargoPacket::loaded_at_xy
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:52
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:677
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:270
GoodsEntry::cargo
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Definition: station_base.h:255
CargoPacket::source_xy
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:50
Save_CAPA
static void Save_CAPA()
Save the cargo packets.
Definition: cargopacket_sl.cpp:109
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:686
SLV_120
@ SLV_120
120 16439
Definition: saveload.h:187
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:815
SLV_125
@ SLV_125
125 17113
Definition: saveload.h:193
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
CargoPacket::AfterLoad
static void AfterLoad()
Savegame conversion for cargopackets.
Definition: cargopacket_sl.cpp:21
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:328
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:170
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:621
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:378
SLV_44
@ SLV_44
44 8144
Definition: saveload.h:95
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
SLV_68
@ SLV_68
68 10266
Definition: saveload.h:124
_cargopacket_chunk_handlers
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:42
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
SLV_121
@ SLV_121
121 16694
Definition: saveload.h:188
SaveLoad
SaveLoad type struct.
Definition: saveload.h:516
MultiMap
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:17
Load_CAPA
static void Load_CAPA()
Load the cargo packets.
Definition: cargopacket_sl.cpp:120
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:631