From 1245e222a761ffc3ac4e8be831b01140e5b057df Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 8 Oct 2018 19:23:43 -0500 Subject: [PATCH] Begin path api prep --- .../baritone/behavior/PathingBehavior.java | 26 ++++++++- .../baritone/pathing/path/CutoffResult.java | 54 +++++++++++++++++++ .../java/baritone/pathing/path/IPath.java | 29 +++++----- 3 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 src/main/java/baritone/pathing/path/CutoffResult.java diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 2092c656..16c88b92 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -30,6 +30,7 @@ import baritone.pathing.calc.AStarPathFinder; import baritone.pathing.calc.AbstractNodeCostSearch; import baritone.pathing.calc.IPathFinder; import baritone.pathing.movement.MovementHelper; +import baritone.pathing.path.CutoffResult; import baritone.pathing.path.IPath; import baritone.pathing.path.PathExecutor; import baritone.utils.BlockBreakHelper; @@ -283,9 +284,30 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, Optional path = findPath(start, previous); if (Baritone.settings().cutoffAtLoadBoundary.get()) { - path = path.map(IPath::cutoffAtLoadedChunks); + path = path.map(p -> { + CutoffResult result = p.cutoffAtLoadedChunks(); + + if (result.wasCut()) { + logDebug("Cutting off path at edge of loaded chunks"); + logDebug("Length decreased by " + result.getRemoved()); + } else { + logDebug("Path ends within loaded chunks"); + } + + return result.getPath(); + }); } - Optional executor = path.map(p -> p.staticCutoff(goal)).map(PathExecutor::new); + + Optional executor = path.map(p -> { + CutoffResult result = p.staticCutoff(goal); + + if (result.wasCut()) { + logDebug("Static cutoff " + p.length() + " to " + result.getPath().length()); + } + + return result.getPath(); + }).map(PathExecutor::new); + synchronized (pathPlanLock) { if (current == null) { if (executor.isPresent()) { diff --git a/src/main/java/baritone/pathing/path/CutoffResult.java b/src/main/java/baritone/pathing/path/CutoffResult.java new file mode 100644 index 00000000..d1c03c1e --- /dev/null +++ b/src/main/java/baritone/pathing/path/CutoffResult.java @@ -0,0 +1,54 @@ +/* + * 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.pathing.path; + +/** + * @author Brady + * @since 10/8/2018 + */ +public final class CutoffResult { + + private final IPath path; + + private final int removed; + + private CutoffResult(IPath path, int removed) { + this.path = path; + this.removed = removed; + } + + public final boolean wasCut() { + return this.removed > 0; + } + + public final int getRemoved() { + return this.removed; + } + + public final IPath getPath() { + return this.path; + } + + public static CutoffResult cutoffPath(IPath path, int index) { + return new CutoffResult(new CutoffPath(path, index), path.positions().size() - index - 1); + } + + public static CutoffResult preservePath(IPath path) { + return new CutoffResult(path, 0); + } +} diff --git a/src/main/java/baritone/pathing/path/IPath.java b/src/main/java/baritone/pathing/path/IPath.java index 0701abf6..5741c440 100644 --- a/src/main/java/baritone/pathing/path/IPath.java +++ b/src/main/java/baritone/pathing/path/IPath.java @@ -17,10 +17,9 @@ package baritone.pathing.path; -import baritone.Baritone; +import baritone.api.BaritoneAPI; import baritone.api.pathing.goals.Goal; import baritone.pathing.movement.Movement; -import baritone.utils.Helper; import baritone.utils.Utils; import baritone.utils.pathing.BetterBlockPos; import net.minecraft.client.Minecraft; @@ -33,7 +32,7 @@ import java.util.List; /** * @author leijurv */ -public interface IPath extends Helper { +public interface IPath { /** * Ordered list of movements to carry out. @@ -87,7 +86,7 @@ public interface IPath extends Helper { /** * Where does this path start */ - default BetterBlockPos getSrc() { + default BlockPos getSrc() { return positions().get(0); } @@ -110,29 +109,25 @@ public interface IPath extends Helper { int getNumNodesConsidered(); - default IPath cutoffAtLoadedChunks() { + default CutoffResult cutoffAtLoadedChunks() { for (int i = 0; i < positions().size(); i++) { BlockPos pos = positions().get(i); if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { - logDebug("Cutting off path at edge of loaded chunks"); - logDebug("Length decreased by " + (positions().size() - i - 1)); - return new CutoffPath(this, i); + return CutoffResult.cutoffPath(this, i); } } - logDebug("Path ends within loaded chunks"); - return this; + return CutoffResult.preservePath(this); } - default IPath staticCutoff(Goal destination) { - if (length() < Baritone.settings().pathCutoffMinimumLength.get()) { - return this; + default CutoffResult staticCutoff(Goal destination) { + if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { + return CutoffResult.preservePath(this); } if (destination == null || destination.isInGoal(getDest())) { - return this; + return CutoffResult.preservePath(this); } - double factor = Baritone.settings().pathCutoffFactor.get(); + double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); int newLength = (int) (length() * factor); - logDebug("Static cutoff " + length() + " to " + newLength); - return new CutoffPath(this, newLength); + return CutoffResult.cutoffPath(this, newLength); } }