Skip to content

Commit

Permalink
Scan leaves to get what is connected to them
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakambda committed Nov 28, 2023
1 parent 61c1f52 commit 233a853
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public interface ITreeConfiguration{

int getMaxSize();

int getMaxLeafDistanceFromLog();

@NotNull
MaxSizeAction getMaxSizeAction();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public int getMaxSize(){
return delegate.getMaxSize();
}

@Override
public int getMaxLeafDistanceFromLog(){
return delegate.getMaxLeafDistanceFromLog();
}

@Override
@NotNull
public MaxSizeAction getMaxSizeAction(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class TreeConfiguration implements ITreeConfiguration, IResettable{
@Expose
private int maxSize = 100;
@Expose
private int maxLeafDistanceFromLog = 15;
@Expose
@NotNull
private MaxSizeAction maxSizeAction = MaxSizeAction.ABORT;
@Expose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
@Getter
@RequiredArgsConstructor
public enum TreePartType{
LEAF(false),
LEAF_NEED_BREAK(true),
LOG(true),
NETHER_WART(true),
MANGROVE_ROOTS(true),
OTHER(false);
LEAF(false, true, false),
LEAF_NEED_BREAK(true, true, true),
LOG(true, false, true),
NETHER_WART(true, false, true),
MANGROVE_ROOTS(true, false, true),
OTHER(false, false, false);

@Getter
private static final TreePartType[] values = values();

private final boolean breakable;
private final boolean edge;
private final boolean includeInTree;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public record ToAnalyzePos(@NotNull IPositionFetcher positionFetcher,
@NotNull IBlockPos checkPos,
@NotNull IBlock checkBlock,
@NotNull TreePartType treePartType,
int sequence)
int sequence,
int sequenceSinceLastLog)
implements Comparable<ToAnalyzePos>{

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Optional<Tree> getTree(@NotNull IPlayer player, @NotNull ILevel level, @N
var toAnalyzePos = new PriorityQueue<ToAnalyzePos>();
var analyzedPos = new HashSet<ToAnalyzePos>();
var tree = new Tree(level, originPos);
toAnalyzePos.add(new ToAnalyzePos(getFirstPositionFetcher(), originPos, originBlock, originPos, originBlock, TreePartType.LOG, 0));
toAnalyzePos.add(new ToAnalyzePos(getFirstPositionFetcher(), originPos, originBlock, originPos, originBlock, TreePartType.LOG, 0, 0));

var boundingBoxSearch = getBoundingBoxSearch(originPos);
var adjacentPredicate = getAdjacentPredicate();
Expand All @@ -53,13 +53,18 @@ public Optional<Tree> getTree(@NotNull IPlayer player, @NotNull ILevel level, @N

while(!toAnalyzePos.isEmpty()){
var analyzingPos = toAnalyzePos.remove();
tree.addPart(analyzingPos.toTreePart());
if(analyzingPos.toTreePart().treePartType().isIncludeInTree()){
tree.addPart(analyzingPos.toTreePart());
}
analyzedPos.add(analyzingPos);

if(tree.getSize() > maxScanSize){
log.debug("Tree at {} reached max scan size of {}", tree.getHitPos(), maxScanSize);
throw new TreeTooBigException();
}
if(analyzingPos.treePartType().isEdge() && analyzingPos.sequenceSinceLastLog() >= mod.getConfiguration().getTrees().getMaxLeafDistanceFromLog()){
continue;
}

var potentialPositions = analyzingPos.positionFetcher().getPositions(level, originPos, analyzingPos);
var nextPositions = filterPotentialPos(boundingBoxSearch, adjacentPredicate, level, originPos, originBlock, analyzingPos, potentialPositions, analyzedPos);
Expand Down Expand Up @@ -134,9 +139,9 @@ private Predicate<IBlockPos> getBoundingBoxSearch(@NotNull IBlockPos originPos){
var maxZ = originPos.getZ() + radius;

return pos -> minX <= pos.getX()
&& maxX >= pos.getX()
&& minZ <= pos.getZ()
&& maxZ >= pos.getZ();
&& maxX >= pos.getX()
&& minZ <= pos.getZ()
&& maxZ >= pos.getZ();
}

@NotNull
Expand Down Expand Up @@ -194,6 +199,9 @@ private long getLeavesAround(@NotNull ILevel level, @NotNull IBlockPos blockPos)
}

private boolean shouldIncludeInChain(@NotNull Predicate<IBlockPos> boundingBoxSearch, @NotNull IBlockPos originPos, @NotNull IBlock originBlock, @NotNull ToAnalyzePos parent, @NotNull ToAnalyzePos check){
if(parent.treePartType().isEdge() && !check.treePartType().isEdge()){
return false;
}
if(parent.treePartType() == TreePartType.LOG && isSameTree(originBlock, check) && boundingBoxSearch.test(check.checkPos())){
return true;
}
Expand All @@ -210,7 +218,7 @@ private boolean shouldIncludeInChain(@NotNull Predicate<IBlockPos> boundingBoxSe
return true;
}
}
return check.treePartType() == TreePartType.LEAF_NEED_BREAK;
return check.treePartType().isEdge();
}

private boolean isSameTree(@NotNull IBlock parentLogBlock, @NotNull ToAnalyzePos check){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.rakambda.fallingtree.common.tree.builder.position;

import fr.rakambda.fallingtree.common.tree.TreePartType;
import fr.rakambda.fallingtree.common.tree.builder.ToAnalyzePos;
import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.wrapper.IBlockPos;
Expand Down Expand Up @@ -33,7 +34,9 @@ public Collection<ToAnalyzePos> getPositions(@NotNull ILevel level, @NotNull IBl
return parentPos.betweenClosedStream(parentPos.above().north().east(), lowerPosProvider.apply(parentPos).south().west())
.map(checkPos -> {
var checkBlock = level.getBlockState(checkPos).getBlock();
return new ToAnalyzePos(positionFetcherSupplier.get(), parentPos, parentBlock, checkPos.immutable(), checkBlock, mod.getTreePart(checkBlock), parent.sequence() + 1);
var treePart = mod.getTreePart(checkBlock);
var logSequence = treePart == TreePartType.LOG ? 0 : (parent.sequenceSinceLastLog() + 1);
return new ToAnalyzePos(this, parentPos, parentBlock, checkPos.immutable(), checkBlock, treePart, parent.sequence() + 1, logSequence);
})
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.rakambda.fallingtree.common.tree.builder.position;

import fr.rakambda.fallingtree.common.tree.TreePartType;
import fr.rakambda.fallingtree.common.tree.builder.ToAnalyzePos;
import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.wrapper.IBlockPos;
Expand Down Expand Up @@ -27,7 +28,9 @@ public Collection<ToAnalyzePos> getPositions(@NotNull ILevel level, @NotNull IBl
.filter(pos -> pos.getY() > originPos.getY())
.map(checkPos -> {
var checkBlock = level.getBlockState(checkPos).getBlock();
return new ToAnalyzePos(this, parentPos, parentBlock, checkPos.immutable(), checkBlock, mod.getTreePart(checkBlock), parent.sequence() + 1);
var treePart = mod.getTreePart(checkBlock);
var logSequence = treePart == TreePartType.LOG ? 0 : (parent.sequenceSinceLastLog() + 1);
return new ToAnalyzePos(this, parentPos, parentBlock, checkPos.immutable(), checkBlock, treePart, parent.sequence() + 1, logSequence);
})
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.rakambda.fallingtree.common.tree.builder.position;

import fr.rakambda.fallingtree.common.tree.TreePartType;
import fr.rakambda.fallingtree.common.tree.builder.ToAnalyzePos;
import fr.rakambda.fallingtree.common.FallingTreeCommon;
import fr.rakambda.fallingtree.common.wrapper.IBlockPos;
Expand All @@ -26,7 +27,9 @@ public Collection<ToAnalyzePos> getPositions(@NotNull ILevel level, @NotNull IBl
return parentPos.betweenClosedStream(parentPos.above().north().east(), parentPos.below().south().west())
.map(checkPos -> {
var checkBlock = level.getBlockState(checkPos).getBlock();
return new ToAnalyzePos(this, parentPos, parentBlock, checkPos.immutable(), checkBlock, mod.getTreePart(checkBlock), parent.sequence() + 1);
var treePart = mod.getTreePart(checkBlock);
var logSequence = treePart == TreePartType.LOG ? 0 : (parent.sequenceSinceLastLog() + 1);
return new ToAnalyzePos(this, parentPos, parentBlock, checkPos.immutable(), checkBlock, treePart, parent.sequence() + 1, logSequence);
})
.collect(toList());
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/fallingtree/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"text.autoconfig.fallingtree.option.trees.maxSizeAction.@Tooltip[1]": "",
"text.autoconfig.fallingtree.option.trees.maxSizeAction.@Tooltip[2]": "ABORT: Tree won't be cut.",
"text.autoconfig.fallingtree.option.trees.maxSizeAction.@Tooltip[3]": "CUT: Tree will still be cut leaving some of it untouched.",
"text.autoconfig.fallingtree.option.trees.maxLeafDistanceFromLog": "Maximum leaf distance from log",
"text.autoconfig.fallingtree.option.trees.maxLeafDistanceFromLog.@Tooltip[0]": "The maximum distance a leaf can be from the tree.",
"text.autoconfig.fallingtree.option.trees.maxLeafDistanceFromLog.@Tooltip[1]": "This only affects scanning a tree.",
"text.autoconfig.fallingtree.option.trees.breakOrder": "Break order",
"text.autoconfig.fallingtree.option.trees.breakOrder.@Tooltip[0]": "In what order logs are broken.",
"text.autoconfig.fallingtree.option.trees.breakOrder.@Tooltip[1]": "This only makes sense if maxSize < maxScanSize.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ private void fillTreesConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tree
.setTooltip(getTooltips("trees", "maxSizeAction", 4))
.setSaveConsumer(config::setMaxSizeAction)
.build();
var maxLeafDistanceFromLogEntry = builder.entryBuilder()
.startIntField(translatable(getFieldName("trees", "maxLeafDistanceFromLog")), config.getMaxLeafDistanceFromLog())
.setDefaultValue(15)
.setTooltip(getTooltips("trees", "maxLeafDistanceFromLog", 2))
.setSaveConsumer(config::setMaxLeafDistanceFromLog)
.build();
var breakOrderEntry = builder.entryBuilder()
.startEnumSelector(translatable(getFieldName("trees", "breakOrder")), BreakOrder.class, config.getBreakOrder())
.setDefaultValue(BreakOrder.FURTHEST_FIRST)
Expand Down Expand Up @@ -251,6 +257,7 @@ private void fillTreesConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tree
trees.addEntry(maxScanSizeEntry);
trees.addEntry(maxSizeEntry);
trees.addEntry(maxSizeActionEntry);
trees.addEntry(maxLeafDistanceFromLogEntry);
trees.addEntry(breakOrderEntry);
trees.addEntry(treeBreakingEntry);
trees.addEntry(leavesBreakingEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ private void fillTreesConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tree
.setTooltip(getTooltips("trees", "maxSizeAction", 4))
.setSaveConsumer(config::setMaxSizeAction)
.build();
var maxLeafDistanceFromLogEntry = builder.entryBuilder()
.startIntField(translatable(getFieldName("trees", "maxLeafDistanceFromLog")), config.getMaxLeafDistanceFromLog())
.setDefaultValue(15)
.setTooltip(getTooltips("trees", "maxLeafDistanceFromLog", 2))
.setSaveConsumer(config::setMaxLeafDistanceFromLog)
.build();
var breakOrderEntry = builder.entryBuilder()
.startEnumSelector(translatable(getFieldName("trees", "breakOrder")), BreakOrder.class, config.getBreakOrder())
.setDefaultValue(BreakOrder.FURTHEST_FIRST)
Expand Down Expand Up @@ -251,6 +257,7 @@ private void fillTreesConfigScreen(@NotNull ConfigBuilder builder, @NotNull Tree
trees.addEntry(maxScanSizeEntry);
trees.addEntry(maxSizeEntry);
trees.addEntry(maxSizeActionEntry);
trees.addEntry(maxLeafDistanceFromLogEntry);
trees.addEntry(breakOrderEntry);
trees.addEntry(treeBreakingEntry);
trees.addEntry(leavesBreakingEntry);
Expand Down
3 changes: 3 additions & 0 deletions wiki/Settings---Trees.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ NOTE: Only applied in `Instantaneous` breaking mode.
- ABORT: Tree won't be cut.
- CUT: Tree will still be cut leaving some of it untouched.
|Maximum leaf distance from log
|The maximum distance a leaf can be from the tree. This only affects scanning a tree.

|Max size order
|In what order logs are broken.

Expand Down

0 comments on commit 233a853

Please sign in to comment.