Correctly crash touching trains

This commit is contained in:
Arne Keller 2020-02-19 10:14:02 +01:00
parent 43ee608975
commit 2e336a9bcb
4 changed files with 23 additions and 3 deletions

View File

@ -479,7 +479,7 @@ public class ModelRailwaySimulation {
} }
boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains)
|| occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains);
if (anyIntersection) { if (anyIntersection || train.touches(otherTrain)) {
train.removeFromRails(); train.removeFromRails();
otherTrain.removeFromRails(); otherTrain.removeFromRails();
// try to find/merge existing collisions // try to find/merge existing collisions

View File

@ -357,7 +357,7 @@ public final class Train {
* @return whether this train touches the other train * @return whether this train touches the other train
*/ */
public boolean touches(final Train other) { public boolean touches(final Train other) {
return other.isOnAnyPosition(position); return position != null && other.isOnAnyPosition(position);
} }
/** /**
@ -373,7 +373,7 @@ public final class Train {
* @return whether this train is on any of the specified positions * @return whether this train is on any of the specified positions
*/ */
public boolean isOnAnyPosition(final List<Vector2D> positions) { public boolean isOnAnyPosition(final List<Vector2D> positions) {
return position.stream().anyMatch(positions::contains); return position != null && position.stream().anyMatch(positions::contains);
} }
/** /**

View File

@ -170,3 +170,13 @@ create engine steam T5 Worsch 9 true true
add train 8 T5-Worsch add train 8 T5-Worsch
put train 8 at (0,0) in direction 0,-1 put train 8 at (0,0) in direction 0,-1
create engine electrical T5 Kühnlein 17 true true create engine electrical T5 Kühnlein 17 true true
add train 9 T5-Kühnlein
delete track 1
delete track 2
delete track 3
delete track 4
add track (-100,0) -> (0,0)
add track (0,0) -> (100,0)
put train 8 at (1,0) in direction -1,0
put train 9 at (-1,0) in direction 1,0
step 1

View File

@ -194,3 +194,13 @@ T5-Worsch
steam engine T5-Worsch added to train 8 steam engine T5-Worsch added to train 8
Error, could not place train Error, could not place train
T5-Kühnlein T5-Kühnlein
electrical engine T5-Kühnlein added to train 9
OK
OK
OK
OK
1
2
OK
OK
Crash of train 8,9