baritone/src/main/java/baritone/pathing/movement/movements/MovementParkour.java

265 lines
12 KiB
Java
Raw Normal View History

2018-09-06 14:48:27 +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-09-06 14:48:27 +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-09-06 14:48:27 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-09-06 14:48:27 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.pathing.movement.movements;
import baritone.Baritone;
2018-10-09 01:37:52 +00:00
import baritone.api.pathing.movement.MovementStatus;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.RayTraceUtils;
import baritone.api.utils.Rotation;
import baritone.api.utils.RotationUtils;
2018-10-09 01:37:52 +00:00
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement;
import baritone.pathing.movement.MovementHelper;
import baritone.pathing.movement.MovementState;
import baritone.utils.BlockStateInterface;
import baritone.utils.Helper;
import baritone.utils.InputOverrideHandler;
import baritone.utils.pathing.MutableMoveResult;
2018-10-02 22:34:46 +00:00
import net.minecraft.block.Block;
2018-09-06 14:48:27 +00:00
import net.minecraft.block.state.IBlockState;
2018-09-12 03:09:53 +00:00
import net.minecraft.client.Minecraft;
2018-09-06 14:48:27 +00:00
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
2018-09-12 03:09:53 +00:00
import net.minecraft.util.math.Vec3d;
import java.util.Objects;
2018-09-06 14:48:27 +00:00
public class MovementParkour extends Movement {
2018-09-23 20:26:57 +00:00
2018-09-23 19:24:07 +00:00
private static final EnumFacing[] HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP = {EnumFacing.NORTH, EnumFacing.SOUTH, EnumFacing.EAST, EnumFacing.WEST, EnumFacing.DOWN};
2018-09-22 15:47:02 +00:00
private static final BetterBlockPos[] EMPTY = new BetterBlockPos[]{};
2018-09-12 03:09:53 +00:00
2018-09-17 00:49:19 +00:00
private final EnumFacing direction;
private final int dist;
2018-09-06 14:48:27 +00:00
private MovementParkour(BetterBlockPos src, int dist, EnumFacing dir) {
2018-10-05 04:28:34 +00:00
super(src, src.offset(dir, dist), EMPTY, src.offset(dir, dist).down());
2018-09-06 14:48:27 +00:00
this.direction = dir;
this.dist = dist;
}
2018-09-23 19:24:07 +00:00
public static MovementParkour cost(CalculationContext context, BetterBlockPos src, EnumFacing direction) {
MutableMoveResult res = new MutableMoveResult();
cost(context, src.x, src.y, src.z, direction, res);
int dist = Math.abs(res.x - src.x) + Math.abs(res.z - src.z);
2018-09-23 19:24:07 +00:00
return new MovementParkour(src, dist, direction);
}
public static void cost(CalculationContext context, int x, int y, int z, EnumFacing dir, MutableMoveResult res) {
2018-09-06 14:48:27 +00:00
if (!Baritone.settings().allowParkour.get()) {
return;
2018-09-06 14:48:27 +00:00
}
IBlockState standingOn = context.get(x, y - 1, z);
2018-09-06 14:48:27 +00:00
if (standingOn.getBlock() == Blocks.VINE || standingOn.getBlock() == Blocks.LADDER || MovementHelper.isBottomSlab(standingOn)) {
return;
2018-09-06 14:48:27 +00:00
}
2018-09-23 19:24:07 +00:00
int xDiff = dir.getXOffset();
int zDiff = dir.getZOffset();
IBlockState adj = context.get(x + xDiff, y - 1, z + zDiff);
2018-09-12 01:33:03 +00:00
if (MovementHelper.avoidWalkingInto(adj.getBlock()) && adj.getBlock() != Blocks.WATER && adj.getBlock() != Blocks.FLOWING_WATER) { // magma sucks
return;
2018-09-06 14:48:27 +00:00
}
if (MovementHelper.canWalkOn(context,x + xDiff, y - 1, z + zDiff, adj)) { // don't parkour if we could just traverse (for now)
return;
2018-09-06 14:48:27 +00:00
}
if (!MovementHelper.fullyPassable(context,x + xDiff, y, z + zDiff)) {
return;
2018-09-06 14:48:27 +00:00
}
if (!MovementHelper.fullyPassable(context,x + xDiff, y + 1, z + zDiff)) {
return;
2018-09-06 14:48:27 +00:00
}
if (!MovementHelper.fullyPassable(context,x + xDiff, y + 2, z + zDiff)) {
return;
2018-09-10 16:22:32 +00:00
}
if (!MovementHelper.fullyPassable(context,x, y + 2, z)) {
return;
2018-09-10 16:22:32 +00:00
}
int maxJump;
if (standingOn.getBlock() == Blocks.SOUL_SAND) {
maxJump = 2; // 1 block gap
} else {
if (context.canSprint()) {
maxJump = 4;
} else {
maxJump = 3;
}
}
for (int i = 2; i <= maxJump; i++) {
2018-09-06 17:14:41 +00:00
// TODO perhaps dest.up(3) doesn't need to be fullyPassable, just canWalkThrough, possibly?
2018-09-23 19:24:07 +00:00
for (int y2 = 0; y2 < 4; y2++) {
if (!MovementHelper.fullyPassable(context,x + xDiff * i, y + y2, z + zDiff * i)) {
return;
}
2018-09-06 14:48:27 +00:00
}
if (MovementHelper.canWalkOn(context,x + xDiff * i, y - 1, z + zDiff * i)) {
res.x = x + xDiff * i;
res.y = y;
res.z = z + zDiff * i;
res.cost = costFromJumpDistance(i);
return;
2018-09-06 14:48:27 +00:00
}
}
if (maxJump != 4) {
return;
2018-09-14 15:40:34 +00:00
}
if (!Baritone.settings().allowParkourPlace.get()) {
return;
2018-09-14 15:40:34 +00:00
}
2018-10-04 02:00:58 +00:00
if (!Baritone.settings().allowPlace.get()) {
Helper.HELPER.logDirect("allowParkourPlace enabled but allowPlace disabled?");
return;
2018-10-04 02:00:58 +00:00
}
2018-09-23 19:24:07 +00:00
int destX = x + 4 * xDiff;
int destZ = z + 4 * zDiff;
IBlockState toPlace = context.get(destX, y - 1, destZ);
if (!context.canPlaceThrowawayAt(destX, y - 1, destZ)) {
return;
2018-09-12 03:09:53 +00:00
}
if (toPlace.getBlock() != Blocks.AIR && !MovementHelper.isWater(toPlace.getBlock()) && !MovementHelper.isReplacable(destX, y - 1, destZ, toPlace)) {
return;
2018-09-12 03:09:53 +00:00
}
for (int i = 0; i < 5; i++) {
2018-09-23 19:24:07 +00:00
int againstX = destX + HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i].getXOffset();
int againstZ = destZ + HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i].getZOffset();
if (againstX == x + xDiff * 3 && againstZ == z + zDiff * 3) { // we can't turn around that fast
2018-09-12 03:09:53 +00:00
continue;
}
if (MovementHelper.canPlaceAgainst(context,againstX, y - 1, againstZ)) {
res.x = destX;
res.y = y;
res.z = destZ;
res.cost = costFromJumpDistance(4) + context.placeBlockCost();
return;
2018-09-12 03:09:53 +00:00
}
}
2018-09-06 14:48:27 +00:00
}
private static double costFromJumpDistance(int dist) {
switch (dist) {
case 2:
return WALK_ONE_BLOCK_COST * 2; // IDK LOL
case 3:
return WALK_ONE_BLOCK_COST * 3;
case 4:
2018-09-06 14:54:12 +00:00
return SPRINT_ONE_BLOCK_COST * 4;
2018-09-17 02:50:07 +00:00
default:
2018-10-02 02:23:13 +00:00
throw new IllegalStateException("LOL " + dist);
2018-09-06 14:48:27 +00:00
}
}
@Override
protected double calculateCost(CalculationContext context) {
MutableMoveResult res = new MutableMoveResult();
cost(context, src.x, src.y, src.z, direction, res);
if (res.x != dest.x || res.z != dest.z) {
2018-09-06 14:48:27 +00:00
return COST_INF;
}
2018-09-23 20:26:57 +00:00
return res.cost;
2018-09-06 14:48:27 +00:00
}
@Override
public boolean safeToCancel(MovementState state) {
// once this movement is instantiated, the state is default to PREPPING
// but once it's ticked for the first time it changes to RUNNING
// since we don't really know anything about momentum, it suffices to say Parkour can only be canceled on the 0th tick
2018-10-09 00:57:22 +00:00
return state.getStatus() != MovementStatus.RUNNING;
}
2018-09-06 14:48:27 +00:00
@Override
public MovementState updateState(MovementState state) {
super.updateState(state);
2018-10-09 00:57:22 +00:00
if (state.getStatus() != MovementStatus.RUNNING) {
2018-09-14 22:57:46 +00:00
return state;
2018-09-06 14:48:27 +00:00
}
if (player().isHandActive()) {
logDebug("Pausing parkour since hand is active");
return state;
}
2018-09-06 14:48:27 +00:00
if (dist >= 4) {
state.setInput(InputOverrideHandler.Input.SPRINT, true);
}
MovementHelper.moveTowards(state, dest);
if (playerFeet().equals(dest)) {
2018-10-02 22:34:46 +00:00
Block d = BlockStateInterface.getBlock(dest);
if (d == Blocks.VINE || d == Blocks.LADDER) {
// it physically hurt me to add support for parkour jumping onto a vine
// but i did it anyway
2018-10-09 00:57:22 +00:00
return state.setStatus(MovementStatus.SUCCESS);
2018-10-02 22:34:46 +00:00
}
if (player().posY - playerFeet().getY() < 0.094) { // lilypads
2018-10-09 00:57:22 +00:00
state.setStatus(MovementStatus.SUCCESS);
2018-09-06 14:48:27 +00:00
}
} else if (!playerFeet().equals(src)) {
if (playerFeet().equals(src.offset(direction)) || player().posY - playerFeet().getY() > 0.0001) {
2018-09-12 03:09:53 +00:00
2018-10-16 03:37:10 +00:00
if (!MovementHelper.canWalkOn(dest.down()) && !player().onGround) {
2018-09-12 03:09:53 +00:00
BlockPos positionToPlace = dest.down();
for (int i = 0; i < 5; i++) {
2018-09-23 19:24:07 +00:00
BlockPos against1 = positionToPlace.offset(HORIZONTALS_BUT_ALSO_DOWN____SO_EVERY_DIRECTION_EXCEPT_UP[i]);
2018-09-12 03:09:53 +00:00
if (against1.up().equals(src.offset(direction, 3))) { // we can't turn around that fast
continue;
}
if (MovementHelper.canPlaceAgainst(against1)) {
if (!MovementHelper.throwaway(true)) {//get ready to place a throwaway block
2018-10-09 00:57:22 +00:00
return state.setStatus(MovementStatus.UNREACHABLE);
2018-09-12 03:09:53 +00:00
}
double faceX = (dest.getX() + against1.getX() + 1.0D) * 0.5D;
double faceY = (dest.getY() + against1.getY()) * 0.5D;
double faceZ = (dest.getZ() + against1.getZ() + 1.0D) * 0.5D;
2018-10-14 05:55:30 +00:00
Rotation place = RotationUtils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations());
RayTraceResult res = RayTraceUtils.rayTraceTowards(player(), place, playerController().getBlockReachDistance());
if (res != null && res.typeOfHit == RayTraceResult.Type.BLOCK && res.getBlockPos().equals(against1) && res.getBlockPos().offset(res.sideHit).equals(dest.down())) {
state.setTarget(new MovementState.MovementTarget(place, true));
}
2018-10-14 05:55:30 +00:00
RayTraceUtils.getSelectedBlock().ifPresent(selectedBlock -> {
EnumFacing side = Minecraft.getMinecraft().objectMouseOver.sideHit;
2018-09-12 03:09:53 +00:00
if (Objects.equals(selectedBlock, against1) && selectedBlock.offset(side).equals(dest.down())) {
state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true);
}
});
}
}
}
if (dist == 3) { // this is a 2 block gap, dest = src + direction * 3
double xDiff = (src.x + 0.5) - player().posX;
double zDiff = (src.z + 0.5) - player().posZ;
double distFromStart = Math.max(Math.abs(xDiff), Math.abs(zDiff));
if (distFromStart < 0.7) {
return state;
}
}
2018-09-12 03:09:53 +00:00
2018-09-06 14:48:27 +00:00
state.setInput(InputOverrideHandler.Input.JUMP, true);
} else if (!playerFeet().equals(dest.offset(direction, -1))) {
2018-09-06 14:48:27 +00:00
state.setInput(InputOverrideHandler.Input.SPRINT, false);
if (playerFeet().equals(src.offset(direction, -1))) {
MovementHelper.moveTowards(state, src);
} else {
MovementHelper.moveTowards(state, src.offset(direction, -1));
}
}
}
return state;
}
}