working on route actions:

start block of route should only be freed, if corresponding action is fired.
  however, train should not be available from previous start block after route has finished.
This commit is contained in:
Stephan Richter
2020-11-03 23:28:23 +01:00
parent a2cb3d813e
commit 596317049c
14 changed files with 183 additions and 123 deletions

View File

@@ -22,6 +22,7 @@ public abstract class Block extends StretchableTile{
private static final String ALLOW_TURN = "allowTurn";
public boolean turnAllowed = false;
private Train trailingTrain = null;
private static final String TRAIN = Train.class.getSimpleName();
@@ -34,9 +35,9 @@ public abstract class Block extends StretchableTile{
@Override
public boolean free() {
return train == null && super.free();
return super.free() && trailingTrain == null;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
@@ -53,7 +54,7 @@ public abstract class Block extends StretchableTile{
turnAllowed = json.has(ALLOW_TURN) && json.getBoolean(ALLOW_TURN);
if (json.has(TRAIN)) {
Train tr = Train.get(json.getInt(TRAIN));
train(tr);
train(tr.block(this, false));
}
return this;
}
@@ -91,9 +92,11 @@ public abstract class Block extends StretchableTile{
@Override
public Tag tag(Map<String, Object> replacements) throws IOException {
if (replacements == null) replacements = new HashMap<String, Object>();
replacements.put("%text%",train == null ? name : train.name());
replacements.put("%text%",name);
if (trailingTrain != null) replacements.put("%text%","("+trailingTrain.name()+")");
if (train != null) replacements.put("%text%",train.directedName());
Tag tag = super.tag(replacements);
if (train != null) tag.clazz(tag.get("class")+" occupied");
if (train != null || trailingTrain != null) tag.clazz(tag.get("class")+" occupied");
return tag;
}
@@ -107,11 +110,20 @@ public abstract class Block extends StretchableTile{
return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")";
}
public Tile train(Train newTrain) throws IOException {
if (train == newTrain) return this;
if (train != null) train.block(null); // vorherigen Zug rauswerfen
if (newTrain != null) newTrain.block(this);
return super.train(newTrain);
public void trailingTrain(Train train) {
trailingTrain = train;
this.train = null;
plan.place(this);
}
public Train trailingTrain() {
return trailingTrain;
}
@Override
public void unlock() {
trailingTrain = null;
super.unlock();
}
@Override
@@ -119,14 +131,14 @@ public abstract class Block extends StretchableTile{
if (params.containsKey(NAME)) name=params.get(NAME);
if (params.containsKey(TRAIN)) {
int trainId = Integer.parseInt(params.get(TRAIN));
Train t = Train.get(trainId);
if (t != null) {
Block oldBlock = t.block();
if (oldBlock != null) oldBlock.train(null);
train(t);
}
if (trainId == 0) {
train(null);
} else {
Train t = Train.get(trainId);
if (t != null) train = t.block(this,true);
}
}
turnAllowed = params.containsKey(ALLOW_TURN) && params.get(ALLOW_TURN).equals("on");
return super.update(params);
}
}
}

View File

@@ -181,10 +181,8 @@ public class Relay extends Tile implements Device{
@Override
public void onSuccess() {
super.onSuccess();
try {
Relay.this.state = newState;
plan.place(Relay.this);
} catch (IOException e) {}
Relay.this.state = newState;
plan.place(Relay.this);
}
@Override

View File

@@ -26,9 +26,13 @@ public abstract class Signal extends Tile{
public abstract boolean isAffectedFrom(Direction dir);
public boolean state(String state) throws IOException {
public boolean state(String state) {
this.state = state;
plan.stream("place "+tag(null));
try {
plan.stream("place "+tag(null));
} catch (IOException e) {
e.printStackTrace();
}
return true;
}

View File

@@ -95,7 +95,10 @@ public abstract class Tile implements Constants{
}
public boolean free() {
return (!disabled) && route == null;
if (disabled) return false;
if (route != null) return false;
if (train != null) return false;
return true;
}
public int height() {
@@ -160,8 +163,9 @@ public abstract class Tile implements Constants{
return this;
}
public Tile lock(Route lockingRoute) throws IOException {
if (route != null && route != lockingRoute) throw new IllegalStateException(this.toString());
public Tile lock(Route lockingRoute) {
if (route == lockingRoute) return this;
if (route != null && lockingRoute != null) throw new IllegalStateException(this.toString());
route = lockingRoute;
return plan.place(this);
}
@@ -353,12 +357,13 @@ public abstract class Tile implements Constants{
return train;
}
public Tile train(Train train) throws IOException {
public Tile train(Train train) {
if (this.train == train) return this; // nothing to update
this.train = train;
return plan.place(this);
}
public void unlock() throws IOException {
public void unlock() {
route = null;
train = null;
plan.place(this);

View File

@@ -149,10 +149,8 @@ public abstract class Turnout extends Tile implements Device{
@Override
public void onSuccess() {
super.onSuccess();
try {
Turnout.this.state = newState;
plan.place(Turnout.this);
} catch (IOException e) {}
Turnout.this.state = newState;
plan.place(Turnout.this);
}
@Override