Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize path, then new chunks in work queue. Purge old chunks #4057

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/main/java/baritone/behavior/ElytraBehavior.java
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,6 @@ public void onTick(final TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
return;
}
final long now = System.currentTimeMillis();
if ((now - this.timeLastCacheCull) / 1000 > Baritone.settings().elytraTimeBetweenCacheCullSecs.value) {
this.context.queueCacheCulling(ctx.player().chunkCoordX, ctx.player().chunkCoordZ, Baritone.settings().elytraCacheCullDistance.value, this.boi);
this.timeLastCacheCull = now;
}

// Fetch the previous solution, regardless of if it's going to be used
this.pendingSolution = null;
Expand Down Expand Up @@ -594,6 +589,12 @@ public void onTick(final TickEvent event) {
Math.max(playerNear - 30, 0),
Math.min(playerNear + 100, path.size())
);

final long now = System.currentTimeMillis();
if ((now - this.timeLastCacheCull) / 1000 > Baritone.settings().elytraTimeBetweenCacheCullSecs.value) {
this.context.queueCacheCulling(ctx.player().chunkCoordX, ctx.player().chunkCoordZ, Baritone.settings().elytraCacheCullDistance.value, this.boi);
this.timeLastCacheCull = now;
}
}

/**
Expand Down
228 changes: 223 additions & 5 deletions src/main/java/baritone/behavior/elytra/NetherPathfinderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package baritone.behavior.elytra;

import baritone.Baritone;
import baritone.api.event.events.BlockChangeEvent;
import baritone.utils.accessor.IBitArray;
import baritone.utils.accessor.IBlockStateContainer;
Expand All @@ -33,11 +34,13 @@
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;

import javax.annotation.Nonnull;
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;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

/**
* @author Brady
Expand All @@ -55,7 +58,7 @@ public final class NetherPathfinderContext {
public NetherPathfinderContext(long seed) {
this.context = NetherPathfinder.newContext(seed);
this.seed = seed;
this.executor = Executors.newSingleThreadExecutor();
this.executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new WorkQueue());
}

public void queueCacheCulling(int chunkX, int chunkZ, int maxDistanceBlocks, BlockStateOctreeInterface boi) {
Expand Down Expand Up @@ -223,6 +226,221 @@ private static void writeChunkData(Chunk chunk, long ptr) {
}
}

private static final class WorkQueue extends TrollBlockingQueue<Runnable> {

private final LinkedList<Runnable> path;
private final LinkedList<Runnable> chunk;

private final ReentrantLock takeLock = new ReentrantLock();
private final ReentrantLock putLock = new ReentrantLock();
private final Condition notEmpty = takeLock.newCondition();

public WorkQueue() {
this.path = new LinkedList<>();
this.chunk = new LinkedList<>();
}

private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
this.notEmpty.signal();
} finally {
takeLock.unlock();
}
}

@Override
public boolean offer(@Nonnull Runnable runnable) {
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
if (runnable instanceof ForkJoinTask) {
this.path.offer(runnable);
} else {
// Put new chunks at the head of the queue
this.chunk.offerFirst(runnable);
// Purge oldest chunks
while (this.chunk.size() > Baritone.settings().chunkPackerQueueMaxSize.value) {
this.chunk.removeLast();
}
}
} finally {
putLock.unlock();
}
signalNotEmpty();
return true;
}

@Override
public Runnable take() throws InterruptedException {
Runnable x;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (size() == 0) {
notEmpty.await();
}
x = dequeue();
if (!isEmpty()) {
notEmpty.signal();
}
} finally {
takeLock.unlock();
}
return x;
}

@Override
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
Runnable x;
long nanos = unit.toNanos(timeout);
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (isEmpty()) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
x = dequeue();
if (!isEmpty())
notEmpty.signal();
} finally {
takeLock.unlock();
}
return x;
}

@Override
public boolean remove(Object o) {
takeLock.lock();
putLock.lock();
try {
return this.path.remove(o) || this.chunk.remove(o);
} finally {
takeLock.unlock();
putLock.unlock();
}
}

@Override
public int drainTo(Collection<? super Runnable> c) {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
int n = size();
try {
if (!this.path.isEmpty()) {
c.add(this.path.remove());
}
if (!this.chunk.isEmpty()) {
c.add(this.chunk.remove());
}
} finally {
takeLock.unlock();
}
return n;
}

@Override
public int size() {
takeLock.lock();
putLock.lock();
try {
return this.path.size() + this.chunk.size();
} finally {
takeLock.unlock();
putLock.unlock();
}
}

@Override
public boolean isEmpty() {
return this.size() == 0;
}

@SuppressWarnings("unchecked")
@Override
public synchronized @Nonnull <T> T[] toArray(@Nonnull T[] a) {
takeLock.lock();
putLock.lock();
try {
return (T[]) Stream.concat(this.path.stream(), this.chunk.stream()).toArray(Runnable[]::new);
} finally {
takeLock.unlock();
putLock.unlock();
}
}

private Runnable dequeue() {
return !this.path.isEmpty() ? this.path.remove() : this.chunk.remove();
}
}

@SuppressWarnings("NullableProblems")
private static class TrollBlockingQueue<T> extends AbstractQueue<T> implements BlockingQueue<T> {

@Override
public Iterator<T> iterator() {
throw new UnsupportedOperationException();
}

@Override
public int size() {
throw new UnsupportedOperationException();
}

@Override
public void put(T t) {
throw new UnsupportedOperationException();
}

@Override
public boolean offer(T t, long timeout, TimeUnit unit) {
throw new UnsupportedOperationException();
}

@Override
public T take() throws InterruptedException {
throw new UnsupportedOperationException();
}

@Override
public T poll(long timeout, TimeUnit unit) throws InterruptedException {
throw new UnsupportedOperationException();
}

@Override
public int remainingCapacity() {
throw new UnsupportedOperationException();
}

@Override
public int drainTo(Collection<? super T> c) {
throw new UnsupportedOperationException();
}

@Override
public int drainTo(Collection<? super T> c, int maxElements) {
throw new UnsupportedOperationException();
}

@Override
public boolean offer(T t) {
throw new UnsupportedOperationException();
}

@Override
public T poll() {
throw new UnsupportedOperationException();
}

@Override
public T peek() {
throw new UnsupportedOperationException();
}
}

public static final class Visibility {

public static final int ALL = 0;
Expand Down