working on route reservation

This commit is contained in:
Stephan Richter
2020-09-19 00:38:18 +02:00
parent cf4df1882e
commit 917ac5dd07
8 changed files with 131 additions and 40 deletions

View File

@@ -1,16 +1,23 @@
package de.srsoftware.web4rail.moving;
import java.util.HashSet;
import java.util.Random;
import java.util.Vector;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Application;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.Tile;
public class Train {
private Vector<Locomotive> locos = new Vector<Locomotive>();
private Vector<Car> cars = new Vector<Car>();
private String name = null;
private Block block = null;
private Route route;
public Train(Locomotive loco) {
add(loco);
@@ -43,4 +50,25 @@ public class Train {
private String t(String message, Object...fills) {
return Translation.get(Application.class, message, fills);
}
public void block(Block block) {
this.block = block;
}
public String start() {
if (block == null) return t("{] not in a block",this);
HashSet<Route> routes = block.routes();
Vector<Route> availableRoutes = new Vector<Route>();
for (Route route : routes) {
Vector<Tile> path = route.path();
if (path.firstElement() != block) continue;
if (route.inUse()) continue;
availableRoutes.add(route);
}
Random rand = new Random();
if (route != null) route.unlock();
int sel = rand.nextInt(availableRoutes.size());
route = availableRoutes.get(sel).lock(this);
return t("started {}",this);
}
}