2018-08-08 03:16:53 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Baritone.
|
|
|
|
*
|
|
|
|
* Baritone is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2018-08-08 04:15:22 +00:00
|
|
|
* Baritone is distributed in the hope that it will be useful,
|
2018-08-08 03:16:53 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-05 04:36:59 +00:00
|
|
|
package baritone.bot.chunk;
|
|
|
|
|
2018-08-21 22:18:35 +00:00
|
|
|
import baritone.bot.utils.pathing.IBlockTypeAccess;
|
2018-08-13 23:59:59 +00:00
|
|
|
import baritone.bot.utils.pathing.PathingBlockType;
|
2018-08-05 04:36:59 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
2018-08-21 22:18:35 +00:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
import java.util.BitSet;
|
2018-08-21 22:18:35 +00:00
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
2018-08-08 00:41:13 +00:00
|
|
|
import java.util.function.Consumer;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Brady
|
|
|
|
* @since 8/4/2018 12:02 AM
|
|
|
|
*/
|
2018-08-21 22:18:35 +00:00
|
|
|
public final class CachedWorld implements IBlockTypeAccess {
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum number of regions in any direction from (0,0)
|
|
|
|
*/
|
|
|
|
private static final int REGION_MAX = 117188;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A map of all of the cached regions.
|
|
|
|
*/
|
|
|
|
private Long2ObjectMap<CachedRegion> cachedRegions = new Long2ObjectOpenHashMap<>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The directory that the cached region files are saved to
|
|
|
|
*/
|
|
|
|
private final String directory;
|
|
|
|
|
2018-08-21 22:18:35 +00:00
|
|
|
private final LinkedBlockingQueue<Chunk> toPack = new LinkedBlockingQueue<>();
|
|
|
|
|
2018-08-05 04:36:59 +00:00
|
|
|
public CachedWorld(String directory) {
|
|
|
|
this.directory = directory;
|
|
|
|
// Insert an invalid region element
|
|
|
|
cachedRegions.put(0, null);
|
2018-08-21 22:18:35 +00:00
|
|
|
new PackerThread().start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void queueForPacking(Chunk chunk) {
|
|
|
|
try {
|
|
|
|
toPack.put(chunk);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final PathingBlockType getBlockType(int x, int y, int z) {
|
2018-08-21 18:54:55 +00:00
|
|
|
CachedRegion region = getOrCreateRegion(x >> 9, z >> 9);
|
2018-08-21 19:18:54 +00:00
|
|
|
return region.getBlockType(x & 511, y, z & 511);
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 22:18:35 +00:00
|
|
|
private void updateCachedChunk(int chunkX, int chunkZ, BitSet data) {
|
2018-08-05 04:36:59 +00:00
|
|
|
CachedRegion region = getOrCreateRegion(chunkX >> 5, chunkZ >> 5);
|
2018-08-22 17:03:02 +00:00
|
|
|
region.updateCachedChunk(chunkX & 31, chunkZ & 31, data, );
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public final void save() {
|
2018-08-21 22:18:35 +00:00
|
|
|
long start = System.currentTimeMillis();
|
2018-08-05 04:36:59 +00:00
|
|
|
this.cachedRegions.values().forEach(region -> {
|
|
|
|
if (region != null)
|
|
|
|
region.save(this.directory);
|
|
|
|
});
|
2018-08-21 22:18:35 +00:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
System.out.println("World save took " + (now - start) + "ms");
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 CachedRegion getRegion(int regionX, int regionZ) {
|
|
|
|
return cachedRegions.get(getRegionID(regionX, regionZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the region at the specified region coordinates. If a
|
|
|
|
* region is not found, then a new one is created.
|
|
|
|
*
|
|
|
|
* @param regionX The region X coordinate
|
|
|
|
* @param regionZ The region Z coordinate
|
|
|
|
* @return The region located at the specified coordinates
|
|
|
|
*/
|
2018-08-08 00:41:13 +00:00
|
|
|
CachedRegion getOrCreateRegion(int regionX, int regionZ) {
|
2018-08-05 04:36:59 +00:00
|
|
|
return cachedRegions.computeIfAbsent(getRegionID(regionX, regionZ), id -> {
|
|
|
|
CachedRegion newRegion = new CachedRegion(regionX, regionZ);
|
|
|
|
newRegion.load(this.directory);
|
|
|
|
return newRegion;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:41:13 +00:00
|
|
|
public void forEachRegion(Consumer<CachedRegion> consumer) {
|
|
|
|
this.cachedRegions.forEach((id, r) -> {
|
|
|
|
if (r != null)
|
|
|
|
consumer.accept(r);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-05 04:36:59 +00:00
|
|
|
/**
|
|
|
|
* Returns the region ID based on the region coordinates. 0 will be
|
|
|
|
* returned if the specified region coordinates are out of bounds.
|
|
|
|
*
|
|
|
|
* @param regionX The region X coordinate
|
|
|
|
* @param regionZ The region Z coordinate
|
|
|
|
* @return The region ID
|
|
|
|
*/
|
|
|
|
private long getRegionID(int regionX, int regionZ) {
|
|
|
|
if (!isRegionInWorld(regionX, regionZ))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (long) regionX & 0xFFFFFFFFL | ((long) regionZ & 0xFFFFFFFFL) << 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether or not the specified region coordinates is within the world bounds.
|
|
|
|
*
|
|
|
|
* @param regionX The region X coordinate
|
|
|
|
* @param regionZ The region Z coordinate
|
|
|
|
* @return Whether or not the region is in world bounds
|
|
|
|
*/
|
|
|
|
private boolean isRegionInWorld(int regionX, int regionZ) {
|
|
|
|
return regionX <= REGION_MAX && regionX >= -REGION_MAX && regionZ <= REGION_MAX && regionZ >= -REGION_MAX;
|
|
|
|
}
|
2018-08-21 22:18:35 +00:00
|
|
|
|
|
|
|
private class PackerThread extends Thread {
|
|
|
|
public void run() {
|
|
|
|
while (true) {
|
|
|
|
LinkedBlockingQueue<Chunk> queue = toPack;
|
|
|
|
if (queue == null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Chunk chunk = queue.take();
|
2018-08-22 17:03:02 +00:00
|
|
|
BitSet packedChunk = ChunkPacker.createPackedChunk(chunk);
|
|
|
|
String[] packedOverview = ChunkPacker.createPackedOverview(chunk);
|
|
|
|
CachedWorld.this.updateCachedChunk(chunk.x, chunk.z, packedChunk);
|
2018-08-21 22:18:35 +00:00
|
|
|
//System.out.println("Processed chunk at " + chunk.x + "," + chunk.z);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|