Skip to content

Commit

Permalink
Made SpaceRequest a utility class that uses ints for data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
powerboat9 committed Mar 26, 2020
1 parent 734ec50 commit 3c5330f
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/main/java/baritone/behavior/PathingBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public BetterBlockPos pathStart() { // TODO move to a helper or util class
// can't possibly be sneaking off of this one, we're too far away
continue;
}
if (MovementHelper.canWalkOn(ctx, possibleSupport.down()) && MovementHelper.canWalkThrough(ctx, possibleSupport, new SpaceRequest(possibleSupportSide)) && MovementHelper.canWalkThrough(ctx, possibleSupport.up(), new SpaceRequest(possibleSupportSide))) {
if (MovementHelper.canWalkOn(ctx, possibleSupport.down()) && MovementHelper.canWalkThrough(ctx, possibleSupport, SpaceRequest.fromFaces(possibleSupportSide)) && MovementHelper.canWalkThrough(ctx, possibleSupport.up(), SpaceRequest.fromFaces(possibleSupportSide))) {
// this is plausible
//logDebug("Faking path start assuming player is standing off the edge of a block");
return possibleSupport;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/baritone/pathing/movement/Movement.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected boolean prepared(MovementState state) {
boolean somethingInTheWay = false;
for (PositionalSpaceRequest request : spaceRequests) {
BetterBlockPos blockPos = request.getPos();
SpaceRequest spaceRequest = request.getRequest();
int spaceRequest = request.getRequest();
if (!ctx.world().getEntitiesWithinAABB(EntityFallingBlock.class, new AxisAlignedBB(0, 0, 0, 1, 1.1, 1).offset(blockPos)).isEmpty() && Baritone.settings().pauseMiningForFallingBlocks.value) {
return false;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public List<BlockPos> toBreak(BlockStateInterface bsi) {
List<BlockPos> result = new ArrayList<>();
for (PositionalSpaceRequest request : spaceRequests) {
BetterBlockPos positionToBreak = request.getPos();
SpaceRequest spaceRequest = request.getRequest();
int spaceRequest = request.getRequest();
if (!MovementHelper.canWalkThrough(bsi, positionToBreak.x, positionToBreak.y, positionToBreak.z, spaceRequest)) {
result.add(positionToBreak);
}
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/baritone/pathing/movement/MovementHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ static boolean canWalkThrough(BlockStateInterface bsi, PositionalSpaceRequest po
return canWalkThrough(bsi, pos.x, pos.y, pos.z, posReq.getRequest());
}

static boolean canWalkThrough(IPlayerContext ctx, BetterBlockPos pos, SpaceRequest request) {
static boolean canWalkThrough(IPlayerContext ctx, BetterBlockPos pos, int request) {
return canWalkThrough(new BlockStateInterface(ctx), pos.x, pos.y, pos.z, request);
}

static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, SpaceRequest request) {
static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, int request) {
return canWalkThrough(bsi, x, y, z, bsi.get0(x, y, z), request);
}

static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, IBlockState state, SpaceRequest request) {
static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, IBlockState state, int request) {
Block block = state.getBlock();
if (block == Blocks.AIR) { // early return for most common case
return true;
Expand Down Expand Up @@ -148,11 +148,7 @@ static boolean canWalkThrough(BlockStateInterface bsi, int x, int y, int z, IBlo
return block.isPassable(bsi.access, bsi.isPassableBlockPos.setPos(x, y, z));
}

static boolean doorCanFulfillRequest(BlockStateInterface bsi, int x, int y, int z, IBlockState state, SpaceRequest req) {
if (req == null) {
return false;
}

static boolean doorCanFulfillRequest(BlockStateInterface bsi, int x, int y, int z, IBlockState state, int req) {
IBlockState lowerDoor;
IBlockState upperDoor;
if (state.getValue(BlockDoor.HALF) == BlockDoor.EnumDoorHalf.UPPER) {
Expand Down Expand Up @@ -185,17 +181,17 @@ static boolean doorCanFulfillRequest(BlockStateInterface bsi, int x, int y, int
return false;
}
}
return !req.requires(doorFacing);
return !SpaceRequest.requires(req, doorFacing);
} else {
// Can we make it work?
EnumFacing doorFacing = lowerDoor.getValue(BlockDoor.FACING).getOpposite();
if (req.requires(doorFacing)) {
if (SpaceRequest.requires(req, doorFacing)) {
BlockDoor.EnumHingePosition e = upperDoor.getValue(BlockDoor.HINGE);
switch (e) {
case LEFT:
return !req.requires(doorFacing.rotateY());
return !SpaceRequest.requires(req, doorFacing.rotateY());
case RIGHT:
return !req.requires(doorFacing.rotateYCCW());
return !SpaceRequest.requires(req, doorFacing.rotateYCCW());
default:
return false;
}
Expand Down Expand Up @@ -441,11 +437,11 @@ static boolean canPlaceAgainst(BlockStateInterface bsi, int x, int y, int z, IBl
return state.isBlockNormalCube() || state.isFullBlock() || state.getBlock() == Blocks.GLASS || state.getBlock() == Blocks.STAINED_GLASS;
}

static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, boolean includeFalling, SpaceRequest request) {
static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, boolean includeFalling, int request) {
return getMiningDurationTicks(context, x, y, z, context.get(x, y, z), includeFalling, request);
}

static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, IBlockState state, boolean includeFalling, SpaceRequest request) {
static double getMiningDurationTicks(CalculationContext context, int x, int y, int z, IBlockState state, boolean includeFalling, int request) {
Block block = state.getBlock();
if (!canWalkThrough(context.bsi, x, y, z, state, request)) {
if (block instanceof BlockLiquid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public MovementAscend(IBaritone baritone, BetterBlockPos src, BetterBlockPos des
private static PositionalSpaceRequest[] buildSpaceRequests(BetterBlockPos src, BetterBlockPos dest, EnumFacing direction) {
EnumFacing opposite = direction.getOpposite();
return new PositionalSpaceRequest[] {
new PositionalSpaceRequest(src.up(), new SpaceRequest(direction, EnumFacing.UP)),
new PositionalSpaceRequest(dest, new SpaceRequest(opposite).withLowerPlayerSpace()),
new PositionalSpaceRequest(src.up(2), new SpaceRequest(direction).withUpperPlayerSpace()),
new PositionalSpaceRequest(dest.up(), new SpaceRequest(opposite).withUpperPlayerSpace())
new PositionalSpaceRequest(src.up(), SpaceRequest.fromFaces(direction, EnumFacing.UP)),
new PositionalSpaceRequest(dest, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(opposite))),
new PositionalSpaceRequest(src.up(2), SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(direction))),
new PositionalSpaceRequest(dest.up(), SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(opposite)))
};
}

Expand Down Expand Up @@ -157,19 +157,19 @@ public static double cost(CalculationContext context, int x, int y, int z, int d
double totalCost = walk + additionalPlacementCost;
// start with srcUp2 since we already have its state
// includeFalling isn't needed because of the falling check above -- if srcUp3 is falling we will have already exited with COST_INF if we'd actually have to break it
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 2, z, srcUp2, false, new SpaceRequest(direction).withUpperPlayerSpace());
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 2, z, srcUp2, false, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(direction)));
if (totalCost >= COST_INF) {
return COST_INF;
}
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, false, new SpaceRequest(direction));
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, false, SpaceRequest.fromFaces(direction));
if (totalCost >= COST_INF) {
return COST_INF;
}
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 1, destZ, false, new SpaceRequest(direction.getOpposite()).withLowerPlayerSpace());
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 1, destZ, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(direction.getOpposite())));
if (totalCost >= COST_INF) {
return COST_INF;
}
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 2, destZ, true, new SpaceRequest(direction.getOpposite()).withUpperPlayerSpace());
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 2, destZ, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(direction.getOpposite())));
return totalCost;
}

Expand Down Expand Up @@ -244,7 +244,7 @@ public boolean headBonkClear() {
BetterBlockPos startUp = src.up(2);
for (int i = 0; i < 4; i++) {
BetterBlockPos check = startUp.offset(EnumFacing.byHorizontalIndex(i));
if (!MovementHelper.canWalkThrough(ctx, check, new SpaceRequest(direction).withLowerPlayerSpace())) {
if (!MovementHelper.canWalkThrough(ctx, check, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(direction)))) {
// We might bonk our head
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public MovementDescend(IBaritone baritone, BetterBlockPos start, BetterBlockPos
private static PositionalSpaceRequest[] buildRequests(BetterBlockPos start, BetterBlockPos end, EnumFacing direction) {
EnumFacing opposite = direction.getOpposite();
return new PositionalSpaceRequest[] {
new PositionalSpaceRequest(start.up(), new SpaceRequest(direction)),
new PositionalSpaceRequest(end.up(2), new SpaceRequest(opposite).withUpperPlayerSpace()),
new PositionalSpaceRequest(start, new SpaceRequest(direction)),
new PositionalSpaceRequest(end.up(), new SpaceRequest(opposite).withUpperPlayerSpace().withLowerPlayerSpace()),
new PositionalSpaceRequest(end, new SpaceRequest().withLowerPlayerSpace())
new PositionalSpaceRequest(start.up(), SpaceRequest.fromFaces(direction)),
new PositionalSpaceRequest(end.up(2), SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(opposite))),
new PositionalSpaceRequest(start, SpaceRequest.fromFaces(direction)),
new PositionalSpaceRequest(end.up(), SpaceRequest.withAllPlayerSpace(SpaceRequest.fromFaces(opposite))),
new PositionalSpaceRequest(end, SpaceRequest.withLowerPlayerSpace(SpaceRequest.none()))
};
}

Expand Down Expand Up @@ -88,23 +88,23 @@ protected Set<BetterBlockPos> calculateValidPositions() {
public static void cost(CalculationContext context, int x, int y, int z, int destX, int destZ, MutableMoveResult res, EnumFacing direction) {
double totalCost = 0;
IBlockState destDown = context.get(destX, y - 1, destZ);
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y - 1, destZ, destDown, false, new SpaceRequest(direction.getOpposite()).withLowerPlayerSpace());
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y - 1, destZ, destDown, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(direction.getOpposite())));
if (totalCost >= COST_INF) {
return;
}
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y, destZ, false, new SpaceRequest(direction.getOpposite()).withUpperPlayerSpace().withLowerPlayerSpace());
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y, destZ, false, SpaceRequest.withAllPlayerSpace(SpaceRequest.fromFaces(direction.getOpposite())));
if (totalCost >= COST_INF) {
return;
}
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 1, destZ, true, new SpaceRequest(direction.getOpposite()).withUpperPlayerSpace()); // only the top block in the 3 we need to mine needs to consider the falling blocks above
totalCost += MovementHelper.getMiningDurationTicks(context, destX, y + 1, destZ, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(direction.getOpposite()))); // only the top block in the 3 we need to mine needs to consider the falling blocks above
if (totalCost >= COST_INF) {
return;
}
totalCost += MovementHelper.getMiningDurationTicks(context, x, y, z, false, new SpaceRequest(direction));
totalCost += MovementHelper.getMiningDurationTicks(context, x, y, z, false, SpaceRequest.fromFaces(direction));
if (totalCost >= COST_INF) {
return;
}
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, false, new SpaceRequest(direction));
totalCost += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, false, SpaceRequest.fromFaces(direction));
if (totalCost >= COST_INF) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,35 +179,35 @@ public static void cost(CalculationContext context, int x, int y, int z, int des
EnumFacing aFace = (destZ > z) ? EnumFacing.SOUTH : EnumFacing.NORTH;
EnumFacing bFace = (destX > x) ? EnumFacing.EAST : EnumFacing.WEST;
IBlockState stateFeet = context.get(x, y, z);
double optionA = MovementHelper.getMiningDurationTicks(context, x, y, z, stateFeet, false, new SpaceRequest(aFace).withLowerPlayerSpace());
double optionB = MovementHelper.getMiningDurationTicks(context, x, y, z, stateFeet, false, new SpaceRequest(bFace).withLowerPlayerSpace());
double optionA = MovementHelper.getMiningDurationTicks(context, x, y, z, stateFeet, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(aFace)));
double optionB = MovementHelper.getMiningDurationTicks(context, x, y, z, stateFeet, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(bFace)));
if (optionA != 0 && optionB != 0) {
// check these one at a time -- if both were blocked, we already know that (optionA != 0 && optionB != 0)
// so no need to check pb1 as well, might as well return early here
return;
}
IBlockState stateHead = context.get(x, y + 1, z);
optionA += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, stateHead, true, new SpaceRequest(aFace).withUpperPlayerSpace());
optionA += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, stateHead, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(aFace)));
if (optionA != 0 && optionB != 0) {
return;
}
optionB += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, stateHead, true, new SpaceRequest(bFace).withUpperPlayerSpace());
optionB += MovementHelper.getMiningDurationTicks(context, x, y + 1, z, stateHead, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(bFace)));
if (optionA != 0 && optionB != 0) {
return;
}
// Begin checking other blocks
EnumFacing aFaceO = (destZ > z) ? EnumFacing.NORTH : EnumFacing.SOUTH;
optionA += MovementHelper.getMiningDurationTicks(context, x, y, destZ, pb0, false, new SpaceRequest(aFaceO, bFace).withLowerPlayerSpace());
optionA += MovementHelper.getMiningDurationTicks(context, x, y, destZ, pb0, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(aFaceO, bFace)));
if (optionA != 0 && optionB != 0) {
return;
}
EnumFacing bFaceO = (destX > x) ? EnumFacing.WEST : EnumFacing.EAST;
optionB += MovementHelper.getMiningDurationTicks(context, destX, y, z, pb2, false, new SpaceRequest(bFaceO, aFace).withLowerPlayerSpace());
optionB += MovementHelper.getMiningDurationTicks(context, destX, y, z, pb2, false, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(bFaceO, aFace)));
if (optionA != 0 && optionB != 0) {
return;
}
IBlockState pb1 = context.get(x, y + 1, destZ);
optionA += MovementHelper.getMiningDurationTicks(context, x, y + 1, destZ, pb1, true, new SpaceRequest(aFaceO).withUpperPlayerSpace());
optionA += MovementHelper.getMiningDurationTicks(context, x, y + 1, destZ, pb1, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(aFaceO)));
if (optionA != 0 && optionB != 0) {
// same deal, if pb1 makes optionA nonzero and option B already was nonzero, pb3 can't affect the result
return;
Expand All @@ -217,7 +217,7 @@ public static void cost(CalculationContext context, int x, int y, int z, int des
// at this point we're done calculating optionA, so we can check if it's actually possible to edge around in that direction
return;
}
optionB += MovementHelper.getMiningDurationTicks(context, destX, y + 1, z, pb3, true, new SpaceRequest(bFaceO).withUpperPlayerSpace());
optionB += MovementHelper.getMiningDurationTicks(context, destX, y + 1, z, pb3, true, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(bFaceO)));
if (optionA != 0 && optionB != 0) {
// and finally, if the cost is nonzero for both ways to approach this diagonal, it's not possible
return;
Expand All @@ -234,12 +234,12 @@ public static void cost(CalculationContext context, int x, int y, int z, int des
}
// Detect partial blocks which would prevent edging around
if (optionA == 0) {
if (MovementHelper.canWalkThrough(context.bsi, destX, y, z, pb2, new SpaceRequest(bFaceO).withLowerPlayerSpace()) && MovementHelper.canWalkThrough(context.bsi, destX, y + 1, z, pb3, new SpaceRequest(bFaceO).withUpperPlayerSpace())) {
if (MovementHelper.canWalkThrough(context.bsi, destX, y, z, pb2, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(bFaceO))) && MovementHelper.canWalkThrough(context.bsi, destX, y + 1, z, pb3, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(bFaceO)))) {
//res.cost = COST_INF;
return;
}
} else {
if (MovementHelper.canWalkThrough(context.bsi, x, y, destZ, pb0, new SpaceRequest(aFaceO).withLowerPlayerSpace()) && MovementHelper.canWalkThrough(context.bsi, x, y + 1, destZ, pb1, new SpaceRequest(aFaceO).withUpperPlayerSpace())) {
if (MovementHelper.canWalkThrough(context.bsi, x, y, destZ, pb0, SpaceRequest.withLowerPlayerSpace(SpaceRequest.fromFaces(aFaceO))) && MovementHelper.canWalkThrough(context.bsi, x, y + 1, destZ, pb1, SpaceRequest.withUpperPlayerSpace(SpaceRequest.fromFaces(aFaceO)))) {
//res.cost = COST_INF;
return;
}
Expand Down
Loading

0 comments on commit 3c5330f

Please sign in to comment.