Move upstream saveload to src/saveload/, move jgrpp saveload to src/sl/
Leave afterload in src/saveload/
This commit is contained in:
@@ -8,100 +8,16 @@
|
||||
/** @file cargopacket_sl.cpp Code handling saving and loading of cargo packets */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../vehicle_base.h"
|
||||
#include "../station_base.h"
|
||||
#include "../scope_info.h"
|
||||
#include "../3rdparty/cpp-btree/btree_map.h"
|
||||
|
||||
#include "saveload.h"
|
||||
#include "compat/cargopacket_sl_compat.h"
|
||||
|
||||
#include "../vehicle_base.h"
|
||||
#include "../station_base.h"
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
extern btree::btree_map<uint64, Money> _cargo_packet_deferred_payments;
|
||||
|
||||
/**
|
||||
* Savegame conversion for cargopackets.
|
||||
*/
|
||||
/* static */ void CargoPacket::AfterLoad()
|
||||
{
|
||||
if (IsSavegameVersionBefore(SLV_44)) {
|
||||
/* If we remove a station while cargo from it is still en route, payment calculation will assume
|
||||
* 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
|
||||
*/
|
||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
||||
const CargoPacketList *packets = v->cargo.Packets();
|
||||
for (VehicleCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
|
||||
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
|
||||
* 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 */
|
||||
for (Station *st : Station::Iterate()) {
|
||||
for (CargoID c = 0; c < NUM_CARGO; c++) {
|
||||
GoodsEntry *ge = &st->goods[c];
|
||||
|
||||
const StationCargoPacketMap *packets = ge->cargo.Packets();
|
||||
for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
|
||||
CargoPacket *cp = *it;
|
||||
cp->source_xy = Station::IsValidID(cp->source) ? Station::Get(cp->source)->xy : st->xy;
|
||||
cp->loaded_at_xy = cp->source_xy;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (IsSavegameVersionBefore(SLV_120)) {
|
||||
/* CargoPacket's source should be either INVALID_STATION or a valid station */
|
||||
for (CargoPacket *cp : CargoPacket::Iterate()) {
|
||||
if (!Station::IsValidID(cp->source)) cp->source = INVALID_STATION;
|
||||
}
|
||||
}
|
||||
|
||||
if (!IsSavegameVersionBefore(SLV_68)) {
|
||||
/* 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. */
|
||||
for (Vehicle *v : Vehicle::Iterate()) v->cargo.InvalidateCache();
|
||||
|
||||
for (Station *st : Station::Iterate()) {
|
||||
for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
|
||||
}
|
||||
}
|
||||
|
||||
if (IsSavegameVersionBefore(SLV_181)) {
|
||||
for (Vehicle *v : Vehicle::Iterate()) v->cargo.KeepAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Savegame conversion for cargopackets.
|
||||
*/
|
||||
/* static */ void CargoPacket::PostVehiclesAfterLoad()
|
||||
{
|
||||
if (SlXvIsFeaturePresent(XSLFI_CHILLPP)) {
|
||||
extern std::map<VehicleID, CargoPacketList> _veh_cpp_packets;
|
||||
for (auto &iter : _veh_cpp_packets) {
|
||||
if (iter.second.empty()) continue;
|
||||
Vehicle *v = Vehicle::Get(iter.first);
|
||||
Station *st = Station::Get(v->First()->last_station_visited);
|
||||
assert_msg(st != nullptr, "%s", scope_dumper().VehicleInfo(v));
|
||||
for (CargoPacket *cp : iter.second) {
|
||||
st->goods[v->cargo_type].cargo.AfterLoadIncreaseReservationCount(cp->count);
|
||||
v->cargo.Append(cp, VehicleCargoList::MTA_LOAD);
|
||||
}
|
||||
}
|
||||
_veh_cpp_packets.clear();
|
||||
}
|
||||
}
|
||||
namespace upstream_sl {
|
||||
|
||||
/**
|
||||
* Wrapper function to get the CargoPacket's internal structure while
|
||||
@@ -111,85 +27,50 @@ extern btree::btree_map<uint64, Money> _cargo_packet_deferred_payments;
|
||||
SaveLoadTable 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_CONDVAR_X(CargoPacket, days_in_transit, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_MORE_CARGO_AGE, 0, 0)),
|
||||
SLE_CONDVAR_X(CargoPacket, days_in_transit, SLE_UINT16, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_MORE_CARGO_AGE)),
|
||||
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
|
||||
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION),
|
||||
|
||||
/* Used to be paid_for, but that got changed. */
|
||||
SLE_CONDNULL(1, SL_MIN_VERSION, SLV_121),
|
||||
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_CONDVAR(CargoPacket, days_in_transit, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_MORE_CARGO_AGE),
|
||||
SLE_CONDVAR(CargoPacket, days_in_transit, SLE_UINT16, SLV_MORE_CARGO_AGE, SL_MAX_VERSION),
|
||||
SLE_VAR(CargoPacket, feeder_share, SLE_INT64),
|
||||
SLE_CONDVAR(CargoPacket, source_type, SLE_UINT8, SLV_125, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(CargoPacket, source_id, SLE_UINT16, SLV_125, SL_MAX_VERSION),
|
||||
};
|
||||
return _cargopacket_desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the cargo packets.
|
||||
*/
|
||||
static void Save_CAPA()
|
||||
{
|
||||
std::vector<SaveLoad> filtered_packet_desc = SlFilterObject(GetCargoPacketDesc());
|
||||
for (CargoPacket *cp : CargoPacket::Iterate()) {
|
||||
SlSetArrayIndex(cp->index);
|
||||
SlObjectSaveFiltered(cp, filtered_packet_desc);
|
||||
}
|
||||
}
|
||||
struct CAPAChunkHandler : ChunkHandler {
|
||||
CAPAChunkHandler() : ChunkHandler('CAPA', CH_TABLE) {}
|
||||
|
||||
/**
|
||||
* Load the cargo packets.
|
||||
*/
|
||||
static void Load_CAPA()
|
||||
{
|
||||
std::vector<SaveLoad> filtered_packet_desc = SlFilterObject(GetCargoPacketDesc());
|
||||
int index;
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
CargoPacket *cp = new (index) CargoPacket();
|
||||
SlObjectLoadFiltered(cp, filtered_packet_desc);
|
||||
}
|
||||
}
|
||||
void Save() const override
|
||||
{
|
||||
SlTableHeader(GetCargoPacketDesc());
|
||||
|
||||
/**
|
||||
* Save cargo packet deferred payments.
|
||||
*/
|
||||
void Save_CPDP()
|
||||
{
|
||||
SlSetLength(16 * _cargo_packet_deferred_payments.size());
|
||||
|
||||
for (auto &it : _cargo_packet_deferred_payments) {
|
||||
SlWriteUint64(it.first);
|
||||
SlWriteUint64(it.second);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load cargo packet deferred payments.
|
||||
*/
|
||||
void Load_CPDP()
|
||||
{
|
||||
uint count = static_cast<uint>(SlGetFieldLength() / 16);
|
||||
uint last_cargo_packet_id = std::numeric_limits<uint32_t>::max();
|
||||
|
||||
for (uint i = 0; i < count; i++) {
|
||||
uint64 k = SlReadUint64();
|
||||
uint64 v = SlReadUint64();
|
||||
_cargo_packet_deferred_payments[k] = v;
|
||||
if (k >> 32 != last_cargo_packet_id) {
|
||||
last_cargo_packet_id = k >> 32;
|
||||
CargoPacket::Get(last_cargo_packet_id)->flags |= CargoPacket::CPF_HAS_DEFERRED_PAYMENT;
|
||||
for (CargoPacket *cp : CargoPacket::Iterate()) {
|
||||
SlSetArrayIndex(cp->index);
|
||||
SlObject(cp, GetCargoPacketDesc());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Load() const override
|
||||
{
|
||||
const std::vector<SaveLoad> slt = SlCompatTableHeader(GetCargoPacketDesc(), _cargopacket_sl_compat);
|
||||
|
||||
int index;
|
||||
|
||||
/** Chunk handlers related to cargo packets. */
|
||||
static const ChunkHandler cargopacket_chunk_handlers[] = {
|
||||
{ 'CAPA', Save_CAPA, Load_CAPA, nullptr, nullptr, CH_ARRAY },
|
||||
{ 'CPDP', Save_CPDP, Load_CPDP, nullptr, nullptr, CH_RIFF },
|
||||
while ((index = SlIterateArray()) != -1) {
|
||||
CargoPacket *cp = new (index) CargoPacket();
|
||||
SlObject(cp, slt);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static const CAPAChunkHandler CAPA;
|
||||
static const ChunkHandlerRef cargopacket_chunk_handlers[] = {
|
||||
CAPA,
|
||||
};
|
||||
|
||||
extern const ChunkHandlerTable _cargopacket_chunk_handlers(cargopacket_chunk_handlers);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user