From 58a1f65044878c568b89700201f31c2ae60ffb71 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Mon, 13 Aug 2018 12:56:08 -0700 Subject: [PATCH] cutoff at edge of loaded chunks --- .../bot/behavior/impl/PathingBehavior.java | 12 +++-- .../baritone/bot/pathing/path/CutoffPath.java | 54 +++++++++++++++++++ .../java/baritone/bot/pathing/path/IPath.java | 19 ++++++- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 src/main/java/baritone/bot/pathing/path/CutoffPath.java diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index a94a0389..2491abac 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -63,6 +63,11 @@ public class PathingBehavior extends Behavior { synchronized (pathPlanLock) { if (current.failed() || current.finished()) { current = null; + if (goal.isInGoal(playerFeet())) { + displayChatMessageRaw("All done. At " + goal); + next = null; + return; + } if (next != null && !next.getPath().positions().contains(playerFeet())) { // if the current path failed, we may not actually be on the next one, so make sure displayChatMessageRaw("Discarding next path as it does not contain current position"); @@ -78,9 +83,6 @@ public class PathingBehavior extends Behavior { next = null; return; } - if (goal.isInGoal(playerFeet())) { - return; - } // at this point, current just ended, but we aren't in the goal and have no plan for the future synchronized (pathCalcLock) { if (isPathCalcInProgress) { @@ -118,7 +120,7 @@ public class PathingBehavior extends Behavior { } if (current.getPath().ticksRemainingFrom(current.getPosition()) < 200) { // and this path has 5 seconds or less left - displayChatMessageRaw("Path almost over; planning ahead"); + displayChatMessageRaw("Path almost over. Planning ahead..."); findPathInNewThread(current.getPath().getDest(), false); } } @@ -210,7 +212,7 @@ public class PathingBehavior extends Behavior { displayChatMessageRaw("Starting to search for path from " + start + " to " + goal); } - findPath(start).map(PathExecutor::new).ifPresent(path -> { + findPath(start).map(IPath::cutoffAtLoadedChunks).map(PathExecutor::new).ifPresent(path -> { synchronized (pathPlanLock) { if (current == null) { current = path; diff --git a/src/main/java/baritone/bot/pathing/path/CutoffPath.java b/src/main/java/baritone/bot/pathing/path/CutoffPath.java new file mode 100644 index 00000000..8cd109c1 --- /dev/null +++ b/src/main/java/baritone/bot/pathing/path/CutoffPath.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 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.bot.pathing.path; + +import baritone.bot.pathing.movement.Movement; +import net.minecraft.util.math.BlockPos; + +import java.util.Collections; +import java.util.List; + +public class CutoffPath implements IPath { + + final List path; + + final List movements; + + private final int numNodes; + + public CutoffPath(IPath prev, int lastPositionToInclude) { + path = prev.positions().subList(0, lastPositionToInclude + 1); + movements = prev.movements().subList(0, lastPositionToInclude + 1); + numNodes = prev.getNumNodesConsidered(); + } + + @Override + public List movements() { + return Collections.unmodifiableList(movements); + } + + @Override + public List positions() { + return Collections.unmodifiableList(path); + } + + @Override + public int getNumNodesConsidered() { + return numNodes; + } +} diff --git a/src/main/java/baritone/bot/pathing/path/IPath.java b/src/main/java/baritone/bot/pathing/path/IPath.java index 5c348b31..95eba5b1 100644 --- a/src/main/java/baritone/bot/pathing/path/IPath.java +++ b/src/main/java/baritone/bot/pathing/path/IPath.java @@ -18,16 +18,19 @@ package baritone.bot.pathing.path; import baritone.bot.pathing.movement.Movement; +import baritone.bot.utils.Helper; import baritone.bot.utils.Utils; +import net.minecraft.client.Minecraft; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.chunk.EmptyChunk; import java.util.List; /** * @author leijurv */ -public interface IPath { +public interface IPath extends Helper { /** * Ordered list of movements to carry out. @@ -117,4 +120,18 @@ public interface IPath { } int getNumNodesConsidered(); + + default IPath cutoffAtLoadedChunks() { + for (int i = 0; i < positions().size(); i++) { + BlockPos pos = positions().get(i); + if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { + displayChatMessageRaw("Cutting off path at edge of loaded chunks"); + displayChatMessageRaw("Length decreased by " + (positions().size() - i - 1)); + return new CutoffPath(this, i); + } + } + displayChatMessageRaw("Path ends within loaded chunks"); + return this; + } + }