improve path checks, and add a overlap splice option
This commit is contained in:
parent
b228f4c6fb
commit
6ed8d617cd
@ -21,9 +21,9 @@ import baritone.api.Settings;
|
||||
import baritone.api.pathing.goals.Goal;
|
||||
import baritone.api.pathing.movement.IMovement;
|
||||
import baritone.api.utils.BetterBlockPos;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -153,9 +153,10 @@ public interface IPath {
|
||||
if (path.size() != movements.size() + 1) {
|
||||
throw new IllegalStateException("Size of path array is unexpected");
|
||||
}
|
||||
HashSet<BetterBlockPos> seenSoFar = new HashSet<>();
|
||||
for (int i = 0; i < path.size() - 1; i++) {
|
||||
BlockPos src = path.get(i);
|
||||
BlockPos dest = path.get(i + 1);
|
||||
BetterBlockPos src = path.get(i);
|
||||
BetterBlockPos dest = path.get(i + 1);
|
||||
IMovement movement = movements.get(i);
|
||||
if (!src.equals(movement.getSrc())) {
|
||||
throw new IllegalStateException("Path source is not equal to the movement source");
|
||||
@ -163,6 +164,10 @@ public interface IPath {
|
||||
if (!dest.equals(movement.getDest())) {
|
||||
throw new IllegalStateException("Path destination is not equal to the movement destination");
|
||||
}
|
||||
if (seenSoFar.contains(src)) {
|
||||
throw new IllegalStateException("Path doubles back on itself, making a loop");
|
||||
}
|
||||
seenSoFar.add(src);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -458,7 +458,7 @@ public class PathExecutor implements IPathExecutor, Helper {
|
||||
if (next == null) {
|
||||
return cutIfTooLong();
|
||||
}
|
||||
return SplicedPath.trySplice(path, next.path).map(path -> {
|
||||
return SplicedPath.trySplice(path, next.path, false).map(path -> {
|
||||
if (!path.getDest().equals(next.getPath().getDest())) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class SplicedPath extends PathBase {
|
||||
return numNodes;
|
||||
}
|
||||
|
||||
public static Optional<SplicedPath> trySplice(IPath first, IPath second) {
|
||||
public static Optional<SplicedPath> trySplice(IPath first, IPath second, boolean allowOverlapCutoff) {
|
||||
if (second == null || first == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@ -72,18 +72,31 @@ public class SplicedPath extends PathBase {
|
||||
if (!first.getDest().equals(second.getSrc())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
HashSet<BetterBlockPos> a = new HashSet<>(first.positions());
|
||||
for (int i = 1; i < second.length(); i++) {
|
||||
if (a.contains(second.positions().get(i))) {
|
||||
HashSet<BetterBlockPos> secondPos = new HashSet<>(second.positions());
|
||||
int firstPositionInSecond = -1;
|
||||
for (int i = 0; i < first.length() - 1; i++) { // overlap in the very last element is fine (and required) so only go up to first.length() - 1
|
||||
if (secondPos.contains(first.positions().get(i))) {
|
||||
firstPositionInSecond = i;
|
||||
}
|
||||
}
|
||||
if (firstPositionInSecond != -1) {
|
||||
if (!allowOverlapCutoff) {
|
||||
return Optional.empty();
|
||||
}
|
||||
} else {
|
||||
firstPositionInSecond = first.length() - 1;
|
||||
}
|
||||
int positionInSecond = second.positions().indexOf(first.positions().get(firstPositionInSecond));
|
||||
if (!allowOverlapCutoff && positionInSecond != 0) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
List<BetterBlockPos> positions = new ArrayList<>();
|
||||
List<IMovement> movements = new ArrayList<>();
|
||||
positions.addAll(first.positions());
|
||||
positions.addAll(second.positions().subList(1, second.length()));
|
||||
movements.addAll(first.movements());
|
||||
movements.addAll(second.movements());
|
||||
positions.addAll(first.positions().subList(0, firstPositionInSecond + 1));
|
||||
movements.addAll(first.movements().subList(0, firstPositionInSecond));
|
||||
|
||||
positions.addAll(second.positions().subList(positionInSecond + 1, second.length()));
|
||||
movements.addAll(second.movements().subList(positionInSecond, second.length() - 1));
|
||||
return Optional.of(new SplicedPath(positions, movements, first.getNumNodesConsidered() + second.getNumNodesConsidered(), first.getGoal()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user