add heuristic(no args) to GoalNear and GoalRunAway

not really a good solution but better than nothing
This commit is contained in:
ZacSharp 2020-09-20 00:29:31 +02:00
parent b4d7f05165
commit b20e095683
No known key found for this signature in database
GPG Key ID: 9453647B005083A3
2 changed files with 66 additions and 0 deletions

View File

@ -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<Double> maybeAlwaysInside = new HashSet<>();
HashSet<Double> 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);

View File

@ -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<Double> maybeAlwaysInside = new HashSet<>();
HashSet<Double> 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) {