src/train_cmd.cpp
changeset 9845 b1056419429f
parent 9840 ac0f57d075f5
child 9857 8d0e4ab99439
equal deleted inserted replaced
9844:705fc23bedba 9845:b1056419429f
  3434 		/* Remove all reservations, also the ones currently under the train
  3434 		/* Remove all reservations, also the ones currently under the train
  3435 		 * and any railway station paltform reservation. */
  3435 		 * and any railway station paltform reservation. */
  3436 		FreeTrainTrackReservation(v);
  3436 		FreeTrainTrackReservation(v);
  3437 		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
  3437 		for (const Vehicle *u = v; u != NULL; u = u->Next()) {
  3438 			ClearPathReservation(u->tile, GetVehicleTrackdir(u));
  3438 			ClearPathReservation(u->tile, GetVehicleTrackdir(u));
       
  3439 			if (IsTileType(u->tile, MP_TUNNELBRIDGE)) {
       
  3440 				/* ClearPathReservation will not free the wormhole exit
       
  3441 				 * if the train has just entered the wormhole. */
       
  3442 				SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(u->tile), false);
       
  3443 			}
  3439 		}
  3444 		}
  3440 	}
  3445 	}
  3441 
  3446 
  3442 	/* we may need to update crossing we were approaching */
  3447 	/* we may need to update crossing we were approaching */
  3443 	TileIndex crossing = TrainApproachingCrossingTile(v);
  3448 	TileIndex crossing = TrainApproachingCrossingTile(v);
  3509 
  3514 
  3510 		/* Try to reserve all tiles directly under the crashed trains.
  3515 		/* Try to reserve all tiles directly under the crashed trains.
  3511 		 * As there might be more than two trains involved, we have to do that for all vehicles */
  3516 		 * As there might be more than two trains involved, we have to do that for all vehicles */
  3512 		const Vehicle *u;
  3517 		const Vehicle *u;
  3513 		FOR_ALL_VEHICLES(u) {
  3518 		FOR_ALL_VEHICLES(u) {
  3514 			if (u->type == VEH_TRAIN && HASBITS(u->vehstatus, VS_CRASHED)) {
  3519 			if (u->type == VEH_TRAIN && HASBITS(u->vehstatus, VS_CRASHED) && (u->u.rail.track & TRACK_BIT_DEPOT) == TRACK_BIT_NONE) {
       
  3520 				TrackBits trackbits = u->u.rail.track;
       
  3521 				if ((trackbits & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
       
  3522 					/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
       
  3523 					trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(u->tile));
       
  3524 				}
  3515 				TryReserveRailTrack(u->tile, TrackBitsToTrack(u->u.rail.track));
  3525 				TryReserveRailTrack(u->tile, TrackBitsToTrack(u->u.rail.track));
  3516 			}
  3526 			}
  3517 		}
  3527 		}
  3518 	}
  3528 	}
  3519 
  3529 
  3841  */
  3851  */
  3842 static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
  3852 static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
  3843 {
  3853 {
  3844 	TrackBits *trackbits = (TrackBits *)data;
  3854 	TrackBits *trackbits = (TrackBits *)data;
  3845 
  3855 
  3846 	if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) *trackbits |= v->u.rail.track;
  3856 	if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) {
       
  3857 		if ((v->u.rail.track & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
       
  3858 			/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
       
  3859 			*trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile));
       
  3860 		} else {
       
  3861 			*trackbits |= v->u.rail.track;
       
  3862 		}
       
  3863 	}
  3847 
  3864 
  3848 	return NULL;
  3865 	return NULL;
  3849 }
  3866 }
  3850 
  3867 
  3851 /**
  3868 /**
  3884 
  3901 
  3885 	MarkSingleVehicleDirty(v);
  3902 	MarkSingleVehicleDirty(v);
  3886 
  3903 
  3887 	/* 'v' shouldn't be accessed after it has been deleted */
  3904 	/* 'v' shouldn't be accessed after it has been deleted */
  3888 	TrackBits trackbits = v->u.rail.track;
  3905 	TrackBits trackbits = v->u.rail.track;
  3889 	Track track = TrackBitsToTrack(trackbits);
       
  3890 	TileIndex tile = v->tile;
  3906 	TileIndex tile = v->tile;
  3891 	Owner owner = v->owner;
  3907 	Owner owner = v->owner;
  3892 
  3908 
  3893 	delete v;
  3909 	delete v;
  3894 	v = NULL; // make sure nobody will try to read 'v' anymore
  3910 	v = NULL; // make sure nobody will try to read 'v' anymore
  3895 
  3911 
       
  3912 	if ((trackbits & TRACK_BIT_WORMHOLE) == TRACK_BIT_WORMHOLE) {
       
  3913 		/* Vehicle is inside a wormhole, v->u.rail.track contains no useful value then. */
       
  3914 		trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(tile));
       
  3915 	}
       
  3916 
       
  3917 	Track track = TrackBitsToTrack(trackbits);
  3896 	if (HasReservedTracks(tile, trackbits)) {
  3918 	if (HasReservedTracks(tile, trackbits)) {
  3897 		UnreserveRailTrack(tile, track);
  3919 		UnreserveRailTrack(tile, track);
  3898 
  3920 
  3899 		/* If there are still crashed vehicles on the tile, give the track reservation to them */
  3921 		/* If there are still crashed vehicles on the tile, give the track reservation to them */
  3900 		TrackBits remaining_trackbits = TRACK_BIT_NONE;
  3922 		TrackBits remaining_trackbits = TRACK_BIT_NONE;