From 28af41b789a9551ec7d9ae21c129f3e14afdadca Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 3 Oct 2018 08:04:30 -0700 Subject: [PATCH] defer movement propagation and assembly to postprocessing --- src/main/java/baritone/pathing/calc/Path.java | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index 7f8cf085..72f1756c 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -86,11 +86,11 @@ class Path implements IPath { throw new IllegalStateException(); } PathNode current = end; - LinkedList tempPath = new LinkedList<>(); // Repeatedly inserting to the beginning of an arraylist is O(n^2) - LinkedList tempMovements = new LinkedList<>(); // Instead, do it into a linked list, then convert at the end + LinkedList tempPath = new LinkedList<>(); + // Repeatedly inserting to the beginning of an arraylist is O(n^2) + // Instead, do it into a linked list, then convert at the end while (!current.equals(start)) { tempPath.addFirst(new BetterBlockPos(current.x, current.y, current.z)); - tempMovements.addFirst(runBackwards(current.previous, current)); current = current.previous; } tempPath.addFirst(this.start); @@ -98,20 +98,6 @@ class Path implements IPath { // inserting into a LinkedList keeps track of length, then when we addall (which calls .toArray) it's able // to performantly do that conversion since it knows the length. path.addAll(tempPath); - movements.addAll(tempMovements); - } - - private static Movement runBackwards(PathNode src0, PathNode dest0) { // TODO this is horrifying - BetterBlockPos src = new BetterBlockPos(src0.x, src0.y, src0.z); - BetterBlockPos dest = new BetterBlockPos(dest0.x, dest0.y, dest0.z); - for (Moves moves : Moves.values()) { - Movement move = moves.apply0(src); - if (move.getDest().equals(dest)) { - return move; - } - } - // leave this as IllegalStateException; it's caught in AbstractNodeCostSearch - throw new IllegalStateException("Movement became impossible during calculation " + src + " " + dest + " " + dest.subtract(src)); } /** @@ -140,12 +126,33 @@ class Path implements IPath { } } + private void assembleMovements() { + if (path.isEmpty() || !movements.isEmpty()) { + throw new IllegalStateException(); + } + for (int i = 0; i < path.size() - 1; i++) { + movements.add(runBackwards(path.get(i), path.get(i + 1))); + } + } + + private static Movement runBackwards(BetterBlockPos src, BetterBlockPos dest) { // TODO this is horrifying + for (Moves moves : Moves.values()) { + Movement move = moves.apply0(src); + if (move.getDest().equals(dest)) { + return move; + } + } + // leave this as IllegalStateException; it's caught in AbstractNodeCostSearch + throw new IllegalStateException("Movement became impossible during calculation " + src + " " + dest + " " + dest.subtract(src)); + } + @Override public void postprocess() { if (verified) { throw new IllegalStateException(); } verified = true; + assembleMovements(); // more post processing here movements.forEach(Movement::checkLoadedChunk); sanityCheck();