Skip to content

Commit 99bd356

Browse files
committed
fix tree index calculation and out of bounds cases with multi-forests
Signed-off-by: douira <[email protected]>
1 parent 9f96fd6 commit 99bd356

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/tree/BaseMultiForest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class BaseMultiForest<T extends Tree> extends BaseForest<T> {
66

77
protected T lastTree;
88

9-
public BaseMultiForest(int baseOffsetX, int baseOffsetY, int baseOffsetZ,float buildDistance) {
9+
public BaseMultiForest(int baseOffsetX, int baseOffsetY, int baseOffsetZ, float buildDistance) {
1010
super(baseOffsetX, baseOffsetY, baseOffsetZ, buildDistance);
1111

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

26-
return treeX + (treeZ * this.forestDim + treeY) * this.forestDim;
27-
}
26+
if (treeX < 0 || treeX >= this.forestDim ||
27+
treeY < 0 || treeY >= this.forestDim ||
28+
treeZ < 0 || treeZ >= this.forestDim) {
29+
return Tree.OUT_OF_BOUNDS;
30+
}
2831

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

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

4345
var treeIndex = this.getTreeIndex(localX, localY, localZ);
46+
if (treeIndex == Tree.OUT_OF_BOUNDS) {
47+
return;
48+
}
49+
4450
var tree = this.trees[treeIndex];
4551

4652
if (tree == null) {
@@ -59,18 +65,26 @@ public void add(int x, int y, int z) {
5965
public int getPresence(int x, int y, int z) {
6066
if (this.lastTree != null) {
6167
var result = this.lastTree.getPresence(x, y, z);
62-
if (result != TraversableTree.OUT_OF_BOUNDS) {
68+
if (result != Tree.OUT_OF_BOUNDS) {
6369
return result;
6470
}
6571
}
6672

67-
var treeIndex = this.getTreeIndexAbsolute(x, y, z);
73+
var localX = x - this.baseOffsetX;
74+
var localY = y - this.baseOffsetY;
75+
var localZ = z - this.baseOffsetZ;
76+
77+
var treeIndex = this.getTreeIndex(localX, localY, localZ);
78+
if (treeIndex == Tree.OUT_OF_BOUNDS) {
79+
return Tree.OUT_OF_BOUNDS;
80+
}
81+
6882
var tree = this.trees[treeIndex];
6983
if (tree != null) {
7084
this.lastTree = tree;
7185
return tree.getPresence(x, y, z);
7286
}
73-
return TraversableTree.OUT_OF_BOUNDS;
87+
return Tree.OUT_OF_BOUNDS;
7488
}
7589

7690
protected abstract T[] makeTrees(int length);

common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/tree/Tree.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package net.caffeinemc.mods.sodium.client.render.chunk.tree;
22

33
public abstract class Tree {
4-
public static final int OUT_OF_BOUNDS = 0;
5-
public static final int NOT_PRESENT = 1;
6-
public static final int PRESENT = 2;
4+
public static final int OUT_OF_BOUNDS = -1;
5+
public static final int NOT_PRESENT = 0;
6+
public static final int PRESENT = 1;
77

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

0 commit comments

Comments
 (0)