changeset 12450:af266072d46c draft

(svn r16884) -Codechange: Add Train::GetFirstEnginePart() and use it.
author frosch <frosch@openttd.org>
date Sun, 19 Jul 2009 19:17:41 +0000
parents 13e1d75b9443
children 7b6e933f3f43
files src/depot_gui.cpp src/train.h src/train_cmd.cpp
diffstat 3 files changed, 26 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -453,10 +453,7 @@
 					if (x < 0) break;
 				}
 
-				/* if an articulated part was selected, find its parent */
-				while (v != NULL && v->IsArticulatedPart()) v = v->Previous();
-
-				d->wagon = v;
+				d->wagon = (v != NULL ? v->GetFirstEnginePart() : NULL);
 
 				return MODE_DRAG_VEHICLE;
 			}
--- a/src/train.h
+++ b/src/train.h
@@ -274,6 +274,28 @@
 	}
 
 	/**
+	 * Get the first part of a multi-part engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE Train *GetFirstEnginePart()
+	{
+		Train *v = this;
+		while (v->IsArticulatedPart()) v = v->Previous();
+		return v;
+	}
+
+	/**
+	 * Get the first part of a multi-part engine.
+	 * @return First part of the engine.
+	 */
+	FORCEINLINE const Train *GetFirstEnginePart() const
+	{
+		const Train *v = this;
+		while (v->IsArticulatedPart()) v = v->Previous();
+		return v;
+	}
+
+	/**
 	 * Get the last part of a multi-part engine.
 	 * @return Last part of the engine.
 	 */
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -1105,9 +1105,9 @@
 	}
 
 	/* if an articulated part is being handled, deal with its parent vehicle */
-	while (src->IsArticulatedPart()) src = src->Previous();
+	src = src->GetFirstEnginePart();
 	if (dst != NULL) {
-		while (dst->IsArticulatedPart()) dst = dst->Previous();
+		dst = dst->GetFirstEnginePart();
 	}
 
 	/* don't move the same vehicle.. */
@@ -1445,7 +1445,7 @@
 
 	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
 
-	while (v->IsArticulatedPart()) v = v->Previous();
+	v = v->GetFirstEnginePart();
 	Train *first = v->First();
 
 	/* make sure the vehicle is stopped in the depot */