From bda2ef8269100d70bd4a2459ea65e9acf3279c32 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 5 Aug 2018 20:27:45 -0400 Subject: [PATCH] ascend cost --- .../bot/pathing/calc/AStarPathFinder.java | 6 +- .../bot/pathing/movement/Movement.java | 68 +++++++++++++++++-- .../bot/pathing/movement/MovementHelper.java | 4 ++ .../movement/movements/MovementAscend.java | 44 +++++++++++- .../bot/pathing/path/PathExecutor.java | 7 +- 5 files changed, 114 insertions(+), 15 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java index 5e930440..6a8e3fee 100644 --- a/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/bot/pathing/calc/AStarPathFinder.java @@ -39,8 +39,8 @@ public class AStarPathFinder extends AbstractNodeCostSearch { } currentlyRunning = this; long startTime = System.currentTimeMillis(); - boolean slowPath=true; - long timeoutTime = startTime + (slowPath ? 40000 :4000); + boolean slowPath = true; + long timeoutTime = startTime + (slowPath ? 40000 : 4000); long lastPrintout = 0; int numNodes = 0; ToolSet ts = new ToolSet(); @@ -77,7 +77,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch { } //long costStart = System.nanoTime(); // TODO cache cost - double actionCost = movementToGetToNeighbor.calculateCost(ts); + double actionCost = movementToGetToNeighbor.getCost(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/Movement.java b/src/main/java/baritone/bot/pathing/movement/Movement.java index 2e5b07e6..6b177eaf 100644 --- a/src/main/java/baritone/bot/pathing/movement/Movement.java +++ b/src/main/java/baritone/bot/pathing/movement/Movement.java @@ -29,6 +29,8 @@ public abstract class Movement implements Helper, MovementHelper { */ public final BlockPos[] positionsToPlace; + private Double cost; + protected Movement(BlockPos src, BlockPos dest, BlockPos[] toBreak, BlockPos[] toPlace) { this.src = src; this.dest = dest; @@ -43,7 +45,61 @@ public abstract class Movement implements Helper, MovementHelper { // .setStatus(MovementStatus.WAITING); } - public abstract double calculateCost(ToolSet ts); // TODO pass in information like whether it's allowed to place throwaway blocks + public double getCost(ToolSet ts) { + if (cost == null) { + if (ts == null) { + ts = new ToolSet(); + } + cost = calculateCost(ts); + } + return cost; + } + + public double getTotalHardnessOfBlocksToBreak(ToolSet ts) { + /* + 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(); + } + } + for (BlockPos pos : toBreak) { + sum += getHardness(ts, Baritone.get(pos), pos); + if (sum >= COST_INF) { + return COST_INF; + } + } + if (!Baritone.allowBreakOrPlace || !Baritone.hasThrowaway) { + for (int i = 0; i < blocksToPlace.length; i++) { + if (!canWalkOn(positionsToPlace[i])) { + return COST_INF; + } + } + }*/ + //^ the above implementation properly deals with falling blocks, TODO integrate + double sum = 0; + for (BlockPos pos : positionsToBreak) { + sum += MovementHelper.getMiningDurationTicks(ts, BlockStateInterface.get(pos), pos); + if (sum >= COST_INF) { + return COST_INF; + } + } + return sum; + } + + protected abstract double calculateCost(ToolSet ts); // TODO pass in information like whether it's allowed to place throwaway blocks + + public double recalculateCost() { + cost = null; + return getCost(null); + } /** * Handles the execution of the latest Movement @@ -59,7 +115,7 @@ public abstract class Movement implements Helper, MovementHelper { // .setGoal()); // } // } - if(isFinished()) { + if (isFinished()) { } MovementState latestState = updateState(currentState); @@ -80,12 +136,12 @@ public abstract class Movement implements Helper, MovementHelper { } private boolean prepare(MovementState state) { - if(state.getStatus() == MovementStatus.WAITING) { + if (state.getStatus() == MovementStatus.WAITING) { return true; } Optional cruftPos; - for(BlockPos blockPos : positionsToBreak) { - if(MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) { + for (BlockPos blockPos : positionsToBreak) { + if (MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) { } } @@ -117,7 +173,7 @@ public abstract class Movement implements Helper, MovementHelper { * @return */ public MovementState updateState(MovementState state) { - if(!prepare(state)) + if (!prepare(state)) return state.setStatus(MovementStatus.PREPPING); return state; } diff --git a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java index 9a4d1e83..81463a2c 100644 --- a/src/main/java/baritone/bot/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/bot/pathing/movement/MovementHelper.java @@ -207,4 +207,8 @@ public interface MovementHelper extends ActionCosts, Helper { Minecraft.getMinecraft().player.inventory.currentItem = ts.getBestSlot(b); } + static boolean isAir(BlockPos pos) { + return BlockStateInterface.get(pos).getBlock().equals(Blocks.AIR); + } + } 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 53c4c143..55fb0f11 100644 --- a/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/bot/pathing/movement/movements/MovementAscend.java @@ -2,20 +2,60 @@ package baritone.bot.pathing.movement.movements; import baritone.bot.InputOverrideHandler; 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; public class MovementAscend extends Movement { + BlockPos[] against = new BlockPos[3]; public MovementAscend(BlockPos src, BlockPos dest) { super(src, dest, new BlockPos[]{dest, src.up(2), dest.up()}, new BlockPos[]{dest.down()}); + + BlockPos placementLocation = positionsToPlace[0];//end.down() + int i = 0; + if (!placementLocation.north().equals(src)) { + against[i] = placementLocation.north(); + i++; + } + if (!placementLocation.south().equals(src)) { + against[i] = placementLocation.south(); + i++; + } + if (!placementLocation.east().equals(src)) { + against[i] = placementLocation.east(); + i++; + } + if (!placementLocation.west().equals(src)) { + against[i] = placementLocation.west(); + i++; + } + //TODO: add ability to place against .down() as well as the cardinal directions + //useful for when you are starting a staircase without anything to place against + // Counterpoint to the above TODO ^ you should move then pillar instead of ascend } @Override - public double calculateCost(ToolSet ts) { - return 1; + protected double calculateCost(ToolSet ts) { + if (!MovementHelper.canWalkOn(positionsToPlace[0])) { + if (!MovementHelper.isAir(positionsToPlace[0]) && !MovementHelper.isWater(positionsToPlace[0])) { + return COST_INF; + } + 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 COST_INF; + } + 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) } @Override diff --git a/src/main/java/baritone/bot/pathing/path/PathExecutor.java b/src/main/java/baritone/bot/pathing/path/PathExecutor.java index 9ef967de..21a68156 100644 --- a/src/main/java/baritone/bot/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/bot/pathing/path/PathExecutor.java @@ -6,7 +6,6 @@ import baritone.bot.pathing.movement.ActionCosts; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementState; import baritone.bot.utils.BlockStateInterface; -import baritone.bot.utils.ToolSet; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.init.Blocks; import net.minecraft.util.Tuple; @@ -115,7 +114,7 @@ public class PathExecutor extends Behavior { } }*/ Movement movement = path.movements().get(pathPosition); - if (movement.calculateCost(new ToolSet()) >= ActionCosts.COST_INF) { + if (movement.recalculateCost() >= ActionCosts.COST_INF) { System.out.println("Something has changed in the world and this movement has become impossible. Cancelling."); pathPosition = path.length() + 3; failed = true; @@ -134,8 +133,8 @@ public class PathExecutor extends Behavior { ticksOnCurrent = 0; } else { ticksOnCurrent++; - if (ticksOnCurrent > movement.calculateCost(new ToolSet()) + 100) { - System.out.println("This movement has taken too long (" + ticksOnCurrent + " ticks, expected " + movement.calculateCost(new ToolSet()) + "). Cancelling."); + if (ticksOnCurrent > movement.recalculateCost() + 100) { + System.out.println("This movement has taken too long (" + ticksOnCurrent + " ticks, expected " + movement.getCost(null) + "). Cancelling."); pathPosition = path.length() + 3; failed = true; return;