train_cmd.c
changeset 2607 08f42ae6fbfc
parent 2602 f0e2dcce3695
child 2608 f7f41be75b48
--- a/train_cmd.c	Sat Nov 05 19:58:16 2005 +0000
+++ b/train_cmd.c	Sun Nov 06 01:15:10 2005 +0000
@@ -953,6 +953,9 @@
 
 	/* do it? */
 	if (flags & DC_EXEC) {
+		Vehicle *new_front = GetNextVehicle(src);	//used if next in line should make a train on it's own
+		bool make_new_front = src->subtype == TS_Front_Engine;
+
 		if (HASBIT(p2, 0)) {
 			// unlink ALL wagons
 			if (src != src_head) {
@@ -1004,6 +1007,15 @@
 			dst->next = src;
 		}
 
+		/* If there is an engine behind first_engine we moved away, it should become new first_engine
+		 * To do this, CmdMoveRailVehicle must be called once more
+		 * since we set p2 to a condition that makes the statement false, we can't loop forever with this one */
+		if (make_new_front && new_front != NULL && !(HASBIT(p2, 0))) {
+			if (!(RailVehInfo(new_front->engine_type)->flags & RVI_WAGON)) {
+				CmdMoveRailVehicle(x, y, flags, new_front->index | (INVALID_VEHICLE << 16), 1);
+			}
+		}
+
 		if (src_head) {
 			TrainConsistChanged(src_head);
 			if (src_head->subtype == TS_Front_Engine) {