Skip to content

Commit

Permalink
fix tree index calculation and out of bounds cases with multi-forests
Browse files Browse the repository at this point in the history
Signed-off-by: douira <[email protected]>
  • Loading branch information
douira committed Jan 19, 2025
1 parent 9f96fd6 commit 99bd356
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public abstract class BaseMultiForest<T extends Tree> extends BaseForest<T> {

protected T lastTree;

public BaseMultiForest(int baseOffsetX, int baseOffsetY, int baseOffsetZ,float buildDistance) {
public BaseMultiForest(int baseOffsetX, int baseOffsetY, int baseOffsetZ, float buildDistance) {
super(baseOffsetX, baseOffsetY, baseOffsetZ, buildDistance);

this.forestDim = forestDimFromBuildDistance(buildDistance);
Expand All @@ -23,11 +23,13 @@ protected int getTreeIndex(int localX, int localY, int localZ) {
var treeY = localY >> 6;
var treeZ = localZ >> 6;

return treeX + (treeZ * this.forestDim + treeY) * this.forestDim;
}
if (treeX < 0 || treeX >= this.forestDim ||
treeY < 0 || treeY >= this.forestDim ||
treeZ < 0 || treeZ >= this.forestDim) {
return Tree.OUT_OF_BOUNDS;
}

protected int getTreeIndexAbsolute(int x, int y, int z) {
return this.getTreeIndex(x - this.baseOffsetX, y - this.baseOffsetY, z - this.baseOffsetZ);
return treeX + (treeZ * this.forestDim + treeY) * this.forestDim;
}

@Override
Expand All @@ -41,6 +43,10 @@ public void add(int x, int y, int z) {
var localZ = z - this.baseOffsetZ;

var treeIndex = this.getTreeIndex(localX, localY, localZ);
if (treeIndex == Tree.OUT_OF_BOUNDS) {
return;
}

var tree = this.trees[treeIndex];

if (tree == null) {
Expand All @@ -59,18 +65,26 @@ public void add(int x, int y, int z) {
public int getPresence(int x, int y, int z) {
if (this.lastTree != null) {
var result = this.lastTree.getPresence(x, y, z);
if (result != TraversableTree.OUT_OF_BOUNDS) {
if (result != Tree.OUT_OF_BOUNDS) {
return result;
}
}

var treeIndex = this.getTreeIndexAbsolute(x, y, z);
var localX = x - this.baseOffsetX;
var localY = y - this.baseOffsetY;
var localZ = z - this.baseOffsetZ;

var treeIndex = this.getTreeIndex(localX, localY, localZ);
if (treeIndex == Tree.OUT_OF_BOUNDS) {
return Tree.OUT_OF_BOUNDS;
}

var tree = this.trees[treeIndex];
if (tree != null) {
this.lastTree = tree;
return tree.getPresence(x, y, z);
}
return TraversableTree.OUT_OF_BOUNDS;
return Tree.OUT_OF_BOUNDS;
}

protected abstract T[] makeTrees(int length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.caffeinemc.mods.sodium.client.render.chunk.tree;

public abstract class Tree {
public static final int OUT_OF_BOUNDS = 0;
public static final int NOT_PRESENT = 1;
public static final int PRESENT = 2;
public static final int OUT_OF_BOUNDS = -1;
public static final int NOT_PRESENT = 0;
public static final int PRESENT = 1;

protected final long[] tree = new long[64 * 64];
protected final int offsetX, offsetY, offsetZ;
Expand Down

0 comments on commit 99bd356

Please sign in to comment.