optimiced trace calculation
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.srsoftware</groupId>
|
<groupId>de.srsoftware</groupId>
|
||||||
<artifactId>web4rail</artifactId>
|
<artifactId>web4rail</artifactId>
|
||||||
<version>1.3.39</version>
|
<version>1.3.40</version>
|
||||||
<name>Web4Rail</name>
|
<name>Web4Rail</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<description>Java Model Railway Control</description>
|
<description>Java Model Railway Control</description>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.io.FileWriter;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
@@ -422,8 +423,8 @@ public class Route extends BaseClass {
|
|||||||
ActionList actions = triggeredActions.get(contact.trigger());
|
ActionList actions = triggeredActions.get(contact.trigger());
|
||||||
LOG.debug("Contact has id {} / trigger {} and is assigned with {}",contact.id(),contact.trigger(),isNull(actions)?t("nothing"):actions);
|
LOG.debug("Contact has id {} / trigger {} and is assigned with {}",contact.id(),contact.trigger(),isNull(actions)?t("nothing"):actions);
|
||||||
if (isNull(actions)) return;
|
if (isNull(actions)) return;
|
||||||
actions.fire(context);
|
|
||||||
traceTrainFrom(contact);
|
traceTrainFrom(contact);
|
||||||
|
actions.fire(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector<Contact> contacts() {
|
public Vector<Contact> contacts() {
|
||||||
@@ -1025,16 +1026,32 @@ public class Route extends BaseClass {
|
|||||||
return getClass().getSimpleName()+"("+(isSet(train)?train+":":"")+name()+")";
|
return getClass().getSimpleName()+"("+(isSet(train)?train+":":"")+name()+")";
|
||||||
}
|
}
|
||||||
|
|
||||||
private void traceTrainFrom(Tile tile) {
|
private void traceTrainFrom(Tile newHead) {
|
||||||
LOG.debug("{}.traceTrainFrom({})",this,tile);
|
LOG.debug("{}.traceTrainFrom({})",this,newHead);
|
||||||
if (isNull(train)) return;
|
if (isNull(train)) return;
|
||||||
Vector<Tile> trace = new Vector<Tile>();
|
if (newHead instanceof BlockContact) newHead = (Tile) ((BlockContact)newHead).parent();
|
||||||
if (tile instanceof BlockContact) tile = (Tile) ((BlockContact)tile).parent();
|
|
||||||
for (Tile t:path) {
|
Tile traceHead = train.traceHead();
|
||||||
trace.add(t);
|
Integer remainingLength = null;
|
||||||
if (t == tile) break;
|
LinkedList<Tile> newTrace = new LinkedList<Tile>();
|
||||||
}
|
for (int i=path.size(); i>0; i--) { // pfad rückwärts ablaufen
|
||||||
train.addToTrace(trace);
|
Tile tile = path.elementAt(i-1);
|
||||||
|
if (isNull(remainingLength)) {
|
||||||
|
if (tile == newHead) traceHead = newHead; // wenn wir zuerst newHead finden: newHead als neuen traceHead übernehmen
|
||||||
|
if (tile == traceHead) remainingLength = train.length(); // sobald wir auf den traceHead stoßen: suche beenden
|
||||||
|
}
|
||||||
|
if (isSet(remainingLength)) {
|
||||||
|
if (remainingLength>0) {
|
||||||
|
newTrace.add(tile);
|
||||||
|
remainingLength -= tile.length();
|
||||||
|
} else if (Route.freeBehindTrain) {
|
||||||
|
try {
|
||||||
|
tile.unset(this);
|
||||||
|
} catch (IllegalArgumentException e) {}
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
train.setTrace(newTrace);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Train train() {
|
public Train train() {
|
||||||
|
|||||||
@@ -188,25 +188,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
tags.add(tag);
|
tags.add(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addToTrace(Vector<Tile> newTiles) {
|
|
||||||
Route.LOG.debug("{}.addToTrace({})",this,newTiles);
|
|
||||||
Route.LOG.debug("old trace: {}",trace);
|
|
||||||
boolean active = trace.isEmpty();
|
|
||||||
for (Tile tile : newTiles) {
|
|
||||||
if (active) {
|
|
||||||
trace.addFirst(tile);
|
|
||||||
} else {
|
|
||||||
if (trace.getFirst() == tile) active = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!active) { // newTiles and old trace do not "touch" : add all new tiles
|
|
||||||
for (Tile tile : newTiles) trace.addFirst(tile);
|
|
||||||
}
|
|
||||||
Route.LOG.debug("new trace: {}",trace);
|
|
||||||
showTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Object addCar(HashMap<String, String> params) {
|
private Object addCar(HashMap<String, String> params) {
|
||||||
LOG.debug("addCar({})",params);
|
LOG.debug("addCar({})",params);
|
||||||
String carId = params.get(CAR_ID);
|
String carId = params.get(CAR_ID);
|
||||||
@@ -509,10 +490,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile headPos() {
|
|
||||||
return trace.getFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isShunting() {
|
public boolean isShunting() {
|
||||||
return shunting;
|
return shunting;
|
||||||
}
|
}
|
||||||
@@ -919,6 +896,22 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
cars.stream().filter(c -> c instanceof Locomotive).forEach(car -> ((Locomotive)car).setSpeed(speed));
|
cars.stream().filter(c -> c instanceof Locomotive).forEach(car -> ((Locomotive)car).setSpeed(speed));
|
||||||
plan.stream(t("Set {} to {} {}",this,speed,speedUnit));
|
plan.stream(t("Set {} to {} {}",this,speed,speedUnit));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setTrace(LinkedList<Tile> newTrace) {
|
||||||
|
LOG.debug("{}.setTrace({})",this,newTrace);
|
||||||
|
LOG.debug("old trace: {}",trace);
|
||||||
|
if (isSet(trace)) {
|
||||||
|
trace.removeAll(newTrace);
|
||||||
|
|
||||||
|
for (Tile tile : trace) {
|
||||||
|
tile.setTrain(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
trace = newTrace;
|
||||||
|
for (Tile tile : trace) tile.setTrain(this);
|
||||||
|
|
||||||
|
LOG.debug("new trace of {}: {}",this,trace);
|
||||||
|
}
|
||||||
|
|
||||||
public void setWaitTime(Range waitTime) {
|
public void setWaitTime(Range waitTime) {
|
||||||
if (isNull(autopilot)) return;
|
if (isNull(autopilot)) return;
|
||||||
@@ -926,29 +919,6 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
String msg = t("{} waiting {} secs...",this,autopilot.waitTime/1000d);
|
String msg = t("{} waiting {} secs...",this,autopilot.waitTime/1000d);
|
||||||
LOG.debug(msg);
|
LOG.debug(msg);
|
||||||
plan.stream(msg);
|
plan.stream(msg);
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showTrace() {
|
|
||||||
Route.LOG.debug("{}.showTrace()",this);
|
|
||||||
int remainingLength = length();
|
|
||||||
if (remainingLength<1) remainingLength=1;
|
|
||||||
for (int i=0; i<trace.size(); i++) {
|
|
||||||
Tile tile = trace.get(i);
|
|
||||||
Route.LOG.debug("current tile: {}, remaining length: {}",tile,remainingLength);
|
|
||||||
if (remainingLength>0) {
|
|
||||||
remainingLength-=tile.length();
|
|
||||||
tile.setTrain(this);
|
|
||||||
} else {
|
|
||||||
tile.setTrain(null);
|
|
||||||
if (Route.freeBehindTrain) try {
|
|
||||||
tile.unset(route);
|
|
||||||
} catch (IllegalArgumentException e) {}
|
|
||||||
trace.remove(i);
|
|
||||||
i--; // do not move to next index: remove shifted the next index towards us
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Route.LOG.debug("remaining length: {}",remainingLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tag slower(int steps) {
|
private Tag slower(int steps) {
|
||||||
@@ -1015,6 +985,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
startSimulation();
|
startSimulation();
|
||||||
|
|
||||||
plan.stream(t("Started {}",this));
|
plan.stream(t("Started {}",this));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -1096,6 +1067,10 @@ public class Train extends BaseClass implements Comparable<Train> {
|
|||||||
return name();
|
return name();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tile traceHead() {
|
||||||
|
return trace == null || trace.isEmpty() ? null : trace.getFirst();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this inverts the direction the train is heading to. Example:
|
* this inverts the direction the train is heading to. Example:
|
||||||
* before: CabCar→ MiddleCar→ Loco→
|
* before: CabCar→ MiddleCar→ Loco→
|
||||||
|
|||||||
Reference in New Issue
Block a user