Skip to content

Commit

Permalink
skip cached mask when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Jan 9, 2025
1 parent fd9013d commit eeea0c8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public final BlockState getBlockRelativeY(int y) {
return states[getArr[this.index + (y << 8)]];
} else if ((layerAdd > 0 && layerAdd < (maxLayer - layer)) || (layerAdd < 0 && layerAdd < (minLayer - layer))) {
final int newLayer = layer + layerAdd;
final int index = this.index + ((y & 15) << 8);
final int index = (this.index + ((y & 15) << 8)) & 4095;
return states[get.sections[newLayer].get(get, newLayer, index)];
}
return BlockTypes.__RESERVED__.getDefaultState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ABlockMask(Extent extent) {

@Override
public boolean test(Extent extent, BlockVector3 vector) {
return test(extent.getBlock(vector));
return test(vector.getBlock(extent));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AngleMask extends AbstractExtentMask implements ResettableMask {
protected static double ADJACENT_MOD = 0.5;
protected static double DIAGONAL_MOD = 1 / Math.sqrt(8);

private final SolidBlockMask fastMask;
protected final CachedMask mask;
protected final double max;
protected final double min;
Expand All @@ -32,7 +33,8 @@ public class AngleMask extends AbstractExtentMask implements ResettableMask {

public AngleMask(Extent extent, double min, double max, boolean overlay, int distance) {
super(extent);
this.mask = new CachedMask(new SolidBlockMask(extent));
this.fastMask = new SolidBlockMask(extent);
this.mask = new CachedMask(this.fastMask);
this.min = min;
this.max = max;
this.checkFirst = max >= (Math.tan(90 * (Math.PI / 180)));
Expand Down Expand Up @@ -126,38 +128,42 @@ protected boolean testSlope(Extent extent, int x, int y, int z) {
}
}

private boolean adjacentAir(Extent extent, MutableBlockVector3 mutable) {
int x = mutable.x();
int y = mutable.y();
int z = mutable.z();
if (!mask.test(extent, mutable.setComponents(x + 1, y, z))) {
// for optimal performance, this method should be called with base being a CharFilterBlock
private boolean adjacentAir(Extent extent, BlockVector3 base) {
int y = base.y();
// we expect the data for blocks above and below to be loaded already
// in which case caching/reading from cache has more overhead
if (y != maxY && !fastMask.test(base.getStateRelativeY(extent, 1))) {
return true;
}
if (!mask.test(extent, mutable.setComponents(x - 1, y, z))) {
if (y != minY && !fastMask.test(base.getStateRelativeY(extent, -1))) {
return true;
}
if (!mask.test(extent, mutable.setComponents(x, y, z + 1))) {
// other positions might be in different chunks, go a slower, cached path there
MutableBlockVector3 mutable = new MutableBlockVector3();
int x = base.x();
int z = base.z();
if (!mask.test(extent, mutable.setComponents(x + 1, y, z))) {
return true;
}
if (!mask.test(extent, mutable.setComponents(x, y, z - 1))) {
if (!mask.test(extent, mutable.setComponents(x - 1, y, z))) {
return true;
}
if (y != maxY && !mask.test(extent, mutable.setComponents(x, y + 1, z))) {
if (!mask.test(extent, mutable.setComponents(x, y, z + 1))) {
return true;
}
return y != minY && !mask.test(extent, mutable.setComponents(x, y - 1, z));
return !mask.test(extent, mutable.setComponents(x, y, z - 1));
}

@Override
public boolean test(BlockVector3 vector) {

if (!mask.test(vector)) {
if (!fastMask.test(vector)) {
return false;
}
int y = vector.y();
if (overlay) {
MutableBlockVector3 mutable = new MutableBlockVector3(vector);
if (y < maxY && !adjacentAir(null, mutable)) {
if (y < maxY && !adjacentAir(getExtent(), vector)) {
return false;
}
}
Expand All @@ -179,13 +185,11 @@ public boolean test(final Extent extent, final BlockVector3 vector) {
}
}

MutableBlockVector3 mutable = new MutableBlockVector3(x, y, z);

if (!mask.test(extent, mutable)) {
if (!fastMask.test(extent, vector)) {
return false;
}
if (overlay) {
if (y < maxY && !adjacentAir(extent, mutable)) {
if (y < maxY && !adjacentAir(extent, vector)) {
return lastValue = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public char[] get(CharBlocks blocks, int layer) {
char[] arr = blocks.blocks[layer];
if (arr == null) {
// Chunk probably trimmed mid-operations, but do nothing about it to avoid other issues
blocks.sections[layer] = EMPTY;
return EMPTY.get(blocks, layer, false);
}
return arr;
Expand Down

0 comments on commit eeea0c8

Please sign in to comment.