Changeset - r2833:35ddad9f0bb4
[Not reviewed]
master
0 1 0
truelight - 19 years ago 2006-01-07 10:57:32
truelight@openttd.org
(svn r3381) -Fix: r3374 left one bug: allow moving around wagons in a 100 long train
1 file changed with 3 insertions and 1 deletions:
0 comments (0 inline, 0 general)
train_cmd.c
Show inline comments
 
@@ -960,97 +960,99 @@ int32 CmdMoveRailVehicle(int x, int y, u
 
			dst = GetPrevVehicleInChain(dst);
 
			/* Now if the vehicle we want to link to is the vehicle itself, drop out */
 
			if (dst == src) return CMD_ERROR;
 
			// if dst is NULL, it means that dst got a rear multiheaded engine as first engine. We can't use that
 
			if (dst == NULL) return CMD_ERROR;
 
		} else {
 
			/* there are more units on this train, so we will add the wagon after the next one*/
 
			dst = dst->next;
 
		}
 
	}
 

	
 
	if (IsTrainEngine(src) && dst_head != NULL) {
 
		/* we need to make sure that we didn't place it between a pair of multiheaded engines */
 
		Vehicle *u, *engine = NULL;
 

	
 
		for(u = dst_head; u != NULL; u = u->next) {
 
			if (IsTrainEngine(u) && IsMultiheaded(u) && u->u.rail.other_multiheaded_part != NULL) {
 
				engine = u;
 
			}
 
				if (engine != NULL && engine->u.rail.other_multiheaded_part == u) {
 
					engine = NULL;
 
				}
 
				if (u == dst) {
 
					if (engine != NULL) {
 
					dst = engine->u.rail.other_multiheaded_part;
 
					}
 
					break;
 
				}
 

	
 
		}
 
	}
 

	
 
	if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR);
 

	
 
	{
 
		int r, num = 0;
 

	
 
		r = CheckTrainStoppedInDepot(src_head);
 
		/* check if all vehicles in the source train are stopped inside a depot */
 
		if (r < 0) return CMD_ERROR;
 

	
 
		num += r;
 

	
 
		/* check if all the vehicles in the dest train are stopped */
 
		if (dst_head != NULL) {
 
			r = CheckTrainStoppedInDepot(dst_head);
 
			if (r < 0) return CMD_ERROR;
 

	
 
			num += r;
 
			/* If we move in the same vehicle, it is okay */
 
			if (dst_head != src_head)
 
				num += r;
 

	
 
			assert(dst_head->tile == src_head->tile);
 
		}
 

	
 
		/* Check that the length of the dest train is no longer than XXX vehicles */
 
		if (num > (_patches.mammoth_trains ? 100 : 9) && IsFrontEngine(dst_head))
 
			return_cmd_error(STR_8819_TRAIN_TOO_LONG);
 
	}
 

	
 
	// when moving all wagons, we can't have the same src_head and dst_head
 
	if (HASBIT(p2, 0) && src_head == dst_head) return 0;
 

	
 
	// moving a loco to a new line?, then we need to assign a unitnumber.
 
	if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) {
 
		UnitID unit_num = GetFreeUnitNumber(VEH_Train);
 
		if (unit_num > _patches.max_trains)
 
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
 

	
 
		if (flags & DC_EXEC)
 
			src->unitnumber = unit_num;
 
	}
 

	
 

	
 
	/* do it? */
 
	if (flags & DC_EXEC) {
 
		/* clear the ->first cache */
 
		{
 
			Vehicle *u;
 

	
 
			for (u = src_head; u != NULL; u = u->next) u->first = NULL;
 
			for (u = dst_head; u != NULL; u = u->next) u->first = NULL;
 
		}
 

	
 
		if (HASBIT(p2, 0)) {
 
			// unlink ALL wagons
 
			if (src != src_head) {
 
				Vehicle *v = src_head;
 
				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
 
				GetLastEnginePart(v)->next = NULL;
 
			} else {
 
				src_head = NULL;
 
			}
 
		} else {
 
			// if moving within the same chain, dont use dst_head as it may get invalidated
 
			if (src_head == dst_head)
 
				dst_head = NULL;
 
			// unlink single wagon from linked list
 
			src_head = UnlinkWagon(src, src_head);
0 comments (0 inline, 0 general)