From adb6904d5318c934f408e56f12ad1d011cdfff7b Mon Sep 17 00:00:00 2001 From: Brady Date: Tue, 7 Aug 2018 16:36:32 -0500 Subject: [PATCH] Replace ToolSet parameter in cost calculation with CalculationContext --- .../bot/pathing/calc/AStarPathFinder.java | 3 +- .../pathing/movement/CalculationContext.java | 24 +++++++++++++++ .../bot/pathing/movement/Movement.java | 11 ++++--- .../movement/movements/MovementAscend.java | 9 +++--- .../movement/movements/MovementDescend.java | 7 +++-- .../movement/movements/MovementDiagonal.java | 6 ++-- .../movement/movements/MovementDownward.java | 30 +++++++++---------- .../movement/movements/MovementFall.java | 10 ++----- .../movement/movements/MovementTraverse.java | 10 +++---- 9 files changed, 66 insertions(+), 44 deletions(-) create mode 100644 src/main/java/baritone/bot/pathing/movement/CalculationContext.java diff --git a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java index 8b1bf35a..0f0fd537 100644 --- a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java @@ -4,6 +4,7 @@ import baritone.bot.pathing.calc.openset.BinaryHeapOpenSet; import baritone.bot.pathing.calc.openset.IOpenSet; import baritone.bot.pathing.goals.Goal; import baritone.bot.pathing.movement.ActionCosts; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.movements.MovementAscend; @@ -88,7 +89,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch { } //long costStart = System.nanoTime(); // TODO cache cost - double actionCost = movementToGetToNeighbor.getCost(ts); + double actionCost = movementToGetToNeighbor.getCost(new CalculationContext(ts)); //long costEnd = System.nanoTime(); //System.out.println(movementToGetToNeighbor.getClass() + "" + (costEnd - costStart)); if (actionCost >= ActionCosts.COST_INF) { diff --git a/src/main/java/baritone/bot/pathing/movement/CalculationContext.java b/src/main/java/baritone/bot/pathing/movement/CalculationContext.java new file mode 100644 index 00000000..2de381f6 --- /dev/null +++ b/src/main/java/baritone/bot/pathing/movement/CalculationContext.java @@ -0,0 +1,24 @@ +package baritone.bot.pathing.movement; + +import baritone.bot.utils.ToolSet; + +/** + * @author Brady + * @since 8/7/2018 4:30 PM + */ +public class CalculationContext { + + private final ToolSet toolSet; + + public CalculationContext() { + this(new ToolSet()); + } + + public CalculationContext(ToolSet toolSet) { + this.toolSet = toolSet; + } + + public ToolSet getToolSet() { + return this.toolSet; + } +} diff --git a/src/main/java/baritone/bot/pathing/movement/Movement.java b/src/main/java/baritone/bot/pathing/movement/Movement.java index a90bee61..a6afffee 100644 --- a/src/main/java/baritone/bot/pathing/movement/Movement.java +++ b/src/main/java/baritone/bot/pathing/movement/Movement.java @@ -45,17 +45,16 @@ public abstract class Movement implements Helper, MovementHelper { this(src, dest, toBreak, toPlace); } - public double getCost(ToolSet ts) { + public double getCost(CalculationContext context) { if (cost == null) { - if (ts == null) { - ts = new ToolSet(); - } - cost = calculateCost(ts); + if (context == null) + context = new CalculationContext(); + cost = calculateCost(context); } return cost; } - protected abstract double calculateCost(ToolSet ts); // TODO pass in information like whether it's allowed to place throwaway blocks + protected abstract double calculateCost(CalculationContext context); // TODO pass in information like whether it's allowed to place throwaway blocks public double recalculateCost() { cost = null; diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java index 1d8f3c8d..ac0e9b98 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -1,12 +1,12 @@ package baritone.bot.pathing.movement.movements; import baritone.bot.InputOverrideHandler; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import net.minecraft.block.BlockFalling; import net.minecraft.util.math.BlockPos; @@ -37,7 +37,7 @@ public class MovementAscend extends Movement { } @Override - protected double calculateCost(ToolSet ts) { + protected double calculateCost(CalculationContext context) { if (!MovementHelper.canWalkOn(positionsToPlace[0])) { if (!BlockStateInterface.isAir(positionsToPlace[0]) && !BlockStateInterface.isWater(positionsToPlace[0])) { return COST_INF; @@ -47,7 +47,7 @@ public class MovementAscend extends Movement { } for (BlockPos against1 : against) { if (BlockStateInterface.get(against1).isBlockNormalCube()) { - return JUMP_ONE_BLOCK_COST + WALK_ONE_BLOCK_COST + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts ); + return JUMP_ONE_BLOCK_COST + WALK_ONE_BLOCK_COST + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } } return COST_INF; @@ -55,7 +55,8 @@ public class MovementAscend extends Movement { if (BlockStateInterface.get(src.up(3)).getBlock() instanceof BlockFalling) {//it would fall on us and possibly suffocate us return COST_INF; } - return WALK_ONE_BLOCK_COST / 2 + Math.max(JUMP_ONE_BLOCK_COST, WALK_ONE_BLOCK_COST / 2) + getTotalHardnessOfBlocksToBreak(ts);//we walk half the block to get to the edge, then we walk the other half while simultaneously jumping (math.max because of how it's in parallel) + // we walk half the block to get to the edge, then we walk the other half while simultaneously jumping (math.max because of how it's in parallel) + return WALK_ONE_BLOCK_COST / 2 + Math.max(JUMP_ONE_BLOCK_COST, WALK_ONE_BLOCK_COST / 2) + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } @Override diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java index 613945f1..2ba17220 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDescend.java @@ -1,12 +1,12 @@ package baritone.bot.pathing.movement.movements; import baritone.bot.InputOverrideHandler; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import net.minecraft.block.Block; import net.minecraft.block.BlockLadder; import net.minecraft.block.BlockVine; @@ -19,7 +19,7 @@ public class MovementDescend extends Movement { } @Override - protected double calculateCost(ToolSet ts) { + protected double calculateCost(CalculationContext context) { if (!MovementHelper.canWalkOn(positionsToPlace[0])) { return COST_INF; } @@ -27,7 +27,8 @@ public class MovementDescend extends Movement { if (tmp1 instanceof BlockLadder || tmp1 instanceof BlockVine) { return COST_INF; } - return WALK_ONE_BLOCK_COST * 0.8 + Math.max(FALL_N_BLOCKS_COST[1], WALK_ONE_BLOCK_COST * 0.2) + getTotalHardnessOfBlocksToBreak(ts);//we walk half the block plus 0.3 to get to the edge, then we walk the other 0.2 while simultaneously falling (math.max because of how it's in parallel) + // we walk half the block plus 0.3 to get to the edge, then we walk the other 0.2 while simultaneously falling (math.max because of how it's in parallel) + return WALK_ONE_BLOCK_COST * 0.8 + Math.max(FALL_N_BLOCKS_COST[1], WALK_ONE_BLOCK_COST * 0.2) + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } @Override diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java index 0a4daea5..56d6348e 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDiagonal.java @@ -1,10 +1,10 @@ package baritone.bot.pathing.movement.movements; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.BlockPos; @@ -46,8 +46,8 @@ public class MovementDiagonal extends Movement { } @Override - protected double calculateCost(ToolSet ts) { - if (getTotalHardnessOfBlocksToBreak(ts) != 0) { + protected double calculateCost(CalculationContext context) { + if (getTotalHardnessOfBlocksToBreak(context.getToolSet()) != 0) { return COST_INF; } if (!MovementHelper.canWalkOn(positionsToPlace[0])) { diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java index cae382e5..6581a8e6 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementDownward.java @@ -1,10 +1,10 @@ package baritone.bot.pathing.movement.movements; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import net.minecraft.block.Block; import net.minecraft.block.BlockLadder; import net.minecraft.block.BlockVine; @@ -18,6 +18,20 @@ public class MovementDownward extends Movement { super(start, start.down(), new BlockPos[]{start.down()}, new BlockPos[0]); } + @Override + protected double calculateCost(CalculationContext context) { + if (!MovementHelper.canWalkOn(dest.down())) { + return COST_INF; + } + Block td = BlockStateInterface.get(dest).getBlock(); + boolean ladder = td instanceof BlockLadder || td instanceof BlockVine; + if (ladder) { + return LADDER_DOWN_ONE_COST; + } else { + return FALL_N_BLOCKS_COST[1] + getTotalHardnessOfBlocksToBreak(context.getToolSet()); + } + } + @Override public MovementState updateState(MovementState state) { super.updateState(state); @@ -46,18 +60,4 @@ public class MovementDownward extends Movement { return state; } } - - @Override - protected double calculateCost(ToolSet ts) { - if (!MovementHelper.canWalkOn(dest.down())) { - return COST_INF; - } - Block td = BlockStateInterface.get(dest).getBlock(); - boolean ladder = td instanceof BlockLadder || td instanceof BlockVine; - if (ladder) { - return LADDER_DOWN_ONE_COST; - } else { - return FALL_N_BLOCKS_COST[1] + getTotalHardnessOfBlocksToBreak(ts); - } - } } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java index a2f0caad..320b29d3 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementFall.java @@ -2,15 +2,11 @@ package baritone.bot.pathing.movement.movements; import baritone.bot.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; -import baritone.bot.pathing.movement.ActionCosts; -import baritone.bot.pathing.movement.Movement; -import baritone.bot.pathing.movement.MovementHelper; -import baritone.bot.pathing.movement.MovementState; +import baritone.bot.pathing.movement.*; import baritone.bot.pathing.movement.MovementState.MovementStatus; import baritone.bot.pathing.movement.MovementState.MovementTarget; import baritone.bot.utils.BlockStateInterface; import baritone.bot.utils.Rotation; -import baritone.bot.utils.ToolSet; import baritone.bot.utils.Utils; import net.minecraft.init.Blocks; import net.minecraft.item.ItemBucket; @@ -25,7 +21,7 @@ public class MovementFall extends Movement { } @Override - protected double calculateCost(ToolSet ts) { + protected double calculateCost(CalculationContext context) { if (!MovementHelper.canWalkOn(positionsToPlace[0])) { return COST_INF; } @@ -33,7 +29,7 @@ public class MovementFall extends Movement { if (!BlockStateInterface.isWater(dest) && src.getY() - dest.getY() > 3) { placeBucketCost = ActionCosts.PLACE_ONE_BLOCK_COST; } - double cost = getTotalHardnessOfBlocksToBreak(ts); + double cost = getTotalHardnessOfBlocksToBreak(context.getToolSet()); if (cost != 0) { return COST_INF; } diff --git a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java index abd3bdf4..374734f2 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementTraverse.java @@ -2,11 +2,11 @@ package baritone.bot.pathing.movement.movements; import baritone.bot.InputOverrideHandler; import baritone.bot.behavior.impl.LookBehaviorUtils; +import baritone.bot.pathing.movement.CalculationContext; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementHelper; import baritone.bot.pathing.movement.MovementState; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import baritone.bot.utils.Utils; import net.minecraft.block.Block; import net.minecraft.block.BlockLadder; @@ -48,7 +48,7 @@ public class MovementTraverse extends Movement { } @Override - protected double calculateCost(ToolSet ts) { + protected double calculateCost(CalculationContext context) { IBlockState pb0 = BlockStateInterface.get(positionsToBreak[0]); IBlockState pb1 = BlockStateInterface.get(positionsToBreak[1]); double WC = BlockStateInterface.isWater(pb0.getBlock()) || BlockStateInterface.isWater(pb1.getBlock()) ? WALK_ONE_IN_WATER_COST : WALK_ONE_BLOCK_COST; @@ -59,7 +59,7 @@ public class MovementTraverse extends Movement { //double hardness1 = blocksToBreak[0].getBlockHardness(Minecraft.getMinecraft().world, positionsToBreak[0]); //double hardness2 = blocksToBreak[1].getBlockHardness(Minecraft.getMinecraft().world, positionsToBreak[1]); //Out.log("Can't walk through " + blocksToBreak[0] + " (hardness" + hardness1 + ") or " + blocksToBreak[1] + " (hardness " + hardness2 + ")"); - return WC + getTotalHardnessOfBlocksToBreak(ts); + return WC + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } else {//this is a bridge, so we need to place a block //return 1000000; Block f = BlockStateInterface.get(src.down()).getBlock(); @@ -70,11 +70,11 @@ public class MovementTraverse extends Movement { if (pp0.getBlock().equals(Blocks.AIR) || (!BlockStateInterface.isWater(pp0.getBlock()) && pp0.getBlock().isReplaceable(Minecraft.getMinecraft().world, positionsToPlace[0]))) { for (BlockPos against1 : against) { if (BlockStateInterface.get(against1).isBlockNormalCube()) { - return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts); + return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } } WC = WC * SNEAK_ONE_BLOCK_COST / WALK_ONE_BLOCK_COST;//since we are placing, we are sneaking - return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(ts); + return WC + PLACE_ONE_BLOCK_COST + getTotalHardnessOfBlocksToBreak(context.getToolSet()); } return COST_INF; //Out.log("Can't walk on " + Baritone.get(positionsToPlace[0]).getBlock());