From 6b7a8e2bd3f7c88e081fed8d87dba976bf1739c3 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 12 Oct 2018 14:34:33 -0700 Subject: [PATCH] better protection logic, fix placing outside worldborder --- .../pathing/movement/CalculationContext.java | 28 +++++++++++++++++++ .../pathing/movement/MovementHelper.java | 11 +------- .../movement/movements/MovementAscend.java | 2 +- .../movement/movements/MovementParkour.java | 4 +-- .../movement/movements/MovementPillar.java | 2 +- .../movement/movements/MovementTraverse.java | 2 +- .../utils/pathing/BetterWorldBorder.java | 7 +++++ 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/CalculationContext.java b/src/main/java/baritone/pathing/movement/CalculationContext.java index 0b40539e..9f08d578 100644 --- a/src/main/java/baritone/pathing/movement/CalculationContext.java +++ b/src/main/java/baritone/pathing/movement/CalculationContext.java @@ -21,6 +21,7 @@ import baritone.Baritone; import baritone.api.pathing.movement.ActionCosts; import baritone.utils.Helper; import baritone.utils.ToolSet; +import baritone.utils.pathing.BetterWorldBorder; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.init.Items; @@ -44,6 +45,7 @@ public class CalculationContext implements Helper { private final int maxFallHeightBucket; private final double waterWalkSpeed; private final double breakBlockAdditionalCost; + private final BetterWorldBorder worldBorder; public CalculationContext() { this(new ToolSet()); @@ -68,6 +70,32 @@ public class CalculationContext implements Helper { // why cache these things here, why not let the movements just get directly from settings? // because if some movements are calculated one way and others are calculated another way, // then you get a wildly inconsistent path that isn't optimal for either scenario. + this.worldBorder = new BetterWorldBorder(world().getWorldBorder()); + } + + public boolean canPlaceThrowawayAt(int x, int y, int z) { + if (!hasThrowaway()) { // only true if allowPlace is true, see constructor + return false; + } + if (isPossiblyProtected(x, y, z)) { + return false; + } + return worldBorder.canPlaceAt(x, z); // TODO perhaps MovementHelper.canPlaceAgainst could also use this? + } + + public boolean canBreakAt(int x, int y, int z) { + if (!allowBreak()) { + return false; + } + if (isPossiblyProtected(x, y, z)) { + return false; + } + return true; + } + + public boolean isPossiblyProtected(int x, int y, int z) { + // TODO more protection logic here; see #220 + return false; } public ToolSet getToolSet() { diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index d7cce82d..e393c92e 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -343,15 +343,6 @@ public interface MovementHelper extends ActionCosts, Helper { return state.isBlockNormalCube(); } - static double getMiningDurationTicks(CalculationContext context, BetterBlockPos position, boolean includeFalling) { - IBlockState state = BlockStateInterface.get(position); - return getMiningDurationTicks(context, position.x, position.y, position.z, state, includeFalling); - } - - static double getMiningDurationTicks(CalculationContext context, BetterBlockPos position, IBlockState state, boolean includeFalling) { - return getMiningDurationTicks(context, position.x, position.y, position.z, state, includeFalling); - } - static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, boolean includeFalling) { return getMiningDurationTicks(context, x, y, z, BlockStateInterface.get(x, y, z), includeFalling); } @@ -359,7 +350,7 @@ public interface MovementHelper extends ActionCosts, Helper { static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, IBlockState state, boolean includeFalling) { Block block = state.getBlock(); if (!canWalkThrough(x, y, z, state)) { - if (!context.allowBreak()) { + if (!context.canBreakAt(x, y, z)) { return COST_INF; } if (avoidBreaking(x, y, z, state)) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 6989179b..09271779 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -72,7 +72,7 @@ public class MovementAscend extends Movement { } boolean hasToPlace = false; if (!MovementHelper.canWalkOn(destX, y, destZ, toPlace)) { - if (!context.hasThrowaway()) { + if (!context.canPlaceThrowawayAt(destX, y, destZ)) { return COST_INF; } if (toPlace.getBlock() != Blocks.AIR && !BlockStateInterface.isWater(toPlace.getBlock()) && !MovementHelper.isReplacable(destX, y, destZ, toPlace)) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 94b7dfea..23348990 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -118,7 +118,7 @@ public class MovementParkour extends Movement { int destX = x + 4 * xDiff; int destZ = z + 4 * zDiff; IBlockState toPlace = BlockStateInterface.get(destX, y - 1, destZ); - if (!context.hasThrowaway()) { + if (!context.canPlaceThrowawayAt(destX, y - 1, destZ)) { return; } if (toPlace.getBlock() != Blocks.AIR && !BlockStateInterface.isWater(toPlace.getBlock()) && !MovementHelper.isReplacable(destX, y - 1, destZ, toPlace)) { @@ -225,7 +225,7 @@ public class MovementParkour extends Movement { } state.setInput(InputOverrideHandler.Input.JUMP, true); - } else if(!playerFeet().equals(dest.offset(direction, -1))) { + } else if (!playerFeet().equals(dest.offset(direction, -1))) { state.setInput(InputOverrideHandler.Input.SPRINT, false); if (playerFeet().equals(src.offset(direction, -1))) { MovementHelper.moveTowards(state, src); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java index 853b34d5..799a4e9b 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementPillar.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementPillar.java @@ -76,7 +76,7 @@ public class MovementPillar extends Movement { return LADDER_UP_ONE_COST; } } - if (!context.hasThrowaway() && !ladder) { + if (!ladder && !context.canPlaceThrowawayAt(x, y, z)) { return COST_INF; } double hardness = MovementHelper.getMiningDurationTicks(context, x, y + 2, z, toBreak, true); diff --git a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java index f029619a..367d2c17 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementTraverse.java @@ -108,7 +108,7 @@ public class MovementTraverse extends Movement { if (BlockStateInterface.isWater(destOn.getBlock()) && throughWater) { return COST_INF; } - if (!context.hasThrowaway()) { + if (!context.canPlaceThrowawayAt(destX, y - 1, destZ)) { return COST_INF; } double hardness1 = MovementHelper.getMiningDurationTicks(context, destX, y, destZ, pb0, false); diff --git a/src/main/java/baritone/utils/pathing/BetterWorldBorder.java b/src/main/java/baritone/utils/pathing/BetterWorldBorder.java index 8c105275..c9ffe5bd 100644 --- a/src/main/java/baritone/utils/pathing/BetterWorldBorder.java +++ b/src/main/java/baritone/utils/pathing/BetterWorldBorder.java @@ -36,4 +36,11 @@ public class BetterWorldBorder implements Helper { public boolean entirelyContains(int x, int z) { return x + 1 > minX && x < maxX && z + 1 > minZ && z < maxZ; } + + public boolean canPlaceAt(int x, int z) { + // move it in 1 block on all sides + // because we can't place a block at the very edge against a block outside the border + // it won't let us right click it + return x > minX && x + 1 < maxX && z > minZ && z + 1 < maxZ; + } }