Skip to content

Commit

Permalink
added recursion bounds to leaves updating
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassimo committed Nov 27, 2024
1 parent 06d2ae1 commit 7d4fc41
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
modName=DynamicTrees
modId=dynamictrees
modVersion=1.4.0-ALPHA02
modVersion=1.4.0-ALPHA03

group=com.ferreusveritas.dynamictrees

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target, BlockGett

@Override
public int age(LevelAccessor level, BlockPos pos, BlockState state, RandomSource rand, SafeChunkBounds safeBounds) {
return updateLeaves(level, pos, state, rand,safeBounds == SafeChunkBounds.ANY_WG,null);
return updateLeaves(level, pos, state, rand,safeBounds == SafeChunkBounds.ANY_WG,null, 0);
}

@Override
Expand All @@ -159,7 +159,7 @@ public void randomTick(BlockState state, ServerLevel level, BlockPos pos, Random
}
}
//Every once in a blue moon, age the leaves
if (rand.nextFloat() < 0.05){
if (rand.nextFloat() < 0.01){
age(level, pos, state, rand, SafeChunkBounds.ANY);
}
}
Expand All @@ -185,16 +185,18 @@ public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource
* @param visitedPositions keeps track of visited blocks for recursive calls. Set to null to make it pulse this block only.
* @return the new hydro value
*/
public int updateLeaves(LevelAccessor level, BlockPos pos, BlockState state, RandomSource rand, boolean worldGen, @Nullable HashSet<BlockPos> visitedPositions){
public int updateLeaves(LevelAccessor level, BlockPos pos, BlockState state, RandomSource rand, boolean worldGen, @Nullable HashSet<BlockPos> visitedPositions, int fromHydro){
boolean recursive = visitedPositions != null;
final LeavesProperties leavesProperties = getProperties();
if (recursive){
if (visitedPositions.contains(pos)) return 0;
if (visitedPositions.contains(pos) || visitedPositions.size() > leavesProperties.maxLeavesRecursion()) return 0;
visitedPositions.add(pos);
}
final LeavesProperties leavesProperties = getProperties();

int newHydro = updateHydro(level, pos, state, worldGen);
if (recursive && newHydro > fromHydro) return newHydro; //We do not recurse back through bigger hydro values
//Grows new leaves around
// We should do this even if the hydro is only 1. Since there could be adjacent branch blocks that could use a leaves block
// We should do this even if the hydro is only 1. Since there could be adjacent branch blocks that could use a leaves block
for (Direction dir : Direction.values()) { // Go on all 6 sides of this block
if (newHydro > 1 || rand.nextInt(4) == 0) { // we'll give it a 1 in 4 chance to grow leaves if hydro is low to help performance
BlockPos offpos = pos.relative(dir);
Expand All @@ -205,7 +207,7 @@ public int updateLeaves(LevelAccessor level, BlockPos pos, BlockState state, Ran
//We recursively visit nearby leaves telling them to update too
BlockState sideState = level.getBlockState(offpos);
if (TreeHelper.isLeaves(sideState)){
updateLeaves(level, offpos, sideState, rand, worldGen, visitedPositions);
updateLeaves(level, offpos, sideState, rand, worldGen, visitedPositions, newHydro);
}
}

Expand Down Expand Up @@ -484,7 +486,7 @@ public GrowSignal branchOut(Level level, BlockPos pos, GrowSignal signal) {
}

//Pulse through the leaves to update the canopy shape and their hydro values
int hydro = updateLeaves(level, pos, level.getBlockState(pos), signal.rand, false, new HashSet<>());
int hydro = updateLeaves(level, pos, level.getBlockState(pos), signal.rand, false, new HashSet<>(), Integer.MAX_VALUE);
//if hydro was 0 then the leaves have been removed
if (hydro == 0){
signal.success = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ public boolean isCompatibleLeaves(LeavesProperties leaves){
return this.getFamily() == leaves.getFamily();
}

protected int maxLeavesRecursion(){
return 256;
}

///////////////////////////////////////////
// LEAVES COLORS
///////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ public LogsAndSticks getLogsAndSticks(NetVolumeNode.Volume volume, boolean silkT
public boolean handleVoluntaryDrops(Level level, List<BlockPos> endPoints, BlockPos rootPos, BlockPos treePos, int fertility) {
int tickSpeed = level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING);
if (tickSpeed > 0) {
double slowFactor = 3.0 / tickSpeed;//This is an attempt to normalize voluntary drop rates.
double slowFactor = 3.0 / tickSpeed; //This is to prevent high tick-speeds from spamming the floor with seeds
if (level.random.nextDouble() < slowFactor) {
final List<ItemStack> drops = getVoluntaryDrops(level, rootPos, fertility);

Expand Down

0 comments on commit 7d4fc41

Please sign in to comment.