Fix: Don't try to rename OWNER_DEITY signs in-game (#9716)
[openttd-github.git] / src / saveload / cargopacket_sl.cpp
blob95b9bec6bf18c6abcdf9a6b31e6c2294355b2b04
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 */
8 /** @file cargopacket_sl.cpp Code handling saving and loading of cargo packets */
10 #include "../stdafx.h"
12 #include "saveload.h"
13 #include "compat/cargopacket_sl_compat.h"
15 #include "../vehicle_base.h"
16 #include "../station_base.h"
18 #include "../safeguards.h"
20 /**
21 * Savegame conversion for cargopackets.
23 /* static */ void CargoPacket::AfterLoad()
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
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->source) ? Station::Get(cp->source)->xy : v->tile;
37 cp->loaded_at_xy = cp->source_xy;
41 /* Store position of the station where the goods come from, so there
42 * are no very high payments when stations get removed. However, if the
43 * station where the goods came from is already removed, the source
44 * information is lost. In that case we set it to the position of this
45 * station */
46 for (Station *st : Station::Iterate()) {
47 for (CargoID c = 0; c < NUM_CARGO; c++) {
48 GoodsEntry *ge = &st->goods[c];
50 const StationCargoPacketMap *packets = ge->cargo.Packets();
51 for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
52 CargoPacket *cp = *it;
53 cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
54 cp->loaded_at_xy = cp->source_xy;
60 if (IsSavegameVersionBefore(SLV_120)) {
61 /* CargoPacket's source should be either INVALID_STATION or a valid station */
62 for (CargoPacket *cp : CargoPacket::Iterate()) {
63 if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
67 if (!IsSavegameVersionBefore(SLV_68)) {
68 /* Only since version 68 we have cargo packets. Savegames from before used
69 * 'new CargoPacket' + cargolist.Append so their caches are already
70 * correct and do not need rebuilding. */
71 for (Vehicle *v : Vehicle::Iterate()) v->cargo.InvalidateCache();
73 for (Station *st : Station::Iterate()) {
74 for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
78 if (IsSavegameVersionBefore(SLV_181)) {
79 for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
83 /**
84 * Wrapper function to get the CargoPacket's internal structure while
85 * some of the variables itself are private.
86 * @return the saveload description for CargoPackets.
88 SaveLoadTable GetCargoPacketDesc()
90 static const SaveLoad _cargopacket_desc[] = {
91 SLE_VAR(CargoPacket, source, SLE_UINT16),
92 SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
93 SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
94 SLE_VAR(CargoPacket, count, SLE_UINT16),
95 SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
96 SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
97 SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
98 SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION),
100 return _cargopacket_desc;
103 struct CAPAChunkHandler : ChunkHandler {
104 CAPAChunkHandler() : ChunkHandler('CAPA', CH_TABLE) {}
106 void Save() const override
108 SlTableHeader(GetCargoPacketDesc());
110 for (CargoPacket *cp : CargoPacket::Iterate()) {
111 SlSetArrayIndex(cp->index);
112 SlObject(cp, GetCargoPacketDesc());
116 void Load() const override
118 const std::vector<SaveLoad> slt = SlCompatTableHeader(GetCargoPacketDesc(), _cargopacket_sl_compat);
120 int index;
122 while ((index = SlIterateArray()) != -1) {
123 CargoPacket *cp = new (index) CargoPacket();
124 SlObject(cp, slt);
129 static const CAPAChunkHandler CAPA;
130 static const ChunkHandlerRef cargopacket_chunk_handlers[] = {
131 CAPA,
134 extern const ChunkHandlerTable _cargopacket_chunk_handlers(cargopacket_chunk_handlers);