changeset 12460:4acae7d6f5ed draft

(svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
author frosch <frosch@openttd.org>
date Mon, 20 Jul 2009 19:58:33 +0000
parents 3740c8aa080d
children d5ef161973f1
files src/depot_gui.cpp src/train_cmd.cpp src/train_gui.cpp src/vehicle.cpp src/vehicle_base.h src/vehicle_func.h src/yapf/yapf_rail.cpp
diffstat 7 files changed, 42 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -151,7 +151,7 @@
 	if (v == wagon) return;
 
 	if (wagon == NULL) {
-		if (head != NULL) wagon = GetLastVehicleInChain(head);
+		if (head != NULL) wagon = head->Last();
 	} else  {
 		wagon = wagon->Previous();
 		if (wagon == NULL) return;
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -704,7 +704,7 @@
 			if (w->tile == tile && w->IsFreeWagon() &&
 					w->engine_type == engine &&
 					!(w->vehstatus & VS_CRASHED)) {
-				u = GetLastVehicleInChain(w);
+				u = w->Last();
 				break;
 			}
 		}
@@ -1829,7 +1829,7 @@
 {
 	Train *base = v;
 	Train *first = base; // first vehicle to move
-	Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move
+	Train *last = v->Last(); // last vehicle to move
 	uint length = CountVehiclesInChain(v);
 
 	while (length > 2) {
@@ -1878,7 +1878,7 @@
 
 	Train *base = v;
 	Train *first = base; // first vehicle to move
-	Train *last = Train::From(GetLastVehicleInChain(v)); // last vehicle to move
+	Train *last = v->Last(); // last vehicle to move
 	uint length = CountVehiclesInChain(v);
 
 	/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly
@@ -2219,7 +2219,7 @@
 		} break;
 
 		case VPF_NPF: { // NPF
-			const Vehicle *last = GetLastVehicleInChain(v);
+			const Vehicle *last = v->Last();
 			Trackdir trackdir = v->GetVehicleTrackdir();
 			Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
 
@@ -3225,7 +3225,7 @@
 		case VPF_NPF: { // NPF
 			NPFFindStationOrTileData fstd;
 			NPFFoundTargetData ftd;
-			Vehicle *last = GetLastVehicleInChain(v);
+			Vehicle *last = v->Last();
 
 			NPFFillWithOrderData(&fstd, v);
 
--- a/src/train_gui.cpp
+++ b/src/train_gui.cpp
@@ -35,7 +35,7 @@
 
 	/* if we found a loco, */
 	if (found != NULL) {
-		found = GetLastVehicleInChain(found);
+		found = found->Last();
 		/* put the new wagon at the end of the loco. */
 		DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, CMD_MOVE_RAIL_VEHICLE);
 		InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -453,18 +453,6 @@
 	ResetVehiclePosHash();
 }
 
-Vehicle *GetLastVehicleInChain(Vehicle *v)
-{
-	while (v->Next() != NULL) v = v->Next();
-	return v;
-}
-
-const Vehicle *GetLastVehicleInChain(const Vehicle *v)
-{
-	while (v->Next() != NULL) v = v->Next();
-	return v;
-}
-
 uint CountVehiclesInChain(const Vehicle *v)
 {
 	uint count = 0;
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -366,6 +366,27 @@
 	 */
 	inline Vehicle *First() const { return this->first; }
 
+	/**
+	 * Get the last vehicle of this vehicle chain.
+	 * @return the last vehicle of the chain.
+	 */
+	inline Vehicle *Last()
+	{
+		Vehicle *v = this;
+		while (v->Next() != NULL) v = v->Next();
+		return v;
+	}
+
+	/**
+	 * Get the last vehicle of this vehicle chain.
+	 * @return the last vehicle of the chain.
+	 */
+	inline const Vehicle *Last() const
+	{
+		const Vehicle *v = this;
+		while (v->Next() != NULL) v = v->Next();
+		return v;
+	}
 
 	/**
 	 * Get the first order of the vehicles order list.
@@ -538,6 +559,18 @@
 	FORCEINLINE T *First() const { return (T *)this->Vehicle::First(); }
 
 	/**
+	 * Get the last vehicle in the chain
+	 * @return last vehicle in the chain
+	 */
+	FORCEINLINE T *Last() { return (T *)this->Vehicle::Last(); }
+
+	/**
+	 * Get the last vehicle in the chain
+	 * @return last vehicle in the chain
+	 */
+	FORCEINLINE const T *Last() const { return (const T *)this->Vehicle::Last(); }
+
+	/**
 	 * Get next vehicle in the chain
 	 * @return next vehicle in the chain
 	 */
--- a/src/vehicle_func.h
+++ b/src/vehicle_func.h
@@ -23,8 +23,6 @@
 typedef Vehicle *VehicleFromPosProc(Vehicle *v, void *data);
 
 void VehicleServiceInDepot(Vehicle *v);
-Vehicle *GetLastVehicleInChain(Vehicle *v);
-const Vehicle *GetLastVehicleInChain(const Vehicle *v);
 uint CountVehiclesInChain(const Vehicle *v);
 void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
 void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc);
--- a/src/yapf/yapf_rail.cpp
+++ b/src/yapf/yapf_rail.cpp
@@ -537,8 +537,7 @@
 bool YapfCheckReverseTrain(const Vehicle *vt)
 {
 	const Train *v = Train::From(vt);
-	/* last wagon */
-	const Train *last_veh = Train::From(GetLastVehicleInChain(v));
+	const Train *last_veh = v->Last();
 
 	/* get trackdirs of both ends */
 	Trackdir td = v->GetVehicleTrackdir();
@@ -600,7 +599,7 @@
 	*depot_tile = INVALID_TILE;
 	*reversed = false;
 
-	const Vehicle *last_veh = GetLastVehicleInChain(v);
+	const Vehicle *last_veh = v->Last();
 
 	PBSTileInfo origin = FollowTrainReservation(Train::From(v));
 	TileIndex last_tile = last_veh->tile;