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

190 lines
8.3 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +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-08 03:16:53 +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,
2018-08-08 03:16:53 +00:00
* 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-08 03:16:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 20:15:56 +00:00
package baritone.pathing.movement.movements;
2018-08-07 14:52:49 +00:00
2018-10-09 01:37:52 +00:00
import baritone.api.pathing.movement.MovementStatus;
import baritone.api.utils.BetterBlockPos;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement;
import baritone.pathing.movement.MovementHelper;
import baritone.pathing.movement.MovementState;
2018-08-22 20:15:56 +00:00
import baritone.utils.BlockStateInterface;
import baritone.utils.InputOverrideHandler;
import net.minecraft.block.Block;
2018-08-13 14:05:28 +00:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
2018-08-07 14:52:49 +00:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
2018-08-12 15:40:44 +00:00
import java.util.ArrayList;
import java.util.List;
2018-08-12 15:40:44 +00:00
2018-08-07 14:52:49 +00:00
public class MovementDiagonal extends Movement {
2018-08-12 04:27:46 +00:00
private static final double SQRT_2 = Math.sqrt(2);
public MovementDiagonal(BetterBlockPos start, EnumFacing dir1, EnumFacing dir2) {
2018-08-07 14:52:49 +00:00
this(start, start.offset(dir1), start.offset(dir2), dir2);
// super(start, start.offset(dir1).offset(dir2), new BlockPos[]{start.offset(dir1), start.offset(dir1).up(), start.offset(dir2), start.offset(dir2).up(), start.offset(dir1).offset(dir2), start.offset(dir1).offset(dir2).up()}, new BlockPos[]{start.offset(dir1).offset(dir2).down()});
2018-08-07 14:52:49 +00:00
}
private MovementDiagonal(BetterBlockPos start, BetterBlockPos dir1, BetterBlockPos dir2, EnumFacing drr2) {
2018-08-07 14:52:49 +00:00
this(start, dir1.offset(drr2), dir1, dir2);
}
private MovementDiagonal(BetterBlockPos start, BetterBlockPos end, BetterBlockPos dir1, BetterBlockPos dir2) {
2018-09-22 15:47:02 +00:00
super(start, end, new BetterBlockPos[]{dir1, dir1.up(), dir2, dir2.up(), end, end.up()});
2018-08-07 14:52:49 +00:00
}
@Override
protected double calculateCost(CalculationContext context) {
2018-09-23 05:00:28 +00:00
return cost(context, src.x, src.y, src.z, dest.x, dest.z);
}
public static double cost(CalculationContext context, int x, int y, int z, int destX, int destZ) {
Block fromDown = BlockStateInterface.get(x, y - 1, z).getBlock();
2018-09-03 16:15:18 +00:00
if (fromDown == Blocks.LADDER || fromDown == Blocks.VINE) {
return COST_INF;
}
2018-09-24 20:13:24 +00:00
IBlockState destInto = BlockStateInterface.get(destX, y, destZ);
if (!MovementHelper.canWalkThrough(destX, y, destZ, destInto) || !MovementHelper.canWalkThrough(destX, y + 1, destZ)) {
2018-08-07 14:52:49 +00:00
return COST_INF;
}
2018-09-23 05:00:28 +00:00
IBlockState destWalkOn = BlockStateInterface.get(destX, y - 1, destZ);
if (!MovementHelper.canWalkOn(destX, y - 1, destZ, destWalkOn)) {
2018-08-07 14:52:49 +00:00
return COST_INF;
}
2018-08-13 20:40:50 +00:00
double multiplier = WALK_ONE_BLOCK_COST;
// For either possible soul sand, that affects half of our walking
2018-09-23 05:00:28 +00:00
if (destWalkOn.getBlock() == Blocks.SOUL_SAND) {
2018-08-17 19:24:40 +00:00
multiplier += (WALK_ONE_OVER_SOUL_SAND_COST - WALK_ONE_BLOCK_COST) / 2;
}
2018-09-03 16:15:18 +00:00
if (fromDown == Blocks.SOUL_SAND) {
2018-08-17 19:24:40 +00:00
multiplier += (WALK_ONE_OVER_SOUL_SAND_COST - WALK_ONE_BLOCK_COST) / 2;
2018-08-13 14:05:28 +00:00
}
2018-09-23 05:00:28 +00:00
Block cuttingOver1 = BlockStateInterface.get(x, y - 1, destZ).getBlock();
2018-09-24 20:14:36 +00:00
if (cuttingOver1 == Blocks.MAGMA || BlockStateInterface.isLava(cuttingOver1)) {
2018-08-12 15:24:53 +00:00
return COST_INF;
}
2018-09-23 05:00:28 +00:00
Block cuttingOver2 = BlockStateInterface.get(destX, y - 1, z).getBlock();
2018-09-24 20:14:36 +00:00
if (cuttingOver2 == Blocks.MAGMA || BlockStateInterface.isLava(cuttingOver2)) {
2018-08-12 15:24:53 +00:00
return COST_INF;
}
2018-09-23 05:00:28 +00:00
IBlockState pb0 = BlockStateInterface.get(x, y, destZ);
IBlockState pb2 = BlockStateInterface.get(destX, y, z);
2018-09-27 00:25:18 +00:00
double optionA = MovementHelper.getMiningDurationTicks(context, x, y, destZ, pb0, false);
double optionB = MovementHelper.getMiningDurationTicks(context, destX, y, z, pb2, false);
2018-08-12 04:27:46 +00:00
if (optionA != 0 && optionB != 0) {
2018-09-27 00:25:18 +00:00
// check these one at a time -- if pb0 and pb2 were nonzero, we already know that (optionA != 0 && optionB != 0)
// so no need to check pb1 as well, might as well return early here
return COST_INF;
}
2018-10-01 16:57:47 +00:00
IBlockState pb1 = BlockStateInterface.get(x, y + 1, destZ);
2018-09-27 00:25:18 +00:00
optionA += MovementHelper.getMiningDurationTicks(context, x, y + 1, destZ, pb1, true);
if (optionA != 0 && optionB != 0) {
// same deal, if pb1 makes optionA nonzero and option B already was nonzero, pb3 can't affect the result
2018-08-12 04:27:46 +00:00
return COST_INF;
}
2018-10-01 16:57:47 +00:00
IBlockState pb3 = BlockStateInterface.get(destX, y + 1, z);
2018-10-27 21:41:25 +00:00
if (optionA == 0 && ((MovementHelper.avoidWalkingInto(pb2.getBlock()) && pb2.getBlock() != Blocks.WATER) || (MovementHelper.avoidWalkingInto(pb3.getBlock()) && pb3.getBlock() != Blocks.WATER))) {
2018-09-27 00:25:18 +00:00
// at this point we're done calculating optionA, so we can check if it's actually possible to edge around in that direction
2018-10-27 21:41:25 +00:00
return COST_INF;
2018-08-12 04:27:46 +00:00
}
2018-09-27 00:25:18 +00:00
optionB += MovementHelper.getMiningDurationTicks(context, destX, y + 1, z, pb3, true);
if (optionA != 0 && optionB != 0) {
// and finally, if the cost is nonzero for both ways to approach this diagonal, it's not possible
return COST_INF;
}
2018-10-27 21:41:25 +00:00
if (optionB == 0 && ((MovementHelper.avoidWalkingInto(pb0.getBlock()) && pb0.getBlock() != Blocks.WATER) || (MovementHelper.avoidWalkingInto(pb1.getBlock()) && pb1.getBlock() != Blocks.WATER))) {
2018-09-27 00:25:18 +00:00
// and now that option B is fully calculated, see if we can edge around that way
2018-10-27 21:41:25 +00:00
return COST_INF;
2018-08-12 04:27:46 +00:00
}
2018-10-01 17:05:04 +00:00
boolean water = false;
2018-09-24 20:13:24 +00:00
if (BlockStateInterface.isWater(BlockStateInterface.getBlock(x, y, z)) || BlockStateInterface.isWater(destInto.getBlock())) {
// Ignore previous multiplier
// Whatever we were walking on (possibly soul sand) doesn't matter as we're actually floating on water
// Not even touching the blocks below
multiplier = context.waterWalkSpeed();
2018-10-01 17:05:04 +00:00
water = true;
2018-08-17 19:24:40 +00:00
}
2018-08-12 04:27:46 +00:00
if (optionA != 0 || optionB != 0) {
2018-08-13 14:05:28 +00:00
multiplier *= SQRT_2 - 0.001; // TODO tune
2018-08-12 04:27:46 +00:00
}
2018-10-01 17:05:04 +00:00
if (context.canSprint() && !water) {
// If we aren't edging around anything, and we aren't in water
// We can sprint =D
2018-10-01 17:05:04 +00:00
// Don't check for soul sand, since we can sprint on that too
multiplier *= SPRINT_MULTIPLIER;
2018-08-13 20:40:50 +00:00
}
return multiplier * SQRT_2;
2018-08-12 04:27:46 +00:00
}
@Override
public MovementState updateState(MovementState state) {
super.updateState(state);
2018-10-09 00:57:22 +00:00
if (state.getStatus() != MovementStatus.RUNNING) {
return state;
2018-09-08 04:32:25 +00:00
}
if (playerFeet().equals(dest)) {
2018-10-09 00:57:22 +00:00
state.setStatus(MovementStatus.SUCCESS);
return state;
}
if (!BlockStateInterface.isLiquid(playerFeet())) {
state.setInput(InputOverrideHandler.Input.SPRINT, true);
}
MovementHelper.moveTowards(state, dest);
return state;
}
2018-08-12 04:27:46 +00:00
@Override
protected boolean prepared(MovementState state) {
return true;
2018-08-07 14:52:49 +00:00
}
2018-08-12 15:40:44 +00:00
@Override
public List<BlockPos> toBreak() {
2018-08-12 15:40:44 +00:00
if (toBreakCached != null) {
return toBreakCached;
}
List<BlockPos> result = new ArrayList<>();
2018-08-12 15:40:44 +00:00
for (int i = 4; i < 6; i++) {
if (!MovementHelper.canWalkThrough(positionsToBreak[i])) {
result.add(positionsToBreak[i]);
}
}
toBreakCached = result;
return result;
}
@Override
public List<BlockPos> toWalkInto() {
2018-08-12 15:40:44 +00:00
if (toWalkIntoCached == null) {
toWalkIntoCached = new ArrayList<>();
}
List<BlockPos> result = new ArrayList<>();
2018-08-12 15:40:44 +00:00
for (int i = 0; i < 4; i++) {
if (!MovementHelper.canWalkThrough(positionsToBreak[i])) {
result.add(positionsToBreak[i]);
}
}
toWalkIntoCached = result;
return toWalkIntoCached;
}
2018-08-07 14:52:49 +00:00
}