loiter around ores to anticipate a possible drop

This commit is contained in:
Leijurv
2019-10-06 13:26:29 -07:00
parent 5f0d3a4018
commit 22d5cb8068
3 changed files with 54 additions and 27 deletions

View File

@@ -30,8 +30,8 @@ import java.awt.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
import java.util.*; import java.util.*;
import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
@@ -798,6 +798,14 @@ public final class Settings {
*/ */
public final Setting<Boolean> mineScanDroppedItems = new Setting<>(true); public final Setting<Boolean> mineScanDroppedItems = new Setting<>(true);
/**
* While mining, wait this number of milliseconds after mining an ore to see if it will drop an item
* instead of immediately going onto the next one
* <p>
* Thanks Louca
*/
public final Setting<Long> mineDropLoiterDurationMSThanksLouca = new Setting<>(250L);
/** /**
* Trim incorrect positions too far away, helps performance but hurts reliability in very large schematics * Trim incorrect positions too far away, helps performance but hurts reliability in very large schematics
*/ */

View File

@@ -33,10 +33,7 @@ import net.minecraft.init.Blocks;
import net.minecraft.inventory.ContainerPlayer; import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.ArrayList; import java.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public final class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess { public final class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess {
@@ -175,7 +172,7 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG
} }
private synchronized void rescan(List<BlockPos> known, CalculationContext context) { private synchronized void rescan(List<BlockPos> known, CalculationContext context) {
List<BlockPos> positions = MineProcess.searchWorld(context, new BlockOptionalMetaLookup(gettingTo), 64, known, blacklist); List<BlockPos> positions = MineProcess.searchWorld(context, new BlockOptionalMetaLookup(gettingTo), 64, known, blacklist, Collections.emptyList());
positions.removeIf(blacklist::contains); positions.removeIf(blacklist::contains);
knownLocations = positions; knownLocations = positions;
} }

View File

@@ -39,7 +39,6 @@ import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -58,6 +57,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
private BlockOptionalMetaLookup filter; private BlockOptionalMetaLookup filter;
private List<BlockPos> knownOreLocations; private List<BlockPos> knownOreLocations;
private List<BlockPos> blacklist; // inaccessible private List<BlockPos> blacklist; // inaccessible
private Map<BlockPos, Long> anticipatedDrops;
private BlockPos branchPoint; private BlockPos branchPoint;
private GoalRunAway branchPointRunaway; private GoalRunAway branchPointRunaway;
private int desiredQuantity; private int desiredQuantity;
@@ -101,6 +101,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
cancel(); cancel();
return null; return null;
} }
updateLoucaSystem();
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.value; int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.value;
List<BlockPos> curr = new ArrayList<>(knownOreLocations); List<BlockPos> curr = new ArrayList<>(knownOreLocations);
if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain
@@ -141,6 +142,23 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
return command; return command;
} }
private void updateLoucaSystem() {
Map<BlockPos, Long> copy = new HashMap<>(anticipatedDrops);
ctx.getSelectedBlock().ifPresent(pos -> {
if (knownOreLocations.contains(pos)) {
copy.put(pos, System.currentTimeMillis() + Baritone.settings().mineDropLoiterDurationMSThanksLouca.value);
}
});
// elaborate dance to avoid concurrentmodificationexcepption since rescan thread reads this
// don't want to slow everything down with a gross lock do we now
for (BlockPos pos : anticipatedDrops.keySet()) {
if (copy.get(pos) < System.currentTimeMillis()) {
copy.remove(pos);
}
}
anticipatedDrops = copy;
}
@Override @Override
public void onLostControl() { public void onLostControl() {
mine(0, (BlockOptionalMetaLookup) null); mine(0, (BlockOptionalMetaLookup) null);
@@ -155,9 +173,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
boolean legit = Baritone.settings().legitMine.value; boolean legit = Baritone.settings().legitMine.value;
List<BlockPos> locs = knownOreLocations; List<BlockPos> locs = knownOreLocations;
if (!locs.isEmpty()) { if (!locs.isEmpty()) {
List<BlockPos> locs2 = prune(new CalculationContext(baritone), new ArrayList<>(locs), filter, ORE_LOCATIONS_COUNT, blacklist); CalculationContext context = new CalculationContext(baritone);
List<BlockPos> locs2 = prune(context, new ArrayList<>(locs), filter, ORE_LOCATIONS_COUNT, blacklist, droppedItemsScan());
// 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 // 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(loc, locs2)).toArray(Goal[]::new)); Goal goal = new GoalComposite(locs2.stream().map(loc -> coalesce(loc, locs2, context)).toArray(Goal[]::new));
knownOreLocations = locs2; knownOreLocations = locs2;
return new PathingCommand(goal, legit ? PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH : PathingCommandType.REVALIDATE_GOAL_AND_PATH); return new PathingCommand(goal, legit ? PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH : PathingCommandType.REVALIDATE_GOAL_AND_PATH);
} }
@@ -197,8 +216,9 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
if (Baritone.settings().legitMine.value) { if (Baritone.settings().legitMine.value) {
return; return;
} }
List<BlockPos> locs = searchWorld(context, filter, ORE_LOCATIONS_COUNT, already, blacklist); List<BlockPos> dropped = droppedItemsScan();
locs.addAll(droppedItemsScan(filter, ctx.world())); List<BlockPos> locs = searchWorld(context, filter, ORE_LOCATIONS_COUNT, already, blacklist, dropped);
locs.addAll(dropped);
if (locs.isEmpty()) { if (locs.isEmpty()) {
logDirect("No locations for " + filter + " known, cancelling"); logDirect("No locations for " + filter + " known, cancelling");
cancel(); cancel();
@@ -207,19 +227,19 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
knownOreLocations = locs; knownOreLocations = locs;
} }
private boolean internalMiningGoal(BlockPos pos, IPlayerContext ctx, List<BlockPos> locs) { private boolean internalMiningGoal(BlockPos pos, CalculationContext context, List<BlockPos> locs) {
// Here, BlockStateInterface is used because the position may be in a cached chunk (the targeted block is one that is kept track of) // Here, BlockStateInterface is used because the position may be in a cached chunk (the targeted block is one that is kept track of)
if (locs.contains(pos)) { if (locs.contains(pos)) {
return true; return true;
} }
IBlockState state = BlockStateInterface.get(ctx, pos); IBlockState state = context.bsi.get0(pos);
if (Baritone.settings().internalMiningAirException.value && state.getBlock() instanceof BlockAir) { if (Baritone.settings().internalMiningAirException.value && state.getBlock() instanceof BlockAir) {
return true; return true;
} }
return filter.has(state); return filter.has(state) && plausibleToBreak(context, pos);
} }
private Goal coalesce(BlockPos loc, List<BlockPos> locs) { private Goal coalesce(BlockPos loc, List<BlockPos> locs, CalculationContext context) {
boolean assumeVerticalShaftMine = !(baritone.bsi.get0(loc.up()).getBlock() instanceof BlockFalling); boolean assumeVerticalShaftMine = !(baritone.bsi.get0(loc.up()).getBlock() instanceof BlockFalling);
if (!Baritone.settings().forceInternalMining.value) { if (!Baritone.settings().forceInternalMining.value) {
if (assumeVerticalShaftMine) { if (assumeVerticalShaftMine) {
@@ -230,9 +250,9 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
return new GoalTwoBlocks(loc); return new GoalTwoBlocks(loc);
} }
} }
boolean upwardGoal = internalMiningGoal(loc.up(), ctx, locs); boolean upwardGoal = internalMiningGoal(loc.up(), context, locs);
boolean downwardGoal = internalMiningGoal(loc.down(), ctx, locs); boolean downwardGoal = internalMiningGoal(loc.down(), context, locs);
boolean doubleDownwardGoal = internalMiningGoal(loc.down(2), ctx, locs); boolean doubleDownwardGoal = internalMiningGoal(loc.down(2), context, locs);
if (upwardGoal == downwardGoal) { // symmetric if (upwardGoal == downwardGoal) { // symmetric
if (doubleDownwardGoal && assumeVerticalShaftMine) { if (doubleDownwardGoal && assumeVerticalShaftMine) {
// we have a checkerboard like pattern // we have a checkerboard like pattern
@@ -281,12 +301,12 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
} }
} }
public static List<BlockPos> droppedItemsScan(BlockOptionalMetaLookup filter, World world) { public List<BlockPos> droppedItemsScan() {
if (!Baritone.settings().mineScanDroppedItems.value) { if (!Baritone.settings().mineScanDroppedItems.value) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<BlockPos> ret = new ArrayList<>(); List<BlockPos> ret = new ArrayList<>();
for (Entity entity : world.loadedEntityList) { for (Entity entity : ctx.world().loadedEntityList) {
if (entity instanceof EntityItem) { if (entity instanceof EntityItem) {
EntityItem ei = (EntityItem) entity; EntityItem ei = (EntityItem) entity;
if (filter.has(ei.getItem())) { if (filter.has(ei.getItem())) {
@@ -294,10 +314,11 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
} }
} }
} }
ret.addAll(anticipatedDrops.keySet());
return ret; return ret;
} }
public static List<BlockPos> searchWorld(CalculationContext ctx, BlockOptionalMetaLookup filter, int max, List<BlockPos> alreadyKnown, List<BlockPos> blacklist) { public static List<BlockPos> searchWorld(CalculationContext ctx, BlockOptionalMetaLookup filter, int max, List<BlockPos> alreadyKnown, List<BlockPos> blacklist, List<BlockPos> dropped) {
List<BlockPos> locs = new ArrayList<>(); List<BlockPos> locs = new ArrayList<>();
List<Block> untracked = new ArrayList<>(); List<Block> untracked = new ArrayList<>();
for (BlockOptionalMeta bom : filter.blocks()) { for (BlockOptionalMeta bom : filter.blocks()) {
@@ -318,7 +339,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
} }
} }
locs = prune(ctx, locs, filter, max, blacklist); locs = prune(ctx, locs, filter, max, blacklist, dropped);
if (!untracked.isEmpty() || (Baritone.settings().extendCacheOnThreshold.value && locs.size() < max)) { if (!untracked.isEmpty() || (Baritone.settings().extendCacheOnThreshold.value && locs.size() < max)) {
locs.addAll(WorldScanner.INSTANCE.scanChunkRadius( locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(
@@ -332,11 +353,12 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
locs.addAll(alreadyKnown); locs.addAll(alreadyKnown);
return prune(ctx, locs, filter, max, blacklist); return prune(ctx, locs, filter, max, blacklist, dropped);
} }
private void addNearby() { private void addNearby() {
knownOreLocations.addAll(droppedItemsScan(filter, ctx.world())); List<BlockPos> dropped = droppedItemsScan();
knownOreLocations.addAll(dropped);
BlockPos playerFeet = ctx.playerFeet(); BlockPos playerFeet = ctx.playerFeet();
BlockStateInterface bsi = new BlockStateInterface(ctx); BlockStateInterface bsi = new BlockStateInterface(ctx);
int searchDist = 10; int searchDist = 10;
@@ -355,11 +377,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
} }
} }
} }
knownOreLocations = prune(new CalculationContext(baritone), knownOreLocations, filter, ORE_LOCATIONS_COUNT, blacklist); knownOreLocations = prune(new CalculationContext(baritone), knownOreLocations, filter, ORE_LOCATIONS_COUNT, blacklist, dropped);
} }
private static List<BlockPos> prune(CalculationContext ctx, List<BlockPos> locs2, BlockOptionalMetaLookup filter, int max, List<BlockPos> blacklist) { private static List<BlockPos> prune(CalculationContext ctx, List<BlockPos> locs2, BlockOptionalMetaLookup filter, int max, List<BlockPos> blacklist, List<BlockPos> dropped) {
List<BlockPos> dropped = droppedItemsScan(filter, ctx.world);
dropped.removeIf(drop -> { dropped.removeIf(drop -> {
for (BlockPos pos : locs2) { for (BlockPos pos : locs2) {
if (pos.distanceSq(drop) <= 9 && filter.has(ctx.get(pos.getX(), pos.getY(), pos.getZ())) && MineProcess.plausibleToBreak(ctx, pos)) { // TODO maybe drop also has to be supported? no lava below? if (pos.distanceSq(drop) <= 9 && filter.has(ctx.get(pos.getX(), pos.getY(), pos.getZ())) && MineProcess.plausibleToBreak(ctx, pos)) { // TODO maybe drop also has to be supported? no lava below?
@@ -416,6 +437,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
this.blacklist = new ArrayList<>(); this.blacklist = new ArrayList<>();
this.branchPoint = null; this.branchPoint = null;
this.branchPointRunaway = null; this.branchPointRunaway = null;
this.anticipatedDrops = new HashMap<>();
if (filter != null) { if (filter != null) {
rescan(new ArrayList<>(), new CalculationContext(baritone)); rescan(new ArrayList<>(), new CalculationContext(baritone));
} }