2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
/** @file cargopacket_sl.cpp Code handling saving and loading of cargo packets */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
2009-10-18 17:47:38 +00:00
|
|
|
#include "../vehicle_base.h"
|
|
|
|
#include "../station_base.h"
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
#include "saveload.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Savegame conversion for cargopackets.
|
|
|
|
*/
|
2009-10-18 17:47:38 +00:00
|
|
|
/* static */ void CargoPacket::AfterLoad()
|
|
|
|
{
|
2019-01-26 01:48:40 +00:00
|
|
|
if (IsSavegameVersionBefore(SLV_44)) {
|
2009-10-18 17:47:38 +00:00
|
|
|
Vehicle *v;
|
2013-01-08 22:46:42 +00:00
|
|
|
/* If we remove a station while cargo from it is still en route, payment calculation will assume
|
2009-11-03 17:30:08 +00:00
|
|
|
* 0, 0 to be the source of the cargo, resulting in very high payments usually. v->source_xy
|
|
|
|
* stores the coordinates, preserving them even if the station is removed. However, if a game is loaded
|
|
|
|
* where this situation exists, the cargo-source information is lost. in this case, we set the source
|
|
|
|
* to the current tile of the vehicle to prevent excessive profits
|
|
|
|
*/
|
2009-10-18 17:47:38 +00:00
|
|
|
FOR_ALL_VEHICLES(v) {
|
2013-06-09 13:03:48 +00:00
|
|
|
const CargoPacketList *packets = v->cargo.Packets();
|
2009-10-19 09:15:47 +00:00
|
|
|
for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
|
2009-10-18 17:47:38 +00:00
|
|
|
CargoPacket *cp = *it;
|
|
|
|
cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : v->tile;
|
|
|
|
cp->loaded_at_xy = cp->source_xy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store position of the station where the goods come from, so there
|
2009-11-03 17:30:08 +00:00
|
|
|
* are no very high payments when stations get removed. However, if the
|
|
|
|
* station where the goods came from is already removed, the source
|
|
|
|
* information is lost. In that case we set it to the position of this
|
|
|
|
* station */
|
2019-12-15 04:55:59 +00:00
|
|
|
for (Station *st : Station::Iterate()) {
|
2009-10-18 17:47:38 +00:00
|
|
|
for (CargoID c = 0; c < NUM_CARGO; c++) {
|
|
|
|
GoodsEntry *ge = &st->goods[c];
|
|
|
|
|
2013-06-09 13:03:48 +00:00
|
|
|
const StationCargoPacketMap *packets = ge->cargo.Packets();
|
2009-10-19 09:15:47 +00:00
|
|
|
for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
|
2009-10-18 17:47:38 +00:00
|
|
|
CargoPacket *cp = *it;
|
|
|
|
cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
|
|
|
|
cp->loaded_at_xy = cp->source_xy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:48:40 +00:00
|
|
|
if (IsSavegameVersionBefore(SLV_120)) {
|
2009-10-18 17:47:38 +00:00
|
|
|
/* CargoPacket's source should be either INVALID_STATION or a valid station */
|
2019-12-15 16:37:35 +00:00
|
|
|
for (CargoPacket *cp : CargoPacket::Iterate()) {
|
2009-10-18 17:47:38 +00:00
|
|
|
if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
|
|
|
|
}
|
|
|
|
}
|
2009-10-20 12:20:53 +00:00
|
|
|
|
2019-01-26 01:48:40 +00:00
|
|
|
if (!IsSavegameVersionBefore(SLV_68)) {
|
2009-10-20 12:20:53 +00:00
|
|
|
/* Only since version 68 we have cargo packets. Savegames from before used
|
|
|
|
* 'new CargoPacket' + cargolist.Append so their caches are already
|
|
|
|
* correct and do not need rebuilding. */
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) v->cargo.InvalidateCache();
|
|
|
|
|
2019-12-15 04:55:59 +00:00
|
|
|
for (Station *st : Station::Iterate()) {
|
2009-10-20 12:20:53 +00:00
|
|
|
for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
|
|
|
|
}
|
|
|
|
}
|
2013-02-17 14:54:50 +00:00
|
|
|
|
2019-01-26 01:48:40 +00:00
|
|
|
if (IsSavegameVersionBefore(SLV_181)) {
|
2013-02-17 14:54:50 +00:00
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) v->cargo.KeepAll();
|
|
|
|
}
|
2009-10-18 17:47:38 +00:00
|
|
|
}
|
|
|
|
|
2009-10-06 17:23:15 +00:00
|
|
|
/**
|
|
|
|
* Wrapper function to get the CargoPacket's internal structure while
|
|
|
|
* some of the variables itself are private.
|
|
|
|
* @return the saveload description for CargoPackets.
|
|
|
|
*/
|
|
|
|
const SaveLoad *GetCargoPacketDesc()
|
|
|
|
{
|
|
|
|
static const SaveLoad _cargopacket_desc[] = {
|
|
|
|
SLE_VAR(CargoPacket, source, SLE_UINT16),
|
|
|
|
SLE_VAR(CargoPacket, source_xy, SLE_UINT32),
|
|
|
|
SLE_VAR(CargoPacket, loaded_at_xy, SLE_UINT32),
|
|
|
|
SLE_VAR(CargoPacket, count, SLE_UINT16),
|
|
|
|
SLE_VAR(CargoPacket, days_in_transit, SLE_UINT8),
|
|
|
|
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
|
2019-01-26 01:48:40 +00:00
|
|
|
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION),
|
2009-10-06 17:23:15 +00:00
|
|
|
|
|
|
|
/* Used to be paid_for, but that got changed. */
|
2019-01-26 01:48:40 +00:00
|
|
|
SLE_CONDNULL(1, SL_MIN_VERSION, SLV_121),
|
2009-10-06 17:23:15 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
return _cargopacket_desc;
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Save the cargo packets.
|
|
|
|
*/
|
2009-01-04 15:32:25 +00:00
|
|
|
static void Save_CAPA()
|
|
|
|
{
|
2019-12-15 16:37:35 +00:00
|
|
|
for (CargoPacket *cp : CargoPacket::Iterate()) {
|
2009-01-04 15:32:25 +00:00
|
|
|
SlSetArrayIndex(cp->index);
|
2009-10-06 17:23:15 +00:00
|
|
|
SlObject(cp, GetCargoPacketDesc());
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Load the cargo packets.
|
|
|
|
*/
|
2009-01-04 15:32:25 +00:00
|
|
|
static void Load_CAPA()
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
|
|
|
CargoPacket *cp = new (index) CargoPacket();
|
2009-10-06 17:23:15 +00:00
|
|
|
SlObject(cp, GetCargoPacketDesc());
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/** Chunk handlers related to cargo packets. */
|
2009-01-04 15:32:25 +00:00
|
|
|
extern const ChunkHandler _cargopacket_chunk_handlers[] = {
|
2019-04-10 21:07:06 +00:00
|
|
|
{ 'CAPA', Save_CAPA, Load_CAPA, nullptr, nullptr, CH_ARRAY | CH_LAST},
|
2009-01-04 15:32:25 +00:00
|
|
|
};
|