Replace ToolSet parameter in cost calculation with CalculationContext

This commit is contained in:
Brady 2018-08-07 16:36:32 -05:00
parent 317b3fc683
commit adb6904d53
No known key found for this signature in database
GPG Key ID: 73A788379A197567
9 changed files with 66 additions and 44 deletions

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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])) {

View File

@ -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);
}
}
}

View File

@ -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;
}

View File

@ -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());