Declare copper rails through AstraPlus and simplify train state
This commit is contained in:
@@ -57,6 +57,7 @@ public final class TrainData extends SavedData {
|
||||
TrainData::new,
|
||||
CODEC);
|
||||
private final List<Link> links = new ArrayList<>();
|
||||
private final Map<UUID, Set<UUID>> components = new HashMap<>();
|
||||
|
||||
public TrainData() {}
|
||||
|
||||
@@ -73,24 +74,30 @@ public final class TrainData extends SavedData {
|
||||
}
|
||||
|
||||
public Link at(UUID cart, int end) {
|
||||
return links.stream()
|
||||
.filter(l -> l.touches(cart) && l.end(cart) == end)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
for (Link link : links) {
|
||||
if (link.touches(cart) && link.end(cart) == end) return link;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void add(Link link) {
|
||||
links.add(link);
|
||||
components.clear();
|
||||
setDirty();
|
||||
}
|
||||
|
||||
public boolean remove(Link link) {
|
||||
boolean removed = links.remove(link);
|
||||
if (removed) setDirty();
|
||||
if (removed) {
|
||||
components.clear();
|
||||
setDirty();
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
public Set<UUID> component(UUID root) {
|
||||
var cached = components.get(root);
|
||||
if (cached != null) return cached;
|
||||
var seen = new HashSet<UUID>();
|
||||
var pending = new ArrayDeque<UUID>();
|
||||
pending.add(root);
|
||||
@@ -99,6 +106,11 @@ public final class TrainData extends SavedData {
|
||||
if (!seen.add(id)) continue;
|
||||
for (var link : links) if (link.touches(id)) pending.add(link.other(id));
|
||||
}
|
||||
return seen;
|
||||
var component = Set.copyOf(seen);
|
||||
// Collision checks reuse topology until a chain changes; ordinary carts are not cached.
|
||||
if (component.size() > 1) {
|
||||
for (UUID cart : component) components.put(cart, component);
|
||||
}
|
||||
return component;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user