mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-11 10:16:15 +00:00
implement blockstate cache in Column
This commit is contained in:
@@ -13,6 +13,8 @@ import com.dfsek.terra.api.structure.feature.Locator;
|
||||
import com.dfsek.terra.api.util.Range;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Column;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
|
||||
public class PatternLocator implements Locator {
|
||||
private final Pattern pattern;
|
||||
@@ -25,6 +27,9 @@ public class PatternLocator implements Locator {
|
||||
|
||||
@Override
|
||||
public BinaryColumn getSuitableCoordinates(Column<?> column) {
|
||||
return new BinaryColumn(search, y -> pattern.matches(y, column));
|
||||
int min = FastMath.max(column.getMinY(), search.getMin());
|
||||
int max = FastMath.min(column.getMaxY(), search.getMax());
|
||||
if(min >= max) return BinaryColumn.getNull();
|
||||
return new BinaryColumn(min, max, y -> pattern.matches(y, column));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import com.dfsek.terra.api.block.state.BlockState;
|
||||
import com.dfsek.terra.api.util.Range;
|
||||
import com.dfsek.terra.api.world.chunk.generation.util.Column;
|
||||
|
||||
import net.jafama.FastMath;
|
||||
|
||||
|
||||
public class MatchPattern implements Pattern {
|
||||
private final Range range;
|
||||
@@ -25,8 +27,10 @@ public class MatchPattern implements Pattern {
|
||||
|
||||
@Override
|
||||
public boolean matches(int y, Column<?> column) {
|
||||
for(int i = range.getMin(); i < range.getMax(); i++) {
|
||||
if(!matches.test(column.getBlock(y + i))) return false;
|
||||
int min = FastMath.max(column.getMinY(), range.getMin() + y);
|
||||
int max = FastMath.min(column.getMaxY(), range.getMax() + y);
|
||||
for(int i = min; i < max; i++) {
|
||||
if(!matches.test(column.getBlock(i))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ public class Column<T extends WritableWorld> {
|
||||
private final int max;
|
||||
private final T world;
|
||||
|
||||
private final BlockState[] cache;
|
||||
|
||||
public Column(int x, int z, T world) {
|
||||
this(x, z, world, world.getMinHeight(), world.getMaxHeight());
|
||||
}
|
||||
@@ -37,6 +39,7 @@ public class Column<T extends WritableWorld> {
|
||||
this.world = world;
|
||||
this.max = max;
|
||||
this.min = min;
|
||||
this.cache = new BlockState[world.getMaxHeight() - world.getMinHeight()];
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
@@ -48,7 +51,13 @@ public class Column<T extends WritableWorld> {
|
||||
}
|
||||
|
||||
public BlockState getBlock(int y) {
|
||||
return world.getBlockState(x, y, z);
|
||||
int i = y - world.getMinHeight();
|
||||
BlockState state = cache[i];
|
||||
if(state == null) {
|
||||
state = world.getBlockState(x, y, z);
|
||||
cache[i] = state;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
public T getWorld() {
|
||||
|
||||
Reference in New Issue
Block a user