implemented one-way tracks

This commit is contained in:
Stephan Richter
2020-09-20 17:49:54 +02:00
parent cd022ce4a3
commit bcfa6e79aa
14 changed files with 136 additions and 8 deletions

View File

@@ -5,4 +5,9 @@ public class Locomotive extends Car {
public Locomotive(String name) {
super(name);
}
public void setSpeed(int v) {
// TODO Auto-generated method stub
}
}

View File

@@ -82,23 +82,25 @@ public class Train {
public void setSpeed(int v) {
LOG.debug("Setting speed to {} kmh.",v);
for (Locomotive loco : locos) loco.setSpeed(v);
this.speed = v;
}
public String start() throws IOException {
if (block == null) return t("{} not in a block",this);
if (route != null) route.unlock().setSignals(Signal.STOP);
HashSet<Route> routes = block.routes();
Vector<Route> availableRoutes = new Vector<Route>();
for (Route route : routes) {
if (route.path().firstElement() != block) continue; // route does not start with current location of loco
if (direction != null && route.startDirection != direction) continue;
if (!route.free()) {
LOG.debug("{} is not free!",route);
for (Route rt : routes) {
if (rt == route) continue; // andere Route als zuvor wählen
if (rt.path().firstElement() != block) continue; // keine Route wählen, die nicht vom aktuellen Block des Zuges startet
if (direction != null && rt.startDirection != direction) continue; // keine Routen entgegen der Fahrtrichtung wählen
if (!rt.free()) { // keine belegten Routen wählen
LOG.debug("{} is not free!",rt);
continue;
}
availableRoutes.add(route);
availableRoutes.add(rt);
}
if (route != null) route.unlock().setSignals(Signal.STOP);
Random rand = new Random();
if (availableRoutes.isEmpty()) return t("No free routes from {}",block);
int sel = rand.nextInt(availableRoutes.size());