From 4a1951b027867a1400fc31dd183901d3782a5015 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 7 Nov 2018 14:09:23 -0800 Subject: [PATCH] many fixes --- src/api/java/baritone/api/Settings.java | 2 +- .../api/pathing/calc/IPathFinder.java | 3 +- .../api/process/PathingCommandType.java | 2 +- .../api/utils/PathCalculationResult.java | 41 +++++++++++++++++++ .../baritone/behavior/PathingBehavior.java | 15 ++++--- .../pathing/calc/AbstractNodeCostSearch.java | 15 ++++++- .../baritone/process/GetToBlockProcess.java | 13 +++--- .../java/baritone/process/MineProcess.java | 30 +++++++++----- .../baritone/utils/PathingControlManager.java | 13 +++++- 9 files changed, 108 insertions(+), 26 deletions(-) create mode 100644 src/api/java/baritone/api/utils/PathCalculationResult.java diff --git a/src/api/java/baritone/api/Settings.java b/src/api/java/baritone/api/Settings.java index 419e8e71..e2e4604d 100644 --- a/src/api/java/baritone/api/Settings.java +++ b/src/api/java/baritone/api/Settings.java @@ -411,7 +411,7 @@ public class Settings { *

* Also on cosmic prisons this should be set to true since you don't actually mine the ore it just gets replaced with stone. */ - public Setting cancelOnGoalInvalidation = new Setting<>(false); + public Setting cancelOnGoalInvalidation = new Setting<>(true); /** * The "axis" command (aka GoalAxis) will go to a axis, or diagonal axis, at this Y level. diff --git a/src/api/java/baritone/api/pathing/calc/IPathFinder.java b/src/api/java/baritone/api/pathing/calc/IPathFinder.java index 446f7e05..f70196a6 100644 --- a/src/api/java/baritone/api/pathing/calc/IPathFinder.java +++ b/src/api/java/baritone/api/pathing/calc/IPathFinder.java @@ -18,6 +18,7 @@ package baritone.api.pathing.calc; import baritone.api.pathing.goals.Goal; +import baritone.api.utils.PathCalculationResult; import java.util.Optional; @@ -35,7 +36,7 @@ public interface IPathFinder { * * @return The final path */ - Optional calculate(long timeout); + PathCalculationResult calculate(long timeout); /** * Intended to be called concurrently with calculatePath from a different thread to tell if it's finished yet diff --git a/src/api/java/baritone/api/process/PathingCommandType.java b/src/api/java/baritone/api/process/PathingCommandType.java index f2336d26..da64748a 100644 --- a/src/api/java/baritone/api/process/PathingCommandType.java +++ b/src/api/java/baritone/api/process/PathingCommandType.java @@ -49,7 +49,7 @@ public enum PathingCommandType { /** * Set the goal and path. *

- * Revalidate the current goal, and cancel if it's no longer valid, or if the new goal is {@code null}. + * Cancel the current path if the goals are not equal */ FORCE_REVALIDATE_GOAL_AND_PATH } diff --git a/src/api/java/baritone/api/utils/PathCalculationResult.java b/src/api/java/baritone/api/utils/PathCalculationResult.java new file mode 100644 index 00000000..69d2a9b3 --- /dev/null +++ b/src/api/java/baritone/api/utils/PathCalculationResult.java @@ -0,0 +1,41 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils; + +import baritone.api.pathing.calc.IPath; + +import java.util.Optional; + +public class PathCalculationResult { + + public final Optional path; + public final Type type; + + public PathCalculationResult(Type type, Optional path) { + this.path = path; + this.type = type; + } + + public enum Type { + SUCCESS_TO_GOAL, + SUCCESS_SEGMENT, + FAILURE, + CANCELLATION, + EXCEPTION, + } +} diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 53b771da..6315e15f 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -28,6 +28,7 @@ import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.BetterBlockPos; +import baritone.api.utils.PathCalculationResult; import baritone.api.utils.interfaces.IGoalRenderPos; import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; @@ -379,7 +380,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, logDebug("Starting to search for path from " + start + " to " + goal); } - Optional path = findPath(start, previous, context); + PathCalculationResult calcResult = findPath(start, previous, context); + Optional path = calcResult.path; if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(p -> { IPath result = p.cutoffAtLoadedChunks(); @@ -411,7 +413,10 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); current = executor.get(); } else { - queuePathEvent(PathEvent.CALC_FAILED); + if (calcResult.type != PathCalculationResult.Type.CANCELLATION && calcResult.type != PathCalculationResult.Type.EXCEPTION) { + // don't dispatch CALC_FAILED on cancellation + queuePathEvent(PathEvent.CALC_FAILED); + } } } else { if (next == null) { @@ -447,11 +452,11 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, * @param start * @return */ - private Optional findPath(BlockPos start, Optional previous, CalculationContext context) { + private PathCalculationResult findPath(BlockPos start, Optional previous, CalculationContext context) { Goal goal = this.goal; if (goal == null) { logDebug("no goal"); - return Optional.empty(); + return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, Optional.empty()); } if (Baritone.settings().simplifyUnloadedYCoord.get() && goal instanceof IGoalRenderPos) { BlockPos pos = ((IGoalRenderPos) goal).getGoalPos(); @@ -478,7 +483,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } catch (Exception e) { logDebug("Pathing exception: " + e); e.printStackTrace(); - return Optional.empty(); + return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty()); } } diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index bbbe32ad..981ff733 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -21,6 +21,7 @@ import baritone.Baritone; import baritone.api.pathing.calc.IPath; import baritone.api.pathing.calc.IPathFinder; import baritone.api.pathing.goals.Goal; +import baritone.api.utils.PathCalculationResult; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import java.util.Optional; @@ -82,7 +83,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { cancelRequested = true; } - public synchronized Optional calculate(long timeout) { + public synchronized PathCalculationResult calculate(long timeout) { if (isFinished) { throw new IllegalStateException("Path Finder is currently in use, and cannot be reused!"); } @@ -91,7 +92,17 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { Optional path = calculate0(timeout); path.ifPresent(IPath::postProcess); isFinished = true; - return path; + if (cancelRequested) { + return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path); + } + if (!path.isPresent()) { + return new PathCalculationResult(PathCalculationResult.Type.FAILURE, path); + } + if (goal.isInGoal(path.get().getDest())) { + return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path); + } else { + return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path); + } } finally { // this is run regardless of what exception may or may not be raised by calculate0 currentlyRunning = null; diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 94e02e03..241fafa1 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -28,6 +28,7 @@ import baritone.utils.BaritoneProcessHelper; import net.minecraft.block.Block; import net.minecraft.util.math.BlockPos; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -44,7 +45,8 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl @Override public void getToBlock(Block block) { gettingTo = block; - rescan(); + knownLocations = null; + rescan(new ArrayList<>()); } @Override @@ -55,7 +57,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl @Override public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) { if (knownLocations == null) { - rescan(); + rescan(new ArrayList<>()); } if (knownLocations.isEmpty()) { logDirect("No known locations of " + gettingTo + ", canceling GetToBlock"); @@ -73,7 +75,8 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl } int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain - Baritone.getExecutor().execute(this::rescan); + List current = new ArrayList<>(knownLocations); + Baritone.getExecutor().execute(() -> rescan(current)); } Goal goal = new GoalComposite(knownLocations.stream().map(GoalGetToBlock::new).toArray(Goal[]::new)); if (goal.isInGoal(playerFeet())) { @@ -93,7 +96,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl return "Get To Block " + gettingTo; } - private void rescan() { - knownLocations = MineProcess.searchWorld(Collections.singletonList(gettingTo), 64, world()); + private void rescan(List known) { + knownLocations = MineProcess.searchWorld(Collections.singletonList(gettingTo), 64, world(), known); } } \ No newline at end of file diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index bfceac75..48325dbb 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -87,7 +87,8 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain - baritone.getExecutor().execute(this::rescan); + List curr = new ArrayList<>(knownOreLocations); + baritone.getExecutor().execute(() -> rescan(curr)); } if (Baritone.settings().legitMine.get()) { addNearby(); @@ -99,7 +100,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro cancel(); return null; } - return new PathingCommand(goal, PathingCommandType.REVALIDATE_GOAL_AND_PATH); + return new PathingCommand(goal, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH); } @Override @@ -126,8 +127,8 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro return null; } // only in non-Xray mode (aka legit mode) do we do this + int y = Baritone.settings().legitMineYLevel.get(); if (branchPoint == null) { - int y = Baritone.settings().legitMineYLevel.get(); if (!baritone.getPathingBehavior().isPathing() && playerFeet().y == y) { // cool, path is over and we are at desired y branchPoint = playerFeet(); @@ -136,21 +137,26 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } } - if (playerFeet().equals(branchPoint)) { + /*if (playerFeet().equals(branchPoint)) { // TODO mine 1x1 shafts to either side branchPoint = branchPoint.north(10); - } - return new GoalBlock(branchPoint); + }*/ + return new GoalRunAway(1, Optional.of(y), branchPoint) { + @Override + public boolean isInGoal(int x, int y, int z) { + return false; + } + }; } - private void rescan() { + private void rescan(List already) { if (mining == null) { return; } if (Baritone.settings().legitMine.get()) { return; } - List locs = searchWorld(mining, ORE_LOCATIONS_COUNT, world()); + List locs = searchWorld(mining, ORE_LOCATIONS_COUNT, world(), already); locs.addAll(droppedItemsScan(mining, world())); if (locs.isEmpty()) { logDebug("No locations for " + mining + " known, cancelling"); @@ -205,7 +211,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro return ret; } - public static List searchWorld(List mining, int max, World world) { + /*public static List searchWorld(List mining, int max, World world) { + + }*/ + public static List searchWorld(List mining, int max, World world, List alreadyKnown) { List locs = new ArrayList<>(); List uninteresting = new ArrayList<>(); //long b = System.currentTimeMillis(); @@ -225,6 +234,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(uninteresting, max, 10, 26)); //System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms"); } + locs.addAll(alreadyKnown); return prune(locs, mining, max, world); } @@ -283,6 +293,6 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro this.desiredQuantity = quantity; this.knownOreLocations = new ArrayList<>(); this.branchPoint = null; - rescan(); + rescan(new ArrayList<>()); } } diff --git a/src/main/java/baritone/utils/PathingControlManager.java b/src/main/java/baritone/utils/PathingControlManager.java index e75f7796..5d1b0303 100644 --- a/src/main/java/baritone/utils/PathingControlManager.java +++ b/src/main/java/baritone/utils/PathingControlManager.java @@ -78,7 +78,7 @@ public class PathingControlManager { break; case FORCE_REVALIDATE_GOAL_AND_PATH: p.secretInternalSetGoalAndPath(cmd.goal); - if (cmd.goal == null || revalidateGoal(cmd.goal)) { + if (cmd.goal == null || forceRevalidate(cmd.goal) || revalidateGoal(cmd.goal)) { // pwnage p.cancelSegmentIfSafe(); } @@ -100,6 +100,17 @@ public class PathingControlManager { } } + public boolean forceRevalidate(Goal newGoal) { + PathExecutor current = baritone.getPathingBehavior().getCurrent(); + if (current != null) { + if (newGoal.isInGoal(current.getPath().getDest())) { + return false; + } + return !newGoal.toString().equals(current.getPath().getGoal().toString()); + } + return false; + } + public boolean revalidateGoal(Goal newGoal) { PathExecutor current = baritone.getPathingBehavior().getCurrent(); if (current != null) {