changeset 13933:a71da8c8c5e2 draft

(svn r18465) -Codechange: simplify CheckTrainInDepot and remove some unneeded wrapper functions
author rubidium <rubidium@openttd.org>
date Fri, 11 Dec 2009 22:04:57 +0000
parents a503c6f8fa5e
children 6c8c9b6e2b7b
files src/train.h src/train_cmd.cpp src/vehicle_cmd.cpp
diffstat 3 files changed, 13 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/train.h
+++ b/src/train.h
@@ -50,7 +50,6 @@
 byte FreightWagonMult(CargoID cargo);
 
 int CheckTrainInDepot(const Train *v, bool needs_to_be_stopped);
-int CheckTrainStoppedInDepot(const Train *v);
 void UpdateTrainAcceleration(Train *v);
 void CheckTrainsLengths();
 
@@ -133,7 +132,7 @@
 	Money GetRunningCost() const;
 	int GetDisplayImageWidth(Point *offset = NULL) const;
 	bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
-	bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
+	bool IsStoppedInDepot() const { return CheckTrainInDepot(this, true) != -1; }
 	bool Tick();
 	void OnNewDay();
 	uint Crash(bool flooded = false);
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -966,7 +966,10 @@
 	TileIndex tile = v->tile;
 
 	/* check if stopped in a depot */
-	if (!IsRailDepotTile(tile) || v->cur_speed != 0) return -1;
+	if (!IsRailDepotTile(tile) || v->cur_speed != 0 ||
+			(needs_to_be_stopped && v->IsFrontEngine() && !(v->vehstatus & VS_STOPPED))) {
+		return -1;
+	}
 
 	int count = 0;
 	for (; v != NULL; v = v->Next()) {
@@ -976,27 +979,12 @@
 		 *
 		 * Also skip counting rear ends of multiheaded engines */
 		if (!v->IsArticulatedPart() && !v->IsRearDualheaded()) count++;
-		if (v->track != TRACK_BIT_DEPOT || v->tile != tile ||
-				(v->IsFrontEngine() && needs_to_be_stopped && !(v->vehstatus & VS_STOPPED))) {
-			return -1;
-		}
+		if (v->track != TRACK_BIT_DEPOT || v->tile != tile) return -1;
 	}
 
 	return count;
 }
 
-/* Used to check if the train is inside the depot and verifying that the VS_STOPPED flag is set */
-int CheckTrainStoppedInDepot(const Train *v)
-{
-	return CheckTrainInDepot(v, true);
-}
-
-/* Used to check if the train is inside the depot, but not checking the VS_STOPPED flag */
-inline bool CheckTrainIsInsideDepot(const Train *v)
-{
-	return CheckTrainInDepot(v, false) > 0;
-}
-
 /**
  * Unlink a rail wagon from the consist.
  * @param v Vehicle to remove.
@@ -1092,11 +1080,11 @@
 static CommandCost CheckNewTrainLength(const Train *dst_head, const Train *src_head, const Train *src, bool move_chain)
 {
 	/* Check if all vehicles in the source train are stopped inside a depot. */
-	int src_len = CheckTrainStoppedInDepot(src_head);
+	int src_len = CheckTrainInDepot(src_head, true);
 	if (src_len < 0) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
 
 	/* Check if all vehicles in the destination train are stopped inside a depot. */
-	int dst_len = dst_head != NULL ? CheckTrainStoppedInDepot(dst_head) : 0;
+	int dst_len = dst_head != NULL ? CheckTrainInDepot(dst_head, true) : 0;
 	if (dst_len < 0) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
 
 	/* Determine the maximum length of trains. */
@@ -1137,7 +1125,7 @@
 	/* We are moving between rows, so only count the wagons from the source
 	 * row that are being moved. */
 	if (move_chain) {
-		/* CheckTrainStoppedInDepot() counts units. */
+		/* CheckTrainInDepot() counts units. */
 		for (const Train *u = src_head; u != src && u != NULL; u = u->GetNextUnit()) src_len--;
 	} else {
 		/* If moving only one vehicle, just count that. */
@@ -1544,7 +1532,7 @@
 	Train *first = v->First();
 
 	/* make sure the vehicle is stopped in the depot */
-	if (CheckTrainStoppedInDepot(first) < 0) {
+	if (!first->IsStoppedInDepot()) {
 		return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
 	}
 
@@ -2102,7 +2090,7 @@
 
 		Train *front = v->First();
 		/* make sure the vehicle is stopped in the depot */
-		if (CheckTrainStoppedInDepot(front) < 0) {
+		if (!front->IsStoppedInDepot()) {
 			return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
 		}
 
@@ -2178,7 +2166,7 @@
 	Train *v = Train::GetIfValid(p1);
 	if (v == NULL || !CheckOwnership(v->owner)) return CMD_ERROR;
 
-	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
+	if (!v->IsStoppedInDepot()) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
 	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
 
 	/* Check cargo */
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -152,7 +152,7 @@
 
 		if (!vehicle_list_window) {
 			if (vehicle_type == VEH_TRAIN) {
-				if (CheckTrainInDepot(Train::From(v), false) == -1) continue;
+				if (!Train::From(v)->IsInDepot()) continue;
 			} else {
 				if (!(v->vehstatus & VS_HIDDEN)) continue;
 			}