baritone/src/api/java/baritone/api/pathing/goals/GoalNear.java

100 lines
3.2 KiB
Java
Raw Normal View History

2018-08-28 01:00:11 +00:00
/*
* 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
2018-08-28 01:00:11 +00:00
* 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.
2018-08-28 01:00:11 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-28 01:00:11 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-09-25 01:32:39 +00:00
package baritone.api.pathing.goals;
2018-08-28 01:00:11 +00:00
2019-09-01 16:47:27 +00:00
import baritone.api.utils.SettingsUtil;
2018-09-25 01:32:39 +00:00
import baritone.api.utils.interfaces.IGoalRenderPos;
2021-02-09 23:29:45 +00:00
import it.unimi.dsi.fastutil.doubles.DoubleIterator;
2023-01-15 09:02:52 +00:00
import it.unimi.dsi.fastutil.doubles.DoubleOpenHashSet;
2021-06-23 17:24:32 +00:00
import net.minecraft.core.BlockPos;
2018-08-28 01:00:11 +00:00
2018-09-08 01:23:32 +00:00
public class GoalNear implements Goal, IGoalRenderPos {
2019-09-19 20:40:46 +00:00
2018-09-17 00:49:19 +00:00
private final int x;
private final int y;
private final int z;
private final int rangeSq;
2018-08-28 01:00:11 +00:00
public GoalNear(BlockPos pos, int range) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
this.rangeSq = range * range;
}
@Override
public boolean isInGoal(int x, int y, int z) {
int xDiff = x - this.x;
int yDiff = y - this.y;
int zDiff = z - this.z;
return xDiff * xDiff + yDiff * yDiff + zDiff * zDiff <= rangeSq;
2018-08-28 01:00:11 +00:00
}
@Override
public double heuristic(int x, int y, int z) {
int xDiff = x - this.x;
int yDiff = y - this.y;
int zDiff = z - this.z;
return GoalBlock.calculate(xDiff, yDiff, zDiff);
2018-08-28 01:00:11 +00:00
}
@Override
2020-10-15 19:48:17 +00:00
public double heuristic() {// TODO less hacky solution
2020-10-14 22:05:54 +00:00
int range = (int) Math.ceil(Math.sqrt(rangeSq));
2021-02-10 09:15:14 +00:00
DoubleOpenHashSet maybeAlwaysInside = new DoubleOpenHashSet(); // see pull request #1978
2020-10-15 23:30:04 +00:00
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++) {
2020-10-14 22:05:54 +00:00
double h = heuristic(x + dx, y + dy, z + dz);
2020-10-15 23:30:04 +00:00
if (h < minOutside && isInGoal(x + dx, y + dy, z + dz)) {
maybeAlwaysInside.add(h);
} else {
2020-10-15 23:30:04 +00:00
minOutside = Math.min(minOutside, h);
}
}
}
}
double maxInside = Double.NEGATIVE_INFINITY;
2021-02-09 23:29:45 +00:00
DoubleIterator it = maybeAlwaysInside.iterator();
2021-02-10 09:15:14 +00:00
while (it.hasNext()) {
2021-02-09 23:29:45 +00:00
double inside = it.nextDouble();
if (inside < minOutside) {
maxInside = Math.max(maxInside, inside);
}
}
return maxInside;
}
2018-09-17 02:18:39 +00:00
@Override
public BlockPos getGoalPos() {
return new BlockPos(x, y, z);
}
2018-08-31 21:32:15 +00:00
@Override
public String toString() {
2019-09-01 16:47:27 +00:00
return String.format(
2019-09-19 19:53:15 +00:00
"GoalNear{x=%s, y=%s, z=%s, rangeSq=%d}",
SettingsUtil.maybeCensor(x),
SettingsUtil.maybeCensor(y),
SettingsUtil.maybeCensor(z),
rangeSq
2019-09-01 16:47:27 +00:00
);
2018-08-31 21:32:15 +00:00
}
2018-08-28 01:00:11 +00:00
}