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 net.minecraft.util.EnumFacing;
022import net.minecraft.util.math.BlockPos;
023
024/**
025 * Dig a tunnel in a certain direction, but if you have to deviate from the path, go back to where you started
026 */
027public class GoalStrictDirection implements Goal {
028
029    public final int x;
030    public final int y;
031    public final int z;
032    public final int dx;
033    public final int dz;
034
035    public GoalStrictDirection(BlockPos origin, EnumFacing direction) {
036        x = origin.getX();
037        y = origin.getY();
038        z = origin.getZ();
039        dx = direction.getXOffset();
040        dz = direction.getZOffset();
041        if (dx == 0 && dz == 0) {
042            throw new IllegalArgumentException(direction + "");
043        }
044    }
045
046    @Override
047    public boolean isInGoal(int x, int y, int z) {
048        return false;
049    }
050
051    @Override
052    public double heuristic(int x, int y, int z) {
053        int distanceFromStartInDesiredDirection = (x - this.x) * dx + (z - this.z) * dz;
054
055        int distanceFromStartInIncorrectDirection = Math.abs((x - this.x) * dz) + Math.abs((z - this.z) * dx);
056
057        int verticalDistanceFromStart = Math.abs(y - this.y);
058
059        // we want heuristic to decrease as desiredDirection increases
060        double heuristic = -distanceFromStartInDesiredDirection * 100;
061
062        heuristic += distanceFromStartInIncorrectDirection * 1000;
063        heuristic += verticalDistanceFromStart * 1000;
064        return heuristic;
065    }
066
067    @Override
068    public double heuristic() {
069        return Double.NEGATIVE_INFINITY;
070    }
071
072    @Override
073    public String toString() {
074        return String.format(
075                "GoalStrictDirection{x=%s, y=%s, z=%s, dx=%s, dz=%s}",
076                SettingsUtil.maybeCensor(x),
077                SettingsUtil.maybeCensor(y),
078                SettingsUtil.maybeCensor(z),
079                SettingsUtil.maybeCensor(dx),
080                SettingsUtil.maybeCensor(dz)
081        );
082    }
083}