implemented pre-reservation of routes when autopilot is active
This commit is contained in:
@@ -13,15 +13,16 @@ import de.srsoftware.web4rail.Command;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
import de.srsoftware.web4rail.tags.Table;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
|
||||
public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
@@ -76,16 +77,18 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
public static Tag cockpit(Object locoOrTrain) {
|
||||
String realm = null;
|
||||
int id = 0;
|
||||
int speed = 0;
|
||||
String realm = null;
|
||||
Train train = null;
|
||||
Locomotive loco = null;
|
||||
if (locoOrTrain instanceof Locomotive) {
|
||||
Locomotive loco = (Locomotive) locoOrTrain;
|
||||
loco = (Locomotive) locoOrTrain;
|
||||
realm = REALM_LOCO;
|
||||
id = loco.id();
|
||||
speed = loco.speed;
|
||||
} else if (locoOrTrain instanceof Train) {
|
||||
Train train = (Train)locoOrTrain;
|
||||
train = (Train)locoOrTrain;
|
||||
realm = REALM_TRAIN;
|
||||
id = train.id;
|
||||
speed = train.speed;
|
||||
@@ -95,7 +98,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
Fieldset fieldset = new Fieldset(t("Control"));
|
||||
|
||||
new Tag("span").content(t("Current velocity: {} {}",speed)).addTo(fieldset);
|
||||
new Tag("span").content(t("Current velocity: {} {}",speed,speedUnit)).addTo(fieldset);
|
||||
|
||||
Tag par = new Tag("p");
|
||||
Map.of("Slower (10 steps)",ACTION_SLOWER10,"Faster (10 steps)",ACTION_FASTER10).entrySet().forEach(e -> {
|
||||
@@ -105,10 +108,30 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
par.addTo(fieldset);
|
||||
|
||||
Tag direction = new Tag("p");
|
||||
Map.of("Turn",ACTION_TURN,"Stop",ACTION_STOP).entrySet().forEach(e -> {
|
||||
params.put(ACTION, e.getValue());
|
||||
new Button(t(e.getKey()),params).clazz(e.getValue()).addTo(direction);
|
||||
});
|
||||
if ((isSet(train) && train.speed > 0) || (isSet(loco) && loco.speed > 0)) {
|
||||
params.put(ACTION, ACTION_STOP);
|
||||
new Button(t("Stop"),params).clazz(ACTION_STOP).addTo(direction);
|
||||
}
|
||||
|
||||
params.put(ACTION, ACTION_TURN);
|
||||
new Button(t("Turn"),params).clazz(ACTION_TURN).addTo(direction);
|
||||
|
||||
if (isSet(train)) {
|
||||
Block currentBlock = train.currentBlock();
|
||||
if (isSet(currentBlock)) {
|
||||
if (isNull(train.route)) {
|
||||
params.put(ACTION, ACTION_START);
|
||||
new Button(t("start"),params).addTo(direction);
|
||||
}
|
||||
if (train.usesAutopilot()) {
|
||||
params.put(ACTION, ACTION_QUIT);
|
||||
new Button(t("quit autopilot"),params).addTo(direction);
|
||||
} else {
|
||||
params.put(ACTION, ACTION_AUTO);
|
||||
new Button(t("auto"),params).addTo(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
direction.addTo(fieldset);
|
||||
|
||||
Tag functions = new Tag("p");
|
||||
@@ -117,6 +140,8 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
new Button(t(e.getKey()),params).addTo(functions);
|
||||
});
|
||||
functions.addTo(fieldset);
|
||||
|
||||
|
||||
return fieldset.clazz("cockpit");
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private Block currentBlock,destination = null;
|
||||
LinkedList<Tile> trace = new LinkedList<Tile>();
|
||||
|
||||
private String brakeId = null;
|
||||
|
||||
private class Autopilot extends Thread{
|
||||
boolean stop = false;
|
||||
@@ -108,6 +110,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
private Autopilot autopilot = null;
|
||||
|
||||
private Plan plan;
|
||||
|
||||
private Route nextRoute;
|
||||
|
||||
public Train(Locomotive loco) {
|
||||
this(loco,null);
|
||||
@@ -147,7 +151,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
case ACTION_MOVE:
|
||||
return train.setDestination(params);
|
||||
case ACTION_PROPS:
|
||||
return train.props();
|
||||
return train.properties();
|
||||
case ACTION_QUIT:
|
||||
return train.quitAutopilot();
|
||||
case ACTION_SLOWER10:
|
||||
@@ -182,11 +186,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!");
|
||||
Car car = Car.get(params.get(CAR_ID));
|
||||
if (isNull(car)) return t("No car with id \"{}\" known!",params.get(CAR_ID));
|
||||
if (car instanceof Locomotive) {
|
||||
locos.add((Locomotive) car);
|
||||
} else cars.add(car);
|
||||
car.train(this);
|
||||
return props();
|
||||
add(car);
|
||||
return properties();
|
||||
}
|
||||
|
||||
public void add(Car car) {
|
||||
@@ -194,6 +195,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
if (car instanceof Locomotive) {
|
||||
locos.add((Locomotive) car);
|
||||
} else cars.add(car);
|
||||
brakeId = null;
|
||||
car.train(this);
|
||||
}
|
||||
|
||||
@@ -205,6 +207,17 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return t("{} now in auto-mode",this);
|
||||
}
|
||||
|
||||
public String brakeId() {
|
||||
if (isNull(brakeId)) {
|
||||
TreeSet<Integer> carIds = new TreeSet<Integer>();
|
||||
locos.stream().map(loco -> loco.id()).forEach(carIds::add);
|
||||
cars.stream().map(car -> car.id()).forEach(carIds::add);
|
||||
brakeId = md5sum(carIds);
|
||||
LOG.debug("generated new brake id for {}: {}",brakeId,this);
|
||||
}
|
||||
return brakeId;
|
||||
}
|
||||
|
||||
private Tag carList() {
|
||||
Tag locoProp = new Tag("li").content(t("Cars:"));
|
||||
Tag locoList = new Tag("ul").clazz("carlist").content("");
|
||||
@@ -291,10 +304,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
Locomotive loco = Locomotive.get(params.get(LOCO_ID));
|
||||
if (isSet(loco)) {
|
||||
locos.remove(loco);
|
||||
locos.remove(loco);
|
||||
loco.train(null);
|
||||
}
|
||||
return props();
|
||||
brakeId = null;
|
||||
return properties();
|
||||
}
|
||||
|
||||
public void dropTrace() {
|
||||
@@ -303,7 +317,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private Tag faster(int steps) {
|
||||
setSpeed(speed+steps);
|
||||
return props();
|
||||
return properties();
|
||||
}
|
||||
|
||||
public static Train get(int id) {
|
||||
@@ -502,7 +516,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Tag props() {
|
||||
public Tag properties() {
|
||||
Window window = new Window("train-properties",t("Properties of {}",this));
|
||||
|
||||
Locomotive.cockpit(this).addTo(window);
|
||||
@@ -595,6 +609,22 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
trace.remove(tile);
|
||||
}
|
||||
|
||||
public void reserveNext() {
|
||||
Context context = new Context(null, route, this, route.endBlock(), route.endDirection);
|
||||
Route nextRoute = PathFinder.chooseRoute(context);
|
||||
|
||||
boolean error = !nextRoute.lockIgnoring(route);
|
||||
error = error || !nextRoute.setTurnouts();
|
||||
error = error || !route.fireSetupActions(context);
|
||||
|
||||
if (error) {
|
||||
nextRoute.reset(); // may unlock tiles belonging to the current route.
|
||||
route.lock(); // corrects unlocked tiles of nextRoute
|
||||
} else {
|
||||
this.nextRoute = nextRoute;
|
||||
}
|
||||
}
|
||||
|
||||
private void reverseTrace() {
|
||||
LinkedList<Tile> reversed = new LinkedList<Tile>();
|
||||
LOG.debug("Trace: {}",trace);
|
||||
@@ -680,23 +710,28 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private Tag slower(int steps) {
|
||||
setSpeed(speed-steps);
|
||||
return props();
|
||||
return properties();
|
||||
}
|
||||
|
||||
public String start() throws IOException {
|
||||
if (isNull(currentBlock)) return t("{} not in a block",this);
|
||||
if (isSet(route)) route.reset(); // reset route previously chosen
|
||||
|
||||
|
||||
Context context = new Context(this);
|
||||
route = PathFinder.chooseRoute(context);
|
||||
if (isNull(route)) return t("No free routes from {}",currentBlock);
|
||||
if (!route.lock()) return t("Was not able to lock {}",route);
|
||||
|
||||
String error = null;
|
||||
if (isSet(nextRoute)) {
|
||||
route = nextRoute;
|
||||
if (!route.lock()) return t("Was not able to lock {}",route);
|
||||
nextRoute = null;
|
||||
} else {
|
||||
route = PathFinder.chooseRoute(context);
|
||||
if (isNull(route)) return t("No free routes from {}",currentBlock);
|
||||
if (!route.lock()) return t("Was not able to lock {}",route);
|
||||
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!");
|
||||
if (isNull(error) && !route.fireSetupActions(context)) error = t("Was not able to fire all setup actions of route!");
|
||||
}
|
||||
if (direction != route.startDirection) turn();
|
||||
|
||||
String error = null;
|
||||
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!");
|
||||
if (isNull(error) && !route.fireSetupActions(context)) error = t("Was not able to fire all setup actions of route!");
|
||||
if (isNull(error) && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route);
|
||||
if (isSet(error)) {
|
||||
route.reset();
|
||||
@@ -746,11 +781,16 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
public Object stopNow() {
|
||||
quitAutopilot();
|
||||
setSpeed(0);
|
||||
if (isSet(nextRoute)) {
|
||||
nextRoute.reset();
|
||||
nextRoute = null;
|
||||
}
|
||||
if (isSet(route)) {
|
||||
route.reset();
|
||||
route = null;
|
||||
}
|
||||
return props();
|
||||
|
||||
return properties();
|
||||
}
|
||||
|
||||
private static String t(String message, Object...fills) {
|
||||
@@ -778,7 +818,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
reverseTrace();
|
||||
if (isSet(currentBlock)) plan.place(currentBlock);
|
||||
}
|
||||
return props();
|
||||
return properties();
|
||||
}
|
||||
|
||||
public Train update(HashMap<String, String> params) {
|
||||
|
||||
Reference in New Issue
Block a user