|
|
@ -8,6 +8,7 @@ import java.io.IOException; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.Collection; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.Map.Entry; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.Random; |
|
|
@ -22,7 +23,7 @@ import org.slf4j.LoggerFactory; |
|
|
|
import de.keawe.tools.translations.Translation; |
|
|
|
import de.keawe.tools.translations.Translation; |
|
|
|
import de.srsoftware.tools.Tag; |
|
|
|
import de.srsoftware.tools.Tag; |
|
|
|
import de.srsoftware.web4rail.Application; |
|
|
|
import de.srsoftware.web4rail.Application; |
|
|
|
import de.srsoftware.web4rail.Constants; |
|
|
|
import de.srsoftware.web4rail.BaseClass; |
|
|
|
import de.srsoftware.web4rail.Plan; |
|
|
|
import de.srsoftware.web4rail.Plan; |
|
|
|
import de.srsoftware.web4rail.Plan.Direction; |
|
|
|
import de.srsoftware.web4rail.Plan.Direction; |
|
|
|
import de.srsoftware.web4rail.Route; |
|
|
|
import de.srsoftware.web4rail.Route; |
|
|
@ -36,8 +37,9 @@ import de.srsoftware.web4rail.tags.Input; |
|
|
|
import de.srsoftware.web4rail.tags.Label; |
|
|
|
import de.srsoftware.web4rail.tags.Label; |
|
|
|
import de.srsoftware.web4rail.tags.Select; |
|
|
|
import de.srsoftware.web4rail.tags.Select; |
|
|
|
import de.srsoftware.web4rail.tiles.Block; |
|
|
|
import de.srsoftware.web4rail.tiles.Block; |
|
|
|
|
|
|
|
import de.srsoftware.web4rail.tiles.Tile; |
|
|
|
|
|
|
|
|
|
|
|
public class Train implements Comparable<Train>,Constants { |
|
|
|
public class Train extends BaseClass implements Comparable<Train> { |
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class); |
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class); |
|
|
|
|
|
|
|
|
|
|
|
public static final String HEAD = "train_head"; |
|
|
|
public static final String HEAD = "train_head"; |
|
|
@ -71,7 +73,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Block block = null; |
|
|
|
private Block block = null; |
|
|
|
private Vector<Block> previousBlocks = new Vector<Block>(); |
|
|
|
LinkedList<Tile> trace = new LinkedList<Tile>(); |
|
|
|
|
|
|
|
|
|
|
|
private class Autopilot extends Thread{ |
|
|
|
private class Autopilot extends Thread{ |
|
|
|
boolean stop = false; |
|
|
|
boolean stop = false; |
|
|
@ -81,7 +83,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
try { |
|
|
|
try { |
|
|
|
stop = false; |
|
|
|
stop = false; |
|
|
|
while (true) { |
|
|
|
while (true) { |
|
|
|
if (route == null) { |
|
|
|
if (isNull(route)) { |
|
|
|
Thread.sleep(2000); |
|
|
|
Thread.sleep(2000); |
|
|
|
if (stop) return; |
|
|
|
if (stop) return; |
|
|
|
Train.this.start(); |
|
|
|
Train.this.start(); |
|
|
@ -107,7 +109,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Train(Locomotive loco, Integer id) { |
|
|
|
public Train(Locomotive loco, Integer id) { |
|
|
|
if (id == null) id = Application.createId(); |
|
|
|
if (isNull(id)) id = Application.createId(); |
|
|
|
this.id = id; |
|
|
|
this.id = id; |
|
|
|
add(loco); |
|
|
|
add(loco); |
|
|
|
trains.put(id, this); |
|
|
|
trains.put(id, this); |
|
|
@ -115,7 +117,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
public static Object action(HashMap<String, String> params, Plan plan) throws IOException { |
|
|
|
public static Object action(HashMap<String, String> params, Plan plan) throws IOException { |
|
|
|
String action = params.get(ACTION); |
|
|
|
String action = params.get(ACTION); |
|
|
|
if (action == null) return t("No action passed to Train.action!"); |
|
|
|
if (isNull(action)) return t("No action passed to Train.action!"); |
|
|
|
if (!params.containsKey(Train.ID)) { |
|
|
|
if (!params.containsKey(Train.ID)) { |
|
|
|
switch (action) { |
|
|
|
switch (action) { |
|
|
|
case ACTION_PROPS: |
|
|
|
case ACTION_PROPS: |
|
|
@ -127,7 +129,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
int id = Integer.parseInt(params.get(Train.ID)); |
|
|
|
int id = Integer.parseInt(params.get(Train.ID)); |
|
|
|
Train train = trains.get(id); |
|
|
|
Train train = trains.get(id); |
|
|
|
if (train == null) return(t("No train with id {}!",id)); |
|
|
|
if (isNull(train)) return(t("No train with id {}!",id)); |
|
|
|
switch (action) { |
|
|
|
switch (action) { |
|
|
|
case ACTION_ADD: |
|
|
|
case ACTION_ADD: |
|
|
|
return train.addCar(params); |
|
|
|
return train.addCar(params); |
|
|
@ -151,17 +153,18 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
return t("Unknown action: {}",params.get(ACTION)); |
|
|
|
return t("Unknown action: {}",params.get(ACTION)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Route chooseRoute(Context context) { HashSet<Route> routes = block.routes(); |
|
|
|
private Route chooseRoute(Context context) { |
|
|
|
|
|
|
|
HashSet<Route> routes = block.routes(); |
|
|
|
Vector<Route> availableRoutes = new Vector<Route>(); |
|
|
|
Vector<Route> availableRoutes = new Vector<Route>(); |
|
|
|
for (Route rt : routes) { |
|
|
|
for (Route rt : routes) { |
|
|
|
if (rt == route) continue; // andere Route als zuvor wählen
|
|
|
|
if (rt == route) continue; // andere Route als zuvor wählen
|
|
|
|
if (rt.path().firstElement() != block) continue; // keine Route wählen, die nicht vom aktuellen Block des Zuges startet
|
|
|
|
if (rt.path().firstElement() != block) continue; // keine Route wählen, die nicht vom aktuellen Block des Zuges startet
|
|
|
|
if (direction != null && rt.startDirection != direction) { // Route ist entgegen der Startrichtung des Zuges
|
|
|
|
if (isSet(direction) && rt.startDirection != direction) { // Route ist entgegen der Startrichtung des Zuges
|
|
|
|
if (!pushPull || !block.turnAllowed) { // Zug ist kein Wendezug oder Block erlaubt kein Wenden
|
|
|
|
if (!pushPull || !block.turnAllowed) { // Zug ist kein Wendezug oder Block erlaubt kein Wenden
|
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (!rt.free()) { // keine belegten Routen wählen
|
|
|
|
if (!rt.isFree()) { // keine belegten Routen wählen
|
|
|
|
LOG.debug("{} is not free!",rt); |
|
|
|
LOG.debug("{} is not free!",rt); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
@ -180,7 +183,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
public String directedName() { |
|
|
|
public String directedName() { |
|
|
|
String result = name(); |
|
|
|
String result = name(); |
|
|
|
if (direction == null) return result; |
|
|
|
if (isNull(direction)) return result; |
|
|
|
switch (direction) { |
|
|
|
switch (direction) { |
|
|
|
case NORTH: |
|
|
|
case NORTH: |
|
|
|
case WEST: |
|
|
|
case WEST: |
|
|
@ -194,9 +197,9 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
private Object dropCar(HashMap<String, String> params) { |
|
|
|
private Object dropCar(HashMap<String, String> params) { |
|
|
|
String carId = params.get(CAR_ID); |
|
|
|
String carId = params.get(CAR_ID); |
|
|
|
if (carId != null) cars.remove(Car.get(carId)); |
|
|
|
if (isSet(carId)) cars.remove(Car.get(carId)); |
|
|
|
String locoId = params.get(LOCO_ID); |
|
|
|
String locoId = params.get(LOCO_ID); |
|
|
|
if (locoId != null) locos.remove(Car.get(locoId)); |
|
|
|
if (isSet(locoId)) locos.remove(Car.get(locoId)); |
|
|
|
return props(); |
|
|
|
return props(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -204,7 +207,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
LOG.debug("addCar({})",params); |
|
|
|
LOG.debug("addCar({})",params); |
|
|
|
if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!"); |
|
|
|
if (!params.containsKey(CAR_ID)) return t("No car id passed to Train.addCar!"); |
|
|
|
Car car = Car.get(params.get(CAR_ID)); |
|
|
|
Car car = Car.get(params.get(CAR_ID)); |
|
|
|
if (car == null) return t("No car with id \"{}\" known!",params.get(CAR_ID)); |
|
|
|
if (isNull(car)) return t("No car with id \"{}\" known!",params.get(CAR_ID)); |
|
|
|
if (car instanceof Locomotive) { |
|
|
|
if (car instanceof Locomotive) { |
|
|
|
locos.add((Locomotive) car); |
|
|
|
locos.add((Locomotive) car); |
|
|
|
} else cars.add(car); |
|
|
|
} else cars.add(car); |
|
|
@ -212,7 +215,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void add(Car car) { |
|
|
|
public void add(Car car) { |
|
|
|
if (car == null) return; |
|
|
|
if (isNull(car)) return; |
|
|
|
if (car instanceof Locomotive) { |
|
|
|
if (car instanceof Locomotive) { |
|
|
|
locos.add((Locomotive) car); |
|
|
|
locos.add((Locomotive) car); |
|
|
|
} else cars.add(car); |
|
|
|
} else cars.add(car); |
|
|
@ -220,29 +223,13 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String automatic() { |
|
|
|
private String automatic() { |
|
|
|
if (autopilot == null) { |
|
|
|
if (isNull(autopilot)) { |
|
|
|
autopilot = new Autopilot(); |
|
|
|
autopilot = new Autopilot(); |
|
|
|
autopilot.start(); |
|
|
|
autopilot.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
return t("{} now in auto-mode",this); |
|
|
|
return t("{} now in auto-mode",this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Block block() { |
|
|
|
|
|
|
|
return block; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Train block(Block block, boolean resetPreviousBlocks) { |
|
|
|
|
|
|
|
if (this.block == block) return this; // nothing to update
|
|
|
|
|
|
|
|
if (this.block != null) { |
|
|
|
|
|
|
|
this.block.trailingTrain(this); |
|
|
|
|
|
|
|
previousBlocks.add(this.block); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.block = block; |
|
|
|
|
|
|
|
block.trainHead(this); |
|
|
|
|
|
|
|
if (resetPreviousBlocks) resetPreviousBlocks(); |
|
|
|
|
|
|
|
return this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Tag carList() { |
|
|
|
private Tag carList() { |
|
|
|
Tag locoProp = new Tag("li").content(t("Cars:")); |
|
|
|
Tag locoProp = new Tag("li").content(t("Cars:")); |
|
|
|
Tag locoList = new Tag("ul").clazz("carlist"); |
|
|
|
Tag locoList = new Tag("ul").clazz("carlist"); |
|
|
@ -273,7 +260,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
private static Object create(HashMap<String, String> params, Plan plan) { |
|
|
|
private static Object create(HashMap<String, String> params, Plan plan) { |
|
|
|
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID)); |
|
|
|
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID)); |
|
|
|
if (loco == null) return t("unknown locomotive: {}",params.get(ID)); |
|
|
|
if (isNull(loco)) return t("unknown locomotive: {}",params.get(ID)); |
|
|
|
Train train = new Train(loco).plan(plan); |
|
|
|
Train train = new Train(loco).plan(plan); |
|
|
|
if (params.containsKey(NAME)) train.name(params.get(NAME)); |
|
|
|
if (params.containsKey(NAME)) train.name(params.get(NAME)); |
|
|
|
return train; |
|
|
|
return train; |
|
|
@ -286,7 +273,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
public Train heading(Direction dir) { |
|
|
|
public Train heading(Direction dir) { |
|
|
|
direction = dir; |
|
|
|
direction = dir; |
|
|
|
if (block != null) plan.place(block); |
|
|
|
if (isSet(block)) plan.place(block); |
|
|
|
return this; |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -294,10 +281,10 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
JSONObject json = new JSONObject(); |
|
|
|
JSONObject json = new JSONObject(); |
|
|
|
json.put(ID, id); |
|
|
|
json.put(ID, id); |
|
|
|
json.put(NAME,name); |
|
|
|
json.put(NAME,name); |
|
|
|
if (route != null) json.put(ROUTE, route.id()); |
|
|
|
if (isSet(route)) json.put(ROUTE, route.id()); |
|
|
|
if (direction != null) json.put(DIRECTION, direction); |
|
|
|
if (isSet(direction)) json.put(DIRECTION, direction); |
|
|
|
json.put(PUSH_PULL, pushPull); |
|
|
|
json.put(PUSH_PULL, pushPull); |
|
|
|
if (name != null)json.put(NAME, name); |
|
|
|
if (isSet(name))json.put(NAME, name); |
|
|
|
Vector<Integer> locoIds = new Vector<Integer>(); |
|
|
|
Vector<Integer> locoIds = new Vector<Integer>(); |
|
|
|
for (Locomotive loco : locos) locoIds.add(loco.id()); |
|
|
|
for (Locomotive loco : locos) locoIds.add(loco.id()); |
|
|
|
json.put(LOCOS, locoIds); |
|
|
|
json.put(LOCOS, locoIds); |
|
|
@ -326,7 +313,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
public static void loadAll(String filename, Plan plan) throws IOException { |
|
|
|
public static void loadAll(String filename, Plan plan) throws IOException { |
|
|
|
BufferedReader file = new BufferedReader(new FileReader(filename)); |
|
|
|
BufferedReader file = new BufferedReader(new FileReader(filename)); |
|
|
|
String line = file.readLine(); |
|
|
|
String line = file.readLine(); |
|
|
|
while (line != null) { |
|
|
|
while (isSet(line)) { |
|
|
|
JSONObject json = new JSONObject(line); |
|
|
|
JSONObject json = new JSONObject(line); |
|
|
|
|
|
|
|
|
|
|
|
int id = json.getInt(ID); |
|
|
|
int id = json.getInt(ID); |
|
|
@ -405,7 +392,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String name() { |
|
|
|
public String name() { |
|
|
|
return (name != null ? name : locos.firstElement().name()); |
|
|
|
return (isSet(name) ? name : locos.firstElement().name()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Train name(String newName) { |
|
|
|
private Train name(String newName) { |
|
|
@ -441,11 +428,11 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
carList().addTo(propList); |
|
|
|
carList().addTo(propList); |
|
|
|
new Tag("li").content(t("length: {}",length())).addTo(propList); |
|
|
|
new Tag("li").content(t("length: {}",length())).addTo(propList); |
|
|
|
|
|
|
|
|
|
|
|
if (block != null) { |
|
|
|
if (isSet(block)) { |
|
|
|
new Tag("li").content(t("Current location: {}",block)).addTo(propList); |
|
|
|
new Tag("li").content(t("Current location: {}",block)).addTo(propList); |
|
|
|
Tag actions = new Tag("li").clazz().content(t("Actions:")+NBSP); |
|
|
|
Tag actions = new Tag("li").clazz().content(t("Actions:")+NBSP); |
|
|
|
new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions); |
|
|
|
new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions); |
|
|
|
if (autopilot == null) { |
|
|
|
if (isNull(autopilot)) { |
|
|
|
new Button(t("auto"),"train("+id+",'"+ACTION_AUTO+"')").addTo(actions); |
|
|
|
new Button(t("auto"),"train("+id+",'"+ACTION_AUTO+"')").addTo(actions); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
new Button(t("quit autopilot"),"train("+id+",'"+ACTION_QUIT+"')").addTo(actions); |
|
|
|
new Button(t("quit autopilot"),"train("+id+",'"+ACTION_QUIT+"')").addTo(actions); |
|
|
@ -453,10 +440,10 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
actions.addTo(propList); |
|
|
|
actions.addTo(propList); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (route != null) { |
|
|
|
if (isSet(route)) { |
|
|
|
new Tag("li").content(t("Current route: {}",route)).addTo(propList); |
|
|
|
new Tag("li").content(t("Current route: {}",route)).addTo(propList); |
|
|
|
} |
|
|
|
} |
|
|
|
if (direction != null) new Tag("li").content(t("Direction: heading {}",direction)).addTo(propList); |
|
|
|
if (isSet(direction)) new Tag("li").content(t("Direction: heading {}",direction)).addTo(propList); |
|
|
|
|
|
|
|
|
|
|
|
Tag tagList = new Tag("ul"); |
|
|
|
Tag tagList = new Tag("ul"); |
|
|
|
for (String tag : tags()) new Tag("li").content(tag).addTo(tagList); |
|
|
|
for (String tag : tags()) new Tag("li").content(tag).addTo(tagList); |
|
|
@ -474,26 +461,13 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Object quitAutopilot() { |
|
|
|
public Object quitAutopilot() { |
|
|
|
if (autopilot != null) { |
|
|
|
if (isSet(autopilot)) { |
|
|
|
autopilot.stop = true; |
|
|
|
autopilot.stop = true; |
|
|
|
autopilot = null; |
|
|
|
autopilot = null; |
|
|
|
return t("{} stopping at next block.",this); |
|
|
|
return t("{} stopping at next block.",this); |
|
|
|
} else return t("autopilot not active."); |
|
|
|
} else return t("autopilot not active."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void removeFromBlock(Block block) { |
|
|
|
|
|
|
|
if (block.trainHead() == this) block.trainHead(null); |
|
|
|
|
|
|
|
if (this.block == block) this.block = null; |
|
|
|
|
|
|
|
previousBlocks.remove(block); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void resetPreviousBlocks() { |
|
|
|
|
|
|
|
for (Block block : previousBlocks) { |
|
|
|
|
|
|
|
if (block.trainHead() == this || block.trailingTrain() == this) block.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
previousBlocks.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void saveAll(String filename) throws IOException { |
|
|
|
public static void saveAll(String filename) throws IOException { |
|
|
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename)); |
|
|
|
BufferedWriter file = new BufferedWriter(new FileWriter(filename)); |
|
|
|
for (Entry<Integer, Train> entry:trains.entrySet()) { |
|
|
|
for (Entry<Integer, Train> entry:trains.entrySet()) { |
|
|
@ -504,7 +478,7 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static Select selector(Train preselected,Collection<Train> exclude) { |
|
|
|
public static Select selector(Train preselected,Collection<Train> exclude) { |
|
|
|
if (exclude == null) exclude = new Vector<Train>(); |
|
|
|
if (isNull(exclude)) exclude = new Vector<Train>(); |
|
|
|
Select select = new Select(Train.HEAD); |
|
|
|
Select select = new Select(Train.HEAD); |
|
|
|
new Tag("option").attr("value","0").content(t("unset")).addTo(select); |
|
|
|
new Tag("option").attr("value","0").content(t("unset")).addTo(select); |
|
|
|
for (Train train : Train.list()) { |
|
|
|
for (Train train : Train.list()) { |
|
|
@ -522,21 +496,23 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String start() throws IOException { |
|
|
|
public String start() throws IOException { |
|
|
|
if (block == null) return t("{} not in a block",this); |
|
|
|
if (isNull(block)) return t("{} not in a block",this); |
|
|
|
if (route != null) route.reset(); // reset route previously chosen
|
|
|
|
Context context = isSet(route) ? new Context( route ) : new Context( this); |
|
|
|
Context context = new Context(this); |
|
|
|
|
|
|
|
route = chooseRoute(context); |
|
|
|
|
|
|
|
if (route == null) return t("No free routes from {}",block); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isSet(context.route)) context.route.reset(); // reset route previously chosen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
route = chooseRoute(context); |
|
|
|
|
|
|
|
if (isNull(route)) return t("No free routes from {}",block); |
|
|
|
if (!route.lock()) return t("Was not able to lock {}",route); |
|
|
|
if (!route.lock()) return t("Was not able to lock {}",route); |
|
|
|
|
|
|
|
|
|
|
|
if (direction != route.startDirection) turn(); |
|
|
|
if (direction != route.startDirection) turn(); |
|
|
|
|
|
|
|
|
|
|
|
String error = null; |
|
|
|
String error = null; |
|
|
|
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!"); |
|
|
|
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!"); |
|
|
|
if (error == null && !route.fireSetupActions(context)) error = t("Was not able to fire all setup actions of route!"); |
|
|
|
if (isNull(error) && !route.fireSetupActions(context)) error = t("Was not able to fire all setup actions of route!"); |
|
|
|
if (error == null && !route.setSignals(null)) error = t("Was not able to set all signals!"); |
|
|
|
if (isNull(error) && !route.setSignals(null)) error = t("Was not able to set all signals!"); |
|
|
|
if (error == null && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route); |
|
|
|
if (isNull(error) && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route); |
|
|
|
if (error != null) { |
|
|
|
if (isSet(error)) { |
|
|
|
route.reset(); |
|
|
|
route.reset(); |
|
|
|
return error; |
|
|
|
return error; |
|
|
|
} |
|
|
|
} |
|
|
@ -547,13 +523,6 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
private Object stopNow() { |
|
|
|
private Object stopNow() { |
|
|
|
quitAutopilot(); |
|
|
|
quitAutopilot(); |
|
|
|
setSpeed(0); |
|
|
|
setSpeed(0); |
|
|
|
if (route != null) try { |
|
|
|
|
|
|
|
route.unlock(); |
|
|
|
|
|
|
|
route.endBlock().trainHead(null); |
|
|
|
|
|
|
|
route.startBlock().trainHead(this); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
route = null; |
|
|
|
route = null; |
|
|
|
return t("Stopped {}.",this); |
|
|
|
return t("Stopped {}.",this); |
|
|
|
} |
|
|
|
} |
|
|
@ -564,16 +533,15 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return name != null ? name : locos.firstElement().name(); |
|
|
|
return isSet(name) ? name : locos.firstElement().name(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Object turn() { |
|
|
|
public Object turn() { |
|
|
|
LOG.debug("train.turn()"); |
|
|
|
LOG.debug("train.turn()"); |
|
|
|
if (direction != null) { |
|
|
|
if (isSet(direction)) { |
|
|
|
direction = direction.inverse(); |
|
|
|
direction = direction.inverse(); |
|
|
|
for (Locomotive loco : locos) loco.turn(); |
|
|
|
for (Locomotive loco : locos) loco.turn(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (block != null) plan.place(block.trainHead(this)); |
|
|
|
|
|
|
|
return t("{} turned.",this); |
|
|
|
return t("{} turned.",this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -593,4 +561,48 @@ public class Train implements Comparable<Train>,Constants { |
|
|
|
|
|
|
|
|
|
|
|
return this; |
|
|
|
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); |
|
|
|
|
|
|
|
System.err.println(trace); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Tile dummy = trace.getFirst(); |
|
|
|
|
|
|
|
if (dummy == tile) { |
|
|
|
|
|
|
|
active = true; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
showTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void showTrace() { |
|
|
|
|
|
|
|
int remainingLength = length(); |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|