remove that toxic cloud
This commit is contained in:
		| @@ -71,7 +71,7 @@ public interface IPathingBehavior extends IBehavior { | ||||
|     /** | ||||
|      * @return The current path finder being executed | ||||
|      */ | ||||
|     Optional<IPathFinder> getPathFinder(); | ||||
|     Optional<? extends IPathFinder> getInProgress(); | ||||
|  | ||||
|     /** | ||||
|      * @return The current path executor | ||||
|   | ||||
| @@ -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<IPathFinder> getPathFinder() { | ||||
|         return Optional.ofNullable(AbstractNodeCostSearch.currentlyRunning()); | ||||
|     public Optional<AbstractNodeCostSearch> 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<IPath> 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.<Long>get(); | ||||
|         } else { | ||||
|             timeout = Baritone.settings().planAheadTimeoutMS.<Long>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<IPath> 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<IPath> 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<IPath> 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.<Long>get(); | ||||
|         } else { | ||||
|             timeout = Baritone.settings().planAheadTimeoutMS.<Long>get(); | ||||
|         } | ||||
|         Optional<HashSet<Long>> 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 | ||||
|   | ||||
| @@ -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<AbstractNodeCostSearch> getCurrentlyRunning() { | ||||
|         return Optional.ofNullable(currentlyRunning); | ||||
|     } | ||||
|  | ||||
|     public static AbstractNodeCostSearch currentlyRunning() { | ||||
|         return currentlyRunning; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -295,7 +295,7 @@ public class PathExecutor implements IPathExecutor, Helper { | ||||
|     } | ||||
|  | ||||
|     private boolean shouldPause() { | ||||
|         Optional<AbstractNodeCostSearch> current = AbstractNodeCostSearch.getCurrentlyRunning(); | ||||
|         Optional<AbstractNodeCostSearch> current = behavior.getInProgress(); | ||||
|         if (!current.isPresent()) { | ||||
|             return false; | ||||
|         } | ||||
|   | ||||
| @@ -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); | ||||
|             }); | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user