Saveload: Move vehicle unbunching state to sub-chunk

pull/728/head
Jonathan G Rennison 2 months ago
parent 711f594464
commit 43e1036786

@ -210,7 +210,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_NEWGRF_LAST_SERVICE, XSCF_NULL, 1, 1, "slv_newgrf_last_service", nullptr, nullptr, nullptr },
{ XSLFI_CARGO_TRAVELLED, XSCF_NULL, 1, 1, "slv_cargo_travelled", nullptr, nullptr, nullptr },
{ XSLFI_SHIP_ACCELERATION, XSCF_NULL, 1, 1, "slv_ship_acceleration", nullptr, nullptr, nullptr },
{ XSLFI_DEPOT_UNBUNCHING, XSCF_NULL, 1, 1, "slv_depot_unbunching", nullptr, nullptr, "VUBS" },
{ XSLFI_DEPOT_UNBUNCHING, XSCF_NULL, 2, 2, "slv_depot_unbunching", nullptr, nullptr, nullptr },
{ XSLFI_VEHICLE_ECONOMY_AGE, XSCF_NULL, 1, 1, "slv_vehicle_economy_age", nullptr, nullptr, nullptr },
{ XSLFI_TABLE_PATS, XSCF_NULL, 1, 1, "table_pats", nullptr, nullptr, nullptr },

@ -772,6 +772,36 @@ struct VehicleTypeStructHandler final : public TypedSaveLoadStructHandler<Vehicl
}
};
const NamedSaveLoadTable GetVehicleUnbunchStateDescription()
{
static const NamedSaveLoad _vehicle_unbunch_state_desc[] = {
NSL("last_departure", SLE_VAR(VehicleUnbunchState, depot_unbunching_last_departure, SLE_INT64)),
NSL("next_departure", SLE_VAR(VehicleUnbunchState, depot_unbunching_next_departure, SLE_INT64)),
NSL("round_trip_time", SLE_VAR(VehicleUnbunchState, round_trip_time, SLE_INT32)),
};
return _vehicle_unbunch_state_desc;
}
struct VehicleUnbunchStateStructHandler final : public TypedSaveLoadStructHandler<VehicleUnbunchStateStructHandler, Vehicle> {
NamedSaveLoadTable GetDescription() const override
{
return GetVehicleUnbunchStateDescription();
}
void Save(Vehicle *v) const override
{
if (v->unbunch_state != nullptr) {
SlObjectSaveFiltered(v->unbunch_state.get(), this->GetLoadDescription());
}
}
void Load(Vehicle *v) const override
{
v->unbunch_state.reset(new VehicleUnbunchState());
SlObjectLoadFiltered(v->unbunch_state.get(), this->GetLoadDescription());
}
};
/**
* Make it possible to make the saveload tables "friends" of other classes.
* @param vt the vehicle type. Can be VEH_END for the common vehicle description data
@ -947,6 +977,8 @@ NamedSaveLoadTable GetVehicleDescription(VehicleType vt)
NSL("", SLE_CONDNULL_X(2, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SPRINGPP))),
NSL("", SLE_CONDNULL_X(160, SL_MIN_VERSION, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_JOKERPP))),
NSLT_STRUCT<VehicleUnbunchStateStructHandler>("depot_unbunch_state"),
};
static const NamedSaveLoad _train_desc[] = {
@ -1694,35 +1726,16 @@ void Load_VLKA()
}
}
const SaveLoadTable GetVehicleUnbunchStateDescription()
{
static const SaveLoad _vehicle_unbunch_state_desc[] = {
SLE_VAR(VehicleUnbunchState, depot_unbunching_last_departure, SLE_INT64),
SLE_VAR(VehicleUnbunchState, depot_unbunching_next_departure, SLE_INT64),
SLE_VAR(VehicleUnbunchState, round_trip_time, SLE_INT32),
};
return _vehicle_unbunch_state_desc;
}
void Save_VUBS()
{
for (Vehicle *v : Vehicle::IterateFrontOnly()) {
if (v->unbunch_state != nullptr) {
SlSetArrayIndex(v->index);
SlObject(v->unbunch_state.get(), GetVehicleUnbunchStateDescription());
}
}
}
void Load_VUBS()
{
std::vector<SaveLoad> unbunch_desc = SlFilterNamedSaveLoadTable(GetVehicleUnbunchStateDescription());
int index;
while ((index = SlIterateArray()) != -1) {
Vehicle *v = Vehicle::GetIfValid(index);
assert(v != nullptr);
v->unbunch_state.reset(new VehicleUnbunchState());
SlObject(v->unbunch_state.get(), GetVehicleUnbunchStateDescription());
SlObjectLoadFiltered(v->unbunch_state.get(), unbunch_desc);
}
}
@ -1732,7 +1745,7 @@ static const ChunkHandler veh_chunk_handlers[] = {
{ 'VESR', Save_VESR, Load_VESR, nullptr, nullptr, CH_SPARSE_ARRAY },
{ 'VENC', Save_VENC, Load_VENC, nullptr, nullptr, CH_RIFF, Special_VENC },
{ 'VLKA', Save_VLKA, Load_VLKA, nullptr, nullptr, CH_SPARSE_ARRAY },
{ 'VUBS', Save_VUBS, Load_VUBS, nullptr, nullptr, CH_SPARSE_ARRAY },
{ 'VUBS', nullptr, Load_VUBS, nullptr, nullptr, CH_READONLY },
};
extern const ChunkHandlerTable _veh_chunk_handlers(veh_chunk_handlers);

Loading…
Cancel
Save