From b5a4e65fbf9cd05a54624cb7e46662a52808c92c Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 07:47:28 -0800 Subject: [PATCH 1/5] unable to start a parkour jump from stairs --- src/main/java/baritone/pathing/movement/MovementHelper.java | 2 +- .../baritone/pathing/movement/movements/MovementParkour.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index 72177669..ff73b0f2 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -274,7 +274,7 @@ public interface MovementHelper extends ActionCosts, Helper { // if assumeWalkOnWater is off, we can only walk on water if there is water above it return isWater(up) ^ Baritone.settings().assumeWalkOnWater.get(); } - if (block instanceof BlockGlass || block instanceof BlockStainedGlass) { + if (block == Blocks.GLASS || block == Blocks.STAINED_GLASS) { return true; } if (block instanceof BlockSlab) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 44334569..9010251d 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -33,6 +33,7 @@ import baritone.utils.BlockStateInterface; import baritone.utils.Helper; import baritone.utils.pathing.MutableMoveResult; import net.minecraft.block.Block; +import net.minecraft.block.BlockStairs; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.util.EnumFacing; @@ -68,7 +69,7 @@ public class MovementParkour extends Movement { return; } IBlockState standingOn = context.get(x, y - 1, z); - if (standingOn.getBlock() == Blocks.VINE || standingOn.getBlock() == Blocks.LADDER || MovementHelper.isBottomSlab(standingOn)) { + if (standingOn.getBlock() == Blocks.VINE || standingOn.getBlock() == Blocks.LADDER || standingOn.getBlock() instanceof BlockStairs || MovementHelper.isBottomSlab(standingOn)) { return; } int xDiff = dir.getXOffset(); From 0d7131413aaad269ca6dec5348f02e5df0bb3827 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 07:55:59 -0800 Subject: [PATCH 2/5] prevent double jumping --- .../baritone/pathing/movement/movements/MovementAscend.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java index 9c33742e..0baef881 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementAscend.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementAscend.java @@ -211,6 +211,10 @@ public class MovementAscend extends Movement { return state; } + if (ctx.playerFeet().equals(src.up())) { + return state; // no need to hit space if we're already jumping + } + if (headBonkClear()) { return state.setInput(Input.JUMP, true); } From 70f8d1d4aeac648581b8afd8486bb30d8e875bd9 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 09:00:52 -0800 Subject: [PATCH 3/5] fix bsi creation on wrong thread, cache chunks, fixes #210 --- .../mixins/MixinChunkProviderClient.java | 39 +++++++ src/launch/resources/mixins.baritone.json | 1 + src/main/java/baritone/pathing/calc/Path.java | 2 +- .../java/baritone/pathing/movement/Moves.java | 107 +++++++++--------- .../movement/movements/MovementParkour.java | 6 +- .../baritone/process/GetToBlockProcess.java | 12 +- .../java/baritone/process/MineProcess.java | 38 ++++--- .../baritone/utils/BlockStateInterface.java | 19 ++++ .../utils/ExampleBaritoneControl.java | 3 +- .../utils/accessor/IChunkProviderClient.java | 25 ++++ 10 files changed, 172 insertions(+), 80 deletions(-) create mode 100644 src/launch/java/baritone/launch/mixins/MixinChunkProviderClient.java create mode 100644 src/main/java/baritone/utils/accessor/IChunkProviderClient.java diff --git a/src/launch/java/baritone/launch/mixins/MixinChunkProviderClient.java b/src/launch/java/baritone/launch/mixins/MixinChunkProviderClient.java new file mode 100644 index 00000000..b4cada6e --- /dev/null +++ b/src/launch/java/baritone/launch/mixins/MixinChunkProviderClient.java @@ -0,0 +1,39 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.launch.mixins; + +import baritone.utils.accessor.IChunkProviderClient; +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import net.minecraft.client.multiplayer.ChunkProviderClient; +import net.minecraft.world.chunk.Chunk; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(ChunkProviderClient.class) +public class MixinChunkProviderClient implements IChunkProviderClient { + + @Shadow + @Final + private Long2ObjectMap loadedChunks; + + @Override + public Long2ObjectMap loadedChunks() { + return this.loadedChunks; + } +} diff --git a/src/launch/resources/mixins.baritone.json b/src/launch/resources/mixins.baritone.json index 9ea70330..09ebbafd 100644 --- a/src/launch/resources/mixins.baritone.json +++ b/src/launch/resources/mixins.baritone.json @@ -10,6 +10,7 @@ "client": [ "MixinAnvilChunkLoader", "MixinBlockPos", + "MixinChunkProviderClient", "MixinChunkProviderServer", "MixinEntity", "MixinEntityLivingBase", diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index ee3b182b..0b5b2cc7 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -129,7 +129,7 @@ class Path extends PathBase { private Movement runBackwards(BetterBlockPos src, BetterBlockPos dest, double cost) { for (Moves moves : Moves.values()) { - Movement move = moves.apply0(context.getBaritone(), src); + Movement move = moves.apply0(context, src); if (move.getDest().equals(dest)) { // have to calculate the cost at calculation time so we can accurately judge whether a cost increase happened between cached calculation and real execution move.override(cost); diff --git a/src/main/java/baritone/pathing/movement/Moves.java b/src/main/java/baritone/pathing/movement/Moves.java index ebb97081..c5a6ceb6 100644 --- a/src/main/java/baritone/pathing/movement/Moves.java +++ b/src/main/java/baritone/pathing/movement/Moves.java @@ -17,7 +17,6 @@ package baritone.pathing.movement; -import baritone.api.IBaritone; import baritone.api.utils.BetterBlockPos; import baritone.pathing.movement.movements.*; import baritone.utils.pathing.MutableMoveResult; @@ -31,8 +30,8 @@ import net.minecraft.util.EnumFacing; public enum Moves { DOWNWARD(0, -1, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementDownward(baritone, src, src.down()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementDownward(context.getBaritone(), src, src.down()); } @Override @@ -43,8 +42,8 @@ public enum Moves { PILLAR(0, +1, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementPillar(baritone, src, src.up()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementPillar(context.getBaritone(), src, src.up()); } @Override @@ -55,8 +54,8 @@ public enum Moves { TRAVERSE_NORTH(0, 0, -1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementTraverse(baritone, src, src.north()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementTraverse(context.getBaritone(), src, src.north()); } @Override @@ -67,8 +66,8 @@ public enum Moves { TRAVERSE_SOUTH(0, 0, +1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementTraverse(baritone, src, src.south()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementTraverse(context.getBaritone(), src, src.south()); } @Override @@ -79,8 +78,8 @@ public enum Moves { TRAVERSE_EAST(+1, 0, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementTraverse(baritone, src, src.east()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementTraverse(context.getBaritone(), src, src.east()); } @Override @@ -91,8 +90,8 @@ public enum Moves { TRAVERSE_WEST(-1, 0, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementTraverse(baritone, src, src.west()); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementTraverse(context.getBaritone(), src, src.west()); } @Override @@ -103,8 +102,8 @@ public enum Moves { ASCEND_NORTH(0, +1, -1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementAscend(baritone, src, new BetterBlockPos(src.x, src.y + 1, src.z - 1)); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementAscend(context.getBaritone(), src, new BetterBlockPos(src.x, src.y + 1, src.z - 1)); } @Override @@ -115,8 +114,8 @@ public enum Moves { ASCEND_SOUTH(0, +1, +1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementAscend(baritone, src, new BetterBlockPos(src.x, src.y + 1, src.z + 1)); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementAscend(context.getBaritone(), src, new BetterBlockPos(src.x, src.y + 1, src.z + 1)); } @Override @@ -127,8 +126,8 @@ public enum Moves { ASCEND_EAST(+1, +1, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementAscend(baritone, src, new BetterBlockPos(src.x + 1, src.y + 1, src.z)); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementAscend(context.getBaritone(), src, new BetterBlockPos(src.x + 1, src.y + 1, src.z)); } @Override @@ -139,8 +138,8 @@ public enum Moves { ASCEND_WEST(-1, +1, 0) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementAscend(baritone, src, new BetterBlockPos(src.x - 1, src.y + 1, src.z)); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementAscend(context.getBaritone(), src, new BetterBlockPos(src.x - 1, src.y + 1, src.z)); } @Override @@ -151,13 +150,13 @@ public enum Moves { DESCEND_EAST(+1, -1, 0, false, true) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(baritone), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { - return new MovementDescend(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementDescend(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } else { - return new MovementFall(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementFall(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } } @@ -169,13 +168,13 @@ public enum Moves { DESCEND_WEST(-1, -1, 0, false, true) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(baritone), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { - return new MovementDescend(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementDescend(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } else { - return new MovementFall(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementFall(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } } @@ -187,13 +186,13 @@ public enum Moves { DESCEND_NORTH(0, -1, -1, false, true) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(baritone), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { - return new MovementDescend(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementDescend(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } else { - return new MovementFall(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementFall(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } } @@ -205,13 +204,13 @@ public enum Moves { DESCEND_SOUTH(0, -1, +1, false, true) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { + public Movement apply0(CalculationContext context, BetterBlockPos src) { MutableMoveResult res = new MutableMoveResult(); - apply(new CalculationContext(baritone), src.x, src.y, src.z, res); + apply(context, src.x, src.y, src.z, res); if (res.y == src.y - 1) { - return new MovementDescend(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementDescend(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } else { - return new MovementFall(baritone, src, new BetterBlockPos(res.x, res.y, res.z)); + return new MovementFall(context.getBaritone(), src, new BetterBlockPos(res.x, res.y, res.z)); } } @@ -223,8 +222,8 @@ public enum Moves { DIAGONAL_NORTHEAST(+1, 0, -1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementDiagonal(baritone, src, EnumFacing.NORTH, EnumFacing.EAST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementDiagonal(context.getBaritone(), src, EnumFacing.NORTH, EnumFacing.EAST); } @Override @@ -235,8 +234,8 @@ public enum Moves { DIAGONAL_NORTHWEST(-1, 0, -1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementDiagonal(baritone, src, EnumFacing.NORTH, EnumFacing.WEST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementDiagonal(context.getBaritone(), src, EnumFacing.NORTH, EnumFacing.WEST); } @Override @@ -247,8 +246,8 @@ public enum Moves { DIAGONAL_SOUTHEAST(+1, 0, +1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementDiagonal(baritone, src, EnumFacing.SOUTH, EnumFacing.EAST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementDiagonal(context.getBaritone(), src, EnumFacing.SOUTH, EnumFacing.EAST); } @Override @@ -259,8 +258,8 @@ public enum Moves { DIAGONAL_SOUTHWEST(-1, 0, +1) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return new MovementDiagonal(baritone, src, EnumFacing.SOUTH, EnumFacing.WEST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return new MovementDiagonal(context.getBaritone(), src, EnumFacing.SOUTH, EnumFacing.WEST); } @Override @@ -271,8 +270,8 @@ public enum Moves { PARKOUR_NORTH(0, 0, -4, true, false) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return MovementParkour.cost(baritone, src, EnumFacing.NORTH); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.NORTH); } @Override @@ -283,8 +282,8 @@ public enum Moves { PARKOUR_SOUTH(0, 0, +4, true, false) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return MovementParkour.cost(baritone, src, EnumFacing.SOUTH); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.SOUTH); } @Override @@ -295,8 +294,8 @@ public enum Moves { PARKOUR_EAST(+4, 0, 0, true, false) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return MovementParkour.cost(baritone, src, EnumFacing.EAST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.EAST); } @Override @@ -307,8 +306,8 @@ public enum Moves { PARKOUR_WEST(-4, 0, 0, true, false) { @Override - public Movement apply0(IBaritone baritone, BetterBlockPos src) { - return MovementParkour.cost(baritone, src, EnumFacing.WEST); + public Movement apply0(CalculationContext context, BetterBlockPos src) { + return MovementParkour.cost(context, src, EnumFacing.WEST); } @Override @@ -336,7 +335,7 @@ public enum Moves { this(x, y, z, false, false); } - public abstract Movement apply0(IBaritone baritone, BetterBlockPos src); + public abstract Movement apply0(CalculationContext context, BetterBlockPos src); public void apply(CalculationContext context, int x, int y, int z, MutableMoveResult result) { if (dynamicXZ || dynamicY) { diff --git a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java index 9010251d..80d6feb4 100644 --- a/src/main/java/baritone/pathing/movement/movements/MovementParkour.java +++ b/src/main/java/baritone/pathing/movement/movements/MovementParkour.java @@ -57,11 +57,11 @@ public class MovementParkour extends Movement { this.dist = dist; } - public static MovementParkour cost(IBaritone baritone, BetterBlockPos src, EnumFacing direction) { + public static MovementParkour cost(CalculationContext context, BetterBlockPos src, EnumFacing direction) { MutableMoveResult res = new MutableMoveResult(); - cost(new CalculationContext(baritone), src.x, src.y, src.z, direction, res); + cost(context, src.x, src.y, src.z, direction, res); int dist = Math.abs(res.x - src.x) + Math.abs(res.z - src.z); - return new MovementParkour(baritone, src, dist, direction); + return new MovementParkour(context.getBaritone(), src, dist, direction); } public static void cost(CalculationContext context, int x, int y, int z, EnumFacing dir, MutableMoveResult res) { diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 5eefe6a9..95f2122b 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -24,6 +24,7 @@ import baritone.api.pathing.goals.GoalGetToBlock; import baritone.api.process.IGetToBlockProcess; import baritone.api.process.PathingCommand; import baritone.api.process.PathingCommandType; +import baritone.pathing.movement.CalculationContext; import baritone.utils.BaritoneProcessHelper; import net.minecraft.block.Block; import net.minecraft.util.math.BlockPos; @@ -46,7 +47,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl public void getToBlock(Block block) { gettingTo = block; knownLocations = null; - rescan(new ArrayList<>()); + rescan(new ArrayList<>(), new CalculationContext(baritone)); } @Override @@ -57,7 +58,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl @Override public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) { if (knownLocations == null) { - rescan(new ArrayList<>()); + rescan(new ArrayList<>(), new CalculationContext(baritone)); } if (knownLocations.isEmpty()) { logDirect("No known locations of " + gettingTo + ", canceling GetToBlock"); @@ -76,7 +77,8 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain List current = new ArrayList<>(knownLocations); - Baritone.getExecutor().execute(() -> rescan(current)); + CalculationContext context = new CalculationContext(baritone); + Baritone.getExecutor().execute(() -> rescan(current, context)); } Goal goal = new GoalComposite(knownLocations.stream().map(GoalGetToBlock::new).toArray(Goal[]::new)); if (goal.isInGoal(ctx.playerFeet())) { @@ -96,7 +98,7 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl return "Get To Block " + gettingTo; } - private void rescan(List known) { - knownLocations = MineProcess.searchWorld(ctx, Collections.singletonList(gettingTo), 64, known); + private void rescan(List known, CalculationContext context) { + knownLocations = MineProcess.searchWorld(context, Collections.singletonList(gettingTo), 64, known); } } \ No newline at end of file diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index a0495ade..a59695c7 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -27,6 +27,7 @@ import baritone.api.utils.RotationUtils; import baritone.cache.CachedChunk; import baritone.cache.ChunkPacker; import baritone.cache.WorldScanner; +import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.MovementHelper; import baritone.utils.BaritoneProcessHelper; import baritone.utils.BlockStateInterface; @@ -88,7 +89,8 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.get(); if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain List curr = new ArrayList<>(knownOreLocations); - Baritone.getExecutor().execute(() -> rescan(curr)); + CalculationContext context = new CalculationContext(baritone); + Baritone.getExecutor().execute(() -> rescan(curr, context)); } if (Baritone.settings().legitMine.get()) { addNearby(); @@ -116,7 +118,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro private Goal updateGoal() { List locs = knownOreLocations; if (!locs.isEmpty()) { - List locs2 = prune(ctx, new ArrayList<>(locs), mining, ORE_LOCATIONS_COUNT); + List locs2 = prune(new CalculationContext(baritone), new ArrayList<>(locs), mining, ORE_LOCATIONS_COUNT); // can't reassign locs, gotta make a new var locs2, because we use it in a lambda right here, and variables you use in a lambda must be effectively final Goal goal = new GoalComposite(locs2.stream().map(loc -> coalesce(ctx, loc, locs2)).toArray(Goal[]::new)); knownOreLocations = locs2; @@ -151,14 +153,14 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro return branchPointRunaway; } - private void rescan(List already) { + private void rescan(List already, CalculationContext context) { if (mining == null) { return; } if (Baritone.settings().legitMine.get()) { return; } - List locs = searchWorld(ctx, mining, ORE_LOCATIONS_COUNT, already); + List locs = searchWorld(context, mining, ORE_LOCATIONS_COUNT, already); locs.addAll(droppedItemsScan(mining, ctx.world())); if (locs.isEmpty()) { logDebug("No locations for " + mining + " known, cancelling"); @@ -205,13 +207,14 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro /*public static List searchWorld(List mining, int max, World world) { }*/ - public static List searchWorld(IPlayerContext ctx, List mining, int max, List alreadyKnown) { + public static List searchWorld(CalculationContext ctx, List mining, int max, List alreadyKnown) { + IPlayerContext ipc; List locs = new ArrayList<>(); List uninteresting = new ArrayList<>(); //long b = System.currentTimeMillis(); for (Block m : mining) { if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) { - locs.addAll(ctx.worldData().getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), 1, ctx.playerFeet().getX(), ctx.playerFeet().getZ(), 1)); + locs.addAll(ctx.worldData().getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), 1, ctx.getBaritone().getPlayerContext().playerFeet().getX(), ctx.getBaritone().getPlayerContext().playerFeet().getZ(), 1)); } else { uninteresting.add(m); } @@ -222,7 +225,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } if (!uninteresting.isEmpty()) { //long before = System.currentTimeMillis(); - locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(ctx, uninteresting, max, 10, 26)); + locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(ctx.getBaritone().getPlayerContext(), uninteresting, max, 10, 26)); //System.out.println("Scan of loaded chunks took " + (System.currentTimeMillis() - before) + "ms"); } locs.addAll(alreadyKnown); @@ -246,22 +249,22 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro } } } - knownOreLocations = prune(ctx, knownOreLocations, mining, ORE_LOCATIONS_COUNT); + knownOreLocations = prune(new CalculationContext(baritone), knownOreLocations, mining, ORE_LOCATIONS_COUNT); } - public static List prune(IPlayerContext ctx, List locs2, List mining, int max) { + public static List prune(CalculationContext ctx, List locs2, List mining, int max) { List dropped = droppedItemsScan(mining, ctx.world()); List locs = locs2 .stream() .distinct() // remove any that are within loaded chunks that aren't actually what we want - .filter(pos -> ctx.world().getChunk(pos) instanceof EmptyChunk || mining.contains(BlockStateInterface.getBlock(ctx, pos)) || dropped.contains(pos)) + .filter(pos -> ctx.world().getChunk(pos) instanceof EmptyChunk || mining.contains(ctx.getBlock(pos.getX(), pos.getY(), pos.getZ())) || dropped.contains(pos)) // remove any that are implausible to mine (encased in bedrock, or touching lava) - .filter(pos -> MineProcess.plausibleToBreak(ctx, pos)) + .filter(pos -> MineProcess.plausibleToBreak(ctx.bsi(), pos)) - .sorted(Comparator.comparingDouble(ctx.playerFeet()::distanceSq)) + .sorted(Comparator.comparingDouble(ctx.getBaritone().getPlayerContext().playerFeet()::distanceSq)) .collect(Collectors.toList()); if (locs.size() > max) { @@ -270,12 +273,13 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro return locs; } - public static boolean plausibleToBreak(IPlayerContext ctx, BlockPos pos) { - if (MovementHelper.avoidBreaking(new BlockStateInterface(ctx), pos.getX(), pos.getY(), pos.getZ(), BlockStateInterface.get(ctx, pos))) { + public static boolean plausibleToBreak(BlockStateInterface bsi, BlockPos pos) { + if (MovementHelper.avoidBreaking(bsi, pos.getX(), pos.getY(), pos.getZ(), bsi.get0(pos))) { return false; } + // bedrock above and below makes it implausible, otherwise we're good - return !(BlockStateInterface.getBlock(ctx, pos.up()) == Blocks.BEDROCK && BlockStateInterface.getBlock(ctx, pos.down()) == Blocks.BEDROCK); + return !(bsi.get0(pos.up()).getBlock() == Blocks.BEDROCK && bsi.get0(pos.down()).getBlock() == Blocks.BEDROCK); } @Override @@ -290,6 +294,8 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro this.knownOreLocations = new ArrayList<>(); this.branchPoint = null; this.branchPointRunaway = null; - rescan(new ArrayList<>()); + if (mining != null) { + rescan(new ArrayList<>(), new CalculationContext(baritone)); + } } } diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 63ba8d83..9b6401e9 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -21,12 +21,17 @@ import baritone.Baritone; import baritone.api.utils.IPlayerContext; import baritone.cache.CachedRegion; import baritone.cache.WorldData; +import baritone.utils.accessor.IChunkProviderClient; +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; +import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ChunkPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.EmptyChunk; /** * Wraps get for chuck caching capability @@ -36,6 +41,7 @@ import net.minecraft.world.chunk.Chunk; public class BlockStateInterface { private final World world; + private final Long2ObjectMap loadedChunks; private final WorldData worldData; private Chunk prev = null; @@ -50,6 +56,10 @@ public class BlockStateInterface { public BlockStateInterface(World world, WorldData worldData) { this.worldData = worldData; this.world = world; + this.loadedChunks = ((IChunkProviderClient) world.getChunkProvider()).loadedChunks(); + if (!Minecraft.getMinecraft().isCallingFromMinecraftThread()) { + throw new IllegalStateException(); + } } public World getWorld() { @@ -66,6 +76,10 @@ public class BlockStateInterface { // and toBreak and stuff fails when the movement is instantiated out of load range but it's not able to BlockStateInterface.get what it's going to walk on } + public IBlockState get0(BlockPos pos) { + return get0(pos.getX(), pos.getY(), pos.getZ()); + } + public IBlockState get0(int x, int y, int z) { // Mickey resigned // Invalid vertical position @@ -84,7 +98,12 @@ public class BlockStateInterface { if (cached != null && cached.x == x >> 4 && cached.z == z >> 4) { return cached.getBlockState(x, y, z); } + Chunk c2 = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); Chunk chunk = world.getChunk(x >> 4, z >> 4); + + if ((c2 != null && c2 != chunk) || (c2 == null && !(chunk instanceof EmptyChunk))) { + throw new IllegalStateException((((IChunkProviderClient) world.getChunkProvider()).loadedChunks() == loadedChunks) + " " + x + " " + y + " " + c2 + " " + chunk); + } if (chunk.isLoaded()) { prev = chunk; return chunk.getBlockState(x, y, z); diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 841349e4..93e2742a 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -28,6 +28,7 @@ import baritone.behavior.Behavior; import baritone.behavior.PathingBehavior; import baritone.cache.ChunkPacker; import baritone.cache.Waypoint; +import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; import baritone.process.CustomGoalProcess; @@ -446,7 +447,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper { return true; } if (msg.equals("costs")) { - List moves = Stream.of(Moves.values()).map(x -> x.apply0(baritone, ctx.playerFeet())).collect(Collectors.toCollection(ArrayList::new)); + List moves = Stream.of(Moves.values()).map(x -> x.apply0(new CalculationContext(baritone), ctx.playerFeet())).collect(Collectors.toCollection(ArrayList::new)); while (moves.contains(null)) { moves.remove(null); } diff --git a/src/main/java/baritone/utils/accessor/IChunkProviderClient.java b/src/main/java/baritone/utils/accessor/IChunkProviderClient.java new file mode 100644 index 00000000..19f14685 --- /dev/null +++ b/src/main/java/baritone/utils/accessor/IChunkProviderClient.java @@ -0,0 +1,25 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.utils.accessor; + +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import net.minecraft.world.chunk.Chunk; + +public interface IChunkProviderClient { + Long2ObjectMap loadedChunks(); +} From b228f4c6fb6d8a952634a5949dc3dba2145f5f75 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 09:03:51 -0800 Subject: [PATCH 4/5] remove extraneous checks in bsi --- .../pathing/movement/MovementHelper.java | 2 +- .../baritone/utils/BlockStateInterface.java | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/main/java/baritone/pathing/movement/MovementHelper.java b/src/main/java/baritone/pathing/movement/MovementHelper.java index ff73b0f2..917adc7c 100644 --- a/src/main/java/baritone/pathing/movement/MovementHelper.java +++ b/src/main/java/baritone/pathing/movement/MovementHelper.java @@ -86,7 +86,7 @@ public interface MovementHelper extends ActionCosts, Helper { // so the only remaining dynamic isPassables are snow and trapdoor // if they're cached as a top block, we don't know their metadata // default to true (mostly because it would otherwise make long distance pathing through snowy biomes impossible) - if (bsi.getWorld().getChunk(x >> 4, z >> 4) instanceof EmptyChunk) { + if (!bsi.worldContainsLoadedChunk(x, z)) { return true; } if (snow) { diff --git a/src/main/java/baritone/utils/BlockStateInterface.java b/src/main/java/baritone/utils/BlockStateInterface.java index 9b6401e9..b6b264d0 100644 --- a/src/main/java/baritone/utils/BlockStateInterface.java +++ b/src/main/java/baritone/utils/BlockStateInterface.java @@ -31,7 +31,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.EmptyChunk; /** * Wraps get for chuck caching capability @@ -40,7 +39,6 @@ import net.minecraft.world.chunk.EmptyChunk; */ public class BlockStateInterface { - private final World world; private final Long2ObjectMap loadedChunks; private final WorldData worldData; @@ -55,15 +53,14 @@ public class BlockStateInterface { public BlockStateInterface(World world, WorldData worldData) { this.worldData = worldData; - this.world = world; this.loadedChunks = ((IChunkProviderClient) world.getChunkProvider()).loadedChunks(); if (!Minecraft.getMinecraft().isCallingFromMinecraftThread()) { throw new IllegalStateException(); } } - public World getWorld() { - return world; + public boolean worldContainsLoadedChunk(int blockX, int blockZ) { + return loadedChunks.containsKey(ChunkPos.asLong(blockX >> 4, blockZ >> 4)); } public static Block getBlock(IPlayerContext ctx, BlockPos pos) { // won't be called from the pathing thread because the pathing thread doesn't make a single blockpos pog @@ -98,13 +95,9 @@ public class BlockStateInterface { if (cached != null && cached.x == x >> 4 && cached.z == z >> 4) { return cached.getBlockState(x, y, z); } - Chunk c2 = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); - Chunk chunk = world.getChunk(x >> 4, z >> 4); + Chunk chunk = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); - if ((c2 != null && c2 != chunk) || (c2 == null && !(chunk instanceof EmptyChunk))) { - throw new IllegalStateException((((IChunkProviderClient) world.getChunkProvider()).loadedChunks() == loadedChunks) + " " + x + " " + y + " " + c2 + " " + chunk); - } - if (chunk.isLoaded()) { + if (chunk != null && chunk.isLoaded()) { prev = chunk; return chunk.getBlockState(x, y, z); } @@ -135,8 +128,8 @@ public class BlockStateInterface { if (prevChunk != null && prevChunk.x == x >> 4 && prevChunk.z == z >> 4) { return true; } - prevChunk = world.getChunk(x >> 4, z >> 4); - if (prevChunk.isLoaded()) { + prevChunk = loadedChunks.get(ChunkPos.asLong(x >> 4, z >> 4)); + if (prevChunk != null && prevChunk.isLoaded()) { prev = prevChunk; return true; } From 6ed8d617cd01e2fcf97859ba4c5ef1ec14b675ba Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 23 Nov 2018 09:31:25 -0800 Subject: [PATCH 5/5] improve path checks, and add a overlap splice option --- .../java/baritone/api/pathing/calc/IPath.java | 11 +++++-- .../baritone/pathing/path/PathExecutor.java | 2 +- .../baritone/pathing/path/SplicedPath.java | 29 ++++++++++++++----- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/api/java/baritone/api/pathing/calc/IPath.java b/src/api/java/baritone/api/pathing/calc/IPath.java index 0844ab90..133de5ef 100644 --- a/src/api/java/baritone/api/pathing/calc/IPath.java +++ b/src/api/java/baritone/api/pathing/calc/IPath.java @@ -21,9 +21,9 @@ import baritone.api.Settings; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; -import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +import java.util.HashSet; import java.util.List; /** @@ -153,9 +153,10 @@ public interface IPath { if (path.size() != movements.size() + 1) { throw new IllegalStateException("Size of path array is unexpected"); } + HashSet seenSoFar = new HashSet<>(); for (int i = 0; i < path.size() - 1; i++) { - BlockPos src = path.get(i); - BlockPos dest = path.get(i + 1); + BetterBlockPos src = path.get(i); + BetterBlockPos dest = path.get(i + 1); IMovement movement = movements.get(i); if (!src.equals(movement.getSrc())) { throw new IllegalStateException("Path source is not equal to the movement source"); @@ -163,6 +164,10 @@ public interface IPath { if (!dest.equals(movement.getDest())) { throw new IllegalStateException("Path destination is not equal to the movement destination"); } + if (seenSoFar.contains(src)) { + throw new IllegalStateException("Path doubles back on itself, making a loop"); + } + seenSoFar.add(src); } } } diff --git a/src/main/java/baritone/pathing/path/PathExecutor.java b/src/main/java/baritone/pathing/path/PathExecutor.java index af0fe142..171dd769 100644 --- a/src/main/java/baritone/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/pathing/path/PathExecutor.java @@ -458,7 +458,7 @@ public class PathExecutor implements IPathExecutor, Helper { if (next == null) { return cutIfTooLong(); } - return SplicedPath.trySplice(path, next.path).map(path -> { + return SplicedPath.trySplice(path, next.path, false).map(path -> { if (!path.getDest().equals(next.getPath().getDest())) { throw new IllegalStateException(); } diff --git a/src/main/java/baritone/pathing/path/SplicedPath.java b/src/main/java/baritone/pathing/path/SplicedPath.java index 5048a84a..92610c86 100644 --- a/src/main/java/baritone/pathing/path/SplicedPath.java +++ b/src/main/java/baritone/pathing/path/SplicedPath.java @@ -62,7 +62,7 @@ public class SplicedPath extends PathBase { return numNodes; } - public static Optional trySplice(IPath first, IPath second) { + public static Optional trySplice(IPath first, IPath second, boolean allowOverlapCutoff) { if (second == null || first == null) { return Optional.empty(); } @@ -72,18 +72,31 @@ public class SplicedPath extends PathBase { if (!first.getDest().equals(second.getSrc())) { return Optional.empty(); } - HashSet a = new HashSet<>(first.positions()); - for (int i = 1; i < second.length(); i++) { - if (a.contains(second.positions().get(i))) { + HashSet secondPos = new HashSet<>(second.positions()); + int firstPositionInSecond = -1; + for (int i = 0; i < first.length() - 1; i++) { // overlap in the very last element is fine (and required) so only go up to first.length() - 1 + if (secondPos.contains(first.positions().get(i))) { + firstPositionInSecond = i; + } + } + if (firstPositionInSecond != -1) { + if (!allowOverlapCutoff) { return Optional.empty(); } + } else { + firstPositionInSecond = first.length() - 1; + } + int positionInSecond = second.positions().indexOf(first.positions().get(firstPositionInSecond)); + if (!allowOverlapCutoff && positionInSecond != 0) { + throw new IllegalStateException(); } List positions = new ArrayList<>(); List movements = new ArrayList<>(); - positions.addAll(first.positions()); - positions.addAll(second.positions().subList(1, second.length())); - movements.addAll(first.movements()); - movements.addAll(second.movements()); + positions.addAll(first.positions().subList(0, firstPositionInSecond + 1)); + movements.addAll(first.movements().subList(0, firstPositionInSecond)); + + positions.addAll(second.positions().subList(positionInSecond + 1, second.length())); + movements.addAll(second.movements().subList(positionInSecond, second.length() - 1)); return Optional.of(new SplicedPath(positions, movements, first.getNumNodesConsidered() + second.getNumNodesConsidered(), first.getGoal())); } }