train_cmd.c
changeset 2883 90f7db6e078e
parent 2874 3dcc067a7946
child 2916 b687477adcba
equal deleted inserted replaced
2882:126cf95f7446 2883:90f7db6e078e
   992 		}
   992 		}
   993 	}
   993 	}
   994 
   994 
   995 	if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
   995 	if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
   996 
   996 
   997 	{
       
   998 		int r, num = 0;
       
   999 
       
  1000 		r = CheckTrainStoppedInDepot(src_head);
       
  1001 		/* check if all vehicles in the source train are stopped inside a depot */
       
  1002 		if (r < 0) return CMD_ERROR;
       
  1003 
       
  1004 		if (HASBIT(p2, 0)) {
       
  1005 			/* If moving the rest of the train, exclude wagons before the
       
  1006 			 * selected one. */
       
  1007 
       
  1008 			Vehicle *u;
       
  1009 			for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u))
       
  1010 				r--;
       
  1011 
       
  1012 			num += r;
       
  1013 		} else {
       
  1014 			// If moving only one vehicle, just count that.
       
  1015 			num++;
       
  1016 		}
       
  1017 
       
  1018 		/* check if all the vehicles in the dest train are stopped */
       
  1019 		if (dst_head != NULL) {
       
  1020 			r = CheckTrainStoppedInDepot(dst_head);
       
  1021 			if (r < 0) return CMD_ERROR;
       
  1022 
       
  1023 			/* If we move in the same vehicle, it is okay */
       
  1024 			if (dst_head != src_head)
       
  1025 				num += r;
       
  1026 
       
  1027 			assert(dst_head->tile == src_head->tile);
       
  1028 		}
       
  1029 
       
  1030 		/* Check that the length of the dest train is no longer than XXX vehicles */
       
  1031 		if (num > (_patches.mammoth_trains ? 100 : 9) && IsFrontEngine(dst_head))
       
  1032 			return_cmd_error(STR_8819_TRAIN_TOO_LONG);
       
  1033 	}
       
  1034 
       
  1035 	// when moving all wagons, we can't have the same src_head and dst_head
   997 	// when moving all wagons, we can't have the same src_head and dst_head
  1036 	if (HASBIT(p2, 0) && src_head == dst_head) return 0;
   998 	if (HASBIT(p2, 0) && src_head == dst_head) return 0;
       
   999 
       
  1000 	{
       
  1001 		int src_len = 0;
       
  1002 		int max_len = _patches.mammoth_trains ? 100 : 9;
       
  1003 
       
  1004 		// check if all vehicles in the source train are stopped inside a depot.
       
  1005 		src_len = CheckTrainStoppedInDepot(src_head);
       
  1006 		if (src_len < 0) return CMD_ERROR;
       
  1007 
       
  1008 		// check the destination row if the source and destination aren't the same.
       
  1009 		if (src_head != dst_head) {
       
  1010 			int dst_len = 0;
       
  1011 
       
  1012 			if (dst_head != NULL) {
       
  1013 				// check if all vehicles in the dest train are stopped.
       
  1014 				dst_len = CheckTrainStoppedInDepot(dst_head);
       
  1015 				if (dst_len < 0) return CMD_ERROR;
       
  1016 
       
  1017 				assert(dst_head->tile == src_head->tile);
       
  1018 			}
       
  1019 
       
  1020 			// We are moving between rows, so only count the wagons from the source
       
  1021 			// row that are being moved.
       
  1022 			if (HASBIT(p2, 0)) {
       
  1023 				const Vehicle *u;
       
  1024 				for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u))
       
  1025 					src_len--;
       
  1026 			} else {
       
  1027 				// If moving only one vehicle, just count that.
       
  1028 				src_len = 1;
       
  1029 			}
       
  1030 
       
  1031 			if (src_len + dst_len > max_len) {
       
  1032 				// Abort if we're adding too many wagons to a train.
       
  1033 				if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
       
  1034 				// Abort if we're making a train on a new row.
       
  1035 				if (dst_head == NULL && IsTrainEngine(src)) return_cmd_error(STR_8819_TRAIN_TOO_LONG);
       
  1036 			}
       
  1037 		} else {
       
  1038 			// Abort if we're creating a new train on an existing row.
       
  1039 			if (src_len > max_len && src == src_head && IsTrainEngine(GetNextVehicle(src_head)))
       
  1040 				return_cmd_error(STR_8819_TRAIN_TOO_LONG);
       
  1041 		}
       
  1042 	}
  1037 
  1043 
  1038 	// moving a loco to a new line?, then we need to assign a unitnumber.
  1044 	// moving a loco to a new line?, then we need to assign a unitnumber.
  1039 	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
  1045 	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
  1040 		UnitID unit_num = GetFreeUnitNumber(VEH_Train);
  1046 		UnitID unit_num = GetFreeUnitNumber(VEH_Train);
  1041 		if (unit_num > _patches.max_trains)
  1047 		if (unit_num > _patches.max_trains)