From 5abadc6fe50884afdc8da3a2d0be5cbf8307214f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 26 Aug 2018 13:48:08 -0700 Subject: [PATCH] address cost calculation of blockfalling stacks --- .../baritone/pathing/movement/Movement.java | 69 +++++++++++-------- .../pathing/movement/MovementHelper.java | 18 +++-- .../movement/movements/MovementDiagonal.java | 4 +- .../movement/movements/MovementDownward.java | 3 +- .../movement/movements/MovementFall.java | 5 +- 5 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/Movement.java b/src/main/java/baritone/pathing/movement/Movement.java index 5a3f26f5..11a88ad1 100644 --- a/src/main/java/baritone/pathing/movement/Movement.java +++ b/src/main/java/baritone/pathing/movement/Movement.java @@ -33,6 +33,7 @@ import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -214,39 +215,53 @@ public abstract class Movement implements Helper, MovementHelper { } public double getTotalHardnessOfBlocksToBreak(CalculationContext ctx) { - /* - double sum = 0; - HashSet toBreak = new HashSet(); - for (BlockPos positionsToBreak1 : positionsToBreak) { - toBreak.add(positionsToBreak1); - if (this instanceof ActionFall) {//if we are digging straight down, assume we have already broken the sand above us - continue; - } - BlockPos tmp = positionsToBreak1.up(); - while (canFall(tmp)) { - toBreak.add(tmp); - tmp = tmp.up(); - } + if (positionsToBreak.length == 0) { + return 0; } - for (BlockPos pos : toBreak) { - sum += getHardness(ts, Baritone.get(pos), pos); - if (sum >= COST_INF) { - return COST_INF; - } + if (positionsToBreak.length == 1) { + return MovementHelper.getMiningDurationTicks(ctx, positionsToBreak[0], true); } - if (!Baritone.allowBreakOrPlace || !Baritone.hasThrowaway) { - for (int i = 0; i < blocksToPlace.length; i++) { - if (!canWalkOn(positionsToPlace[i])) { - return COST_INF; + int firstColumnX = positionsToBreak[0].getX(); + int firstColumnZ = positionsToBreak[0].getZ(); + int firstColumnMaxY = positionsToBreak[0].getY(); + int firstColumnMaximalIndex = 0; + boolean hasSecondColumn = false; + int secondColumnX = -1; + int secondColumnZ = -1; + int secondColumnMaxY = -1; + int secondColumnMaximalIndex = -1; + for (int i = 0; i < positionsToBreak.length; i++) { + BlockPos pos = positionsToBreak[i]; + if (pos.getX() == firstColumnX && pos.getZ() == firstColumnZ) { + if (pos.getY() > firstColumnMaxY) { + firstColumnMaxY = pos.getY(); + firstColumnMaximalIndex = i; + } + } else { + if (!hasSecondColumn || (pos.getX() == secondColumnX && pos.getZ() == secondColumnZ)) { + if (hasSecondColumn) { + if (pos.getY() > secondColumnMaxY) { + secondColumnMaxY = pos.getY(); + secondColumnMaximalIndex = i; + } + } else { + hasSecondColumn = true; + secondColumnX = pos.getX(); + secondColumnZ = pos.getZ(); + secondColumnMaxY = pos.getY(); + secondColumnMaximalIndex = i; + } + } else { + throw new IllegalStateException("I literally have no idea " + Arrays.asList(positionsToBreak)); } } - }*/ - //^ the above implementation properly deals with falling blocks, TODO integrate + } + double sum = 0; - for (BlockPos pos : positionsToBreak) { - sum += MovementHelper.getMiningDurationTicks(ctx, pos); + for (int i = 0; i < positionsToBreak.length; i++) { + sum += MovementHelper.getMiningDurationTicks(ctx, positionsToBreak[i], firstColumnMaximalIndex == i || secondColumnMaximalIndex == i); if (sum >= COST_INF) { - return COST_INF; + break; } } return sum; diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 93bd34fb..549ee621 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -208,12 +208,12 @@ public interface MovementHelper extends ActionCosts, Helper { return BlockStateInterface.get(pos).getBlock() instanceof BlockFalling; } - static double getMiningDurationTicks(CalculationContext context, BlockPos position) { + static double getMiningDurationTicks(CalculationContext context, BlockPos position, boolean includeFalling) { IBlockState state = BlockStateInterface.get(position); - return getMiningDurationTicks(context, position, state); + return getMiningDurationTicks(context, position, state, includeFalling); } - static double getMiningDurationTicks(CalculationContext context, BlockPos position, IBlockState state) { + static double getMiningDurationTicks(CalculationContext context, BlockPos position, IBlockState state, boolean includeFalling) { Block block = state.getBlock(); if (!block.equals(Blocks.AIR) && !canWalkThrough(position, state)) { // TODO is the air check really necessary? Isn't air canWalkThrough? if (!context.allowBreak()) { @@ -223,9 +223,17 @@ public interface MovementHelper extends ActionCosts, Helper { return COST_INF; } double m = Blocks.CRAFTING_TABLE.equals(block) ? 10 : 1; // TODO see if this is still necessary. it's from MineBot when we wanted to penalize breaking its crafting table - return m / context.getToolSet().getStrVsBlock(state, position); + double result = m / context.getToolSet().getStrVsBlock(state, position); + if (includeFalling) { + BlockPos up = position.up(); + IBlockState above = BlockStateInterface.get(up); + if (above.getBlock() instanceof BlockFalling) { + result += getMiningDurationTicks(context, up, above, true); + } + } + return result; } - return 0; + return 0; // we won't actually mine it, so don't check fallings above } /** diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java index cd29bdbf..20defe13 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDiagonal.java @@ -97,8 +97,8 @@ public class MovementDiagonal extends Movement { if (BlockStateInterface.get(positionsToBreak[4].down()).getBlock() instanceof BlockMagma) { return COST_INF; } - double optionA = MovementHelper.getMiningDurationTicks(context, positionsToBreak[0]) + MovementHelper.getMiningDurationTicks(context, positionsToBreak[1]); - double optionB = MovementHelper.getMiningDurationTicks(context, positionsToBreak[2]) + MovementHelper.getMiningDurationTicks(context, positionsToBreak[3]); + double optionA = MovementHelper.getMiningDurationTicks(context, positionsToBreak[0], false) + MovementHelper.getMiningDurationTicks(context, positionsToBreak[1], true); + double optionB = MovementHelper.getMiningDurationTicks(context, positionsToBreak[2], false) + MovementHelper.getMiningDurationTicks(context, positionsToBreak[3], true); if (optionA != 0 && optionB != 0) { return COST_INF; } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java index 724b8688..38232396 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementDownward.java @@ -53,7 +53,8 @@ public class MovementDownward extends Movement { if (ladder) { return LADDER_DOWN_ONE_COST; } else { - return FALL_N_BLOCKS_COST[1] + MovementHelper.getMiningDurationTicks(context, dest, d); + // we're standing on it, while it might be block falling, it'll be air by the time we get here in the movement + return FALL_N_BLOCKS_COST[1] + MovementHelper.getMiningDurationTicks(context, dest, d, false); } } diff --git a/src/main/java/baritone/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/pathing/movement/movements/MovementFall.java index 04e42938..c42a8661 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementFall.java @@ -58,7 +58,8 @@ public class MovementFall extends Movement { } double frontThree = 0; for (int i = 0; i < 3; i++) { - frontThree += MovementHelper.getMiningDurationTicks(context, positionsToBreak[i]); + frontThree += MovementHelper.getMiningDurationTicks(context, positionsToBreak[i], false); + // don't include falling because we will check falling right after this, and if it's there it's COST_INF if (frontThree >= COST_INF) { return COST_INF; } @@ -72,7 +73,7 @@ public class MovementFall extends Movement { // Lilypads (i think?) are 0 ticks to mine, but they definitely cause fall damage // Same thing for falling through water... we can't actually do that // And falling through signs is possible, but they do have a mining duration, right? - if (MovementHelper.getMiningDurationTicks(context, positionsToBreak[i]) > 0) { + if (MovementHelper.getMiningDurationTicks(context, positionsToBreak[i], false) > 0) { //can't break while falling return COST_INF; }