various bugfixes

This commit is contained in:
Stephan Richter
2020-11-06 09:50:16 +01:00
parent 06a80a2a8d
commit ad8375e7f4
4 changed files with 60 additions and 52 deletions

View File

@@ -152,6 +152,19 @@ public class Train extends BaseClass implements Comparable<Train> {
}
return t("Unknown action: {}",params.get(ACTION));
}
public void addToTrace(Vector<Tile> newTiles) {
boolean active = trace.isEmpty();
for (Tile tile : newTiles) {
if (active) {
trace.addFirst(tile);
} else {
Tile dummy = trace.getFirst();
if (dummy == tile) active = true;
}
}
showTrace();
}
private Route chooseRoute(Context context) {
HashSet<Route> routes = block.routes();
@@ -270,10 +283,13 @@ public class Train extends BaseClass implements Comparable<Train> {
return props();
}
public void dropTrace() {
while (!trace.isEmpty()) trace.removeFirst().set(null);
}
public static Train get(int id) {
return trains.get(id);
}
public Train heading(Direction dir) {
direction = dir;
@@ -281,6 +297,10 @@ public class Train extends BaseClass implements Comparable<Train> {
return this;
}
public Tile headPos() {
return trace.getFirst();
}
private JSONObject json() {
JSONObject json = new JSONObject();
json.put(ID, id);
@@ -480,13 +500,6 @@ public class Train extends BaseClass implements Comparable<Train> {
return window;
}
public SortedSet<String> tags() {
TreeSet<String> list = new TreeSet<String>(tags);
for (Locomotive loco : locos) list.addAll(loco.tags());
for (Car car:cars) list.addAll(car.tags());
return list;
}
public Object quitAutopilot() {
if (isSet(autopilot)) {
autopilot.stop = true;
@@ -495,6 +508,10 @@ public class Train extends BaseClass implements Comparable<Train> {
} else return t("autopilot not active.");
}
private void reverseTrace() {
// TODO Auto-generated method stub
}
public static void saveAll(String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
for (Entry<Integer, Train> entry:trains.entrySet()) {
@@ -516,12 +533,33 @@ public class Train extends BaseClass implements Comparable<Train> {
return select;
}
public void set(Block newBlock) {
block = newBlock;
if (isSet(block)) block.set(this);
}
public void setSpeed(int v) {
LOG.debug("Setting speed to {} kmh.",v);
for (Locomotive loco : locos) loco.setSpeed(v);
this.speed = v;
}
public void showTrace() {
int remainingLength = length();
if (remainingLength<1) remainingLength=1;
for (int i=0; i<trace.size(); i++) {
Tile tile = trace.get(i);
if (remainingLength>0) {
remainingLength-=tile.length();
tile.set(this);
} else {
tile.set(null);
trace.remove(i);
i--; // do not move to next index: remove shifted the next index towards us
}
}
}
public String start() throws IOException {
if (isNull(block)) return t("{} not in a block",this);
Context context = isSet(route) ? new Context( route ) : new Context( this);
@@ -558,6 +596,14 @@ public class Train extends BaseClass implements Comparable<Train> {
return Translation.get(Application.class, message, fills);
}
public SortedSet<String> tags() {
TreeSet<String> list = new TreeSet<String>(tags);
for (Locomotive loco : locos) list.addAll(loco.tags());
for (Car car:cars) list.addAll(car.tags());
return list;
}
@Override
public String toString() {
return isSet(name) ? name : locos.firstElement().name();
@@ -567,12 +613,13 @@ public class Train extends BaseClass implements Comparable<Train> {
LOG.debug("train.turn()");
if (isSet(direction)) {
direction = direction.inverse();
for (Locomotive loco : locos) loco.turn();
for (Locomotive loco : locos) loco.turn();
reverseTrace();
if (isSet(block)) plan.place(block);
}
return t("{} turned.",this);
}
public Train update(HashMap<String, String> params) {
LOG.debug("update({})",params);
pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
@@ -589,47 +636,8 @@ public class Train extends BaseClass implements Comparable<Train> {
return this;
}
public void set(Block newBlock) {
block = newBlock;
if (isSet(block)) block.set(this);
}
public Tile headPos() {
return trace.getFirst();
}
public void addToTrace(Vector<Tile> newTiles) {
boolean active = trace.isEmpty();
for (Tile tile : newTiles) {
if (active) {
trace.addFirst(tile);
} else {
Tile dummy = trace.getFirst();
if (dummy == tile) active = true;
}
}
showTrace();
}
public void showTrace() {
int remainingLength = length();
if (remainingLength<1) remainingLength=1;
for (int i=0; i<trace.size(); i++) {
Tile tile = trace.get(i);
if (remainingLength>0) {
remainingLength-=tile.length();
tile.set(this);
} else {
tile.set(null);
trace.remove(i);
i--; // do not move to next index: remove shifted the next index towards us
}
}
}
public void dropTrace() {
while (!trace.isEmpty()) trace.removeFirst().set(null);
}
public void removeFromTrace(Tile tile) {
trace.remove(tile);