changeset 14753:3e27a2798ca0 draft

(svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle. -Codechange: Move GetSlopeResistance to GroundVehicle.
author terkhen <terkhen@openttd.org>
date Sat, 06 Mar 2010 12:50:55 +0000
parents fd1a3d7dda38
children 691e61140880
files src/ground_vehicle.cpp src/ground_vehicle.hpp src/saveload/afterload.cpp src/saveload/vehicle_sl.cpp src/train.h src/train_cmd.cpp src/tunnelbridge_cmd.cpp
diffstat 7 files changed, 60 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/ground_vehicle.cpp
+++ b/src/ground_vehicle.cpp
@@ -116,7 +116,7 @@
 		resistance += (area * this->acc_cache.cached_air_drag * speed * speed) / 20000;
 	}
 
-	resistance += v->GetSlopeResistance();
+	resistance += this->GetSlopeResistance();
 	resistance *= 4; //[N]
 
 	/* This value allows to know if the vehicle is accelerating or braking. */
--- a/src/ground_vehicle.hpp
+++ b/src/ground_vehicle.hpp
@@ -37,6 +37,12 @@
 	uint16 cached_max_track_speed;  ///< Maximum consist speed limited by track type.
 };
 
+/** Ground vehicle flags. */
+enum GroundVehicleFlags {
+	GVF_GOINGUP_BIT       = 0,
+	GVF_GOINGDOWN_BIT     = 1,
+};
+
 /**
  * Base class for all vehicles that move through ground.
  *
@@ -58,6 +64,7 @@
 template <class T, VehicleType Type>
 struct GroundVehicle : public SpecializedVehicle<T, Type> {
 	AccelerationCache acc_cache;
+	uint16 gv_flags; ///< @see GroundVehicleFlags
 
 	/**
 	 * The constructor at SpecializedVehicle must be called.
@@ -67,6 +74,25 @@
 	void PowerChanged();
 	void CargoChanged();
 	int GetAcceleration() const;
+
+ 	/**
+	 * Calculates the total slope resistance for this vehicle.
+	 * @return Slope resistance.
+	 */
+	FORCEINLINE int32 GetSlopeResistance() const
+	{
+		int32 incl = 0;
+
+		for (const T *u = T::From(this); u != NULL; u = u->Next()) {
+			if (HasBit(u->gv_flags, GVF_GOINGUP_BIT)) {
+				incl += u->acc_cache.cached_slope_resistance;
+			} else if (HasBit(u->gv_flags, GVF_GOINGDOWN_BIT)) {
+				incl -= u->acc_cache.cached_slope_resistance;
+			}
+		}
+
+		return incl;
+	}
 };
 
 #endif /* GROUND_VEHICLE_HPP */
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -2072,6 +2072,18 @@
 				st->airport.h = st->GetAirportSpec()->size_y;
 			}
 		}
+
+		Train *t;
+		FOR_ALL_TRAINS(t) {
+			/* Copy old GOINGUP / GOINGDOWN flags. */
+			if (HasBit(t->flags, 1)) {
+				ClrBit(t->flags, 1);
+				SetBit(t->gv_flags, GVF_GOINGUP_BIT);
+			} else if (HasBit(t->flags, 2)) {
+				ClrBit(t->flags, 2);
+				SetBit(t->gv_flags, GVF_GOINGDOWN_BIT);
+			}
+		}
 	}
 
 	/* Road stops is 'only' updating some caches */
--- a/src/saveload/vehicle_sl.cpp
+++ b/src/saveload/vehicle_sl.cpp
@@ -544,6 +544,7 @@
 		 SLE_CONDVAR(Train, wait_counter,        SLE_UINT16,                 136, SL_MAX_VERSION),
 
 		SLE_CONDNULL(2, 2, 19),
+		 SLE_CONDVAR(Train, gv_flags,            SLE_UINT16,                 139, SL_MAX_VERSION),
 		/* reserve extra space in savegame here. (currently 11 bytes) */
 		SLE_CONDNULL(11, 2, SL_MAX_VERSION),
 
--- a/src/train.h
+++ b/src/train.h
@@ -24,10 +24,6 @@
 enum VehicleRailFlags {
 	VRF_REVERSING         = 0,
 
-	/* used to calculate if train is going up or down */
-	VRF_GOINGUP           = 1,
-	VRF_GOINGDOWN         = 2,
-
 	/* used to store if a wagon is powered or not */
 	VRF_POWEREDWAGON      = 3,
 
@@ -453,25 +449,6 @@
 	}
 
 	/**
-	 * Calculates the total slope resistance for this vehicle.
-	 * @return Slope resistance.
-	 */
-	FORCEINLINE int32 GetSlopeResistance() const
-	{
-		int32 incl = 0;
-
-		for (const Train *u = this; u != NULL; u = u->Next()) {
-			if (HasBit(u->flags, VRF_GOINGUP)) {
-				incl += u->acc_cache.cached_slope_resistance;
-			} else if (HasBit(u->flags, VRF_GOINGDOWN)) {
-				incl -= u->acc_cache.cached_slope_resistance;
-			}
-		}
-
-		return incl;
-	}
-
-	/**
 	 * Allows to know the acceleration type of a vehicle.
 	 * @return Acceleration type of the vehicle.
 	 */
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1477,22 +1477,22 @@
 	uint16 flag2 = *swap_flag2;
 
 	/* Clear the flags */
-	ClrBit(*swap_flag1, VRF_GOINGUP);
-	ClrBit(*swap_flag1, VRF_GOINGDOWN);
-	ClrBit(*swap_flag2, VRF_GOINGUP);
-	ClrBit(*swap_flag2, VRF_GOINGDOWN);
+	ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
+	ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
+	ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
+	ClrBit(*swap_flag2, GVF_GOINGDOWN_BIT);
 
 	/* Reverse the rail-flags (if needed) */
-	if (HasBit(flag1, VRF_GOINGUP)) {
-		SetBit(*swap_flag2, VRF_GOINGDOWN);
-	} else if (HasBit(flag1, VRF_GOINGDOWN)) {
-		SetBit(*swap_flag2, VRF_GOINGUP);
-	}
-	if (HasBit(flag2, VRF_GOINGUP)) {
-		SetBit(*swap_flag1, VRF_GOINGDOWN);
-	} else if (HasBit(flag2, VRF_GOINGDOWN)) {
-		SetBit(*swap_flag1, VRF_GOINGUP);
-	}
+	if (HasBit(flag1, GVF_GOINGUP_BIT)) {
+		SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
+	} else if (HasBit(flag1, GVF_GOINGDOWN_BIT)) {
+		SetBit(*swap_flag2, GVF_GOINGUP_BIT);
+ 	}
+	if (HasBit(flag2, GVF_GOINGUP_BIT)) {
+		SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
+	} else if (HasBit(flag2, GVF_GOINGDOWN_BIT)) {
+		SetBit(*swap_flag1, GVF_GOINGUP_BIT);
+ 	}
 }
 
 static void ReverseTrainSwapVeh(Train *v, int l, int r)
@@ -1523,7 +1523,7 @@
 		Swap(a->tile,  b->tile);
 		Swap(a->z_pos, b->z_pos);
 
-		SwapTrainFlags(&a->flags, &b->flags);
+		SwapTrainFlags(&a->gv_flags, &b->gv_flags);
 
 		/* update other vars */
 		a->UpdateViewport(true, true);
@@ -2875,8 +2875,8 @@
 	v->z_pos = GetSlopeZ(v->x_pos, v->y_pos);
 
 	if (new_tile) {
-		ClrBit(v->flags, VRF_GOINGUP);
-		ClrBit(v->flags, VRF_GOINGDOWN);
+		ClrBit(v->gv_flags, GVF_GOINGUP_BIT);
+		ClrBit(v->gv_flags, GVF_GOINGDOWN_BIT);
 
 		if (v->track == TRACK_BIT_X || v->track == TRACK_BIT_Y) {
 			/* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped.
@@ -2890,7 +2890,7 @@
 			byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE);
 
 			if (middle_z != v->z_pos) {
-				SetBit(v->flags, (middle_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
+				SetBit(v->gv_flags, (middle_z > old_z) ? GVF_GOINGUP_BIT : GVF_GOINGDOWN_BIT);
 			}
 		}
 	}
--- a/src/tunnelbridge_cmd.cpp
+++ b/src/tunnelbridge_cmd.cpp
@@ -1473,8 +1473,8 @@
 				case VEH_TRAIN: {
 					Train *t = Train::From(v);
 					t->track = TRACK_BIT_WORMHOLE;
-					ClrBit(t->flags, VRF_GOINGUP);
-					ClrBit(t->flags, VRF_GOINGDOWN);
+					ClrBit(t->gv_flags, GVF_GOINGUP_BIT);
+					ClrBit(t->gv_flags, GVF_GOINGDOWN_BIT);
 				} break;
 
 				case VEH_ROAD: