OpenTTD
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 {
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 
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 
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 
77  for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
78  }
79 }
80 
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),
97 
98  /* Used to be paid_for, but that got changed. */
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);
114  }
115 }
116 
120 static void Load_CAPA()
121 {
122  int index;
123 
124  while ((index = SlIterateArray()) != -1) {
125  CargoPacket *cp = new (index) CargoPacket();
127  }
128 }
129 
131 extern const ChunkHandler _cargopacket_chunk_handlers[] = {
132  { 'CAPA', Save_CAPA, Load_CAPA, nullptr, nullptr, CH_ARRAY | CH_LAST},
133 };
#define SLE_CONDNULL(length, from, to)
Empty space in some savegame versions.
Definition: saveload.h:642
44 8144
Definition: saveload.h:94
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:763
CargoPacket()
Create a new packet for savegame loading.
Definition: cargopacket.cpp:27
Maximal number of cargo types in a game.
Definition: cargo_type.h:64
120 16439
Definition: saveload.h:186
121 16694
Definition: saveload.h:187
125 17113
Definition: saveload.h:192
Vehicle data structure.
Definition: vehicle_base.h:210
static void AfterLoad()
Savegame conversion for cargopackets.
static void Save_CAPA()
Save the cargo packets.
Stores station stats for a single cargo.
Definition: station_base.h:170
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Definition: station_base.h:255
uint16 count
The amount of cargo in this packet.
Definition: cargopacket.h:45
SourceType source_type
Type of source_id.
Definition: cargopacket.h:47
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:17
Functions/types related to saving and loading games.
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:534
68 10266
Definition: saveload.h:123
SourceID source_id
Index of source, INVALID_SOURCE if unknown/invalid.
Definition: cargopacket.h:48
Highest possible saveload version.
Definition: saveload.h:305
First savegame version.
Definition: saveload.h:30
Container for cargo from the same location and time.
Definition: cargopacket.h:42
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:637
Handlers and description of chunk.
Definition: saveload.h:356
#define SLE_END()
End marker of a struct/class save or load.
Definition: saveload.h:651
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:340
const ChunkHandler _cargopacket_chunk_handlers[]
Chunk handlers related to cargo packets.
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:207
181 25012
Definition: saveload.h:259
TileIndex xy
Base tile of the station.
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:49
void SlObject(void *object, const SaveLoad *sld)
Main SaveLoad function.
Definition: saveload.cpp:1546
static bool IsValidID(size_t index)
Tests whether given index is a valid index for station of this type.
SaveLoad type struct.
Definition: saveload.h:496
static void Load_CAPA()
Load the cargo packets.
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:594
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:246
friend const struct SaveLoad * GetCargoPacketDesc()
We want this to be saved, right?
Money feeder_share
Value of feeder pickup to be paid for on delivery of cargo.
Definition: cargopacket.h:44
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:52
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:50
static Station * Get(size_t index)
Gets station with given index.
byte days_in_transit
Amount of days this packet has been in transit.
Definition: cargopacket.h:46
Station data structure.
Definition: station_base.h:450
Last chunk in this array.
Definition: saveload.h:391