From 44f6fe292e46e114f69641de2e0702e2a0d8e9a8 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 29 Dec 2020 13:33:35 +0100 Subject: [PATCH] improved route planner --- pom.xml | 2 +- .../de/srsoftware/web4rail/PathFinder.java | 31 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index ef7e8df..a1322f9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 de.srsoftware web4rail - 1.2.62 + 1.2.63 Web4Rail jar Java Model Railway Control diff --git a/src/main/java/de/srsoftware/web4rail/PathFinder.java b/src/main/java/de/srsoftware/web4rail/PathFinder.java index 87f6acf..1baf484 100644 --- a/src/main/java/de/srsoftware/web4rail/PathFinder.java +++ b/src/main/java/de/srsoftware/web4rail/PathFinder.java @@ -53,8 +53,14 @@ public class PathFinder extends BaseClass{ LOG.debug("{}→ Candidate {} would create loop, skipping",inset,routeCandidate.shortName()); continue; } - if (!routeCandidate.allowed(context)) continue; // Zug darf auf Grund einer nicht erfüllten Bedingung nicht auf die Route - if (!routeCandidate.isFreeFor(context.route(routeCandidate))) continue; // Route ist nicht frei + if (!routeCandidate.allowed(context)) { + if (routeCandidate.endBlock() != destination) { // allowance may be overridden by destination + LOG.debug("{} not allowed for {}",routeCandidate,context); + continue; // Zug darf auf Grund einer nicht erfüllten Bedingung nicht auf die Route + } + LOG.debug("{} not allowed for {} – overridden by selected destination",routeCandidate,context); + } + int priority = 0; if (isSet(direction) && routeCandidate.startDirection != direction) { // Route startet entgegen der aktuellen Fahrtrichtung des Zuges if (!train.pushPull) continue; // Zug kann nicht wenden @@ -102,12 +108,21 @@ public class PathFinder extends BaseClass{ public static Route chooseRoute(Context context) { LOG.debug("PathFinder.chooseRoute({})",context); TreeMap> availableRoutes = PathFinder.availableRoutes(context,new HashSet()); - if (availableRoutes.isEmpty()) return null; - Entry> entry = availableRoutes.lastEntry(); - List preferredRoutes = entry.getValue(); - Route selectetRoute = preferredRoutes.get(random.nextInt(preferredRoutes.size())); - LOG.debug("Chose \"{}\" with priority {}.",selectetRoute,entry.getKey()); + while (!availableRoutes.isEmpty()) { + LOG.debug("availableRoutes: {}",availableRoutes); + Entry> entry = availableRoutes.lastEntry(); + List preferredRoutes = entry.getValue(); + LOG.debug("preferredRoutes: {}",preferredRoutes); + Route selectedRoute = preferredRoutes.get(random.nextInt(preferredRoutes.size())); + if (selectedRoute.isFreeFor(context.route(selectedRoute))) { + LOG.debug("Chose \"{}\" with priority {}.",selectedRoute,entry.getKey()); + return selectedRoute; + } - return selectetRoute; + LOG.debug("Selected route \"{}\" is not free for {}",selectedRoute,context); + preferredRoutes.remove(selectedRoute); + if (preferredRoutes.isEmpty()) availableRoutes.remove(availableRoutes.lastKey()); + } + return null; } }