Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/1.20/dev' into feat/multi-load…
Browse files Browse the repository at this point in the history
…er-1.21
  • Loading branch information
IThundxr committed Nov 16, 2024
2 parents 89d8755 + 3811da1 commit eb1c56e
Show file tree
Hide file tree
Showing 13 changed files with 449 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.engine_room.flywheel.backend;

public class BackendDebugFlags {
public static boolean LIGHT_STORAGE_VIEW = false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.engine_room.flywheel.backend;

import org.jetbrains.annotations.Nullable;

import net.minecraft.world.level.chunk.DataLayer;

public interface SkyLightSectionStorageExtension {
@Nullable DataLayer flywheel$skyDataLayer(long section);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public void add(long position, int index) {
.set(z, index + 1);
}

public void prune() {
// Maybe this could be done better incrementally?
indices.prune((middle) -> middle.prune(IntLayer::prune));
}

public void remove(long section) {
final var x = SectionPos.x(section);
final var y = SectionPos.y(section);
Expand All @@ -49,6 +54,11 @@ public IntArrayList flatten() {
return out;
}

@FunctionalInterface
public interface Prune<T> {
boolean prune(T t);
}

public static final class Layer<T> {
private boolean hasBase = false;
private int base = 0;
Expand Down Expand Up @@ -135,6 +145,69 @@ public T computeIfAbsent(int i, Supplier<T> ifAbsent) {
return (T) out;
}

/**
* @return {@code true} if the layer is now empty.
*/
public boolean prune(Prune<T> inner) {
if (!hasBase) {
return true;
}

// Prune the next layer before checking for leading/trailing zeros.
for (var i = 0; i < nextLayer.length; i++) {
var o = nextLayer[i];
if (o != null && inner.prune((T) o)) {
nextLayer[i] = null;
}
}

var leadingZeros = getLeadingZeros();

if (leadingZeros == nextLayer.length) {
return true;
}

var trailingZeros = getTrailingZeros();

if (leadingZeros == 0 && trailingZeros == 0) {
return false;
}

final var newIndices = new Object[nextLayer.length - leadingZeros - trailingZeros];

System.arraycopy(nextLayer, leadingZeros, newIndices, 0, newIndices.length);
nextLayer = newIndices;
base += leadingZeros;

return false;
}

private int getLeadingZeros() {
int out = 0;

for (Object index : nextLayer) {
if (index == null) {
out++;
} else {
break;
}
}
return out;
}

private int getTrailingZeros() {
int out = 0;

for (int i = nextLayer.length - 1; i >= 0; i--) {
if (nextLayer[i] == null) {
out++;
} else {
break;
}
}
return out;
}

private void resize(int length) {
final var newIndices = new Object[length];
System.arraycopy(nextLayer, 0, newIndices, 0, nextLayer.length);
Expand Down Expand Up @@ -214,6 +287,61 @@ public void set(int i, int index) {
indices[offset] = index;
}

/**
* @return {@code true} if the layer is now empty.
*/
public boolean prune() {
if (!hasBase) {
return true;
}

var leadingZeros = getLeadingZeros();

if (leadingZeros == indices.length) {
return true;
}

var trailingZeros = getTrailingZeros();

if (leadingZeros == 0 && trailingZeros == 0) {
return false;
}

final var newIndices = new int[indices.length - leadingZeros - trailingZeros];

System.arraycopy(indices, leadingZeros, newIndices, 0, newIndices.length);
indices = newIndices;
base += leadingZeros;

return false;
}

private int getTrailingZeros() {
int out = 0;

for (int i = indices.length - 1; i >= 0; i--) {
if (indices[i] == 0) {
out++;
} else {
break;
}
}
return out;
}

private int getLeadingZeros() {
int out = 0;

for (int index : indices) {
if (index == 0) {
out++;
} else {
break;
}
}
return out;
}

public void clear(int i) {
if (!hasBase) {
return;
Expand Down
Loading

0 comments on commit eb1c56e

Please sign in to comment.