Browse Source

optimiced trace calculation

lookup-tables
Stephan Richter 5 years ago
parent
commit
c515e922dc
  1. 2
      pom.xml
  2. 37
      src/main/java/de/srsoftware/web4rail/Route.java
  3. 67
      src/main/java/de/srsoftware/web4rail/moving/Train.java

2
pom.xml

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>1.3.39</version>
<version>1.3.40</version>
<name>Web4Rail</name>
<packaging>jar</packaging>
<description>Java Model Railway Control</description>

37
src/main/java/de/srsoftware/web4rail/Route.java

@ -6,6 +6,7 @@ import java.io.FileWriter; @@ -6,6 +6,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -422,8 +423,8 @@ public class Route extends BaseClass { @@ -422,8 +423,8 @@ public class Route extends BaseClass {
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);
if (isNull(actions)) return;
actions.fire(context);
traceTrainFrom(contact);
actions.fire(context);
}
public Vector<Contact> contacts() {
@ -1025,16 +1026,32 @@ public class Route extends BaseClass { @@ -1025,16 +1026,32 @@ public class Route extends BaseClass {
return getClass().getSimpleName()+"("+(isSet(train)?train+":":"")+name()+")";
}
private void traceTrainFrom(Tile tile) {
LOG.debug("{}.traceTrainFrom({})",this,tile);
private void traceTrainFrom(Tile newHead) {
LOG.debug("{}.traceTrainFrom({})",this,newHead);
if (isNull(train)) return;
Vector<Tile> trace = new Vector<Tile>();
if (tile instanceof BlockContact) tile = (Tile) ((BlockContact)tile).parent();
for (Tile t:path) {
trace.add(t);
if (t == tile) break;
}
train.addToTrace(trace);
if (newHead instanceof BlockContact) newHead = (Tile) ((BlockContact)newHead).parent();
Tile traceHead = train.traceHead();
Integer remainingLength = null;
LinkedList<Tile> newTrace = new LinkedList<Tile>();
for (int i=path.size(); i>0; i--) { // pfad rückwärts ablaufen
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() {

67
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -188,25 +188,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -188,25 +188,6 @@ public class Train extends BaseClass implements Comparable<Train> {
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) {
LOG.debug("addCar({})",params);
String carId = params.get(CAR_ID);
@ -509,10 +490,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -509,10 +490,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return this;
}
public Tile headPos() {
return trace.getFirst();
}
public boolean isShunting() {
return shunting;
}
@ -919,6 +896,22 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -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));
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) {
if (isNull(autopilot)) return;
@ -926,29 +919,6 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -926,29 +919,6 @@ public class Train extends BaseClass implements Comparable<Train> {
String msg = t("{} waiting {} secs...",this,autopilot.waitTime/1000d);
LOG.debug(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) {
@ -1015,6 +985,7 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -1015,6 +985,7 @@ public class Train extends BaseClass implements Comparable<Train> {
return error;
}
startSimulation();
plan.stream(t("Started {}",this));
return this;
}
@ -1096,6 +1067,10 @@ public class Train extends BaseClass implements Comparable<Train> { @@ -1096,6 +1067,10 @@ public class Train extends BaseClass implements Comparable<Train> {
return name();
}
public Tile traceHead() {
return trace == null || trace.isEmpty() ? null : trace.getFirst();
}
/**
* this inverts the direction the train is heading to. Example:
* before: CabCar MiddleCar Loco

Loading…
Cancel
Save