OpenTTD Source  12.0-beta2
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 
12 #include "saveload.h"
14 
15 #include "../vehicle_base.h"
16 #include "../station_base.h"
17 
18 #include "../safeguards.h"
19 
23 /* static */ void CargoPacket::AfterLoad()
24 {
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->source) ? Station::Get(cp->source)->xy : v->tile;
37  cp->loaded_at_xy = cp->source_xy;
38  }
39  }
40 
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];
49 
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;
55  }
56  }
57  }
58  }
59 
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;
64  }
65  }
66 
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();
72 
73  for (Station *st : Station::Iterate()) {
74  for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
75  }
76  }
77 
79  for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
80  }
81 }
82 
89 {
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),
99  };
100  return _cargopacket_desc;
101 }
102 
104  CAPAChunkHandler() : ChunkHandler('CAPA', CH_TABLE) {}
105 
106  void Save() const override
107  {
109 
110  for (CargoPacket *cp : CargoPacket::Iterate()) {
111  SlSetArrayIndex(cp->index);
113  }
114  }
115 
116  void Load() const override
117  {
118  const std::vector<SaveLoad> slt = SlCompatTableHeader(GetCargoPacketDesc(), _cargopacket_sl_compat);
119 
120  int index;
121 
122  while ((index = SlIterateArray()) != -1) {
123  CargoPacket *cp = new (index) CargoPacket();
124  SlObject(cp, slt);
125  }
126  }
127 };
128 
129 static const CAPAChunkHandler CAPA;
130 static const ChunkHandlerRef cargopacket_chunk_handlers[] = {
131  CAPA,
132 };
133 
134 extern const ChunkHandlerTable _cargopacket_chunk_handlers(cargopacket_chunk_handlers);
CargoList< VehicleCargoList, CargoPacketList >::ConstIterator
CargoPacketList ::const_iterator ConstIterator
The const iterator for our container.
Definition: cargopacket.h:208
CAPAChunkHandler
Definition: cargopacket_sl.cpp:103
CargoList::Packets
const Tcont * Packets() const
Returns a pointer to the cargo packet list (so you can iterate over it etc).
Definition: cargopacket.h:247
Station
Station data structure.
Definition: station_base.h:447
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
CargoPacket::source
StationID source
The station where the cargo came from first.
Definition: cargopacket.h:50
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:702
saveload.h
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:219
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:210
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
SLV_181
@ SLV_181
181 25012
Definition: saveload.h:264
CargoPacket::loaded_at_xy
TileOrStationID loaded_at_xy
Location where this cargo has been loaded into the vehicle.
Definition: cargopacket.h:53
SpecializedStation< Station, false >::Iterate
static Pool::IterateWrapper< Station > Iterate(size_t from=0)
Returns an iterable ensemble of all valid stations of type T.
Definition: base_station_base.h:270
GoodsEntry::cargo
StationCargoList cargo
The cargo packets of cargo waiting in this station.
Definition: station_base.h:252
cargopacket_sl_compat.h
CargoPacket::source_xy
TileIndex source_xy
The origin of the cargo (first station in feeder chain).
Definition: cargopacket.h:51
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
GetCargoPacketDesc
SaveLoadTable GetCargoPacketDesc()
Wrapper function to get the CargoPacket's internal structure while some of the variables itself are p...
Definition: cargopacket_sl.cpp:88
SLV_120
@ SLV_120
120 16439
Definition: saveload.h:191
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
SLV_125
@ SLV_125
125 17113
Definition: saveload.h:197
CAPAChunkHandler::Save
void Save() const override
Save the chunk.
Definition: cargopacket_sl.cpp:106
CargoPacket::AfterLoad
static void AfterLoad()
Savegame conversion for cargopackets.
Definition: cargopacket_sl.cpp:23
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:342
GoodsEntry
Stores station stats for a single cargo.
Definition: station_base.h:167
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:772
Pool::PoolItem<&_vehicle_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
CAPAChunkHandler::Load
void Load() const override
Load the chunk.
Definition: cargopacket_sl.cpp:116
SLV_44
@ SLV_44
44 8144
Definition: saveload.h:99
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
SLV_68
@ SLV_68
68 10266
Definition: saveload.h:128
CargoPacket
Container for cargo from the same location and time.
Definition: cargopacket.h:43
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
MultiMap
Hand-rolled multimap as map of lists.
Definition: multimap.hpp:17
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
_cargopacket_sl_compat
const SaveLoadCompat _cargopacket_sl_compat[]
Original field order for _cargopacket_desc.
Definition: cargopacket_sl_compat.h:16