2022-02-01 01:14:21 +01:00
|
|
|
package com.simibubi.create.content.logistics.trains.entity;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map.Entry;
|
2022-03-09 21:33:28 +01:00
|
|
|
import java.util.Objects;
|
2022-02-01 01:14:21 +01:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Vector;
|
2022-02-16 03:14:31 +01:00
|
|
|
import java.util.function.BiConsumer;
|
2022-02-03 02:51:14 +01:00
|
|
|
import java.util.function.BiFunction;
|
2022-03-17 15:41:09 +01:00
|
|
|
import java.util.function.BiPredicate;
|
2022-03-08 03:51:03 +01:00
|
|
|
import java.util.function.Consumer;
|
2022-05-15 21:19:02 +02:00
|
|
|
import java.util.function.Predicate;
|
2022-03-08 03:51:03 +01:00
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
import com.simibubi.create.Create;
|
2022-05-15 21:19:02 +02:00
|
|
|
import com.simibubi.create.content.logistics.trains.DimensionPalette;
|
2022-03-08 03:51:03 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.GraphLocation;
|
2022-02-01 01:14:21 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.TrackEdge;
|
|
|
|
import com.simibubi.create.content.logistics.trains.TrackGraph;
|
|
|
|
import com.simibubi.create.content.logistics.trains.TrackNode;
|
2022-03-09 21:33:28 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.TrackNodeLocation;
|
2022-03-08 03:51:03 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.management.edgePoint.EdgeData;
|
2022-03-17 15:41:09 +01:00
|
|
|
import com.simibubi.create.content.logistics.trains.management.edgePoint.signal.TrackEdgePoint;
|
2022-02-16 03:14:31 +01:00
|
|
|
import com.simibubi.create.foundation.utility.Couple;
|
2022-02-05 02:06:31 +01:00
|
|
|
import com.simibubi.create.foundation.utility.Pair;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-03-09 21:33:28 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.nbt.Tag;
|
2022-05-04 01:05:03 +02:00
|
|
|
import net.minecraft.util.Mth;
|
2022-02-01 01:14:21 +01:00
|
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
|
2022-02-03 02:51:14 +01:00
|
|
|
public class TravellingPoint {
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-02-16 03:14:31 +01:00
|
|
|
public TrackNode node1, node2;
|
|
|
|
public TrackEdge edge;
|
|
|
|
public double position;
|
|
|
|
public boolean blocked;
|
2023-05-09 18:23:47 +02:00
|
|
|
public boolean upsideDown;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
public static enum SteerDirection {
|
|
|
|
NONE(0), LEFT(-1), RIGHT(1);
|
|
|
|
|
|
|
|
float targetDot;
|
|
|
|
|
|
|
|
private SteerDirection(float targetDot) {
|
|
|
|
this.targetDot = targetDot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static interface ITrackSelector
|
2022-02-05 02:06:31 +01:00
|
|
|
extends BiFunction<TrackGraph, Pair<Boolean, List<Entry<TrackNode, TrackEdge>>>, Entry<TrackNode, TrackEdge>> {
|
2022-02-01 01:14:21 +01:00
|
|
|
};
|
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
public static interface IEdgePointListener extends BiPredicate<Double, Pair<TrackEdgePoint, Couple<TrackNode>>> {
|
2022-02-16 03:14:31 +01:00
|
|
|
};
|
|
|
|
|
2022-03-11 23:37:41 +01:00
|
|
|
public static interface ITurnListener extends BiConsumer<Double, TrackEdge> {
|
|
|
|
};
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
public static interface IPortalListener extends Predicate<Couple<TrackNodeLocation>> {
|
|
|
|
};
|
|
|
|
|
2022-02-16 03:14:31 +01:00
|
|
|
public TravellingPoint() {}
|
|
|
|
|
2023-05-09 18:23:47 +02:00
|
|
|
public TravellingPoint(TrackNode node1, TrackNode node2, TrackEdge edge, double position, boolean upsideDown) {
|
2022-02-01 01:14:21 +01:00
|
|
|
this.node1 = node1;
|
|
|
|
this.node2 = node2;
|
|
|
|
this.edge = edge;
|
|
|
|
this.position = position;
|
2023-05-09 18:23:47 +02:00
|
|
|
this.upsideDown = upsideDown;
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
public IEdgePointListener ignoreEdgePoints() {
|
|
|
|
return (d, c) -> false;
|
2022-02-16 03:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 23:37:41 +01:00
|
|
|
public ITurnListener ignoreTurns() {
|
|
|
|
return (d, c) -> {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
public IPortalListener ignorePortals() {
|
|
|
|
return $ -> false;
|
|
|
|
}
|
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
public ITrackSelector random() {
|
2022-02-05 02:06:31 +01:00
|
|
|
return (graph, pair) -> pair.getSecond()
|
|
|
|
.get(Create.RANDOM.nextInt(pair.getSecond()
|
|
|
|
.size()));
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:51:14 +01:00
|
|
|
public ITrackSelector follow(TravellingPoint other) {
|
2022-03-08 03:51:03 +01:00
|
|
|
return follow(other, null);
|
|
|
|
}
|
2022-03-09 21:33:28 +01:00
|
|
|
|
2022-03-08 03:51:03 +01:00
|
|
|
public ITrackSelector follow(TravellingPoint other, @Nullable Consumer<Boolean> success) {
|
2022-02-05 02:06:31 +01:00
|
|
|
return (graph, pair) -> {
|
|
|
|
List<Entry<TrackNode, TrackEdge>> validTargets = pair.getSecond();
|
|
|
|
boolean forward = pair.getFirst();
|
|
|
|
TrackNode target = forward ? other.node1 : other.node2;
|
|
|
|
TrackNode secondary = forward ? other.node2 : other.node1;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
for (Entry<TrackNode, TrackEdge> entry : validTargets)
|
2022-03-08 03:51:03 +01:00
|
|
|
if (entry.getKey() == target || entry.getKey() == secondary) {
|
|
|
|
if (success != null)
|
|
|
|
success.accept(true);
|
2022-02-01 01:14:21 +01:00
|
|
|
return entry;
|
2022-03-08 03:51:03 +01:00
|
|
|
}
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
Vector<List<Entry<TrackNode, TrackEdge>>> frontiers = new Vector<>(validTargets.size());
|
|
|
|
Vector<Set<TrackEdge>> visiteds = new Vector<>(validTargets.size());
|
|
|
|
|
|
|
|
for (int j = 0; j < validTargets.size(); j++) {
|
|
|
|
ArrayList<Entry<TrackNode, TrackEdge>> e = new ArrayList<>();
|
|
|
|
Entry<TrackNode, TrackEdge> entry = validTargets.get(j);
|
|
|
|
e.add(entry);
|
|
|
|
frontiers.add(e);
|
|
|
|
HashSet<TrackEdge> e2 = new HashSet<>();
|
|
|
|
e2.add(entry.getValue());
|
|
|
|
visiteds.add(e2);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
for (int j = 0; j < validTargets.size(); j++) {
|
|
|
|
Entry<TrackNode, TrackEdge> entry = validTargets.get(j);
|
|
|
|
List<Entry<TrackNode, TrackEdge>> frontier = frontiers.get(j);
|
|
|
|
if (frontier.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Entry<TrackNode, TrackEdge> currentEntry = frontier.remove(0);
|
|
|
|
for (Entry<TrackNode, TrackEdge> nextEntry : graph.getConnectionsFrom(currentEntry.getKey())
|
|
|
|
.entrySet()) {
|
|
|
|
TrackEdge nextEdge = nextEntry.getValue();
|
|
|
|
if (!visiteds.get(j)
|
|
|
|
.add(nextEdge))
|
|
|
|
continue;
|
2022-07-03 13:56:02 +02:00
|
|
|
if (!currentEntry.getValue()
|
|
|
|
.canTravelTo(nextEdge))
|
|
|
|
continue;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
TrackNode nextNode = nextEntry.getKey();
|
2022-03-08 03:51:03 +01:00
|
|
|
if (nextNode == target) {
|
|
|
|
if (success != null)
|
|
|
|
success.accept(true);
|
2022-02-01 01:14:21 +01:00
|
|
|
return entry;
|
2022-03-08 03:51:03 +01:00
|
|
|
}
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
frontier.add(nextEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:51:03 +01:00
|
|
|
if (success != null)
|
|
|
|
success.accept(false);
|
2022-02-01 01:14:21 +01:00
|
|
|
return validTargets.get(0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public ITrackSelector steer(SteerDirection direction, Vec3 upNormal) {
|
2022-02-05 02:06:31 +01:00
|
|
|
return (graph, pair) -> {
|
|
|
|
List<Entry<TrackNode, TrackEdge>> validTargets = pair.getSecond();
|
2022-02-01 01:14:21 +01:00
|
|
|
double closest = Double.MAX_VALUE;
|
|
|
|
Entry<TrackNode, TrackEdge> best = null;
|
|
|
|
|
|
|
|
for (Entry<TrackNode, TrackEdge> entry : validTargets) {
|
2022-04-25 17:53:45 +02:00
|
|
|
Vec3 trajectory = edge.getDirection(false);
|
2022-02-01 01:14:21 +01:00
|
|
|
Vec3 entryTrajectory = entry.getValue()
|
2022-04-25 17:53:45 +02:00
|
|
|
.getDirection(true);
|
2022-02-01 01:14:21 +01:00
|
|
|
Vec3 normal = trajectory.cross(upNormal);
|
2022-02-07 01:54:20 +01:00
|
|
|
double dot = normal.dot(entryTrajectory);
|
2022-02-01 01:14:21 +01:00
|
|
|
double diff = Math.abs(direction.targetDot - dot);
|
|
|
|
if (diff > closest)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
closest = diff;
|
|
|
|
best = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (best == null) {
|
|
|
|
Create.LOGGER.warn("Couldn't find steer target, choosing first");
|
|
|
|
return validTargets.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
public double travel(TrackGraph graph, double distance, ITrackSelector trackSelector) {
|
|
|
|
return travel(graph, distance, trackSelector, ignoreEdgePoints());
|
|
|
|
}
|
|
|
|
|
|
|
|
public double travel(TrackGraph graph, double distance, ITrackSelector trackSelector,
|
|
|
|
IEdgePointListener signalListener) {
|
|
|
|
return travel(graph, distance, trackSelector, signalListener, ignoreTurns());
|
|
|
|
}
|
|
|
|
|
2022-02-16 03:14:31 +01:00
|
|
|
public double travel(TrackGraph graph, double distance, ITrackSelector trackSelector,
|
2022-03-17 15:41:09 +01:00
|
|
|
IEdgePointListener signalListener, ITurnListener turnListener) {
|
2022-05-15 21:19:02 +02:00
|
|
|
return travel(graph, distance, trackSelector, signalListener, turnListener, ignorePortals());
|
|
|
|
}
|
|
|
|
|
|
|
|
public double travel(TrackGraph graph, double distance, ITrackSelector trackSelector,
|
|
|
|
IEdgePointListener signalListener, ITurnListener turnListener, IPortalListener portalListener) {
|
2022-02-03 02:51:14 +01:00
|
|
|
blocked = false;
|
2022-07-10 18:22:47 +02:00
|
|
|
if (edge == null)
|
|
|
|
return 0;
|
2022-04-25 17:53:45 +02:00
|
|
|
double edgeLength = edge.getLength();
|
2022-05-04 01:05:03 +02:00
|
|
|
if (Mth.equal(distance, 0))
|
2022-02-01 01:14:21 +01:00
|
|
|
return 0;
|
|
|
|
|
2022-02-16 03:14:31 +01:00
|
|
|
double prevPos = position;
|
2022-02-01 01:14:21 +01:00
|
|
|
double traveled = distance;
|
2022-05-15 21:19:02 +02:00
|
|
|
double currentT = edgeLength == 0 ? 0 : position / edgeLength;
|
2022-04-25 17:53:45 +02:00
|
|
|
double incrementT = edge.incrementT(currentT, distance);
|
2022-02-01 01:14:21 +01:00
|
|
|
position = incrementT * edgeLength;
|
2022-03-17 23:14:53 +01:00
|
|
|
|
|
|
|
// FIXME: using incrementT like this becomes inaccurate at medium-long distances
|
|
|
|
// travelling points would travel only 50m instead of 100m due to the low
|
|
|
|
// incrementT at their starting position (e.g. bezier turn)
|
|
|
|
// In an ideal scenario the amount added to position would iterate the traversed
|
|
|
|
// edges for context first
|
2022-04-25 17:53:45 +02:00
|
|
|
|
2022-03-17 23:14:53 +01:00
|
|
|
// A workaround was added in TrackEdge::incrementT
|
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
List<Entry<TrackNode, TrackEdge>> validTargets = new ArrayList<>();
|
|
|
|
|
2022-02-16 03:14:31 +01:00
|
|
|
boolean forward = distance > 0;
|
|
|
|
double collectedDistance = forward ? -prevPos : -edgeLength + prevPos;
|
2022-03-17 15:41:09 +01:00
|
|
|
|
|
|
|
Double blockedLocation =
|
|
|
|
edgeTraversedFrom(graph, forward, signalListener, turnListener, prevPos, collectedDistance);
|
|
|
|
if (blockedLocation != null) {
|
|
|
|
position = blockedLocation.doubleValue();
|
|
|
|
traveled = position - prevPos;
|
|
|
|
return traveled;
|
|
|
|
}
|
2022-02-16 03:14:31 +01:00
|
|
|
|
|
|
|
if (forward) {
|
2022-02-05 02:06:31 +01:00
|
|
|
// Moving forward
|
|
|
|
while (position > edgeLength) {
|
|
|
|
validTargets.clear();
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
for (Entry<TrackNode, TrackEdge> entry : graph.getConnectionsFrom(node2)
|
|
|
|
.entrySet()) {
|
|
|
|
TrackNode newNode = entry.getKey();
|
|
|
|
if (newNode == node1)
|
|
|
|
continue;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
TrackEdge newEdge = entry.getValue();
|
2022-05-15 21:19:02 +02:00
|
|
|
if (!edge.canTravelTo(newEdge))
|
2022-02-05 02:06:31 +01:00
|
|
|
continue;
|
2022-02-01 01:14:21 +01:00
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
validTargets.add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validTargets.isEmpty()) {
|
|
|
|
traveled -= position - edgeLength;
|
|
|
|
position = edgeLength;
|
|
|
|
blocked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry<TrackNode, TrackEdge> entry = validTargets.size() == 1 ? validTargets.get(0)
|
|
|
|
: trackSelector.apply(graph, Pair.of(true, validTargets));
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
if (entry.getValue()
|
|
|
|
.getLength() == 0 && portalListener.test(
|
|
|
|
Couple.create(node2.getLocation(), entry.getKey()
|
|
|
|
.getLocation()))) {
|
|
|
|
traveled -= position - edgeLength;
|
|
|
|
position = edgeLength;
|
|
|
|
blocked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
node1 = node2;
|
|
|
|
node2 = entry.getKey();
|
|
|
|
edge = entry.getValue();
|
|
|
|
position -= edgeLength;
|
2022-02-16 03:14:31 +01:00
|
|
|
|
|
|
|
collectedDistance += edgeLength;
|
2022-03-11 23:37:41 +01:00
|
|
|
if (edge.isTurn())
|
|
|
|
turnListener.accept(collectedDistance, edge);
|
2022-02-16 03:14:31 +01:00
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
blockedLocation = edgeTraversedFrom(graph, forward, signalListener, turnListener, 0, collectedDistance);
|
|
|
|
|
|
|
|
if (blockedLocation != null) {
|
|
|
|
traveled -= position;
|
|
|
|
position = blockedLocation.doubleValue();
|
|
|
|
traveled += position;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPos = 0;
|
2022-04-25 17:53:45 +02:00
|
|
|
edgeLength = edge.getLength();
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
} else {
|
|
|
|
// Moving backwards
|
|
|
|
while (position < 0) {
|
|
|
|
validTargets.clear();
|
|
|
|
|
|
|
|
for (Entry<TrackNode, TrackEdge> entry : graph.getConnectionsFrom(node1)
|
|
|
|
.entrySet()) {
|
|
|
|
TrackNode newNode = entry.getKey();
|
|
|
|
if (newNode == node2)
|
|
|
|
continue;
|
2022-05-15 21:19:02 +02:00
|
|
|
if (!graph.getConnectionsFrom(newNode)
|
|
|
|
.get(node1)
|
|
|
|
.canTravelTo(edge))
|
2022-02-05 02:06:31 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
validTargets.add(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validTargets.isEmpty()) {
|
2022-05-04 01:05:03 +02:00
|
|
|
traveled -= position;
|
2022-02-05 02:06:31 +01:00
|
|
|
position = 0;
|
|
|
|
blocked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry<TrackNode, TrackEdge> entry = validTargets.size() == 1 ? validTargets.get(0)
|
|
|
|
: trackSelector.apply(graph, Pair.of(false, validTargets));
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
if (entry.getValue()
|
|
|
|
.getLength() == 0 && portalListener.test(
|
|
|
|
Couple.create(entry.getKey()
|
|
|
|
.getLocation(), node1.getLocation()))) {
|
|
|
|
traveled -= position;
|
|
|
|
position = 0;
|
|
|
|
blocked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-05 02:06:31 +01:00
|
|
|
node2 = node1;
|
|
|
|
node1 = entry.getKey();
|
|
|
|
edge = graph.getConnectionsFrom(node1)
|
|
|
|
.get(node2);
|
2022-02-16 03:14:31 +01:00
|
|
|
collectedDistance += edgeLength;
|
2022-04-25 17:53:45 +02:00
|
|
|
edgeLength = edge.getLength();
|
2022-02-05 02:06:31 +01:00
|
|
|
position += edgeLength;
|
2022-02-16 03:14:31 +01:00
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
blockedLocation =
|
|
|
|
edgeTraversedFrom(graph, forward, signalListener, turnListener, edgeLength, collectedDistance);
|
|
|
|
|
|
|
|
if (blockedLocation != null) {
|
|
|
|
traveled -= position;
|
|
|
|
position = blockedLocation.doubleValue();
|
|
|
|
traveled += position;
|
|
|
|
break;
|
|
|
|
}
|
2022-02-05 02:06:31 +01:00
|
|
|
}
|
2022-02-01 01:14:21 +01:00
|
|
|
|
|
|
|
}
|
2022-02-05 02:06:31 +01:00
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
return traveled;
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
private Double edgeTraversedFrom(TrackGraph graph, boolean forward, IEdgePointListener edgePointListener,
|
2022-03-11 23:37:41 +01:00
|
|
|
ITurnListener turnListener, double prevPos, double totalDistance) {
|
|
|
|
if (edge.isTurn())
|
|
|
|
turnListener.accept(Math.max(0, totalDistance), edge);
|
2022-02-16 03:14:31 +01:00
|
|
|
|
|
|
|
double from = forward ? prevPos : position;
|
|
|
|
double to = forward ? position : prevPos;
|
2022-05-15 21:19:02 +02:00
|
|
|
|
|
|
|
EdgeData edgeData = edge.getEdgeData();
|
2022-03-17 15:41:09 +01:00
|
|
|
List<TrackEdgePoint> edgePoints = edgeData.getPoints();
|
|
|
|
|
2022-04-25 17:53:45 +02:00
|
|
|
double length = edge.getLength();
|
2022-03-17 15:41:09 +01:00
|
|
|
for (int i = 0; i < edgePoints.size(); i++) {
|
|
|
|
int index = forward ? i : edgePoints.size() - i - 1;
|
|
|
|
TrackEdgePoint nextBoundary = edgePoints.get(index);
|
2022-04-25 17:53:45 +02:00
|
|
|
double locationOn = nextBoundary.getLocationOn(edge);
|
2022-03-17 15:41:09 +01:00
|
|
|
double distance = forward ? locationOn : length - locationOn;
|
|
|
|
if (forward ? (locationOn < from || locationOn >= to) : (locationOn <= from || locationOn > to))
|
|
|
|
continue;
|
|
|
|
Couple<TrackNode> nodes = Couple.create(node1, node2);
|
|
|
|
if (edgePointListener.test(totalDistance + distance, Pair.of(nextBoundary, forward ? nodes : nodes.swap())))
|
|
|
|
return locationOn;
|
2022-02-16 03:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-17 15:41:09 +01:00
|
|
|
return null;
|
2022-02-16 03:14:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:51:14 +01:00
|
|
|
public void reverse(TrackGraph graph) {
|
2022-02-01 01:14:21 +01:00
|
|
|
TrackNode n = node1;
|
|
|
|
node1 = node2;
|
|
|
|
node2 = n;
|
2022-04-25 17:53:45 +02:00
|
|
|
position = edge.getLength() - position;
|
2022-02-01 01:14:21 +01:00
|
|
|
edge = graph.getConnectionsFrom(node1)
|
|
|
|
.get(node2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3 getPosition() {
|
2023-05-09 18:23:47 +02:00
|
|
|
return getPosition(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3 getPosition(boolean flipped) {
|
|
|
|
return getPositionWithOffset(0, flipped);
|
2022-05-15 21:19:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3 getPositionWithOffset(double offset) {
|
2023-05-09 18:23:47 +02:00
|
|
|
return getPositionWithOffset(offset, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vec3 getPositionWithOffset(double offset, boolean flipUpsideDown) {
|
2022-05-15 21:19:02 +02:00
|
|
|
double t = (position + offset) / edge.getLength();
|
2022-04-25 17:53:45 +02:00
|
|
|
return edge.getPosition(t)
|
2023-05-09 18:23:47 +02:00
|
|
|
.add(edge.getNormal(node1, node2, t)
|
|
|
|
.scale(upsideDown^flipUpsideDown ? -1 : 1));
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:51:14 +01:00
|
|
|
public void migrateTo(List<GraphLocation> locations) {
|
|
|
|
GraphLocation location = locations.remove(0);
|
|
|
|
TrackGraph graph = location.graph;
|
|
|
|
node1 = graph.locateNode(location.edge.getFirst());
|
|
|
|
node2 = graph.locateNode(location.edge.getSecond());
|
|
|
|
position = location.position;
|
|
|
|
edge = graph.getConnectionsFrom(node1)
|
|
|
|
.get(node2);
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
public CompoundTag write(DimensionPalette dimensions) {
|
2022-03-09 21:33:28 +01:00
|
|
|
CompoundTag tag = new CompoundTag();
|
|
|
|
Couple<TrackNode> nodes = Couple.create(node1, node2);
|
|
|
|
if (nodes.either(Objects::isNull))
|
|
|
|
return tag;
|
|
|
|
tag.put("Nodes", nodes.map(TrackNode::getLocation)
|
2022-05-15 21:19:02 +02:00
|
|
|
.serializeEach(loc -> loc.write(dimensions)));
|
2022-03-09 21:33:28 +01:00
|
|
|
tag.putDouble("Position", position);
|
2023-05-09 18:23:47 +02:00
|
|
|
tag.putBoolean("UpsideDown", upsideDown);
|
2022-03-09 21:33:28 +01:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
public static TravellingPoint read(CompoundTag tag, TrackGraph graph, DimensionPalette dimensions) {
|
2022-03-09 21:33:28 +01:00
|
|
|
if (graph == null)
|
2023-05-09 18:23:47 +02:00
|
|
|
return new TravellingPoint(null, null, null, 0, false);
|
2022-03-09 21:33:28 +01:00
|
|
|
|
2022-05-15 21:19:02 +02:00
|
|
|
Couple<TrackNode> locs = tag.contains("Nodes")
|
|
|
|
? Couple.deserializeEach(tag.getList("Nodes", Tag.TAG_COMPOUND), c -> TrackNodeLocation.read(c, dimensions))
|
|
|
|
.map(graph::locateNode)
|
|
|
|
: Couple.create(null, null);
|
2022-03-09 21:33:28 +01:00
|
|
|
|
|
|
|
if (locs.either(Objects::isNull))
|
2023-05-09 18:23:47 +02:00
|
|
|
return new TravellingPoint(null, null, null, 0, false);
|
2022-03-09 21:33:28 +01:00
|
|
|
|
|
|
|
double position = tag.getDouble("Position");
|
|
|
|
return new TravellingPoint(locs.getFirst(), locs.getSecond(), graph.getConnectionsFrom(locs.getFirst())
|
2023-05-09 18:23:47 +02:00
|
|
|
.get(locs.getSecond()), position, tag.getBoolean("UpsideDown"));
|
2022-03-09 21:33:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 01:14:21 +01:00
|
|
|
}
|