OpenTTD: src/saveload/cargopacket_sl.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 };