From b628d67961615ef59351769254ea4b295738794c Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 26 Aug 2020 23:52:44 +0200 Subject: [PATCH 01/25] =?UTF-8?q?=E2=9C=A8=20Add=20ETA=20for=20full=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baritone/api/behavior/IPathingBehavior.java | 9 +++++++++ .../java/baritone/behavior/PathingBehavior.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/api/java/baritone/api/behavior/IPathingBehavior.java b/src/api/java/baritone/api/behavior/IPathingBehavior.java index 5444bb838..83db7ab3a 100644 --- a/src/api/java/baritone/api/behavior/IPathingBehavior.java +++ b/src/api/java/baritone/api/behavior/IPathingBehavior.java @@ -58,6 +58,15 @@ public interface IPathingBehavior extends IBehavior { return Optional.of(current.getPath().ticksRemainingFrom(start)); } + /** + * Returns the estimated remaining ticks to the current goal. + * Given that the return type is an optional, {@link Optional#empty()} + * will be returned in the case that there is no current goal. + * + * @return The estimated remaining ticks to the current goal. + */ + Optional estimatedTicksToGoal(); + /** * @return The current pathing goal */ diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 50ca7425f..4f47a8323 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -52,6 +52,10 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, private Goal goal; private CalculationContext context; + /*eta*/ + private int ticksElapsedSoFar; + private BetterBlockPos startPosition; + private boolean safeToCancel; private boolean pauseRequestedLastTick; private boolean unpausedLastTick; @@ -98,6 +102,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, expectedSegmentStart = pathStart(); baritone.getPathingControlManager().preTick(); tickPath(); + ticksElapsedSoFar++; dispatchEvents(); } @@ -372,6 +377,16 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return context; } + public Optional estimatedTicksToGoal(){ + if (goal == null){ + return Optional.empty(); + } + BetterBlockPos currentPos = ctx.playerFeet(); + double current = goal.heuristic(currentPos.x, currentPos.y, currentPos.z); + double start = goal.heuristic(startPosition.x, startPosition.y, startPosition.z); + return Optional.of(current * ticksElapsedSoFar / (start - current)); + } + /** * See issue #209 * @@ -468,6 +483,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (executor.get().getPath().positions().contains(expectedSegmentStart)) { queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); current = executor.get(); + ticksElapsedSoFar = 0; + startPosition = expectedSegmentStart; } else { logDebug("Warning: discarding orphan path segment with incorrect start"); } From 56f13d314a525b41055c71d0ea5fb270bcaa6042 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 26 Aug 2020 23:53:02 +0200 Subject: [PATCH 02/25] =?UTF-8?q?=E2=9C=A8=20Add=20eta=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/defaults/DefaultCommands.java | 1 + .../baritone/command/defaults/EtaCommand.java | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/main/java/baritone/command/defaults/EtaCommand.java diff --git a/src/main/java/baritone/command/defaults/DefaultCommands.java b/src/main/java/baritone/command/defaults/DefaultCommands.java index 67555ed51..08b8a627f 100644 --- a/src/main/java/baritone/command/defaults/DefaultCommands.java +++ b/src/main/java/baritone/command/defaults/DefaultCommands.java @@ -38,6 +38,7 @@ public final class DefaultCommands { new GotoCommand(baritone), new PathCommand(baritone), new ProcCommand(baritone), + new EtaCommand(baritone), new VersionCommand(baritone), new RepackCommand(baritone), new BuildCommand(baritone), diff --git a/src/main/java/baritone/command/defaults/EtaCommand.java b/src/main/java/baritone/command/defaults/EtaCommand.java new file mode 100644 index 000000000..b953bd9af --- /dev/null +++ b/src/main/java/baritone/command/defaults/EtaCommand.java @@ -0,0 +1,78 @@ +/* + * 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.command.defaults; + +import baritone.api.IBaritone; +import baritone.api.pathing.calc.IPathingControlManager; +import baritone.api.process.IBaritoneProcess; +import baritone.api.behavior.IPathingBehavior; +import baritone.api.command.Command; +import baritone.api.command.exception.CommandException; +import baritone.api.command.exception.CommandInvalidStateException; +import baritone.api.command.argument.IArgConsumer; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +public class EtaCommand extends Command { + + public EtaCommand(IBaritone baritone) { + super(baritone, "eta"); + } + + @Override + public void execute(String label, IArgConsumer args) throws CommandException { + args.requireMax(0); + IPathingControlManager pathingControlManager = baritone.getPathingControlManager(); + IBaritoneProcess process = pathingControlManager.mostRecentInControl().orElse(null); + if (process == null) { + throw new CommandInvalidStateException("No process in control"); + } + IPathingBehavior pathingBehavior = baritone.getPathingBehavior(); + logDirect(String.format( + "Next segment: %.2f\n" + + "Goal: %.2f", + pathingBehavior.ticksRemainingInSegment().orElse(-1.0), + pathingBehavior.estimatedTicksToGoal().orElse(-1.0) + )); + } + + @Override + public Stream tabComplete(String label, IArgConsumer args) { + return Stream.empty(); + } + + @Override + public String getShortDesc() { + return "View the current eta"; + } + + @Override + public List getLongDesc() { + return Arrays.asList( + "The eta command provides information about the estimated time until the next segment.", + "and the goal", + "", + "Be aware that the eta to your goal is really unprecise", + "", + "Usage:", + "> proc - View eta, if present" + ); + } +} From 71dd6c633372834938f50825d2d4d5c5fb06a6ef Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Thu, 27 Aug 2020 01:16:17 +0200 Subject: [PATCH 03/25] =?UTF-8?q?=F0=9F=91=8Cformatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baritone/behavior/PathingBehavior.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 4f47a8323..0026e8106 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -377,8 +377,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return context; } - public Optional estimatedTicksToGoal(){ - if (goal == null){ + public Optional estimatedTicksToGoal() { + if (goal == null) { return Optional.empty(); } BetterBlockPos currentPos = ctx.playerFeet(); From d9cecb35cb1fc5240f534214ad588d8dd3f19b73 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 28 Aug 2020 00:52:55 +0200 Subject: [PATCH 04/25] rename Eta to ETA --- .../baritone/command/defaults/DefaultCommands.java | 2 +- .../defaults/{EtaCommand.java => ETACommand.java} | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) rename src/main/java/baritone/command/defaults/{EtaCommand.java => ETACommand.java} (89%) diff --git a/src/main/java/baritone/command/defaults/DefaultCommands.java b/src/main/java/baritone/command/defaults/DefaultCommands.java index 08b8a627f..9e17468fa 100644 --- a/src/main/java/baritone/command/defaults/DefaultCommands.java +++ b/src/main/java/baritone/command/defaults/DefaultCommands.java @@ -38,7 +38,7 @@ public final class DefaultCommands { new GotoCommand(baritone), new PathCommand(baritone), new ProcCommand(baritone), - new EtaCommand(baritone), + new ETACommand(baritone), new VersionCommand(baritone), new RepackCommand(baritone), new BuildCommand(baritone), diff --git a/src/main/java/baritone/command/defaults/EtaCommand.java b/src/main/java/baritone/command/defaults/ETACommand.java similarity index 89% rename from src/main/java/baritone/command/defaults/EtaCommand.java rename to src/main/java/baritone/command/defaults/ETACommand.java index b953bd9af..dd56ada30 100644 --- a/src/main/java/baritone/command/defaults/EtaCommand.java +++ b/src/main/java/baritone/command/defaults/ETACommand.java @@ -30,9 +30,9 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Stream; -public class EtaCommand extends Command { +public class ETACommand extends Command { - public EtaCommand(IBaritone baritone) { + public ETACommand(IBaritone baritone) { super(baritone, "eta"); } @@ -60,19 +60,19 @@ public class EtaCommand extends Command { @Override public String getShortDesc() { - return "View the current eta"; + return "View the current ETA"; } @Override public List getLongDesc() { return Arrays.asList( - "The eta command provides information about the estimated time until the next segment.", + "The ETA command provides information about the estimated time until the next segment.", "and the goal", "", - "Be aware that the eta to your goal is really unprecise", + "Be aware that the ETA to your goal is really unprecise", "", "Usage:", - "> proc - View eta, if present" + "> proc - View ETA, if present" ); } } From d29b3ee893f42bef3c975a67f4e6a1003da4f5a3 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 28 Aug 2020 01:19:06 +0200 Subject: [PATCH 05/25] Fix copy/paste mistake in ETACommand.getLongDesc() --- src/main/java/baritone/command/defaults/ETACommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/command/defaults/ETACommand.java b/src/main/java/baritone/command/defaults/ETACommand.java index dd56ada30..3c16bd113 100644 --- a/src/main/java/baritone/command/defaults/ETACommand.java +++ b/src/main/java/baritone/command/defaults/ETACommand.java @@ -72,7 +72,7 @@ public class ETACommand extends Command { "Be aware that the ETA to your goal is really unprecise", "", "Usage:", - "> proc - View ETA, if present" + "> eta - View ETA, if present" ); } } From 695954bdb08986ca1cd487927bd04216d97baaa8 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 28 Aug 2020 23:37:23 +0200 Subject: [PATCH 06/25] no eta after 0 ticks or with division by 0 --- src/main/java/baritone/behavior/PathingBehavior.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 0026e8106..ad2df5320 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -378,12 +378,15 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } public Optional estimatedTicksToGoal() { - if (goal == null) { + if (goal == null || ticksElapsedSoFar == 0) { return Optional.empty(); } BetterBlockPos currentPos = ctx.playerFeet(); double current = goal.heuristic(currentPos.x, currentPos.y, currentPos.z); double start = goal.heuristic(startPosition.x, startPosition.y, startPosition.z); + if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not + return Optional.empty(); + } return Optional.of(current * ticksElapsedSoFar / (start - current)); } From 35f3be9296ef3e384413a92caf2787186f58512d Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 2 Sep 2020 22:59:13 +0200 Subject: [PATCH 07/25] get rid of negative ETAs the lazy way --- src/main/java/baritone/behavior/PathingBehavior.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index ad2df5320..65ee41127 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -387,7 +387,11 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not return Optional.empty(); } - return Optional.of(current * ticksElapsedSoFar / (start - current)); + double eta = current * ticksElapsedSoFar / (start - current); + if (eta < 0 || current < 0){ + return Optional.empty(); + } + return Optional.of(eta); } /** From 303aa79ffb311d53a7915f1b0e63d6db1c3a08e1 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 4 Sep 2020 23:56:01 +0200 Subject: [PATCH 08/25] Update ETA formula, assuming heuritic at goal is 0 --- src/main/java/baritone/behavior/PathingBehavior.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 65ee41127..832337e69 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -387,7 +387,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not return Optional.empty(); } - double eta = current * ticksElapsedSoFar / (start - current); + double eta = Math.abs(current) * ticksElapsedSoFar / Math.abs(start - current); if (eta < 0 || current < 0){ return Optional.empty(); } From 10e3a5afc4f7b0bc79196b956ff56995cc462e1c Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sat, 5 Sep 2020 22:13:05 +0200 Subject: [PATCH 09/25] negative ETAs are actually impossible now --- src/main/java/baritone/behavior/PathingBehavior.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 832337e69..5cc9520c7 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -388,9 +388,6 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return Optional.empty(); } double eta = Math.abs(current) * ticksElapsedSoFar / Math.abs(start - current); - if (eta < 0 || current < 0){ - return Optional.empty(); - } return Optional.of(eta); } From 45dc8b949d0196f5bc4bb3e4796e0deccb4d62cd Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sat, 5 Sep 2020 22:32:38 +0200 Subject: [PATCH 10/25] add a method to get the heuristic at the goal this alows the ETA to work with goals not ending with a heuristic of 0 GoalComposite, GoalRunAway and GoalNear are still missing --- src/api/java/baritone/api/pathing/goals/Goal.java | 14 ++++++++++++++ .../baritone/api/pathing/goals/GoalInverted.java | 5 +++++ .../api/pathing/goals/GoalStrictDirection.java | 5 +++++ .../java/baritone/behavior/PathingBehavior.java | 2 +- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/api/java/baritone/api/pathing/goals/Goal.java b/src/api/java/baritone/api/pathing/goals/Goal.java index acee68db8..7d8f42220 100644 --- a/src/api/java/baritone/api/pathing/goals/Goal.java +++ b/src/api/java/baritone/api/pathing/goals/Goal.java @@ -54,4 +54,18 @@ public interface Goal { default double heuristic(BlockPos pos) { return heuristic(pos.getX(), pos.getY(), pos.getZ()); } + + /** + * Returns the heuristic at the goal. + * i.e. {@code heuristic() == heuristic(x,y,z)} + * when {@code isInGoal(x,y,z) == true} + * This is needed by {@code PathingBehavior#estimatedTicksToGoal} because + * some Goals actually do not have a heuristic of 0 when that condition is met + * + * @return The estimate number of ticks to satisfy the goal when the goal + * is already satisfied + */ + default double heuristic() { + return 0; + } } diff --git a/src/api/java/baritone/api/pathing/goals/GoalInverted.java b/src/api/java/baritone/api/pathing/goals/GoalInverted.java index 197369241..354e2ce39 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalInverted.java +++ b/src/api/java/baritone/api/pathing/goals/GoalInverted.java @@ -45,6 +45,11 @@ public class GoalInverted implements Goal { return -origin.heuristic(x, y, z); } + @Override + public double heuristic() { + return Double.NEGATIVE_INFINITY; + } + @Override public String toString() { return String.format("GoalInverted{%s}", origin.toString()); diff --git a/src/api/java/baritone/api/pathing/goals/GoalStrictDirection.java b/src/api/java/baritone/api/pathing/goals/GoalStrictDirection.java index b48a029ad..e93f47ac0 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalStrictDirection.java +++ b/src/api/java/baritone/api/pathing/goals/GoalStrictDirection.java @@ -64,6 +64,11 @@ public class GoalStrictDirection implements Goal { return heuristic; } + @Override + public double heuristic() { + return Double.NEGATIVE_INFINITY; + } + @Override public String toString() { return String.format( diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 5cc9520c7..5f69aabe7 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -387,7 +387,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not return Optional.empty(); } - double eta = Math.abs(current) * ticksElapsedSoFar / Math.abs(start - current); + double eta = Math.abs(current - goal.heuristic()) * ticksElapsedSoFar / Math.abs(start - current); return Optional.of(eta); } From 7255ccbdaac3fc339fb36a93dfb8d7813411f646 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sun, 6 Sep 2020 16:42:05 +0200 Subject: [PATCH 11/25] add heuristic(no args) to GoalComposite GoalNear and GoalRunAway are still missing it --- .../java/baritone/api/pathing/goals/GoalComposite.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/api/java/baritone/api/pathing/goals/GoalComposite.java b/src/api/java/baritone/api/pathing/goals/GoalComposite.java index 415f74e56..fd4e672b0 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalComposite.java +++ b/src/api/java/baritone/api/pathing/goals/GoalComposite.java @@ -57,6 +57,16 @@ public class GoalComposite implements Goal { return min; } + @Override + public double heuristic() { + double min = Double.MAX_VALUE; + for (Goal g: goals) { + //just take the highest value that is guaranteed to be inside the goal + min = Math.min(min, g.heuristic()); + } + return min; + } + @Override public String toString() { return "GoalComposite" + Arrays.toString(goals); From 411b2a0accfd7ca04f93592acd1cfa2618667b42 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 18 Sep 2020 23:16:56 +0200 Subject: [PATCH 12/25] =?UTF-8?q?=F0=9F=94=A8move=20ETA=20reset=20to=20it'?= =?UTF-8?q?s=20own=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baritone/behavior/PathingBehavior.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 5f69aabe7..22513985c 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -391,6 +391,19 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, return Optional.of(eta); } + private void resetEstimatedTicksToGoal() { + resetEstimatedTicksToGoal(expectedSegmentStart); + } + + private void resetEstimatedTicksToGoal(BlockPos start) { + resetEstimatedTicksToGoal(new BetterBlockPos(start)); + } + + private void resetEstimatedTicksToGoal(BetterBlockPos start) { + ticksElapsedSoFar = 0; + startPosition = start; + } + /** * See issue #209 * @@ -487,8 +500,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (executor.get().getPath().positions().contains(expectedSegmentStart)) { queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); current = executor.get(); - ticksElapsedSoFar = 0; - startPosition = expectedSegmentStart; + resetEstimatedTicksToGoal(start); } else { logDebug("Warning: discarding orphan path segment with incorrect start"); } From 46a12754e9a104b47c17c3523f505d5e0d69f011 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 18 Sep 2020 23:24:47 +0200 Subject: [PATCH 13/25] =?UTF-8?q?=F0=9F=90=9BReset=20ETA=20and=20return=20?= =?UTF-8?q?0=20if=20we=20are=20already=20there=20not=20doing=20this=20caus?= =?UTF-8?q?ed=20a=20continuously=20increasing=20ETA=20when=20standing=20in?= =?UTF-8?q?side=20a=20`GoalNear`=20from=20`FollowProcess`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baritone/behavior/PathingBehavior.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 22513985c..acda243a6 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -378,7 +378,14 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } public Optional estimatedTicksToGoal() { - if (goal == null || ticksElapsedSoFar == 0) { + if (goal == null) { + return Optional.empty(); + } + if (goal.isInGoal(ctx.playerFeet())) { + resetEstimatedTicksToGoal(); + return Optional.of(0.0); + } + if (ticksElapsedSoFar == 0) { return Optional.empty(); } BetterBlockPos currentPos = ctx.playerFeet(); From b4d7f0516522c12bbdcb0ccc9802d0bda93772ce Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sat, 19 Sep 2020 00:31:58 +0200 Subject: [PATCH 14/25] =?UTF-8?q?=F0=9F=90=9B=20fix=20two=20NPEs=20in=20es?= =?UTF-8?q?timatedTickToGoal=20apparently=20`ctx.playerFeet()`=20and=20`st?= =?UTF-8?q?artPosition`=20can=20be=20`null`=20before=20pathing=20the=20fir?= =?UTF-8?q?st=20time?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/baritone/behavior/PathingBehavior.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index acda243a6..967df1b33 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -378,7 +378,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } public Optional estimatedTicksToGoal() { - if (goal == null) { + BetterBlockPos currentPos = ctx.playerFeet(); + if (goal == null || currentPos == null || startPosition == null) { return Optional.empty(); } if (goal.isInGoal(ctx.playerFeet())) { @@ -388,7 +389,6 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, if (ticksElapsedSoFar == 0) { return Optional.empty(); } - BetterBlockPos currentPos = ctx.playerFeet(); double current = goal.heuristic(currentPos.x, currentPos.y, currentPos.z); double start = goal.heuristic(startPosition.x, startPosition.y, startPosition.z); if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not From b20e0956835c516116aa59e3b5b77e46eee07da1 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sun, 20 Sep 2020 00:29:31 +0200 Subject: [PATCH 15/25] add heuristic(no args) to GoalNear and GoalRunAway not really a good solution but better than nothing --- .../baritone/api/pathing/goals/GoalNear.java | 24 +++++++++++ .../api/pathing/goals/GoalRunAway.java | 42 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 73d64ed9f..6f493fa54 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -21,6 +21,9 @@ import baritone.api.utils.SettingsUtil; import baritone.api.utils.interfaces.IGoalRenderPos; import net.minecraft.util.math.BlockPos; +import java.util.Collections; +import java.util.HashSet; + public class GoalNear implements Goal, IGoalRenderPos { private final int x; @@ -51,6 +54,27 @@ public class GoalNear implements Goal, IGoalRenderPos { return GoalBlock.calculate(xDiff, yDiff, zDiff); } + @Override + public double heuristic() {//TODO less hacky solution + int range = (int)Math.ceil(Math.abs(Math.sqrt(rangeSq))); + HashSet maybeAlwaysInside = new HashSet<>(); + HashSet sometimesOutside = new HashSet<>(); + for (int dx = -range; dx <= range; dx++) { + for (int dy = -range; dy <= range; dy++) { + for (int dz = -range; dz <= range; dz++) { + double h = heuristic(x+dx, y+dy, z+dz); + if (!sometimesOutside.contains(h) && isInGoal(x+dx, y+dy, z+dz)) { + maybeAlwaysInside.add(h); + } else { + maybeAlwaysInside.remove(h); + sometimesOutside.add(h); + } + } + } + } + return Collections.max(maybeAlwaysInside); + } + @Override public BlockPos getGoalPos() { return new BlockPos(x, y, z); diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index a1a153780..3955de1e9 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -21,6 +21,9 @@ import baritone.api.utils.SettingsUtil; import net.minecraft.util.math.BlockPos; import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.NoSuchElementException; /** * Useful for automated combat (retreating specifically) @@ -80,6 +83,45 @@ public class GoalRunAway implements Goal { return min; } + @Override + public double heuristic() {//TODO less hacky solution + int distance = (int)Math.ceil(Math.abs(Math.sqrt(distanceSq))); + int minX = from[0].getX() - distance; + int minY = from[0].getY() - distance; + int minZ = from[0].getZ() - distance; + int maxX = from[0].getX() + distance; + int maxY = from[0].getY() + distance; + int maxZ = from[0].getZ() + distance; + for (BlockPos p : from) { + minX = Math.min(minX, p.getX() - distance); + minY = Math.min(minY, p.getY() - distance); + minZ = Math.min(minZ, p.getZ() - distance); + maxX = Math.max(minX, p.getX() + distance); + maxY = Math.max(minY, p.getY() + distance); + maxZ = Math.max(minZ, p.getZ() + distance); + } + HashSet maybeAlwaysInside = new HashSet<>(); + HashSet sometimesOutside = new HashSet<>(); + for (int x = minX; x <= maxX; x++) { + for (int y = minX; y <= maxX; y++) { + for (int z = minX; z <= maxX; z++) { + double h = heuristic(x, y, z); + if (!sometimesOutside.contains(h) && isInGoal(x, y, z)) { + maybeAlwaysInside.add(h); + } else { + maybeAlwaysInside.remove(h); + sometimesOutside.add(h); + } + } + } + } + try { + return Collections.max(maybeAlwaysInside); + } catch (NoSuchElementException e) { + return Double.NEGATIVE_INFINITY; + } + } + @Override public String toString() { if (maintainY != null) { From 85cc86346cef57c0052d4ddc3b3f0f2074d2ed08 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sun, 20 Sep 2020 22:20:24 +0200 Subject: [PATCH 16/25] Actually use Y and Z bounds for Y and Z --- src/api/java/baritone/api/pathing/goals/GoalRunAway.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 3955de1e9..76f7110cc 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -103,8 +103,8 @@ public class GoalRunAway implements Goal { HashSet maybeAlwaysInside = new HashSet<>(); HashSet sometimesOutside = new HashSet<>(); for (int x = minX; x <= maxX; x++) { - for (int y = minX; y <= maxX; y++) { - for (int z = minX; z <= maxX; z++) { + for (int y = minY; y <= maxY; y++) { + for (int z = minZ; z <= maxZ; z++) { double h = heuristic(x, y, z); if (!sometimesOutside.contains(h) && isInGoal(x, y, z)) { maybeAlwaysInside.add(h); From aebfbba20ecfef60af5c45ddb855a49042be0bd9 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sun, 20 Sep 2020 22:39:48 +0200 Subject: [PATCH 17/25] Also override heuristic(no args) when overriding isInGoal() --- src/main/java/baritone/process/GetToBlockProcess.java | 4 ++++ src/main/java/baritone/process/MineProcess.java | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 00ec26091..78df95883 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -77,6 +77,10 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG public boolean isInGoal(int x, int y, int z) { return false; } + @Override + public double heuristic() { + return Double.NEGATIVE_INFINITY; + } }, PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH); } logDirect("No known locations of " + gettingTo + ", canceling GetToBlock"); diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 6e8ba245b..f976f71b5 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -211,6 +211,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro public boolean isInGoal(int x, int y, int z) { return false; } + @Override + public double heuristic() { + return Double.NEGATIVE_INFINITY; + } }; } return new PathingCommand(branchPointRunaway, PathingCommandType.REVALIDATE_GOAL_AND_PATH); From e529438c7ef8f4f5593f6fbff6c41d2d81abdd71 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 14 Oct 2020 21:14:17 +0200 Subject: [PATCH 18/25] Don't use Abs on Sqrt --- src/api/java/baritone/api/pathing/goals/GoalNear.java | 2 +- src/api/java/baritone/api/pathing/goals/GoalRunAway.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 6f493fa54..c7e932e23 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -56,7 +56,7 @@ public class GoalNear implements Goal, IGoalRenderPos { @Override public double heuristic() {//TODO less hacky solution - int range = (int)Math.ceil(Math.abs(Math.sqrt(rangeSq))); + int range = (int)Math.ceil(Math.sqrt(rangeSq)); HashSet maybeAlwaysInside = new HashSet<>(); HashSet sometimesOutside = new HashSet<>(); for (int dx = -range; dx <= range; dx++) { diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 76f7110cc..9ee1b1510 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -85,7 +85,7 @@ public class GoalRunAway implements Goal { @Override public double heuristic() {//TODO less hacky solution - int distance = (int)Math.ceil(Math.abs(Math.sqrt(distanceSq))); + int distance = (int)Math.ceil(Math.sqrt(distanceSq)); int minX = from[0].getX() - distance; int minY = from[0].getY() - distance; int minZ = from[0].getZ() - distance; From 0c7741120a10ff617583ffb6bf6eb65f3a4fe3f9 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Thu, 15 Oct 2020 00:05:54 +0200 Subject: [PATCH 19/25] =?UTF-8?q?=F0=9F=91=8Cformatting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/baritone/api/pathing/goals/Goal.java | 18 +++++++++--------- .../api/pathing/goals/GoalComposite.java | 2 +- .../baritone/api/pathing/goals/GoalNear.java | 6 +++--- .../api/pathing/goals/GoalRunAway.java | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/Goal.java b/src/api/java/baritone/api/pathing/goals/Goal.java index 7d8f42220..95462f961 100644 --- a/src/api/java/baritone/api/pathing/goals/Goal.java +++ b/src/api/java/baritone/api/pathing/goals/Goal.java @@ -56,15 +56,15 @@ public interface Goal { } /** - * Returns the heuristic at the goal. - * i.e. {@code heuristic() == heuristic(x,y,z)} - * when {@code isInGoal(x,y,z) == true} - * This is needed by {@code PathingBehavior#estimatedTicksToGoal} because - * some Goals actually do not have a heuristic of 0 when that condition is met - * - * @return The estimate number of ticks to satisfy the goal when the goal - * is already satisfied - */ + * Returns the heuristic at the goal. + * i.e. {@code heuristic() == heuristic(x,y,z)} + * when {@code isInGoal(x,y,z) == true} + * This is needed by {@code PathingBehavior#estimatedTicksToGoal} because + * some Goals actually do not have a heuristic of 0 when that condition is met + * + * @return The estimate number of ticks to satisfy the goal when the goal + * is already satisfied + */ default double heuristic() { return 0; } diff --git a/src/api/java/baritone/api/pathing/goals/GoalComposite.java b/src/api/java/baritone/api/pathing/goals/GoalComposite.java index fd4e672b0..ebaf38eda 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalComposite.java +++ b/src/api/java/baritone/api/pathing/goals/GoalComposite.java @@ -60,7 +60,7 @@ public class GoalComposite implements Goal { @Override public double heuristic() { double min = Double.MAX_VALUE; - for (Goal g: goals) { + for (Goal g : goals) { //just take the highest value that is guaranteed to be inside the goal min = Math.min(min, g.heuristic()); } diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index c7e932e23..0657c1724 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -56,14 +56,14 @@ public class GoalNear implements Goal, IGoalRenderPos { @Override public double heuristic() {//TODO less hacky solution - int range = (int)Math.ceil(Math.sqrt(rangeSq)); + int range = (int) Math.ceil(Math.sqrt(rangeSq)); HashSet maybeAlwaysInside = new HashSet<>(); HashSet sometimesOutside = new HashSet<>(); for (int dx = -range; dx <= range; dx++) { for (int dy = -range; dy <= range; dy++) { for (int dz = -range; dz <= range; dz++) { - double h = heuristic(x+dx, y+dy, z+dz); - if (!sometimesOutside.contains(h) && isInGoal(x+dx, y+dy, z+dz)) { + double h = heuristic(x + dx, y + dy, z + dz); + if (!sometimesOutside.contains(h) && isInGoal(x + dx, y + dy, z + dz)) { maybeAlwaysInside.add(h); } else { maybeAlwaysInside.remove(h); diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 9ee1b1510..7983d6fcc 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -85,7 +85,7 @@ public class GoalRunAway implements Goal { @Override public double heuristic() {//TODO less hacky solution - int distance = (int)Math.ceil(Math.sqrt(distanceSq)); + int distance = (int) Math.ceil(Math.sqrt(distanceSq)); int minX = from[0].getX() - distance; int minY = from[0].getY() - distance; int minZ = from[0].getZ() - distance; From 3cdbc4cb83fa161380b31a0089d019b2b9f3ebd1 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Thu, 15 Oct 2020 21:48:17 +0200 Subject: [PATCH 20/25] =?UTF-8?q?=F0=9F=91=8Cformatting=20in=20of=20commen?= =?UTF-8?q?ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/java/baritone/api/pathing/goals/GoalComposite.java | 2 +- src/api/java/baritone/api/pathing/goals/GoalNear.java | 2 +- src/api/java/baritone/api/pathing/goals/GoalRunAway.java | 4 ++-- src/main/java/baritone/behavior/PathingBehavior.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalComposite.java b/src/api/java/baritone/api/pathing/goals/GoalComposite.java index ebaf38eda..47522492b 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalComposite.java +++ b/src/api/java/baritone/api/pathing/goals/GoalComposite.java @@ -61,7 +61,7 @@ public class GoalComposite implements Goal { public double heuristic() { double min = Double.MAX_VALUE; for (Goal g : goals) { - //just take the highest value that is guaranteed to be inside the goal + // just take the highest value that is guaranteed to be inside the goal min = Math.min(min, g.heuristic()); } return min; diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 0657c1724..2ae2decc2 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -55,7 +55,7 @@ public class GoalNear implements Goal, IGoalRenderPos { } @Override - public double heuristic() {//TODO less hacky solution + public double heuristic() {// TODO less hacky solution int range = (int) Math.ceil(Math.sqrt(rangeSq)); HashSet maybeAlwaysInside = new HashSet<>(); HashSet sometimesOutside = new HashSet<>(); diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 7983d6fcc..e66cbb828 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -68,7 +68,7 @@ public class GoalRunAway implements Goal { } @Override - public double heuristic(int x, int y, int z) {//mostly copied from GoalBlock + public double heuristic(int x, int y, int z) {// mostly copied from GoalBlock double min = Double.MAX_VALUE; for (BlockPos p : from) { double h = GoalXZ.calculate(p.getX() - x, p.getZ() - z); @@ -84,7 +84,7 @@ public class GoalRunAway implements Goal { } @Override - public double heuristic() {//TODO less hacky solution + public double heuristic() {// TODO less hacky solution int distance = (int) Math.ceil(Math.sqrt(distanceSq)); int minX = from[0].getX() - distance; int minY = from[0].getY() - distance; diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 967df1b33..f9c56c5a4 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -391,7 +391,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } double current = goal.heuristic(currentPos.x, currentPos.y, currentPos.z); double start = goal.heuristic(startPosition.x, startPosition.y, startPosition.z); - if (current == start) {//can't check above because current and start can be equal even if currentPos and startPosition are not + if (current == start) {// can't check above because current and start can be equal even if currentPos and startPosition are not return Optional.empty(); } double eta = Math.abs(current - goal.heuristic()) * ticksElapsedSoFar / Math.abs(start - current); From 939319203611aed1dd5e699b7324d313c88e786a Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Fri, 16 Oct 2020 01:30:04 +0200 Subject: [PATCH 21/25] Slight change to heuristic(no args) --- src/api/java/baritone/api/pathing/goals/GoalNear.java | 7 +++---- src/api/java/baritone/api/pathing/goals/GoalRunAway.java | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 2ae2decc2..272636ac5 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -58,16 +58,15 @@ public class GoalNear implements Goal, IGoalRenderPos { public double heuristic() {// TODO less hacky solution int range = (int) Math.ceil(Math.sqrt(rangeSq)); HashSet maybeAlwaysInside = new HashSet<>(); - HashSet sometimesOutside = new HashSet<>(); + double minOutside = Double.POSITIVE_INFINITY; for (int dx = -range; dx <= range; dx++) { for (int dy = -range; dy <= range; dy++) { for (int dz = -range; dz <= range; dz++) { double h = heuristic(x + dx, y + dy, z + dz); - if (!sometimesOutside.contains(h) && isInGoal(x + dx, y + dy, z + dz)) { + if (h < minOutside && isInGoal(x + dx, y + dy, z + dz)) { maybeAlwaysInside.add(h); } else { - maybeAlwaysInside.remove(h); - sometimesOutside.add(h); + minOutside = Math.min(minOutside, h); } } } diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index e66cbb828..36797617e 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -101,16 +101,15 @@ public class GoalRunAway implements Goal { maxZ = Math.max(minZ, p.getZ() + distance); } HashSet maybeAlwaysInside = new HashSet<>(); - HashSet sometimesOutside = new HashSet<>(); + double minOutside = Double.POSITIVE_INFINITY; for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { double h = heuristic(x, y, z); - if (!sometimesOutside.contains(h) && isInGoal(x, y, z)) { + if (h < minOutside && isInGoal(x, y, z)) { maybeAlwaysInside.add(h); } else { - maybeAlwaysInside.remove(h); - sometimesOutside.add(h); + minOutside = Math.min(minOutside, h); } } } From fd6120770969b43a1880f6eb55c4ee89d6c01074 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Thu, 28 Jan 2021 01:27:27 +0100 Subject: [PATCH 22/25] Fix heuristic(no args) returning wrong values --- src/api/java/baritone/api/pathing/goals/GoalNear.java | 8 +++++++- .../java/baritone/api/pathing/goals/GoalRunAway.java | 11 ++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 272636ac5..7f87e8b92 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -71,7 +71,13 @@ public class GoalNear implements Goal, IGoalRenderPos { } } } - return Collections.max(maybeAlwaysInside); + double maxInside = Double.NEGATIVE_INFINITY; + for (double inside : maybeAlwaysInside) { + if (inside < minOutside) { + maxInside = Math.max(maxInside, inside); + } + } + return maxInside; } @Override diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 36797617e..b9c41aeaf 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -23,7 +23,6 @@ import net.minecraft.util.math.BlockPos; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.NoSuchElementException; /** * Useful for automated combat (retreating specifically) @@ -114,11 +113,13 @@ public class GoalRunAway implements Goal { } } } - try { - return Collections.max(maybeAlwaysInside); - } catch (NoSuchElementException e) { - return Double.NEGATIVE_INFINITY; + double maxInside = Double.NEGATIVE_INFINITY; + for (double inside : maybeAlwaysInside) { + if (inside < minOutside) { + maxInside = Math.max(maxInside, inside); + } } + return maxInside; } @Override From ff068d374fb56fd7d9ead0ee9ef530ee1ab26fe7 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 10 Feb 2021 00:29:45 +0100 Subject: [PATCH 23/25] Don't box doubles --- src/api/java/baritone/api/pathing/goals/GoalNear.java | 11 ++++++----- .../java/baritone/api/pathing/goals/GoalRunAway.java | 10 ++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 7f87e8b92..1897d7743 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -19,11 +19,10 @@ package baritone.api.pathing.goals; import baritone.api.utils.SettingsUtil; import baritone.api.utils.interfaces.IGoalRenderPos; +import it.unimi.dsi.fastutil.doubles.DoubleOpenHashSet; +import it.unimi.dsi.fastutil.doubles.DoubleIterator; import net.minecraft.util.math.BlockPos; -import java.util.Collections; -import java.util.HashSet; - public class GoalNear implements Goal, IGoalRenderPos { private final int x; @@ -57,7 +56,7 @@ public class GoalNear implements Goal, IGoalRenderPos { @Override public double heuristic() {// TODO less hacky solution int range = (int) Math.ceil(Math.sqrt(rangeSq)); - HashSet maybeAlwaysInside = new HashSet<>(); + DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); double minOutside = Double.POSITIVE_INFINITY; for (int dx = -range; dx <= range; dx++) { for (int dy = -range; dy <= range; dy++) { @@ -72,7 +71,9 @@ public class GoalNear implements Goal, IGoalRenderPos { } } double maxInside = Double.NEGATIVE_INFINITY; - for (double inside : maybeAlwaysInside) { + DoubleIterator it = maybeAlwaysInside.iterator(); + while(it.hasNext()) { + double inside = it.nextDouble(); if (inside < minOutside) { maxInside = Math.max(maxInside, inside); } diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index b9c41aeaf..b85cb23ee 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -18,11 +18,11 @@ package baritone.api.pathing.goals; import baritone.api.utils.SettingsUtil; +import it.unimi.dsi.fastutil.doubles.DoubleOpenHashSet; +import it.unimi.dsi.fastutil.doubles.DoubleIterator; import net.minecraft.util.math.BlockPos; import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; /** * Useful for automated combat (retreating specifically) @@ -99,7 +99,7 @@ public class GoalRunAway implements Goal { maxY = Math.max(minY, p.getY() + distance); maxZ = Math.max(minZ, p.getZ() + distance); } - HashSet maybeAlwaysInside = new HashSet<>(); + DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); double minOutside = Double.POSITIVE_INFINITY; for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { @@ -114,7 +114,9 @@ public class GoalRunAway implements Goal { } } double maxInside = Double.NEGATIVE_INFINITY; - for (double inside : maybeAlwaysInside) { + DoubleIterator it = maybeAlwaysInside.iterator(); + while(it.hasNext()) { + double inside = it.nextDouble(); if (inside < minOutside) { maxInside = Math.max(maxInside, inside); } From 5926369a56fb853788774ec45223d87beed42cb5 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 10 Feb 2021 10:15:14 +0100 Subject: [PATCH 24/25] formatting and comments --- src/api/java/baritone/api/pathing/goals/GoalNear.java | 4 ++-- src/api/java/baritone/api/pathing/goals/GoalRunAway.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalNear.java b/src/api/java/baritone/api/pathing/goals/GoalNear.java index 1897d7743..6a8226fb4 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalNear.java +++ b/src/api/java/baritone/api/pathing/goals/GoalNear.java @@ -56,7 +56,7 @@ public class GoalNear implements Goal, IGoalRenderPos { @Override public double heuristic() {// TODO less hacky solution int range = (int) Math.ceil(Math.sqrt(rangeSq)); - DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); + DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); // see pull request #1978 double minOutside = Double.POSITIVE_INFINITY; for (int dx = -range; dx <= range; dx++) { for (int dy = -range; dy <= range; dy++) { @@ -72,7 +72,7 @@ public class GoalNear implements Goal, IGoalRenderPos { } double maxInside = Double.NEGATIVE_INFINITY; DoubleIterator it = maybeAlwaysInside.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { double inside = it.nextDouble(); if (inside < minOutside) { maxInside = Math.max(maxInside, inside); diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index b85cb23ee..a1b2fe1cd 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -99,7 +99,7 @@ public class GoalRunAway implements Goal { maxY = Math.max(minY, p.getY() + distance); maxZ = Math.max(minZ, p.getZ() + distance); } - DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); + DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); // see pull request #1978 double minOutside = Double.POSITIVE_INFINITY; for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { @@ -115,7 +115,7 @@ public class GoalRunAway implements Goal { } double maxInside = Double.NEGATIVE_INFINITY; DoubleIterator it = maybeAlwaysInside.iterator(); - while(it.hasNext()) { + while (it.hasNext()) { double inside = it.nextDouble(); if (inside < minOutside) { maxInside = Math.max(maxInside, inside); From 132cc0e13130c457c619a5e76f61ab0f463a9b93 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Wed, 10 Feb 2021 10:25:17 +0100 Subject: [PATCH 25/25] make range initialization more readable --- .../java/baritone/api/pathing/goals/GoalRunAway.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index a1b2fe1cd..152b1c273 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -85,12 +85,12 @@ public class GoalRunAway implements Goal { @Override public double heuristic() {// TODO less hacky solution int distance = (int) Math.ceil(Math.sqrt(distanceSq)); - int minX = from[0].getX() - distance; - int minY = from[0].getY() - distance; - int minZ = from[0].getZ() - distance; - int maxX = from[0].getX() + distance; - int maxY = from[0].getY() + distance; - int maxZ = from[0].getZ() + distance; + int minX = Integer.MAX_VALUE; + int minY = Integer.MAX_VALUE; + int minZ = Integer.MAX_VALUE; + int maxX = Integer.MIN_VALUE; + int maxY = Integer.MIN_VALUE; + int maxZ = Integer.MIN_VALUE; for (BlockPos p : from) { minX = Math.min(minX, p.getX() - distance); minY = Math.min(minY, p.getY() - distance);