baritone/src/main/java/baritone/process/elytra/NetherPathfinderContext.java

244 lines
9.8 KiB
Java

/*
* 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.process.elytra;
import baritone.Baritone;
import baritone.api.event.events.BlockChangeEvent;
import baritone.utils.accessor.IBitArray;
import baritone.utils.accessor.IBlockStateContainer;
import dev.babbaj.pathfinder.NetherPathfinder;
import dev.babbaj.pathfinder.Octree;
import dev.babbaj.pathfinder.PathSegment;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.BitArray;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.BlockStateContainer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import java.lang.ref.SoftReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Brady
*/
public final class NetherPathfinderContext {
private static final IBlockState AIR_BLOCK_STATE = Blocks.AIR.getDefaultState();
// This lock must be held while there are active pointers to chunks in java,
// but we just hold it for the entire tick so we don't have to think much about it.
public final Object cullingLock = new Object();
// Visible for access in BlockStateOctreeInterface
final long context;
private final long seed;
private final ExecutorService executor;
public NetherPathfinderContext(long seed) {
this.context = NetherPathfinder.newContext(seed);
this.seed = seed;
this.executor = Executors.newSingleThreadExecutor();
}
public void queueCacheCulling(int chunkX, int chunkZ, int maxDistanceBlocks, BlockStateOctreeInterface boi) {
this.executor.execute(() -> {
synchronized (this.cullingLock) {
boi.chunkPtr = 0L;
NetherPathfinder.cullFarChunks(this.context, chunkX, chunkZ, maxDistanceBlocks);
}
});
}
public void queueForPacking(final Chunk chunkIn) {
final SoftReference<Chunk> ref = new SoftReference<>(chunkIn);
this.executor.execute(() -> {
// TODO: Prioritize packing recent chunks and/or ones that the path goes through,
// and prune the oldest chunks per chunkPackerQueueMaxSize
final Chunk chunk = ref.get();
if (chunk != null) {
long ptr = NetherPathfinder.getOrCreateChunk(this.context, chunk.x, chunk.z);
writeChunkData(chunk, ptr);
}
});
}
public void queueBlockUpdate(BlockChangeEvent event) {
this.executor.execute(() -> {
ChunkPos chunkPos = event.getChunkPos();
long ptr = NetherPathfinder.getChunkPointer(this.context, chunkPos.x, chunkPos.z);
if (ptr == 0) return; // this shouldn't ever happen
event.getBlocks().forEach(pair -> {
BlockPos pos = pair.first();
if (pos.getY() >= 128) return;
boolean isSolid = pair.second() != AIR_BLOCK_STATE;
Octree.setBlock(ptr, pos.getX() & 15, pos.getY(), pos.getZ() & 15, isSolid);
});
});
}
public CompletableFuture<PathSegment> pathFindAsync(final BlockPos src, final BlockPos dst) {
return CompletableFuture.supplyAsync(() -> {
final PathSegment segment = NetherPathfinder.pathFind(
this.context,
src.getX(), src.getY(), src.getZ(),
dst.getX(), dst.getY(), dst.getZ(),
true,
false,
10000,
!Baritone.settings().elytraPredictTerrain.value
);
if (segment == null) {
throw new PathCalculationException("Path calculation failed");
}
return segment;
}, this.executor);
}
/**
* Performs a raytrace from the given start position to the given end position, returning {@code true} if there is
* visibility between the two points.
*
* @param startX The start X coordinate
* @param startY The start Y coordinate
* @param startZ The start Z coordinate
* @param endX The end X coordinate
* @param endY The end Y coordinate
* @param endZ The end Z coordinate
* @return {@code true} if there is visibility between the points
*/
public boolean raytrace(final double startX, final double startY, final double startZ,
final double endX, final double endY, final double endZ) {
return NetherPathfinder.isVisible(this.context, NetherPathfinder.CACHE_MISS_SOLID, startX, startY, startZ, endX, endY, endZ);
}
/**
* Performs a raytrace from the given start position to the given end position, returning {@code true} if there is
* visibility between the two points.
*
* @param start The starting point
* @param end The ending point
* @return {@code true} if there is visibility between the points
*/
public boolean raytrace(final Vec3d start, final Vec3d end) {
return NetherPathfinder.isVisible(this.context, NetherPathfinder.CACHE_MISS_SOLID, start.x, start.y, start.z, end.x, end.y, end.z);
}
public boolean raytrace(final int count, final double[] src, final double[] dst, final int visibility) {
switch (visibility) {
case Visibility.ALL:
return NetherPathfinder.isVisibleMulti(this.context, NetherPathfinder.CACHE_MISS_SOLID, count, src, dst, false) == -1;
case Visibility.NONE:
return NetherPathfinder.isVisibleMulti(this.context, NetherPathfinder.CACHE_MISS_SOLID, count, src, dst, true) == -1;
case Visibility.ANY:
return NetherPathfinder.isVisibleMulti(this.context, NetherPathfinder.CACHE_MISS_SOLID, count, src, dst, true) != -1;
default:
throw new IllegalArgumentException("lol");
}
}
public void raytrace(final int count, final double[] src, final double[] dst, final boolean[] hitsOut, final double[] hitPosOut) {
NetherPathfinder.raytrace(this.context, NetherPathfinder.CACHE_MISS_SOLID, count, src, dst, hitsOut, hitPosOut);
}
public void cancel() {
NetherPathfinder.cancel(this.context);
}
public void destroy() {
this.cancel();
// Ignore anything that was queued up, just shutdown the executor
this.executor.shutdownNow();
try {
while (!this.executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {}
} catch (InterruptedException e) {
e.printStackTrace();
}
NetherPathfinder.freeContext(this.context);
}
public long getSeed() {
return this.seed;
}
private static void writeChunkData(Chunk chunk, long ptr) {
try {
ExtendedBlockStorage[] chunkInternalStorageArray = chunk.getBlockStorageArray();
for (int y0 = 0; y0 < 8; y0++) {
final ExtendedBlockStorage extendedblockstorage = chunkInternalStorageArray[y0];
if (extendedblockstorage == null) {
continue;
}
final BlockStateContainer bsc = extendedblockstorage.getData();
final int airId = ((IBlockStateContainer) bsc).getPalette().idFor(AIR_BLOCK_STATE);
// pasted from FasterWorldScanner
final BitArray array = ((IBlockStateContainer) bsc).getStorage();
if (array == null) continue;
final long[] longArray = array.getBackingLongArray();
final int arraySize = array.size();
final int bitsPerEntry = ((IBitArray) array).getBitsPerEntry();
final long maxEntryValue = ((IBitArray) array).getMaxEntryValue();
final int yReal = y0 << 4;
for (int idx = 0, kl = bitsPerEntry - 1; idx < arraySize; idx++, kl += bitsPerEntry) {
final int i = idx * bitsPerEntry;
final int j = i >> 6;
final int l = i & 63;
final int k = kl >> 6;
final long jl = longArray[j] >>> l;
final int id;
if (j == k) {
id = (int) (jl & maxEntryValue);
} else {
id = (int) ((jl | longArray[k] << (64 - l)) & maxEntryValue);
}
int x = (idx & 15);
int y = yReal + (idx >> 8);
int z = ((idx >> 4) & 15);
Octree.setBlock(ptr, x, y, z, id != airId);
}
}
Octree.setIsFromJava(ptr);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static final class Visibility {
public static final int ALL = 0;
public static final int NONE = 1;
public static final int ANY = 2;
private Visibility() {}
}
public static boolean isSupported() {
return NetherPathfinder.isThisSystemSupported();
}
}