fix goal get to block

This commit is contained in:
Leijurv 2018-09-03 20:42:28 -07:00
parent bfafca541f
commit a2f0a1839a
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
2 changed files with 78 additions and 14 deletions

View File

@ -17,32 +17,47 @@
package baritone.pathing.goals;
import net.minecraft.util.EnumFacing;
import baritone.utils.pathing.BetterBlockPos;
import net.minecraft.util.math.BlockPos;
/**
* Don't get into the block, but get directly adjacent to it. Useful for chests.
*
* @author avecowa
*/
public class GoalGetToBlock extends GoalComposite {
public class GoalGetToBlock implements Goal {
private final BlockPos pos;
private final int x;
private final int y;
private final int z;
public GoalGetToBlock(BlockPos pos) {
super(adjacentBlocks(pos));
this.pos = pos;
}
private static BlockPos[] adjacentBlocks(BlockPos pos) {
BlockPos[] sides = new BlockPos[6];
for (int i = 0; i < 6; i++) {
sides[i] = pos.offset(EnumFacing.values()[i]);
}
return sides;
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
}
public BlockPos getGoalPos() {
return pos;
return new BetterBlockPos(x, y, z);
}
@Override
public boolean isInGoal(BlockPos pos) {
int xDiff = pos.getX() - this.x;
int yDiff = pos.getY() - this.y;
int zDiff = pos.getZ() - this.z;
if (yDiff == -2 && xDiff == 0 && zDiff == 0) {
return true;
}
return Math.abs(xDiff) + Math.abs(yDiff) + Math.abs(zDiff) <= 1;
}
@Override
public double heuristic(BlockPos pos) {
int xDiff = pos.getX() - this.x;
int yDiff = pos.getY() - this.y;
int zDiff = pos.getZ() - this.z;
return GoalBlock.calculate(xDiff, yDiff, zDiff);
}
}

View File

@ -0,0 +1,49 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package baritone.pathing.goals;
import net.minecraft.util.math.BlockPos;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class GoalGetToBlockTest {
@Test
public void isInGoal() {
List<String> acceptableOffsets = new ArrayList<>(Arrays.asList("0,0,0", "0,0,1", "0,0,-1", "1,0,0", "-1,0,0", "0,1,0", "0,-1,0", "0,-2,0"));
for (int x = -10; x <= 10; x++) {
for (int y = -10; y <= 10; y++) {
for (int z = -10; z <= 10; z++) {
boolean inGoal = new GoalGetToBlock(new BlockPos(0, 0, 0)).isInGoal(new BlockPos(x, y, z));
String repr = x + "," + y + "," + z;
System.out.println(repr + " " + inGoal);
if (inGoal) {
assertTrue(repr, acceptableOffsets.contains(repr));
acceptableOffsets.remove(repr);
}
}
}
}
assertTrue(acceptableOffsets.toString(), acceptableOffsets.isEmpty());
}
}