OpenTTD

cargopacket_sl.cpp
Go to the documentation of this file.
1 /* $Id: cargopacket_sl.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13 #include "../vehicle_base.h"
14 #include "../station_base.h"
15
16 #include "saveload.h"
17
18 #include "../safeguards.h"
19
23 /* static */ void CargoPacket::AfterLoad()
24 {
25 if (IsSavegameVersionBefore(44)) {
26 Vehicle *v;
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
32 */
33 FOR_ALL_VEHICLES(v) {
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;
39 }
40 }
41
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
46 * station */
47 Station *st;
48 FOR_ALL_STATIONS(st) {
49 for (CargoID c = 0; c < NUM_CARGO; c++) {
50 GoodsEntry *ge = &st->goods[c];
51
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;
57 }
58 }
59 }
60 }
61
62 if (IsSavegameVersionBefore(120)) {
63 /* CargoPacket's source should be either INVALID_STATION or a valid station */
64 CargoPacket *cp;
65 FOR_ALL_CARGOPACKETS(cp) {
66 if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
67 }
68 }
69
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. */
74 Vehicle *v;
75 FOR_ALL_VEHICLES(v) v->cargo.InvalidateCache();
76
77 Station *st;
78 FOR_ALL_STATIONS(st) {
79 for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
80 }
81 }
82
83 if (IsSavegameVersionBefore(181)) {
84 Vehicle *v;
85 FOR_ALL_VEHICLES(v) v->cargo.KeepAll();
86 }
87 }
88
94 const SaveLoad *GetCargoPacketDesc()
95 {
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),
105
106 /* Used to be paid_for, but that got changed. */
107 SLE_CONDNULL(1, 0, 120),
108
109 SLE_END()
110 };
111 return _cargopacket_desc;
112 }
113
117 static void Save_CAPA()
118 {
119 CargoPacket *cp;
120
121 FOR_ALL_CARGOPACKETS(cp) {
122 SlSetArrayIndex(cp->index);
123 SlObject(cp, GetCargoPacketDesc());
124 }
125 }
126
130 static void Load_CAPA()
131 {
132 int index;
133
134 while ((index = SlIterateArray()) != -1) {
135 CargoPacket *cp = new (index) CargoPacket();
136 SlObject(cp, GetCargoPacketDesc());
137 }
138 }
139
141 extern const ChunkHandler _cargopacket_chunk_handlers[] = {
142 { 'CAPA', Save_CAPA, Load_CAPA, NULL, NULL, CH_ARRAY | CH_LAST},
143 };
SLE_CONDNULL
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:344
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:309
CargoPacket::source_type
SourceTypeByte source_type
Type of source_id.
Definition: cargopacket.h:49
CargoPacket::CargoPacket
CargoPacket()
Create a new packet for savegame loading.
Definition: cargopacket.cpp:29
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:212
CargoPacket::AfterLoad
static void AfterLoad()
Savegame conversion for cargopackets.
Definition: cargopacket_sl.cpp:23
Save_CAPA
static void Save_CAPA()
Save the cargo packets.
Definition: cargopacket_sl.cpp:117
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:170
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
GoodsEntry::cargo
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Definition: station_base.h:255
Station::goods
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:472
CargoPacket::count
uint16 count
The amount of cargo in this packet.
Definition: cargopacket.h:47
MultiMap
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:19
saveload.h
Functions/types related to saving and loading games.
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:246
CargoPacket::source_id
SourceID source_id
Index of source, INVALID_SOURCE if unknown/invalid.
Definition: cargopacket.h:50
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:44
VehicleCargoList::InvalidateCache
void InvalidateCache()
Invalidates the cached data and rebuild it.
Definition: cargopacket.cpp:542
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
SL_MAX_VERSION
#define SL_MAX_VERSION
Highest possible savegame version.
Definition: saveload.h:96
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(uint16 major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:465
FOR_ALL_CARGOPACKETS
#define FOR_ALL_CARGOPACKETS(var)
Iterate over all valid cargo packets from the begin of the pool.
Definition: cargopacket.h:208
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:828
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:66
NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
SLE_END
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:353
_cargopacket_chunk_handlers
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
CargoList< VehicleCargoList, CargoPacketList >::ConstIterator
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:222
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:55
CargoPacket::source
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:51
SlObject
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1612
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:197
VehicleCargoList::KeepAll
void KeepAll()
Marks all cargo in the vehicle as to be kept.
Definition: cargopacket.h:422
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
Load_CAPA
static void Load_CAPA()
Load the cargo packets.
Definition: cargopacket_sl.cpp:130
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:296
CargoList::Packets
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:261
FOR_ALL_VEHICLES
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:986
CargoPacket::GetCargoPacketDesc
friend const struct SaveLoad * GetCargoPacketDesc()
We want this to be saved, right?
Definition: cargopacket_sl.cpp:94
CargoPacket::feeder_share
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
Definition: cargopacket.h:46
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
CargoList::InvalidateCache
void InvalidateCache()
Invalidates the cached data and rebuilds it.
Definition: cargopacket.cpp:204
CargoPacket::loaded_at_xy
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:54
CargoPacket::source_xy
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:52
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:206
CargoPacket::days_in_transit
byte days_in_transit
Amount of days this packet has been in transit.
Definition: cargopacket.h:48
Station
Station data structure.
Definition: station_base.h:446
CH_LAST
Last chunk in this array.
Definition: saveload.h:104