move context wrapper to new class

This commit is contained in:
Brady 2023-06-17 02:02:26 -05:00
parent a67889ab42
commit 395706edc9
No known key found for this signature in database
GPG Key ID: 73A788379A197567
2 changed files with 117 additions and 82 deletions

View File

@ -25,10 +25,9 @@ import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.Helper;
import baritone.api.utils.Rotation;
import baritone.api.utils.RotationUtils;
import baritone.behavior.elytra.NetherPathfinderContext;
import baritone.utils.BlockStateInterface;
import com.mojang.realmsclient.util.Pair;
import dev.babbaj.pathfinder.NetherPathfinder;
import dev.babbaj.pathfinder.PathSegment;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityFireworkRocket;
@ -39,17 +38,17 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
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.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public final class ElytraBehavior extends Behavior implements Helper {
/**
* 2b2t seed
*/
private static final long NETHER_SEED = 146008555100680L;
// Used exclusively for PathRenderer
@ -58,7 +57,7 @@ public final class ElytraBehavior extends Behavior implements Helper {
public List<BetterBlockPos> visiblePath;
// :sunglasses:
private final Context context;
private final NetherPathfinderContext context;
private List<BetterBlockPos> path;
public int playerNear;
private int goingTo;
@ -66,57 +65,14 @@ public final class ElytraBehavior extends Behavior implements Helper {
public ElytraBehavior(Baritone baritone) {
super(baritone);
this.context = new Context(NETHER_SEED);
this.context = new NetherPathfinderContext(NETHER_SEED);
this.lines = new ArrayList<>();
this.visiblePath = Collections.emptyList();
this.path = new ArrayList<>();
}
private static final class Context {
private final long context;
private final long seed;
private final ExecutorService executor;
public Context(long seed) {
this.context = NetherPathfinder.newContext(seed);
this.seed = seed;
this.executor = Executors.newSingleThreadExecutor();
}
public void queueForPacking(Chunk chunk) {
this.executor.submit(() -> NetherPathfinder.insertChunkData(this.context, chunk.x, chunk.z, pack(chunk)));
}
public CompletableFuture<PathSegment> pathFindAsync(final BlockPos src, final BlockPos dst) {
return CompletableFuture.supplyAsync(() ->
NetherPathfinder.pathFind(
this.context,
src.getX(), src.getY(), src.getZ(),
dst.getX(), dst.getY(), dst.getZ()
), this.executor);
}
public void destroy() {
// 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;
}
}
@Override
public final void onChunkEvent(ChunkEvent event) {
public void onChunkEvent(ChunkEvent event) {
if (event.getState() == EventState.POST && event.getType().isPopulate()) {
final Chunk chunk = ctx.world().getChunk(event.getX(), event.getZ());
this.context.queueForPacking(chunk);
@ -366,7 +322,7 @@ public final class ElytraBehavior extends Behavior implements Helper {
return bestPitch;
}
private static boolean passable(IBlockState state) {
public static boolean passable(IBlockState state) {
return state.getMaterial() == Material.AIR;
}
@ -462,36 +418,6 @@ public final class ElytraBehavior extends Behavior implements Helper {
}
}
private static boolean[] pack(Chunk chunk) {
try {
boolean[] packed = new boolean[16 * 16 * 128];
ExtendedBlockStorage[] chunkInternalStorageArray = chunk.getBlockStorageArray();
for (int y0 = 0; y0 < 8; y0++) {
ExtendedBlockStorage extendedblockstorage = chunkInternalStorageArray[y0];
if (extendedblockstorage == null) {
continue;
}
BlockStateContainer bsc = extendedblockstorage.getData();
int yReal = y0 << 4;
for (int y1 = 0; y1 < 16; y1++) {
int y = y1 | yReal;
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
IBlockState state = bsc.get(x, y1, z);
if (!passable(state)) {
packed[x + (z << 4) + (y << 8)] = true;
}
}
}
}
}
return packed;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// TODO: Use the optimized version from builder-2
private RayTraceResult rayTraceBlocks(Vec3d start, Vec3d end) {
int x1 = MathHelper.floor(end.x);

View File

@ -0,0 +1,109 @@
/*
* 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.behavior.elytra;
import dev.babbaj.pathfinder.NetherPathfinder;
import dev.babbaj.pathfinder.PathSegment;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.BlockStateContainer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static baritone.behavior.ElytraBehavior.passable;
/**
* @author Brady
*/
public final class NetherPathfinderContext {
private 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 queueForPacking(Chunk chunk) {
this.executor.submit(() -> NetherPathfinder.insertChunkData(this.context, chunk.x, chunk.z, pack(chunk)));
}
public CompletableFuture<PathSegment> pathFindAsync(final BlockPos src, final BlockPos dst) {
return CompletableFuture.supplyAsync(() ->
NetherPathfinder.pathFind(
this.context,
src.getX(), src.getY(), src.getZ(),
dst.getX(), dst.getY(), dst.getZ()
), this.executor);
}
public void destroy() {
// 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 boolean[] pack(Chunk chunk) {
try {
boolean[] packed = new boolean[16 * 16 * 128];
ExtendedBlockStorage[] chunkInternalStorageArray = chunk.getBlockStorageArray();
for (int y0 = 0; y0 < 8; y0++) {
ExtendedBlockStorage extendedblockstorage = chunkInternalStorageArray[y0];
if (extendedblockstorage == null) {
continue;
}
BlockStateContainer bsc = extendedblockstorage.getData();
int yReal = y0 << 4;
for (int y1 = 0; y1 < 16; y1++) {
int y = y1 | yReal;
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
IBlockState state = bsc.get(x, y1, z);
if (!passable(state)) {
packed[x + (z << 4) + (y << 8)] = true;
}
}
}
}
}
return packed;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}