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
|
2018-09-17 22:11:40 +00:00
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2018-08-08 03:16:53 +00:00
|
|
|
* 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
|
2018-09-17 22:11:40 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2018-08-08 03:16:53 +00:00
|
|
|
*
|
2018-09-17 22:11:40 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2018-08-08 03:16:53 +00:00
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-09-11 17:28:03 +00:00
|
|
|
package baritone.cache;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
2018-08-23 20:39:13 +00:00
|
|
|
import baritone.Baritone;
|
2018-11-14 05:26:27 +00:00
|
|
|
import baritone.api.BaritoneAPI;
|
|
|
|
import baritone.api.IBaritone;
|
2018-09-24 23:45:41 +00:00
|
|
|
import baritone.api.cache.ICachedWorld;
|
2018-11-14 05:26:27 +00:00
|
|
|
import baritone.api.cache.IWorldData;
|
2018-09-11 18:56:59 +00:00
|
|
|
import baritone.utils.Helper;
|
2018-08-05 04:36:59 +00:00
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
|
|
|
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
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-09-09 18:00:57 +00:00
|
|
|
import java.util.ArrayList;
|
2018-08-23 19:54:12 +00:00
|
|
|
import java.util.LinkedList;
|
2018-09-09 18:00:57 +00:00
|
|
|
import java.util.List;
|
2018-08-21 22:18:35 +00:00
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Brady
|
2018-11-14 03:53:27 +00:00
|
|
|
* @since 8/4/2018
|
2018-08-05 04:36:59 +00:00
|
|
|
*/
|
2018-09-24 23:45:41 +00:00
|
|
|
public final class CachedWorld implements ICachedWorld, Helper {
|
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-11-10 03:24:02 +00:00
|
|
|
private final int dimension;
|
|
|
|
|
|
|
|
CachedWorld(Path directory, int dimension) {
|
2018-08-22 22:35:32 +00:00
|
|
|
if (!Files.exists(directory)) {
|
|
|
|
try {
|
|
|
|
Files.createDirectories(directory);
|
2018-08-29 04:14:50 +00:00
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
2018-08-22 22:35:32 +00:00
|
|
|
}
|
|
|
|
this.directory = directory.toString();
|
2018-11-10 03:24:02 +00:00
|
|
|
this.dimension = dimension;
|
2018-08-22 22:35:32 +00:00
|
|
|
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-11-06 04:01:46 +00:00
|
|
|
Baritone.getExecutor().execute(new PackerThread());
|
|
|
|
Baritone.getExecutor().execute(() -> {
|
2018-08-29 23:32:16 +00:00
|
|
|
try {
|
|
|
|
Thread.sleep(30000);
|
|
|
|
while (true) {
|
|
|
|
// since a region only saves if it's been modified since its last save
|
|
|
|
// saving every 10 minutes means that once it's time to exit
|
|
|
|
// we'll only have a couple regions to save
|
|
|
|
save();
|
|
|
|
Thread.sleep(600000);
|
2018-08-23 20:42:45 +00:00
|
|
|
}
|
2018-08-29 23:32:16 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
2018-08-23 20:42:45 +00:00
|
|
|
}
|
2018-09-16 21:14:50 +00:00
|
|
|
});
|
2018-08-21 22:18:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
2018-08-21 22:18:35 +00:00
|
|
|
public final void queueForPacking(Chunk chunk) {
|
|
|
|
try {
|
|
|
|
toPack.put(chunk);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
2018-09-23 15:52:03 +00:00
|
|
|
public final boolean isCached(int blockX, int blockZ) {
|
|
|
|
CachedRegion region = getRegion(blockX >> 9, blockZ >> 9);
|
2018-08-29 04:14:50 +00:00
|
|
|
if (region == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-23 15:52:03 +00:00
|
|
|
return region.isCached(blockX & 511, blockZ & 511);
|
2018-08-29 04:14:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
2018-11-13 17:50:29 +00:00
|
|
|
public final LinkedList<BlockPos> getLocationsOf(String block, int maximum, int centerX, int centerZ, int maxRegionDistanceSq) {
|
2018-08-23 19:54:12 +00:00
|
|
|
LinkedList<BlockPos> res = new LinkedList<>();
|
2018-11-13 17:50:29 +00:00
|
|
|
int centerRegionX = centerX >> 9;
|
|
|
|
int centerRegionZ = centerZ >> 9;
|
2018-08-23 20:12:13 +00:00
|
|
|
|
|
|
|
int searchRadius = 0;
|
|
|
|
while (searchRadius <= maxRegionDistanceSq) {
|
|
|
|
for (int xoff = -searchRadius; xoff <= searchRadius; xoff++) {
|
|
|
|
for (int zoff = -searchRadius; zoff <= searchRadius; zoff++) {
|
2018-08-24 21:24:09 +00:00
|
|
|
int distance = xoff * xoff + zoff * zoff;
|
2018-08-23 20:12:13 +00:00
|
|
|
if (distance != searchRadius) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-13 17:50:29 +00:00
|
|
|
int regionX = xoff + centerRegionX;
|
|
|
|
int regionZ = zoff + centerRegionZ;
|
2018-08-23 20:12:13 +00:00
|
|
|
CachedRegion region = getOrCreateRegion(regionX, regionZ);
|
2018-09-08 04:32:25 +00:00
|
|
|
if (region != null) {
|
2018-09-24 22:56:13 +00:00
|
|
|
// TODO: 100% verify if this or addAll is faster.
|
|
|
|
region.getLocationsOf(block).forEach(res::add);
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-23 20:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 23:45:41 +00:00
|
|
|
if (res.size() >= maximum) {
|
2018-08-23 20:12:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
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.");
|
2018-10-07 23:57:04 +00:00
|
|
|
allRegions().forEach(region -> {
|
|
|
|
if (region != null) {
|
|
|
|
region.removeExpired();
|
|
|
|
}
|
|
|
|
}); // even if we aren't saving to disk, still delete expired old chunks from RAM
|
2018-10-15 22:19:10 +00:00
|
|
|
prune();
|
2018-08-23 20:39:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-29 23:22:57 +00:00
|
|
|
long start = System.nanoTime() / 1000000L;
|
2018-09-09 18:00:57 +00:00
|
|
|
allRegions().parallelStream().forEach(region -> {
|
2018-09-08 04:32:25 +00:00
|
|
|
if (region != null) {
|
2018-08-05 04:36:59 +00:00
|
|
|
region.save(this.directory);
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-05 04:36:59 +00:00
|
|
|
});
|
2018-08-29 23:22:57 +00:00
|
|
|
long now = System.nanoTime() / 1000000L;
|
2018-08-21 22:18:35 +00:00
|
|
|
System.out.println("World save took " + (now - start) + "ms");
|
2018-10-15 22:19:10 +00:00
|
|
|
prune();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete regions that are too far from the player
|
|
|
|
*/
|
|
|
|
private synchronized void prune() {
|
2018-11-06 01:30:45 +00:00
|
|
|
if (!Baritone.settings().pruneRegionsFromRAM.get()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-15 22:19:10 +00:00
|
|
|
BlockPos pruneCenter = guessPosition();
|
|
|
|
for (CachedRegion region : allRegions()) {
|
2018-10-16 18:32:27 +00:00
|
|
|
if (region == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-15 22:19:10 +00:00
|
|
|
int distX = (region.getX() * 512 + 256) - pruneCenter.getX();
|
|
|
|
int distZ = (region.getZ() * 512 + 256) - pruneCenter.getZ();
|
|
|
|
double dist = Math.sqrt(distX * distX + distZ * distZ);
|
|
|
|
if (dist > 1024) {
|
|
|
|
logDebug("Deleting cached region " + region.getX() + "," + region.getZ() + " from ram");
|
|
|
|
cachedRegions.remove(getRegionID(region.getX(), region.getZ()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we are still in this world and dimension, return player feet, otherwise return most recently modified chunk
|
|
|
|
*/
|
|
|
|
private BlockPos guessPosition() {
|
2018-11-14 05:26:27 +00:00
|
|
|
for (IBaritone ibaritone : BaritoneAPI.getProvider().getAllBaritones()) {
|
|
|
|
IWorldData data = ibaritone.getWorldProvider().getCurrentWorld();
|
|
|
|
if (data != null && data.getCachedWorld() == this) {
|
|
|
|
return ibaritone.getPlayerContext().playerFeet();
|
|
|
|
}
|
2018-10-15 22:19:10 +00:00
|
|
|
}
|
|
|
|
CachedChunk mostRecentlyModified = null;
|
|
|
|
for (CachedRegion region : allRegions()) {
|
2018-10-16 18:32:27 +00:00
|
|
|
if (region == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-10-15 22:19:10 +00:00
|
|
|
CachedChunk ch = region.mostRecentlyModified();
|
|
|
|
if (ch == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (mostRecentlyModified == null || mostRecentlyModified.cacheTimestamp < ch.cacheTimestamp) {
|
|
|
|
mostRecentlyModified = ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mostRecentlyModified == null) {
|
|
|
|
return new BlockPos(0, 0, 0);
|
|
|
|
}
|
|
|
|
return new BlockPos(mostRecentlyModified.x * 16 + 8, 0, mostRecentlyModified.z * 16 + 8);
|
2018-08-05 04:36:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 18:00:57 +00:00
|
|
|
private synchronized List<CachedRegion> allRegions() {
|
|
|
|
return new ArrayList<>(this.cachedRegions.values());
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
2018-08-23 19:54:12 +00:00
|
|
|
public final void reloadAllFromDisk() {
|
2018-08-29 23:22:57 +00:00
|
|
|
long start = System.nanoTime() / 1000000L;
|
2018-09-09 18:00:57 +00:00
|
|
|
allRegions().forEach(region -> {
|
2018-09-08 04:32:25 +00:00
|
|
|
if (region != null) {
|
2018-08-23 19:54:12 +00:00
|
|
|
region.load(this.directory);
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-23 19:54:12 +00:00
|
|
|
});
|
2018-08-29 23:22:57 +00:00
|
|
|
long now = System.nanoTime() / 1000000L;
|
2018-08-23 19:54:12 +00:00
|
|
|
System.out.println("World load took " + (now - start) + "ms");
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:45:41 +00:00
|
|
|
@Override
|
2018-09-09 18:00:57 +00:00
|
|
|
public final synchronized CachedRegion getRegion(int regionX, int regionZ) {
|
2018-08-05 04:36:59 +00:00
|
|
|
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 -> {
|
2018-11-10 03:24:02 +00:00
|
|
|
CachedRegion newRegion = new CachedRegion(regionX, regionZ, dimension);
|
2018-08-05 04:36:59 +00:00
|
|
|
newRegion.load(this.directory);
|
|
|
|
return newRegion;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-09-08 04:32:25 +00:00
|
|
|
if (!isRegionInWorld(regionX, regionZ)) {
|
2018-08-05 04:36:59 +00:00
|
|
|
return 0;
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-05 04:36:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-09-16 21:14:50 +00:00
|
|
|
private class PackerThread implements Runnable {
|
2018-08-21 22:18:35 +00:00
|
|
|
public void run() {
|
|
|
|
while (true) {
|
2018-09-24 23:01:17 +00:00
|
|
|
// TODO: Add CachedWorld unloading to remove the redundancy of having this
|
2018-08-21 22:18:35 +00:00
|
|
|
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
|
|
|
}
|