diff src/vehicle.cpp @ 7493:2068a51c2e6c draft

(svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
author rubidium <rubidium@openttd.org>
date Thu, 30 Aug 2007 13:09:44 +0000
parents 09743324277c
children 797ff0b0e0a5
line wrap: on
line diff
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -2724,8 +2724,15 @@
 static Money  _cargo_feeder_share;
 static uint32 _cargo_loaded_at_xy;
 
+/**
+ * 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
+ * @return the saveload description
+ */
+const SaveLoad *GetVehicleDescription(VehicleType vt)
+{
 /** Save and load of vehicles */
-extern const SaveLoad _common_veh_desc[] = {
+static const SaveLoad _common_veh_desc[] = {
 	    SLE_VAR(Vehicle, subtype,              SLE_UINT8),
 
 	    SLE_REF(Vehicle, next,                 REF_VEHICLE_OLD),
@@ -2848,7 +2855,7 @@
 
 static const SaveLoad _train_desc[] = {
 	SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
-	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
+	SLE_VEH_INCLUDEX(),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos),         SLE_UINT16),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed),          SLE_UINT8),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype),               SLE_UINT8),
@@ -2866,7 +2873,7 @@
 
 static const SaveLoad _roadveh_desc[] = {
 	SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
-	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
+	SLE_VEH_INCLUDEX(),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state),          SLE_UINT8),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame),          SLE_UINT8),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr),    SLE_UINT16),
@@ -2886,7 +2893,7 @@
 
 static const SaveLoad _ship_desc[] = {
 	SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
-	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
+	SLE_VEH_INCLUDEX(),
 	SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
 
 	/* reserve extra space in savegame here. (currently 16 bytes) */
@@ -2897,7 +2904,7 @@
 
 static const SaveLoad _aircraft_desc[] = {
 	SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
-	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
+	SLE_VEH_INCLUDEX(),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16),
 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos),             SLE_UINT8),
 
@@ -2988,8 +2995,12 @@
 	_aircraft_desc,
 	_special_desc,
 	_disaster_desc,
+	_common_veh_desc,
 };
 
+	return _veh_descs[vt];
+}
+
 /** Will be called when the vehicles need to be saved. */
 static void Save_VEHS()
 {
@@ -2997,7 +3008,7 @@
 	/* Write the vehicles */
 	FOR_ALL_VEHICLES(v) {
 		SlSetArrayIndex(v->index);
-		SlObject(v, _veh_descs[v->type]);
+		SlObject(v, GetVehicleDescription(v->type));
 	}
 }
 
@@ -3024,7 +3035,7 @@
 			default: NOT_REACHED();
 		}
 
-		SlObject(v, _veh_descs[vtype]);
+		SlObject(v, GetVehicleDescription(vtype));
 
 		if (_cargo_count != 0 && IsPlayerBuildableVehicleType(v)) {
 			/* Don't construct the packet with station here, because that'll fail with old savegames */
@@ -3157,7 +3168,6 @@
 	InvalidateVehicleOrder(this);
 }
 
-
 void SpecialVehicle::UpdateDeltaXY(Direction direction)
 {
 	this->x_offs        = 0;