From ff108a55e6a3a82418eb631f42b4fcdebf83e737 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 19 Aug 2018 09:37:35 -0700 Subject: [PATCH] performance and testing --- .../baritone/bot/pathing/calc/PathNode.java | 2 +- .../calc/openset/BinaryHeapOpenSet.java | 46 +++++++++---------- .../bot/utils/pathing/BetterBlockPos.java | 4 +- .../pathing/calc/openset/OpenSetsTest.java | 18 +++++++- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/calc/PathNode.java b/src/main/java/baritone/bot/pathing/calc/PathNode.java index fd561320..275d1361 100644 --- a/src/main/java/baritone/bot/pathing/calc/PathNode.java +++ b/src/main/java/baritone/bot/pathing/calc/PathNode.java @@ -95,7 +95,7 @@ public class PathNode { */ @Override public int hashCode() { - throw new IllegalStateException(); + return pos.hashCode() * 7 + 3; } @Override diff --git a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java index 4fc0ada3..c57af18f 100644 --- a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java +++ b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java @@ -53,27 +53,41 @@ public class BinaryHeapOpenSet implements IOpenSet { } @Override - public void insert(PathNode value) { + public final void insert(PathNode value) { if (size >= array.length - 1) { array = Arrays.copyOf(array, array.length * 2); } size++; value.heapPosition = size; array[size] = value; - upHeap(size); - } - - public void update(PathNode node) { - upHeap(node.heapPosition); + update(value); } @Override - public boolean isEmpty() { + public final void update(PathNode node) { + int index = node.heapPosition; + int parentInd = index >>> 1; + PathNode val = array[index]; + double cost = val.combinedCost; + PathNode parentNode = array[parentInd]; + while (index > 1 && parentNode.combinedCost > cost) { + array[index] = parentNode; + array[parentInd] = val; + val.heapPosition = parentInd; + parentNode.heapPosition = index; + index = parentInd; + parentInd = index >>> 1; + parentNode = array[parentInd]; + } + } + + @Override + public final boolean isEmpty() { return size == 0; } @Override - public PathNode removeLowest() { + public final PathNode removeLowest() { if (size == 0) { throw new IllegalStateException(); } @@ -115,20 +129,4 @@ public class BinaryHeapOpenSet implements IOpenSet { } while (smallerChild <= size); return result; } - - private void upHeap(int index) { - int parentInd = index >>> 1; - PathNode val = array[index]; - double cost = val.combinedCost; - PathNode parentNode = array[parentInd]; - while (index > 1 && parentNode.combinedCost > cost) { - array[index] = parentNode; - array[parentInd] = val; - val.heapPosition = parentInd; - parentNode.heapPosition = index; - index = parentInd; - parentInd = index >>> 1; - parentNode = array[parentInd]; - } - } } diff --git a/src/main/java/baritone/bot/utils/pathing/BetterBlockPos.java b/src/main/java/baritone/bot/utils/pathing/BetterBlockPos.java index 0bdae99c..5d09750c 100644 --- a/src/main/java/baritone/bot/utils/pathing/BetterBlockPos.java +++ b/src/main/java/baritone/bot/utils/pathing/BetterBlockPos.java @@ -60,12 +60,12 @@ public class BetterBlockPos extends BlockPos { } @Override - public int hashCode() { + public final int hashCode() { return hashCode; } @Override - public boolean equals(Object o) { + public final boolean equals(Object o) { if (o == null) { return false; } diff --git a/src/test/java/baritone/bot/pathing/calc/openset/OpenSetsTest.java b/src/test/java/baritone/bot/pathing/calc/openset/OpenSetsTest.java index 06817b73..50277bd4 100644 --- a/src/test/java/baritone/bot/pathing/calc/openset/OpenSetsTest.java +++ b/src/test/java/baritone/bot/pathing/calc/openset/OpenSetsTest.java @@ -18,8 +18,9 @@ package baritone.bot.pathing.calc.openset; import baritone.bot.pathing.calc.PathNode; -import baritone.bot.pathing.goals.GoalBlock; +import baritone.bot.pathing.goals.Goal; import baritone.bot.utils.pathing.BetterBlockPos; +import net.minecraft.util.math.BlockPos; import org.junit.Test; import java.util.*; @@ -73,7 +74,20 @@ public class OpenSetsTest { // generate the pathnodes that we'll be testing the sets on PathNode[] toInsert = new PathNode[size]; for (int i = 0; i < size; i++) { - PathNode pn = new PathNode(new BetterBlockPos(0, 0, 0), new GoalBlock(new BetterBlockPos(0, 0, 0))); + // can't use an existing goal + // because they use Baritone.settings() + // and we can't do that because Minecraft itself isn't initted + PathNode pn = new PathNode(new BetterBlockPos(0, 0, 0), new Goal() { + @Override + public boolean isInGoal(BlockPos pos) { + return false; + } + + @Override + public double heuristic(BlockPos pos) { + return 0; + } + }); pn.combinedCost = Math.random(); toInsert[i] = pn; }