Fix searchWorld cache and also speed and stuff

This commit is contained in:
Logan Darklock 2019-08-30 17:12:05 -07:00
parent d687e1203a
commit fe47245b73
No known key found for this signature in database
GPG Key ID: B8C37CEDE1AC60EA
6 changed files with 104 additions and 66 deletions

View File

@ -54,7 +54,7 @@ public final class BlockOptionalMeta {
} }
MatchResult matchResult = matcher.toMatchResult(); MatchResult matchResult = matcher.toMatchResult();
noMeta = matchResult.groupCount() < 2; noMeta = matchResult.group(2) == null;
ResourceLocation id = new ResourceLocation(matchResult.group(1)); ResourceLocation id = new ResourceLocation(matchResult.group(1));
@ -74,13 +74,13 @@ public final class BlockOptionalMeta {
return meta; return meta;
} }
public boolean matches(@Nonnull Block block, int meta) { public boolean matches(@Nonnull Block block) {
// & instead of && is intentional return block == this.block;
return block == this.block & (noMeta || meta == this.meta);
} }
public boolean matches(@Nonnull IBlockState blockstate) { public boolean matches(@Nonnull IBlockState blockstate) {
return matches(blockstate.getBlock(), block.damageDropped(blockstate)); Block block = blockstate.getBlock();
return block == this.block && (noMeta || block.damageDropped(blockstate) == this.meta);
} }
@Override @Override

View File

@ -2,69 +2,34 @@ package baritone.api.utils;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import org.apache.commons.lang3.ArrayUtils;
import java.util.HashMap; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import static java.util.Arrays.asList;
public class BlockOptionalMetaLookup { public class BlockOptionalMetaLookup {
private final Map<Block, int[]> lookup = new HashMap<>(); private final BlockOptionalMeta[] boms;
public BlockOptionalMetaLookup() {
}
public BlockOptionalMetaLookup(BlockOptionalMeta... boms) { public BlockOptionalMetaLookup(BlockOptionalMeta... boms) {
put(boms); this.boms = boms;
} }
public BlockOptionalMetaLookup(Block... blocks) { public BlockOptionalMetaLookup(Block... blocks) {
put(blocks); this.boms = Arrays.stream(blocks)
.map(BlockOptionalMeta::new)
.toArray(BlockOptionalMeta[]::new);
} }
public BlockOptionalMetaLookup(List<Block> blocks) { public BlockOptionalMetaLookup(List<Block> blocks) {
put(blocks); this.boms = blocks.stream()
} .map(BlockOptionalMeta::new)
.toArray(BlockOptionalMeta[]::new);
public void put(BlockOptionalMeta bom) {
final int[] metaArr = new int[] {bom.getMeta()};
lookup.compute(bom.getBlock(), (__, arr) -> arr == null ? metaArr : ArrayUtils.addAll(arr, metaArr));
}
public void put(BlockOptionalMeta... boms) {
for (BlockOptionalMeta bom : boms) {
put(bom);
}
}
public void put(Block... blocks) {
for (Block block : blocks) {
put(new BlockOptionalMeta(block));
}
}
public void put(List<Block> blocks) {
for (Block block : blocks) {
put(new BlockOptionalMeta(block));
}
} }
public boolean has(Block block) { public boolean has(Block block) {
return lookup.containsKey(block); for (BlockOptionalMeta bom : boms) {
} if (bom.getBlock() == block) {
public boolean has(IBlockState state) {
Block block = state.getBlock();
int[] arr = lookup.get(block);
if (arr == null) {
return false;
}
int meta = block.damageDropped(state);
for (int value : arr) {
if (value == meta) {
return true; return true;
} }
} }
@ -72,7 +37,25 @@ public class BlockOptionalMetaLookup {
return false; return false;
} }
public Set<Block> blocks() { public boolean has(IBlockState state) {
return lookup.keySet(); for (BlockOptionalMeta bom : boms) {
if (bom.matches(state)) {
return true;
}
}
return false;
}
public List<BlockOptionalMeta> blocks() {
return asList(boms);
}
@Override
public String toString() {
return String.format(
"BlockOptionalMetaLookup{%s}",
Arrays.toString(boms)
);
} }
} }

View File

@ -47,9 +47,14 @@ public interface IPlayerContext {
default BetterBlockPos playerFeet() { default BetterBlockPos playerFeet() {
// TODO find a better way to deal with soul sand!!!!! // TODO find a better way to deal with soul sand!!!!!
BetterBlockPos feet = new BetterBlockPos(player().posX, player().posY + 0.1251, player().posZ); BetterBlockPos feet = new BetterBlockPos(player().posX, player().posY + 0.1251, player().posZ);
try {
if (world().getBlockState(feet).getBlock() instanceof BlockSlab) { if (world().getBlockState(feet).getBlock() instanceof BlockSlab) {
return feet.up(); return feet.up();
} }
} catch (NullPointerException ignored) {
}
return feet; return feet;
} }

View File

@ -23,20 +23,29 @@ import net.minecraft.util.BitArray;
import net.minecraft.world.chunk.BlockStateContainer; import net.minecraft.world.chunk.BlockStateContainer;
import net.minecraft.world.chunk.IBlockStatePalette; import net.minecraft.world.chunk.IBlockStatePalette;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.gen.Accessor; import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(BlockStateContainer.class) @Mixin(BlockStateContainer.class)
public abstract class MixinBlockStateContainer implements IBlockStateContainer { public abstract class MixinBlockStateContainer implements IBlockStateContainer {
@Accessor @Shadow
public abstract IBlockStatePalette getPalette(); protected BitArray storage;
@Shadow
protected IBlockStatePalette palette;
@Override
@Accessor @Accessor
public abstract BitArray getStorage(); public abstract BitArray getStorage();
@Override
@Accessor
public abstract IBlockStatePalette getPalette();
@Override @Override
@Unique @Unique
public IBlockState getFast(int x, int y, int z) { public IBlockState getFast(int x, int y, int z) {
return getPalette().getBlockState(getStorage().getAt(y << 8 | z << 4 | x)); return palette.getBlockState(storage.getAt(y << 8 | z << 4 | x));
} }
} }

View File

@ -91,6 +91,16 @@ public final class CachedChunk {
Blocks.VINE Blocks.VINE
); );
public static boolean tracked(Block block) {
for (Block tracked : BLOCKS_TO_KEEP_TRACK_OF) {
if (tracked == block) {
return true;
}
}
return false;
}
/** /**
* The size of the chunk data in bits. Equal to 16 KiB. * The size of the chunk data in bits. Equal to 16 KiB.

View File

@ -26,17 +26,21 @@ import baritone.api.pathing.goals.GoalTwoBlocks;
import baritone.api.process.IMineProcess; import baritone.api.process.IMineProcess;
import baritone.api.process.PathingCommand; import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType; import baritone.api.process.PathingCommandType;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.BlockOptionalMeta; import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.BlockOptionalMetaLookup; import baritone.api.utils.BlockOptionalMetaLookup;
import baritone.api.utils.BlockUtils;
import baritone.api.utils.IPlayerContext; import baritone.api.utils.IPlayerContext;
import baritone.api.utils.Rotation; import baritone.api.utils.Rotation;
import baritone.api.utils.RotationUtils; import baritone.api.utils.RotationUtils;
import baritone.api.utils.input.Input; import baritone.api.utils.input.Input;
import baritone.cache.CachedChunk;
import baritone.cache.WorldScanner; import baritone.cache.WorldScanner;
import baritone.pathing.movement.CalculationContext; import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.MovementHelper; import baritone.pathing.movement.MovementHelper;
import baritone.utils.BaritoneProcessHelper; import baritone.utils.BaritoneProcessHelper;
import baritone.utils.BlockStateInterface; import baritone.utils.BlockStateInterface;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir; import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockFalling; import net.minecraft.block.BlockFalling;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
@ -311,9 +315,36 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
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> locs = new ArrayList<>(); List<BlockPos> locs = new ArrayList<>();
locs = prune(ctx, locs, filter, max, blacklist); List<Block> untracked = new ArrayList<>();
locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(ctx.getBaritone().getPlayerContext(), filter, max, 10, 32)); // maxSearchRadius is NOT sq for (BlockOptionalMeta bom : filter.blocks()) {
Block block = bom.getBlock();
if (CachedChunk.tracked(block)) {
BetterBlockPos pf = ctx.baritone.getPlayerContext().playerFeet();
locs.addAll(ctx.worldData.getCachedWorld().getLocationsOf(
BlockUtils.blockToString(block),
Baritone.settings().maxCachedWorldScanCount.value,
pf.x,
pf.z,
2
));
} else {
untracked.add(block);
}
}
if (!untracked.isEmpty()) {
locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(
ctx.getBaritone().getPlayerContext(),
filter,
max,
10,
32
)); // maxSearchRadius is NOT sq
}
locs.addAll(alreadyKnown); locs.addAll(alreadyKnown);
return prune(ctx, locs, filter, max, blacklist); return prune(ctx, locs, filter, max, blacklist);
} }