(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
authorDarkvater
Thu, 03 Mar 2005 23:26:35 +0000
changeset 1418 f71b68950a6f
parent 1417 74779e56d5db
child 1419 87baab2cfda7
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
train_cmd.c
--- a/train_cmd.c	Thu Mar 03 19:54:46 2005 +0000
+++ b/train_cmd.c	Thu Mar 03 23:26:35 2005 +0000
@@ -2599,11 +2599,20 @@
 
 extern uint CheckTunnelBusy(uint tile, int *length);
 
+/**
+ * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
+ * train, then goes to the last wagon and deletes that. Each call to this function
+ * will remove the last wagon of a crashed train. If this wagon was on a crossing,
+ * or inside a tunnel, recalculate the signals as they might need updating
+ * @param v the @Vehicle of which last wagon is to be removed
+ */
 static void DeleteLastWagon(Vehicle *v)
 {
 	Vehicle *u = v;
-	int t;
-
+
+	/* Go to the last wagon and delete the link pointing there
+	 * *u is then the one-before-last wagon, and *v the last
+	 * one which will physicially be removed */
 	while (v->next != NULL) {
 		u = v;
 		v = v->next;
@@ -2619,22 +2628,25 @@
 	EndVehicleMove(v);
 	DeleteVehicle(v);
 
-	if (!((t=v->u.rail.track) & 0xC0)) {
-		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(t));
-	}
+	if (!(v->u.rail.track & 0xC0))
+		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track));
 
 	/* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */
 	DisableTrainCrossing(v->tile);
 
-	if (v->u.rail.track == 0x40) {
+	if (v->u.rail.track == 0x40) { // inside a tunnel
 		int length;
 		TileIndex endtile = CheckTunnelBusy(v->tile, &length);
+
+		if (endtile == (uint)-1) // tunnel is busy (error returned)
+			return;
+
 		if ((v->direction == 1) || (v->direction == 5) )
-			SetSignalsOnBothDir(v->tile,0);
-			SetSignalsOnBothDir(endtile,0);
+			SetSignalsOnBothDir(v->tile, 0);
+			SetSignalsOnBothDir(endtile, 0);
 		if ((v->direction == 3) || (v->direction == 7) )
-			SetSignalsOnBothDir(v->tile,1);
-			SetSignalsOnBothDir(endtile,1);
+			SetSignalsOnBothDir(v->tile, 1);
+			SetSignalsOnBothDir(endtile, 1);
 	}
 }