MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / AfterLoad

Method AfterLoad

src/saveload/cargopacket_sl.cpp:23–118  ·  view source on GitHub ↗

* Savegame conversion for cargopackets. */ static */

Source from the content-addressed store, hash-verified

21 * Savegame conversion for cargopackets.
22 */
23/* static */ void CargoPacket::AfterLoad()
24{
25 if (IsSavegameVersionBefore(SLV_44)) {
26 /* If we remove a station while cargo from it is still en route, payment calculation will assume
27 * 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
28 * stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
29 * where this situation exists, the cargo-source information is lost. in this case, we set the source
30 * to the current tile of the vehicle to prevent excessive profits
31 */
32 for (const Vehicle *v : Vehicle::Iterate()) {
33 const CargoPacketList *packets = v->cargo.Packets();
34 for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
35 CargoPacket *cp = *it;
36 cp->source_xy = Station::IsValidID(cp->first_station) ? Station::Get(cp->first_station)->xy : v->tile;
37 }
38 }
39
40 /* Store position of the station where the goods come from, so there
41 * are no very high payments when stations get removed. However, if the
42 * station where the goods came from is already removed, the source
43 * information is lost. In that case we set it to the position of this
44 * station */
45 for (Station *st : Station::Iterate()) {
46 for (GoodsEntry &ge : st->goods) {
47 if (!ge.HasData()) continue;
48 const StationCargoPacketMap *packets = ge.GetData().cargo.Packets();
49 for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
50 CargoPacket *cp = *it;
51 cp->source_xy = Station::IsValidID(cp->first_station) ? Station::Get(cp->first_station)->xy : st->xy;
52 }
53 }
54 }
55 }
56
57 if (IsSavegameVersionBefore(SLV_120)) {
58 /* CargoPacket's source should be either StationID::Invalid() or a valid station */
59 for (CargoPacket *cp : CargoPacket::Iterate()) {
60 if (!Station::IsValidID(cp->first_station)) cp->first_station = StationID::Invalid();
61 }
62 }
63
64 if (!IsSavegameVersionBefore(SLV_68)) {
65 /* Only since version 68 we have cargo packets. Savegames from before used
66 * 'new CargoPacket' + cargolist.Append so their caches are already
67 * correct and do not need rebuilding. */
68 for (Vehicle *v : Vehicle::Iterate()) v->cargo.InvalidateCache();
69
70 for (Station *st : Station::Iterate()) {
71 for (GoodsEntry &ge : st->goods) {
72 if (!ge.HasData()) continue;
73 ge.GetData().cargo.InvalidateCache();
74 }
75 }
76 }
77
78 if (IsSavegameVersionBefore(SLV_181)) {
79 for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
80 }

Callers

nothing calls this directly

Calls 11

IsSavegameVersionBeforeFunction · 0.85
InvalidFunction · 0.85
TileXFunction · 0.85
TileYFunction · 0.85
PacketsMethod · 0.80
InvalidateCacheMethod · 0.80
KeepAllMethod · 0.80
UpdateLoadingTileMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
HasDataMethod · 0.45

Tested by

no test coverage detected