summaryrefslogtreecommitdiff
path: root/src/saveload/vehicle_sl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/saveload/vehicle_sl.cpp')
-rw-r--r--src/saveload/vehicle_sl.cpp113
1 files changed, 59 insertions, 54 deletions
diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp
index 2be5998f5..24b3a13c9 100644
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -994,77 +994,82 @@ const static SaveLoad _vehicle_desc[] = {
SLEG_STRUCT("disaster", SlVehicleDisaster),
};
-/** Will be called when the vehicles need to be saved. */
-static void Save_VEHS()
-{
- SlTableHeader(_vehicle_desc);
+struct VEHSChunkHandler : ChunkHandler {
+ VEHSChunkHandler() : ChunkHandler('VEHS', CH_SPARSE_TABLE)
+ {
+ this->fix_pointers = true;
+ }
- /* Write the vehicles */
- for (Vehicle *v : Vehicle::Iterate()) {
- SlSetArrayIndex(v->index);
- SlObject(v, _vehicle_desc);
+ void Save() const override
+ {
+ SlTableHeader(_vehicle_desc);
+
+ /* Write the vehicles */
+ for (Vehicle *v : Vehicle::Iterate()) {
+ SlSetArrayIndex(v->index);
+ SlObject(v, _vehicle_desc);
+ }
}
-}
-/** Will be called when vehicles need to be loaded. */
-void Load_VEHS()
-{
- const std::vector<SaveLoad> slt = SlCompatTableHeader(_vehicle_desc, _vehicle_sl_compat);
+ void Load() const override
+ {
+ const std::vector<SaveLoad> slt = SlCompatTableHeader(_vehicle_desc, _vehicle_sl_compat);
- int index;
+ int index;
- _cargo_count = 0;
+ _cargo_count = 0;
- while ((index = SlIterateArray()) != -1) {
- Vehicle *v;
- VehicleType vtype = (VehicleType)SlReadByte();
+ while ((index = SlIterateArray()) != -1) {
+ Vehicle *v;
+ VehicleType vtype = (VehicleType)SlReadByte();
- switch (vtype) {
- case VEH_TRAIN: v = new (index) Train(); break;
- case VEH_ROAD: v = new (index) RoadVehicle(); break;
- case VEH_SHIP: v = new (index) Ship(); break;
- case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
- case VEH_EFFECT: v = new (index) EffectVehicle(); break;
- case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
- case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
- default: SlErrorCorrupt("Invalid vehicle type");
- }
+ switch (vtype) {
+ case VEH_TRAIN: v = new (index) Train(); break;
+ case VEH_ROAD: v = new (index) RoadVehicle(); break;
+ case VEH_SHIP: v = new (index) Ship(); break;
+ case VEH_AIRCRAFT: v = new (index) Aircraft(); break;
+ case VEH_EFFECT: v = new (index) EffectVehicle(); break;
+ case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
+ case VEH_INVALID: // Savegame shouldn't contain invalid vehicles
+ default: SlErrorCorrupt("Invalid vehicle type");
+ }
- SlObject(v, slt);
+ SlObject(v, slt);
- if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
- /* Don't construct the packet with station here, because that'll fail with old savegames */
- CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
- v->cargo.Append(cp);
- }
+ if (_cargo_count != 0 && IsCompanyBuildableVehicleType(v) && CargoPacket::CanAllocateItem()) {
+ /* Don't construct the packet with station here, because that'll fail with old savegames */
+ CargoPacket *cp = new CargoPacket(_cargo_count, _cargo_days, _cargo_source, _cargo_source_xy, _cargo_loaded_at_xy, _cargo_feeder_share);
+ v->cargo.Append(cp);
+ }
- /* Old savegames used 'last_station_visited = 0xFF' */
- if (IsSavegameVersionBefore(SLV_5) && v->last_station_visited == 0xFF) {
- v->last_station_visited = INVALID_STATION;
- }
+ /* Old savegames used 'last_station_visited = 0xFF' */
+ if (IsSavegameVersionBefore(SLV_5) && v->last_station_visited == 0xFF) {
+ v->last_station_visited = INVALID_STATION;
+ }
- if (IsSavegameVersionBefore(SLV_182)) v->last_loading_station = INVALID_STATION;
+ if (IsSavegameVersionBefore(SLV_182)) v->last_loading_station = INVALID_STATION;
- if (IsSavegameVersionBefore(SLV_5)) {
- /* Convert the current_order.type (which is a mix of type and flags, because
- * in those versions, they both were 4 bits big) to type and flags */
- v->current_order.flags = GB(v->current_order.type, 4, 4);
- v->current_order.type &= 0x0F;
- }
+ if (IsSavegameVersionBefore(SLV_5)) {
+ /* Convert the current_order.type (which is a mix of type and flags, because
+ * in those versions, they both were 4 bits big) to type and flags */
+ v->current_order.flags = GB(v->current_order.type, 4, 4);
+ v->current_order.type &= 0x0F;
+ }
- /* Advanced vehicle lists got added */
- if (IsSavegameVersionBefore(SLV_60)) v->group_id = DEFAULT_GROUP;
+ /* Advanced vehicle lists got added */
+ if (IsSavegameVersionBefore(SLV_60)) v->group_id = DEFAULT_GROUP;
+ }
}
-}
-void Ptrs_VEHS()
-{
- for (Vehicle *v : Vehicle::Iterate()) {
- SlObject(v, _vehicle_desc);
+ void FixPointers() const override
+ {
+ for (Vehicle *v : Vehicle::Iterate()) {
+ SlObject(v, _vehicle_desc);
+ }
}
-}
+};
-static const ChunkHandler VEHS{ 'VEHS', Save_VEHS, Load_VEHS, Ptrs_VEHS, nullptr, CH_SPARSE_TABLE };
+static const VEHSChunkHandler VEHS;
static const ChunkHandlerRef veh_chunk_handlers[] = {
VEHS,
};