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] 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) {