use top block in pathing, and reliability improvements

This commit is contained in:
Leijurv 2018-08-22 12:04:44 -07:00
parent 38aad9d92c
commit e730f3a84d
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
8 changed files with 73 additions and 34 deletions

View File

@ -222,6 +222,11 @@ public class Settings {
*/
public Setting<Boolean> freeLook = new Setting<>(true);
/**
* Exclusively use cached chunks for pathing
*/
public Setting<Boolean> pathThroughCachedOnly = new Setting<>(false);
public final Map<String, Setting<?>> byLowerName;
public final List<Setting<?>> allSettings;

View File

@ -19,6 +19,8 @@ package baritone.bot.chunk;
import baritone.bot.utils.pathing.IBlockTypeAccess;
import baritone.bot.utils.pathing.PathingBlockType;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import java.util.BitSet;
@ -76,7 +78,23 @@ public final class CachedChunk implements IBlockTypeAccess {
}
@Override
public final PathingBlockType getBlockType(int x, int y, int z) {
public final IBlockState getBlock(int x, int y, int z) {
int internalPos = z << 4 | x;
if (heightMap[internalPos] == y) {
// we have this exact block, it's a surface block
String name = overview[internalPos];
if (!name.contains(":")) {
name = "minecraft:" + name;
}
IBlockState state = Block.getBlockFromName(name).getDefaultState();
System.out.println("Saying that " + x + "," + y + "," + z + " is " + state);
return state;
}
PathingBlockType type = getType(x, y, z);
return ChunkPacker.pathingTypeToBlock(type);
}
private PathingBlockType getType(int x, int y, int z) {
int index = getPositionIndex(x, y, z);
return PathingBlockType.fromBits(data.get(index), data.get(index + 1));
}
@ -87,8 +105,9 @@ public final class CachedChunk implements IBlockTypeAccess {
int index = z << 4 | x;
heightMap[index] = 0;
for (int y = 256; y >= 0; y--) {
if (getBlockType(x, y, z) != PathingBlockType.AIR) {
if (getType(x, y, z) != PathingBlockType.AIR) {
heightMap[index] = y;
break;
}
}
}

View File

@ -18,13 +18,12 @@
package baritone.bot.chunk;
import baritone.bot.utils.pathing.IBlockTypeAccess;
import baritone.bot.utils.pathing.PathingBlockType;
import net.minecraft.block.state.IBlockState;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.BitSet;
import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;
@ -70,10 +69,10 @@ public final class CachedRegion implements IBlockTypeAccess {
}
@Override
public final PathingBlockType getBlockType(int x, int y, int z) {
public final IBlockState getBlock(int x, int y, int z) {
CachedChunk chunk = this.getChunk(x >> 4, z >> 4);
if (chunk != null) {
return chunk.getBlockType(x & 15, y, z & 15);
return chunk.getBlock(x & 15, y, z & 15);
}
return null;
}
@ -174,6 +173,7 @@ public final class CachedRegion implements IBlockTypeAccess {
// by switching on the magic value, and either loading it normally, or loading through a converter.
throw new IOException("Bad magic value " + magic);
}
CachedChunk[][] tmpCached = new CachedChunk[32][32];
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
int isChunkPresent = in.read();
@ -181,10 +181,10 @@ public final class CachedRegion implements IBlockTypeAccess {
case CHUNK_PRESENT:
byte[] bytes = new byte[CachedChunk.SIZE_IN_BYTES];
in.readFully(bytes);
this.chunks[x][z] = new CachedChunk(x, z, BitSet.valueOf(bytes), new String[256]);
tmpCached[x][z] = new CachedChunk(x, z, BitSet.valueOf(bytes), new String[256]);
break;
case CHUNK_NOT_PRESENT:
this.chunks[x][z] = null;
tmpCached[x][z] = null;
break;
default:
throw new IOException("Malformed stream");
@ -193,11 +193,10 @@ public final class CachedRegion implements IBlockTypeAccess {
}
for (int z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) {
if (chunks[x][z] != null) {
if (tmpCached[x][z] != null) {
for (int i = 0; i < 256; i++) {
chunks[x][z].getOverview()[i] = in.readUTF();
tmpCached[x][z].getOverview()[i] = in.readUTF();
}
System.out.println(Arrays.asList(chunks[x][z].getOverview()));
}
}
}
@ -205,6 +204,12 @@ public final class CachedRegion implements IBlockTypeAccess {
if (fileEndMagic != ~magic) {
throw new IOException("Bad end of file magic");
}
// only if the entire file was uncorrupted do we actually set the chunks
for (int x = 0; x < 32; x++) {
for (int z = 0; z < 32; z++) {
this.chunks[x][z] = tmpCached[x][z];
}
}
}
hasUnsavedChanges = false;
long end = System.currentTimeMillis();

View File

@ -18,9 +18,9 @@
package baritone.bot.chunk;
import baritone.bot.utils.pathing.IBlockTypeAccess;
import baritone.bot.utils.pathing.PathingBlockType;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.block.state.IBlockState;
import net.minecraft.world.chunk.Chunk;
import java.util.BitSet;
@ -66,13 +66,13 @@ public final class CachedWorld implements IBlockTypeAccess {
}
@Override
public final PathingBlockType getBlockType(int x, int y, int z) {
public final IBlockState getBlock(int x, int y, int z) {
// no point in doing getOrCreate region, if we don't have it we don't have it
CachedRegion region = getRegion(x >> 9, z >> 9);
if (region == null) {
return null;
}
return region.getBlockType(x & 511, y, z & 511);
return region.getBlock(x & 511, y, z & 511);
}
private void updateCachedChunk(CachedChunk chunk) {

View File

@ -26,6 +26,7 @@ import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.chunk.Chunk;
@ -106,4 +107,20 @@ public final class ChunkPacker implements Helper {
return PathingBlockType.SOLID;
}
static IBlockState pathingTypeToBlock(PathingBlockType type) {
if (type != null) {
switch (type) {
case AIR:
return Blocks.AIR.getDefaultState();
case WATER:
return Blocks.WATER.getDefaultState();
case AVOID:
return Blocks.LAVA.getDefaultState();
case SOLID:
return Blocks.OBSIDIAN.getDefaultState();
}
}
return null;
}
}

View File

@ -131,7 +131,7 @@ public class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
boolean isPositionCached = false;
if (cache) {
if (CachedWorldProvider.INSTANCE.getCurrentWorld() != null) {
if (CachedWorldProvider.INSTANCE.getCurrentWorld().getBlockType(dest) != null) {
if (CachedWorldProvider.INSTANCE.getCurrentWorld().getBlock(dest) != null) {
isPositionCached = true;
}
}

View File

@ -20,7 +20,6 @@ package baritone.bot.utils;
import baritone.bot.Baritone;
import baritone.bot.chunk.CachedWorld;
import baritone.bot.chunk.CachedWorldProvider;
import baritone.bot.utils.pathing.PathingBlockType;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFalling;
import net.minecraft.block.BlockLiquid;
@ -31,31 +30,24 @@ import net.minecraft.world.chunk.Chunk;
public class BlockStateInterface implements Helper {
public static IBlockState get(BlockPos pos) { // wrappers for future chunk caching capability
public static IBlockState get(BlockPos pos) { // wrappers for chunk caching capability
// Invalid vertical position
if (pos.getY() < 0 || pos.getY() >= 256)
return Blocks.AIR.getDefaultState();
if (!Baritone.settings().pathThroughCachedOnly.get()) {
Chunk chunk = mc.world.getChunk(pos);
if (chunk.isLoaded()) {
return chunk.getBlockState(pos);
}
}
if (Baritone.settings().chunkCaching.get()) {
CachedWorld world = CachedWorldProvider.INSTANCE.getCurrentWorld();
if (world != null) {
PathingBlockType type = world.getBlockType(pos);
IBlockState type = world.getBlock(pos);
if (type != null) {
switch (type) {
case AIR:
return Blocks.AIR.getDefaultState();
case WATER:
return Blocks.WATER.getDefaultState();
case AVOID:
return Blocks.LAVA.getDefaultState();
case SOLID:
return Blocks.OBSIDIAN.getDefaultState();
}
return type;
}
}
}

View File

@ -18,6 +18,7 @@
package baritone.bot.utils.pathing;
import baritone.bot.utils.Helper;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
/**
@ -26,9 +27,9 @@ import net.minecraft.util.math.BlockPos;
*/
public interface IBlockTypeAccess extends Helper {
PathingBlockType getBlockType(int x, int y, int z);
IBlockState getBlock(int x, int y, int z);
default PathingBlockType getBlockType(BlockPos pos) {
return getBlockType(pos.getX(), pos.getY(), pos.getZ());
default IBlockState getBlock(BlockPos pos) {
return getBlock(pos.getX(), pos.getY(), pos.getZ());
}
}