Begin to create interfaces in api for cached World and Region

This commit is contained in:
Brady 2018-09-24 18:45:41 -05:00
parent 6829bc920e
commit 2a8575caa8
No known key found for this signature in database
GPG Key ID: 73A788379A197567
12 changed files with 178 additions and 29 deletions

View File

@ -15,9 +15,8 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>. * along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/ */
package baritone.utils.pathing; package baritone.api.cache;
import baritone.utils.Helper;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -25,7 +24,7 @@ import net.minecraft.util.math.BlockPos;
* @author Brady * @author Brady
* @since 8/4/2018 2:01 AM * @since 8/4/2018 2:01 AM
*/ */
public interface IBlockTypeAccess extends Helper { public interface IBlockTypeAccess {
IBlockState getBlock(int x, int y, int z); IBlockState getBlock(int x, int y, int z);

View File

@ -0,0 +1,49 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package baritone.api.cache;
/**
* @author Brady
* @since 9/24/2018
*/
public interface ICachedRegion extends IBlockTypeAccess {
/**
* Returns whether or not the block at the specified X and Z coordinates
* is cached in this world. Similar to {@link ICachedWorld#isCached(int, int)},
* however, the block coordinates should in on a scale from 0 to 511 (inclusive)
* because region sizes are 512x512 blocks.
*
* @see ICachedWorld#isCached(int, int)
*
* @param blockX The block X coordinate
* @param blockZ The block Z coordinate
* @return Whether or not the specified XZ location is cached
*/
boolean isCached(int blockX, int blockZ);
/**
* The X coordinate of this region
*/
int getX();
/**
* The Z coordinate of this region
*/
int getZ();
}

View File

@ -0,0 +1,82 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package baritone.api.cache;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.Chunk;
import java.util.LinkedList;
/**
* @author Brady
* @since 9/24/2018
*/
public interface ICachedWorld {
/**
* Returns the region at the specified region coordinates
*
* @param regionX The region X coordinate
* @param regionZ The region Z coordinate
* @return The region located at the specified coordinates
*/
ICachedRegion getRegion(int regionX, int regionZ);
/**
* Queues the specified chunk for packing. This entails reading the contents
* of the chunk, then packing the data into the 2-bit format, and storing that
* in this cached world.
*
* @param chunk The chunk to pack and store
*/
void queueForPacking(Chunk chunk);
/**
* Returns whether or not the block at the specified X and Z coordinates
* is cached in this world.
*
* @param blockX The block X coordinate
* @param blockZ The block Z coordinate
* @return Whether or not the specified XZ location is cached
*/
boolean isCached(int blockX, int blockZ);
/**
* Scans the cached chunks for location of the specified special block. The
* information that is returned by this method may not be up to date, because
* older cached chunks can contain data that is much more likely to have changed.
*
* @param block The special block to search for
* @param maximum The maximum number of position results to receive
* @param maxRegionDistanceSq The maximum region distance, squared
* @return The locations found that match the special block
*/
LinkedList<BlockPos> getLocationsOf(String block, int maximum, int maxRegionDistanceSq);
/**
* Reloads all of the cached regions in this world from disk. Anything that is not saved
* will be lost. This operation does not execute in a new thread by default.
*/
void reloadAllFromDisk();
/**
* Saves all of the cached regions in this world to disk. This operation does not execute
* in a new thread by default.
*/
void save();
}

View File

@ -23,6 +23,13 @@ package baritone.api.cache;
*/ */
public interface IWorldData { public interface IWorldData {
/**
* Returns the cached world for this world. A cached world is a simplified format
* of a regular world, intended for use on multiplayer servers where chunks are not
* traditionally stored to disk, allowing for long distance pathing with minimal disk usage.
*/
ICachedWorld getCachedWorld();
/** /**
* Returns the waypoint collection for this world. * Returns the waypoint collection for this world.
* *

View File

@ -139,7 +139,7 @@ public final class MineBehavior extends Behavior implements IMineBehavior, Helpe
//long b = System.currentTimeMillis(); //long b = System.currentTimeMillis();
for (Block m : mining) { for (Block m : mining) {
if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) { if (CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.contains(m)) {
locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(ChunkPacker.blockToString(m), 1, 1)); locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().getLocationsOf(ChunkPacker.blockToString(m), 1, 1));
} else { } else {
uninteresting.add(m); uninteresting.add(m);
} }

View File

@ -17,7 +17,8 @@
package baritone.cache; package baritone.cache;
import baritone.utils.pathing.IBlockTypeAccess; import baritone.api.cache.IBlockTypeAccess;
import baritone.utils.Helper;
import baritone.utils.pathing.PathingBlockType; import baritone.utils.pathing.PathingBlockType;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
@ -30,7 +31,7 @@ import java.util.*;
* @author Brady * @author Brady
* @since 8/3/2018 1:04 AM * @since 8/3/2018 1:04 AM
*/ */
public final class CachedChunk implements IBlockTypeAccess { public final class CachedChunk implements IBlockTypeAccess, Helper {
public static final Set<Block> BLOCKS_TO_KEEP_TRACK_OF = Collections.unmodifiableSet(new HashSet<Block>() {{ public static final Set<Block> BLOCKS_TO_KEEP_TRACK_OF = Collections.unmodifiableSet(new HashSet<Block>() {{
add(Blocks.DIAMOND_ORE); add(Blocks.DIAMOND_ORE);

View File

@ -17,7 +17,7 @@
package baritone.cache; package baritone.cache;
import baritone.utils.pathing.IBlockTypeAccess; import baritone.api.cache.ICachedRegion;
import net.minecraft.block.state.IBlockState; import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -33,7 +33,8 @@ import java.util.zip.GZIPOutputStream;
* @author Brady * @author Brady
* @since 8/3/2018 9:35 PM * @since 8/3/2018 9:35 PM
*/ */
public final class CachedRegion implements IBlockTypeAccess { public final class CachedRegion implements ICachedRegion {
private static final byte CHUNK_NOT_PRESENT = 0; private static final byte CHUNK_NOT_PRESENT = 0;
private static final byte CHUNK_PRESENT = 1; private static final byte CHUNK_PRESENT = 1;
@ -77,6 +78,7 @@ public final class CachedRegion implements IBlockTypeAccess {
return null; return null;
} }
@Override
public final boolean isCached(int x, int z) { public final boolean isCached(int x, int z) {
return chunks[x >> 4][z >> 4] != null; return chunks[x >> 4][z >> 4] != null;
} }
@ -280,6 +282,7 @@ public final class CachedRegion implements IBlockTypeAccess {
/** /**
* @return The region x coordinate * @return The region x coordinate
*/ */
@Override
public final int getX() { public final int getX() {
return this.x; return this.x;
} }
@ -287,6 +290,7 @@ public final class CachedRegion implements IBlockTypeAccess {
/** /**
* @return The region z coordinate * @return The region z coordinate
*/ */
@Override
public final int getZ() { public final int getZ() {
return this.z; return this.z;
} }

View File

@ -18,6 +18,7 @@
package baritone.cache; package baritone.cache;
import baritone.Baritone; import baritone.Baritone;
import baritone.api.cache.ICachedWorld;
import baritone.utils.Helper; import baritone.utils.Helper;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@ -36,7 +37,7 @@ import java.util.concurrent.LinkedBlockingQueue;
* @author Brady * @author Brady
* @since 8/4/2018 12:02 AM * @since 8/4/2018 12:02 AM
*/ */
public final class CachedWorld implements Helper { public final class CachedWorld implements ICachedWorld, Helper {
/** /**
* The maximum number of regions in any direction from (0,0) * The maximum number of regions in any direction from (0,0)
@ -83,6 +84,7 @@ public final class CachedWorld implements Helper {
}); });
} }
@Override
public final void queueForPacking(Chunk chunk) { public final void queueForPacking(Chunk chunk) {
try { try {
toPack.put(chunk); toPack.put(chunk);
@ -91,6 +93,7 @@ public final class CachedWorld implements Helper {
} }
} }
@Override
public final boolean isCached(int blockX, int blockZ) { public final boolean isCached(int blockX, int blockZ) {
CachedRegion region = getRegion(blockX >> 9, blockZ >> 9); CachedRegion region = getRegion(blockX >> 9, blockZ >> 9);
if (region == null) { if (region == null) {
@ -99,7 +102,8 @@ public final class CachedWorld implements Helper {
return region.isCached(blockX & 511, blockZ & 511); return region.isCached(blockX & 511, blockZ & 511);
} }
public final LinkedList<BlockPos> getLocationsOf(String block, int minimum, int maxRegionDistanceSq) { @Override
public final LinkedList<BlockPos> getLocationsOf(String block, int maximum, int maxRegionDistanceSq) {
LinkedList<BlockPos> res = new LinkedList<>(); LinkedList<BlockPos> res = new LinkedList<>();
int playerRegionX = playerFeet().getX() >> 9; int playerRegionX = playerFeet().getX() >> 9;
int playerRegionZ = playerFeet().getZ() >> 9; int playerRegionZ = playerFeet().getZ() >> 9;
@ -121,7 +125,7 @@ public final class CachedWorld implements Helper {
} }
} }
} }
if (res.size() >= minimum) { if (res.size() >= maximum) {
return res; return res;
} }
searchRadius++; searchRadius++;
@ -134,6 +138,7 @@ public final class CachedWorld implements Helper {
region.updateCachedChunk(chunk.x & 31, chunk.z & 31, chunk); region.updateCachedChunk(chunk.x & 31, chunk.z & 31, chunk);
} }
@Override
public final void save() { public final void save() {
if (!Baritone.settings().chunkCaching.get()) { if (!Baritone.settings().chunkCaching.get()) {
System.out.println("Not saving to disk; chunk caching is disabled."); System.out.println("Not saving to disk; chunk caching is disabled.");
@ -153,6 +158,7 @@ public final class CachedWorld implements Helper {
return new ArrayList<>(this.cachedRegions.values()); return new ArrayList<>(this.cachedRegions.values());
} }
@Override
public final void reloadAllFromDisk() { public final void reloadAllFromDisk() {
long start = System.nanoTime() / 1000000L; long start = System.nanoTime() / 1000000L;
allRegions().forEach(region -> { allRegions().forEach(region -> {
@ -164,13 +170,7 @@ public final class CachedWorld implements Helper {
System.out.println("World load took " + (now - start) + "ms"); System.out.println("World load took " + (now - start) + "ms");
} }
/** @Override
* Returns the region at the specified region coordinates
*
* @param regionX The region X coordinate
* @param regionZ The region Z coordinate
* @return The region located at the specified coordinates
*/
public final synchronized CachedRegion getRegion(int regionX, int regionZ) { public final synchronized CachedRegion getRegion(int regionX, int regionZ) {
return cachedRegions.get(getRegionID(regionX, regionZ)); return cachedRegions.get(getRegionID(regionX, regionZ));
} }

View File

@ -18,6 +18,7 @@
package baritone.cache; package baritone.cache;
import baritone.Baritone; import baritone.Baritone;
import baritone.api.cache.ICachedWorld;
import baritone.api.cache.IWaypointCollection; import baritone.api.cache.IWaypointCollection;
import baritone.api.cache.IWorldData; import baritone.api.cache.IWorldData;
@ -30,7 +31,7 @@ import java.nio.file.Path;
*/ */
public class WorldData implements IWorldData { public class WorldData implements IWorldData {
public final CachedWorld cache; private final CachedWorld cache;
private final Waypoints waypoints; private final Waypoints waypoints;
//public final MapData map; //public final MapData map;
public final Path directory; public final Path directory;
@ -48,6 +49,11 @@ public class WorldData implements IWorldData {
}); });
} }
@Override
public ICachedWorld getCachedWorld() {
return this.cache;
}
@Override @Override
public IWaypointCollection getWaypoints() { public IWaypointCollection getWaypoints() {
return this.waypoints; return this.waypoints;

View File

@ -109,7 +109,7 @@ public final class GameEventHandler implements IGameEventListener, Helper {
if (isPostPopulate || isPreUnload) { if (isPostPopulate || isPreUnload) {
WorldProvider.INSTANCE.ifWorldLoaded(world -> { WorldProvider.INSTANCE.ifWorldLoaded(world -> {
Chunk chunk = mc.world.getChunk(event.getX(), event.getZ()); Chunk chunk = mc.world.getChunk(event.getX(), event.getZ());
world.cache.queueForPacking(chunk); world.getCachedWorld().queueForPacking(chunk);
}); });
} }

View File

@ -18,6 +18,7 @@
package baritone.utils; package baritone.utils;
import baritone.Baritone; import baritone.Baritone;
import baritone.api.cache.ICachedRegion;
import baritone.cache.CachedRegion; import baritone.cache.CachedRegion;
import baritone.cache.WorldData; import baritone.cache.WorldData;
import baritone.cache.WorldProvider; import baritone.cache.WorldProvider;
@ -36,7 +37,7 @@ import net.minecraft.world.chunk.Chunk;
public class BlockStateInterface implements Helper { public class BlockStateInterface implements Helper {
private static Chunk prev = null; private static Chunk prev = null;
private static CachedRegion prevCached = null; private static ICachedRegion prevCached = null;
private static IBlockState AIR = Blocks.AIR.getDefaultState(); private static IBlockState AIR = Blocks.AIR.getDefaultState();
@ -70,13 +71,13 @@ public class BlockStateInterface implements Helper {
} }
// same idea here, skip the Long2ObjectOpenHashMap.get if at all possible // same idea here, skip the Long2ObjectOpenHashMap.get if at all possible
// except here, it's 512x512 tiles instead of 16x16, so even better repetition // except here, it's 512x512 tiles instead of 16x16, so even better repetition
CachedRegion cached = prevCached; ICachedRegion cached = prevCached;
if (cached == null || cached.getX() != x >> 9 || cached.getZ() != z >> 9) { if (cached == null || cached.getX() != x >> 9 || cached.getZ() != z >> 9) {
WorldData world = WorldProvider.INSTANCE.getCurrentWorld(); WorldData world = WorldProvider.INSTANCE.getCurrentWorld();
if (world == null) { if (world == null) {
return AIR; return AIR;
} }
CachedRegion region = world.cache.getRegion(x >> 9, z >> 9); ICachedRegion region = world.getCachedWorld().getRegion(x >> 9, z >> 9);
if (region == null) { if (region == null) {
return AIR; return AIR;
} }
@ -100,7 +101,7 @@ public class BlockStateInterface implements Helper {
prev = prevChunk; prev = prevChunk;
return true; return true;
} }
CachedRegion prevRegion = prevCached; ICachedRegion prevRegion = prevCached;
if (prevRegion != null && prevRegion.getX() == x >> 9 && prevRegion.getZ() == z >> 9) { if (prevRegion != null && prevRegion.getX() == x >> 9 && prevRegion.getZ() == z >> 9) {
return prevRegion.isCached(x & 511, z & 511); return prevRegion.isCached(x & 511, z & 511);
} }
@ -108,7 +109,7 @@ public class BlockStateInterface implements Helper {
if (world == null) { if (world == null) {
return false; return false;
} }
prevRegion = world.cache.getRegion(x >> 9, z >> 9); prevRegion = world.getCachedWorld().getRegion(x >> 9, z >> 9);
if (prevRegion == null) { if (prevRegion == null) {
return false; return false;
} }

View File

@ -184,7 +184,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
Chunk chunk = cli.getLoadedChunk(x, z); Chunk chunk = cli.getLoadedChunk(x, z);
if (chunk != null) { if (chunk != null) {
count++; count++;
WorldProvider.INSTANCE.getCurrentWorld().cache.queueForPacking(chunk); WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().queueForPacking(chunk);
} }
} }
} }
@ -272,20 +272,20 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
return; return;
} }
if (msg.equals("reloadall")) { if (msg.equals("reloadall")) {
WorldProvider.INSTANCE.getCurrentWorld().cache.reloadAllFromDisk(); WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().reloadAllFromDisk();
logDirect("ok"); logDirect("ok");
event.cancel(); event.cancel();
return; return;
} }
if (msg.equals("saveall")) { if (msg.equals("saveall")) {
WorldProvider.INSTANCE.getCurrentWorld().cache.save(); WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().save();
logDirect("ok"); logDirect("ok");
event.cancel(); event.cancel();
return; return;
} }
if (msg.startsWith("find")) { if (msg.startsWith("find")) {
String blockType = msg.substring(4).trim(); String blockType = msg.substring(4).trim();
LinkedList<BlockPos> locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType, 1, 4); LinkedList<BlockPos> locs = WorldProvider.INSTANCE.getCurrentWorld().getCachedWorld().getLocationsOf(blockType, 1, 4);
logDirect("Have " + locs.size() + " locations"); logDirect("Have " + locs.size() + " locations");
for (BlockPos pos : locs) { for (BlockPos pos : locs) {
Block actually = BlockStateInterface.get(pos).getBlock(); Block actually = BlockStateInterface.get(pos).getBlock();