001/*
002 * This file is part of Baritone.
003 *
004 * Baritone is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Lesser General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * Baritone is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License
015 * along with Baritone.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package baritone.api.pathing.goals;
019
020import baritone.api.utils.SettingsUtil;
021import baritone.api.utils.interfaces.IGoalRenderPos;
022import net.minecraft.util.math.BlockPos;
023
024
025/**
026 * Don't get into the block, but get directly adjacent to it. Useful for chests.
027 *
028 * @author avecowa
029 */
030public class GoalGetToBlock implements Goal, IGoalRenderPos {
031
032    public final int x;
033    public final int y;
034    public final int z;
035
036    public GoalGetToBlock(BlockPos pos) {
037        this.x = pos.getX();
038        this.y = pos.getY();
039        this.z = pos.getZ();
040    }
041
042    @Override
043    public BlockPos getGoalPos() {
044        return new BlockPos(x, y, z);
045    }
046
047    @Override
048    public boolean isInGoal(int x, int y, int z) {
049        int xDiff = x - this.x;
050        int yDiff = y - this.y;
051        int zDiff = z - this.z;
052        return Math.abs(xDiff) + Math.abs(yDiff < 0 ? yDiff + 1 : yDiff) + Math.abs(zDiff) <= 1;
053    }
054
055    @Override
056    public double heuristic(int x, int y, int z) {
057        int xDiff = x - this.x;
058        int yDiff = y - this.y;
059        int zDiff = z - this.z;
060        return GoalBlock.calculate(xDiff, yDiff < 0 ? yDiff + 1 : yDiff, zDiff);
061    }
062
063    @Override
064    public String toString() {
065        return String.format(
066                "GoalGetToBlock{x=%s,y=%s,z=%s}",
067                SettingsUtil.maybeCensor(x),
068                SettingsUtil.maybeCensor(y),
069                SettingsUtil.maybeCensor(z)
070        );
071    }
072}