File diff r12108:f8d8164485bd → r12109:90df01928018
src/autoreplace_cmd.cpp
Show inline comments
 
@@ -92,18 +92,18 @@ static void TransferCargo(Vehicle *old_v
 
	assert(!part_of_chain || new_head->IsPrimaryVehicle());
 
	/* Loop through source parts */
 
	for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
 
		if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != ((Train *)old_veh)->other_multiheaded_part && !IsArticulatedPart(src)) {
 
		if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !IsArticulatedPart(src)) {
 
			/* Skip vehicles, which do not belong to old_veh */
 
			src = GetLastEnginePart((Train *)src);
 
			src = GetLastEnginePart(Train::From(src));
 
			continue;
 
		}
 
		if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
 

	
 
		/* Find free space in the new chain */
 
		for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
 
			if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != ((Train *)new_head)->other_multiheaded_part && !IsArticulatedPart(dest)) {
 
			if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !IsArticulatedPart(dest)) {
 
				/* Skip vehicles, which do not belong to new_head */
 
				dest = GetLastEnginePart((Train *)dest);
 
				dest = GetLastEnginePart(Train::From(dest));
 
				continue;
 
			}
 
			if (dest->cargo_type != src->cargo_type) continue;
 
@@ -116,7 +116,7 @@ static void TransferCargo(Vehicle *old_v
 
	}
 

	
 
	/* Update train weight etc., the old vehicle will be sold anyway */
 
	if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged((Train *)new_head, true);
 
	if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged(Train::From(new_head), true);
 
}
 

	
 
/**
 
@@ -269,7 +269,7 @@ static CommandCost BuildReplacementVehic
 
	}
 

	
 
	/* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */
 
	if (new_veh->type == VEH_TRAIN && HasBit(((Train *)old_veh)->flags, VRF_REVERSE_DIRECTION)) {
 
	if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
 
		DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
 
	}
 

	
 
@@ -403,10 +403,10 @@ static CommandCost ReplaceChain(Vehicle 
 

	
 
	if (old_head->type == VEH_TRAIN) {
 
		/* Store the length of the old vehicle chain, rounded up to whole tiles */
 
		uint16 old_total_length = (((Train *)old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE;
 
		uint16 old_total_length = (Train::From(old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE;
 

	
 
		int num_units = 0; ///< Number of units in the chain
 
		for (Train *w = (Train *)old_head; w != NULL; w = GetNextUnit(w)) num_units++;
 
		for (Train *w = Train::From(old_head); w != NULL; w = GetNextUnit(w)) num_units++;
 

	
 
		Train **old_vehs = CallocT<Train *>(num_units); ///< Will store vehicles of the old chain in their order
 
		Train **new_vehs = CallocT<Train *>(num_units); ///< New vehicles corresponding to old_vehs or NULL if no replacement
 
@@ -416,7 +416,7 @@ static CommandCost ReplaceChain(Vehicle 
 
		 * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */
 
		int i;
 
		Train *w;
 
		for (w = (Train *)old_head, i = 0; w != NULL; w = GetNextUnit(w), i++) {
 
		for (w = Train::From(old_head), i = 0; w != NULL; w = GetNextUnit(w), i++) {
 
			assert(i < num_units);
 
			old_vehs[i] = w;
 

	
 
@@ -432,7 +432,7 @@ static CommandCost ReplaceChain(Vehicle 
 
		/* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
 
		if (cost.Succeeded()) {
 
			/* Separate the head, so we can start constructing the new chain */
 
			Train *second = GetNextUnit((Train *)old_head);
 
			Train *second = GetNextUnit(Train::From(old_head));
 
			if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
 

	
 
			assert(GetNextUnit(new_head) == NULL);
 
@@ -538,10 +538,10 @@ static CommandCost ReplaceChain(Vehicle 
 
			 * Note: The vehicle attach callback is disabled here :) */
 
			if ((flags & DC_EXEC) == 0) {
 
				/* Separate the head, so we can reattach the old vehicles */
 
				Train *second = GetNextUnit((Train *)old_head);
 
				Train *second = GetNextUnit(Train::From(old_head));
 
				if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
 

	
 
				assert(GetNextUnit((Train *)old_head) == NULL);
 
				assert(GetNextUnit(Train::From(old_head)) == NULL);
 

	
 
				for (int i = num_units - 1; i > 0; i--) {
 
					CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
 
@@ -632,7 +632,7 @@ CommandCost CmdAutoreplaceVehicle(TileIn
 
	bool any_replacements = false;
 
	while (w != NULL && !any_replacements) {
 
		any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE);
 
		w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit((Train *)w) : NULL);
 
		w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit(Train::From(w)) : NULL);
 
	}
 

	
 
	if (any_replacements) {