Correctly handle placing already placed trains and correctly handle small trains on larger rails

This commit is contained in:
Arne Keller 2020-02-17 09:43:54 +01:00
parent 9191028934
commit 2746b9e7cf
2 changed files with 3 additions and 3 deletions

View File

@ -320,7 +320,7 @@ public class ModelRailwaySimulation {
*/ */
public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction) { public boolean putTrain(final int trainId, final Vector2D position, final Vector2D direction) {
Train train = getTrain(trainId); Train train = getTrain(trainId);
if (train == null || !train.isProperTrain()) { if (train == null || !train.isProperTrain() || train.isPlaced()) {
return false; return false;
} }

View File

@ -290,7 +290,7 @@ public final class Train {
// we are really leaving this rail // we are really leaving this rail
occupiedRails.remove(leavingRail); occupiedRails.remove(leavingRail);
} // else: we are still on the rail } // else: we are still on the rail
} else if (getLength() == 1) { } else if (getLength() == 1 && railNetwork.findContainingRail(secondToLastPosition) == null) {
// we evidently just moved into another rail // we evidently just moved into another rail
occupiedRails.clear(); occupiedRails.clear();
} // else: only touched rail } // else: only touched rail
@ -298,7 +298,7 @@ public final class Train {
Rail nextRail = railNetwork.findContainingRail(nextPosition); Rail nextRail = railNetwork.findContainingRail(nextPosition);
if (nextRail != null) { if (nextRail != null) {
occupiedRails.add(nextRail); occupiedRails.add(nextRail);
} else if (getLength() == 1) { } else if (getLength() == 1 && railNetwork.findContainingRail(secondToLastPosition) == null) {
// we evidently just moved into another rail // we evidently just moved into another rail
Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition); Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition);
Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition); Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition);