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-22 20:15:56 +00:00
|
|
|
package baritone.chunk;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
2018-08-23 20:39:13 +00:00
|
|
|
import baritone.Baritone;
|
2018-08-22 20:15:56 +00:00
|
|
|
import baritone.utils.pathing.IBlockTypeAccess;
|
2018-08-05 04:36:59 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
2018-08-22 19:04:44 +00:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-08-23 19:54:12 +00:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2018-08-21 22:18:35 +00:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
2018-08-22 22:35:32 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2018-08-23 19:54:12 +00:00
|
|
|
import java.util.LinkedList;
|
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)
|
|
|
|
*/
|
2018-08-22 21:46:24 +00:00
|
|
|
private static final int REGION_MAX = 58594;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-22 22:35:32 +00:00
|
|
|
CachedWorld(Path directory) {
|
|
|
|
if (!Files.exists(directory)) {
|
|
|
|
try {
|
|
|
|
Files.createDirectories(directory);
|
|
|
|
} catch (IOException ignored) {}
|
|
|
|
}
|
|
|
|
this.directory = directory.toString();
|
|
|
|
System.out.println("Cached world directory: " + directory);
|
2018-08-05 04:36:59 +00:00
|
|
|
// 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
|
2018-08-22 19:04:44 +00:00
|
|
|
public final IBlockState getBlock(int x, int y, int z) {
|
2018-08-22 18:41:31 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2018-08-22 19:04:44 +00:00
|
|
|
return region.getBlock(x & 511, y, z & 511);
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 20:12:13 +00:00
|
|
|
public final LinkedList<BlockPos> getLocationsOf(String block, int minimum, int maxRegionDistanceSq) {
|
2018-08-23 19:54:12 +00:00
|
|
|
LinkedList<BlockPos> res = new LinkedList<>();
|
2018-08-23 20:12:13 +00:00
|
|
|
int playerRegionX = playerFeet().getX() >> 9;
|
|
|
|
int playerRegionZ = playerFeet().getZ() >> 9;
|
|
|
|
|
|
|
|
int searchRadius = 0;
|
|
|
|
while (searchRadius <= maxRegionDistanceSq) {
|
|
|
|
for (int xoff = -searchRadius; xoff <= searchRadius; xoff++) {
|
|
|
|
for (int zoff = -searchRadius; zoff <= searchRadius; zoff++) {
|
|
|
|
double distance = xoff * xoff + zoff * zoff;
|
|
|
|
if (distance != searchRadius) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int regionX = xoff + playerRegionX;
|
|
|
|
int regionZ = zoff + playerRegionZ;
|
|
|
|
CachedRegion region = getOrCreateRegion(regionX, regionZ);
|
|
|
|
if (region != null)
|
|
|
|
for (BlockPos pos : region.getLocationsOf(block))
|
|
|
|
res.add(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res.size() >= minimum) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
searchRadius++;
|
|
|
|
}
|
2018-08-23 19:54:12 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-08-22 18:41:31 +00:00
|
|
|
private void updateCachedChunk(CachedChunk chunk) {
|
|
|
|
CachedRegion region = getOrCreateRegion(chunk.x >> 5, chunk.z >> 5);
|
|
|
|
region.updateCachedChunk(chunk.x & 31, chunk.z & 31, chunk);
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public final void save() {
|
2018-08-23 20:39:13 +00:00
|
|
|
if (!Baritone.settings().chunkCaching.get()) {
|
|
|
|
System.out.println("Not saving to disk; chunk caching is disabled.");
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-08-23 19:54:12 +00:00
|
|
|
public final void reloadAllFromDisk() {
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
this.cachedRegions.values().forEach(region -> {
|
|
|
|
if (region != null)
|
|
|
|
region.load(this.directory);
|
|
|
|
});
|
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
System.out.println("World load 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-23 20:12:13 +00:00
|
|
|
private synchronized 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-23 19:54:12 +00:00
|
|
|
CachedChunk cached = ChunkPacker.pack(chunk);
|
2018-08-22 18:41:31 +00:00
|
|
|
CachedWorld.this.updateCachedChunk(cached);
|
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
|
|
|
}
|