From b56cdcda523a26513612c6236614b5daf2a34d42 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 14 Nov 2018 14:19:24 -0800 Subject: [PATCH] remove that toxic cloud --- .../api/behavior/IPathingBehavior.java | 2 +- .../baritone/behavior/PathingBehavior.java | 80 +++++++++---------- .../pathing/calc/AbstractNodeCostSearch.java | 13 ++- .../baritone/pathing/path/PathExecutor.java | 2 +- .../java/baritone/utils/PathRenderer.java | 3 +- .../baritone/utils/PathingControlManager.java | 5 +- 6 files changed, 46 insertions(+), 59 deletions(-) diff --git a/src/api/java/baritone/api/behavior/IPathingBehavior.java b/src/api/java/baritone/api/behavior/IPathingBehavior.java index e9ebe405..0f44f2ee 100644 --- a/src/api/java/baritone/api/behavior/IPathingBehavior.java +++ b/src/api/java/baritone/api/behavior/IPathingBehavior.java @@ -71,7 +71,7 @@ public interface IPathingBehavior extends IBehavior { /** * @return The current path finder being executed */ - Optional getPathFinder(); + Optional getInProgress(); /** * @return The current path executor diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 7be1d405..14f1694e 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -24,7 +24,6 @@ import baritone.api.event.events.PlayerUpdateEvent; import baritone.api.event.events.RenderEvent; import baritone.api.event.events.TickEvent; import baritone.api.pathing.calc.IPath; -import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.BetterBlockPos; @@ -57,7 +56,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, private boolean cancelRequested; private boolean calcFailedLastTick; - private volatile boolean isPathCalcInProgress; + private volatile AbstractNodeCostSearch inProgress; private final Object pathCalcLock = new Object(); private final Object pathPlanLock = new Object(); @@ -141,7 +140,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } // at this point, current just ended, but we aren't in the goal and have no plan for the future synchronized (pathCalcLock) { - if (isPathCalcInProgress) { + if (inProgress != null) { queuePathEvent(PathEvent.PATH_FINISHED_NEXT_STILL_CALCULATING); // if we aren't calculating right now return; @@ -166,7 +165,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, next = null; } synchronized (pathCalcLock) { - if (isPathCalcInProgress) { + if (inProgress != null) { // if we aren't calculating right now return; } @@ -238,8 +237,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } @Override - public Optional getPathFinder() { - return Optional.ofNullable(AbstractNodeCostSearch.currentlyRunning()); + public Optional getInProgress() { + return Optional.ofNullable(inProgress); } @Override @@ -284,7 +283,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, current = null; next = null; cancelRequested = true; - AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(AbstractNodeCostSearch::cancel); + getInProgress().ifPresent(AbstractNodeCostSearch::cancel); // only cancel ours // do everything BUT clear keys } @@ -294,14 +293,14 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, current = null; next = null; baritone.getInputOverrideHandler().clearAllKeys(); - AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(AbstractNodeCostSearch::cancel); + getInProgress().ifPresent(AbstractNodeCostSearch::cancel); baritone.getInputOverrideHandler().getBlockBreakHelper().stopBreakingBlock(); } public void forceCancel() { // NOT exposed on public api cancelEverything(); secretInternalSegmentCancel(); - isPathCalcInProgress = false; + inProgress = null; } /** @@ -321,7 +320,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return false; } synchronized (pathCalcLock) { - if (isPathCalcInProgress) { + if (inProgress != null) { return false; } queuePathEvent(PathEvent.CALC_STARTED); @@ -383,19 +382,35 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, * @param talkAboutIt */ private void findPathInNewThread(final BlockPos start, final boolean talkAboutIt, final Optional previous) { - synchronized (pathCalcLock) { - if (isPathCalcInProgress) { - throw new IllegalStateException("Already doing it"); - } - isPathCalcInProgress = true; + // this must be called with synchronization on pathCalcLock! + // actually, we can check this, muahaha + if (!Thread.holdsLock(pathCalcLock)) { + throw new IllegalStateException("Must be called with synchronization on pathCalcLock"); + // why do it this way? it's already indented so much that putting the whole thing in a synchronized(pathCalcLock) was just too much lol + } + if (inProgress != null) { + throw new IllegalStateException("Already doing it"); // should have been checked by caller + } + Goal goal = this.goal; + if (goal == null) { + logDebug("no goal"); // TODO should this be an exception too? definitely should be checked by caller + return; + } + long timeout; + if (current == null) { + timeout = Baritone.settings().pathTimeoutMS.get(); + } else { + timeout = Baritone.settings().planAheadTimeoutMS.get(); } CalculationContext context = new CalculationContext(baritone); // not safe to create on the other thread, it looks up a lot of stuff in minecraft + AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, previous, context); + inProgress = pathfinder; Baritone.getExecutor().execute(() -> { if (talkAboutIt) { logDebug("Starting to search for path from " + start + " to " + goal); } - PathCalculationResult calcResult = findPath(start, previous, context); + PathCalculationResult calcResult = pathfinder.calculate(timeout); Optional path = calcResult.path; if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(p -> { @@ -453,51 +468,28 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } } synchronized (pathCalcLock) { - isPathCalcInProgress = false; + inProgress = null; } } }); } - /** - * Actually do the pathing - * - * @param start - * @return - */ - private PathCalculationResult findPath(BlockPos start, Optional previous, CalculationContext context) { - Goal goal = this.goal; - if (goal == null) { - logDebug("no goal"); - return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, Optional.empty()); - } + private AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, Optional previous, CalculationContext context) { + Goal transformed = goal; if (Baritone.settings().simplifyUnloadedYCoord.get() && goal instanceof IGoalRenderPos) { BlockPos pos = ((IGoalRenderPos) goal).getGoalPos(); if (context.world().getChunk(pos) instanceof EmptyChunk) { logDebug("Simplifying " + goal.getClass() + " to GoalXZ due to distance"); - goal = new GoalXZ(pos.getX(), pos.getZ()); + transformed = new GoalXZ(pos.getX(), pos.getZ()); } } - long timeout; - if (current == null) { - timeout = Baritone.settings().pathTimeoutMS.get(); - } else { - timeout = Baritone.settings().planAheadTimeoutMS.get(); - } Optional> favoredPositions; if (Baritone.settings().backtrackCostFavoringCoefficient.get() == 1D) { favoredPositions = Optional.empty(); } else { favoredPositions = previous.map(IPath::positions).map(Collection::stream).map(x -> x.map(BetterBlockPos::longHash)).map(x -> x.collect(Collectors.toList())).map(HashSet::new); // <-- okay this is EPIC } - try { - IPathFinder pf = new AStarPathFinder(start.getX(), start.getY(), start.getZ(), goal, favoredPositions, context); - return pf.calculate(timeout); - } catch (Exception e) { - logDebug("Pathing exception: " + e); - e.printStackTrace(); - return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty()); - } + return new AStarPathFinder(start.getX(), start.getY(), start.getZ(), transformed, favoredPositions, context); } @Override diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 79a56037..a0f4f6e4 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -23,6 +23,7 @@ import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; import baritone.api.utils.PathCalculationResult; import baritone.pathing.movement.CalculationContext; +import baritone.utils.Helper; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import java.util.Optional; @@ -107,6 +108,10 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } else { return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path); } + } catch (Exception e) { + Helper.HELPER.logDebug("Pathing exception: " + e); + e.printStackTrace(); + return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty()); } finally { // this is run regardless of what exception may or may not be raised by calculate0 currentlyRunning = null; @@ -218,12 +223,4 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { public final Goal getGoal() { return goal; } - - public static Optional getCurrentlyRunning() { - return Optional.ofNullable(currentlyRunning); - } - - public static AbstractNodeCostSearch currentlyRunning() { - return currentlyRunning; - } } diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index dca2a0e9..5dfc420d 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -295,7 +295,7 @@ public class PathExecutor implements IPathExecutor, Helper { } private boolean shouldPause() { - Optional current = AbstractNodeCostSearch.getCurrentlyRunning(); + Optional current = behavior.getInProgress(); if (!current.isPresent()) { return false; } diff --git a/src/main/java/baritone/utils/PathRenderer.java b/src/main/java/baritone/utils/PathRenderer.java index 443cd035..e14c14da 100644 --- a/src/main/java/baritone/utils/PathRenderer.java +++ b/src/main/java/baritone/utils/PathRenderer.java @@ -28,7 +28,6 @@ import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.behavior.PathingBehavior; -import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.path.PathExecutor; import net.minecraft.block.state.IBlockState; import net.minecraft.client.renderer.BufferBuilder; @@ -113,7 +112,7 @@ public final class PathRenderer implements Helper { } // If there is a path calculation currently running, render the path calculation process - AbstractNodeCostSearch.getCurrentlyRunning().ifPresent(currentlyRunning -> { + behavior.getInProgress().ifPresent(currentlyRunning -> { currentlyRunning.bestPathSoFar().ifPresent(p -> { drawPath(p, 0, renderView, partialTicks, Baritone.settings().colorBestPathSoFar.get(), Baritone.settings().fadePath.get(), 10, 20); }); diff --git a/src/main/java/baritone/utils/PathingControlManager.java b/src/main/java/baritone/utils/PathingControlManager.java index 9ea193a1..a818b7c5 100644 --- a/src/main/java/baritone/utils/PathingControlManager.java +++ b/src/main/java/baritone/utils/PathingControlManager.java @@ -24,7 +24,6 @@ import baritone.api.pathing.goals.Goal; import baritone.api.process.IBaritoneProcess; import baritone.api.process.PathingCommand; import baritone.behavior.PathingBehavior; -import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.path.PathExecutor; import net.minecraft.util.math.BlockPos; @@ -90,12 +89,12 @@ public class PathingControlManager { p.cancelSegmentIfSafe(); break; case FORCE_REVALIDATE_GOAL_AND_PATH: - if (!p.isPathing() && !AbstractNodeCostSearch.getCurrentlyRunning().isPresent()) { + if (!p.isPathing() && !p.getInProgress().isPresent()) { p.secretInternalSetGoalAndPath(command.goal); } break; case REVALIDATE_GOAL_AND_PATH: - if (!p.isPathing() && !AbstractNodeCostSearch.getCurrentlyRunning().isPresent()) { + if (!p.isPathing() && !p.getInProgress().isPresent()) { p.secretInternalSetGoalAndPath(command.goal); } break;