Train collision checking fixes

This commit is contained in:
Arne Keller 2020-02-15 15:24:31 +01:00
parent aa547d3568
commit 16eae42fa3

View File

@ -518,24 +518,6 @@ public class ModelRailwaySimulation {
private ArrayList<Set<Train>> getCollisionsOfOneStep() { private ArrayList<Set<Train>> getCollisionsOfOneStep() {
ArrayList<Set<Train>> collisions = new ArrayList<>(); ArrayList<Set<Train>> collisions = new ArrayList<>();
// check touching trains
/*
for (Train train : trains) {
if (!train.isPlaced()) {
continue;
}
for (Train otherTrain : trains) {
if (!otherTrain.isPlaced() || train == otherTrain) {
continue;
}
if (train.getFrontPosition().equals(otherTrain.getFrontPosition())) {
train.removeFromRails();
otherTrain.removeFromRails();
collisions.add(Arrays.asList(train, otherTrain));
}
}
}
*/
Map<Train, Set<Rail>> occupiedRails = new HashMap<>(); Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
for (Train train : trains) { for (Train train : trains) {
if (train.isPlaced()) { if (train.isPlaced()) {
@ -570,9 +552,12 @@ public class ModelRailwaySimulation {
continue; // already included in collision continue; // already included in collision
} }
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train); Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train);
trains.stream().filter((x) -> x != train && x.isPlaced()).forEach((otherTrain) -> { trains.stream().filter((x) -> x != train).forEach((otherTrain) -> {
Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain);
Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain);
if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) {
return;
}
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) {
@ -589,6 +574,9 @@ public class ModelRailwaySimulation {
} }
} }
} }
if (existingCollision == null) {
collisions.add(new HashSet<>(Arrays.asList(train, otherTrain)));
}
} }
}); });
} }
@ -597,24 +585,6 @@ public class ModelRailwaySimulation {
private ArrayList<Set<Train>> getCollisionsOfOneReverseStep() { private ArrayList<Set<Train>> getCollisionsOfOneReverseStep() {
ArrayList<Set<Train>> collisions = new ArrayList<>(); ArrayList<Set<Train>> collisions = new ArrayList<>();
// check touching trains
/*
for (Train train : trains) {
if (!train.isPlaced()) {
continue;
}
for (Train otherTrain : trains) {
if (!otherTrain.isPlaced() || train == otherTrain) {
continue;
}
if (train.getBackPosition().equals(otherTrain.getBackPosition())) {
train.removeFromRails();
otherTrain.removeFromRails();
collisions.add(new HashSet<>(Arrays.asList(train, otherTrain)));
}
}
}
*/
Map<Train, Set<Rail>> occupiedRails = new HashMap<>(); Map<Train, Set<Rail>> occupiedRails = new HashMap<>();
for (Train train : trains) { for (Train train : trains) {
if (train.isPlaced()) { if (train.isPlaced()) {
@ -649,9 +619,12 @@ public class ModelRailwaySimulation {
continue; // already included in collision continue; // already included in collision
} }
Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train); Set<Rail> occupiedByThisTrain = nextOccupiedRails.get(train);
trains.stream().filter((x) -> x != train && train.isPlaced()).forEach((otherTrain) -> { trains.stream().filter((x) -> x != train).forEach((otherTrain) -> {
Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); Set<Rail> occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain);
Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); Set<Rail> occupiedByOtherTrain = nextOccupiedRails.get(otherTrain);
if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) {
return;
}
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) {