Search in sources :

Example 1 with NodeCollector

use of com.ferreusveritas.dynamictrees.systems.nodemappers.NodeCollector in project DynamicTrees by DynamicTreesTeam.

the class JoCode method cleanupFrankentree.

/**
 * Attempt to clean up fused trees that have multiple root blocks by simply destroying them both messily
 */
protected void cleanupFrankentree(World world, BlockPos treePos, IBlockState treeState, List<BlockPos> endPoints, SafeChunkBounds safeBounds) {
    Set<BlockPos> blocksToDestroy = new HashSet<>();
    BlockBranch branch = TreeHelper.getBranch(treeState);
    MapSignal signal = new MapSignal(new NodeCollector(blocksToDestroy));
    signal.destroyLoopedNodes = false;
    signal.trackVisited = true;
    branch.analyse(treeState, world, treePos, null, signal);
    BlockBranch.destroyMode = EnumDestroyMode.IGNORE;
    for (BlockPos pos : blocksToDestroy) {
        if (safeBounds.inBounds(pos, false)) {
            IBlockState branchState = world.getBlockState(pos);
            Optional<BlockBranch> branchBlock = TreeHelper.getBranchOpt(branchState);
            if (branchBlock.isPresent()) {
                int radius = branchBlock.get().getRadius(branchState);
                TreeFamily family = branchBlock.get().getFamily();
                Species species = family.getCommonSpecies();
                if (family.getPrimaryThickness() == radius) {
                    ILeavesProperties leavesProperties = species.getLeavesProperties();
                    if (leavesProperties != LeavesProperties.NULLPROPERTIES) {
                        SimpleVoxmap leafCluster = leavesProperties.getCellKit().getLeafCluster();
                        if (leafCluster != LeafClusters.NULLMAP) {
                            for (Cell cell : leafCluster.getAllNonZeroCells()) {
                                BlockPos delPos = pos.add(cell.getPos());
                                if (safeBounds.inBounds(delPos, false)) {
                                    IBlockState leavesState = world.getBlockState(delPos);
                                    if (TreeHelper.isLeaves(leavesState)) {
                                        BlockDynamicLeaves leavesBlock = (BlockDynamicLeaves) leavesState.getBlock();
                                        if (leavesProperties.getTree() == leavesBlock.getProperties(leavesState).getTree()) {
                                            world.setBlockState(delPos, ModBlocks.blockStates.air, 2);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                world.setBlockState(pos, ModBlocks.blockStates.air, 2);
            }
        }
    }
    BlockBranch.destroyMode = EnumDestroyMode.HARVEST;
// Now wreck out all surrounding leaves.  Let them grow back naturally.
/*if(!endPoints.isEmpty()) {
			BlockBounds bounds = new BlockBounds(endPoints);
			bounds.expand(3);
			for(BlockPos pos : bounds.iterate()) {
				if(safeBounds.inBounds(pos, false)) {
					if(TreeHelper.isLeaves(world.getBlockState(pos))) {
						world.setBlockState(pos, ModBlocks.blockStates.air, 2);
					}
				}
			}
		}*/
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) SimpleVoxmap(com.ferreusveritas.dynamictrees.util.SimpleVoxmap) BlockDynamicLeaves(com.ferreusveritas.dynamictrees.blocks.BlockDynamicLeaves) BlockBranch(com.ferreusveritas.dynamictrees.blocks.BlockBranch) NodeCollector(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeCollector) ILeavesProperties(com.ferreusveritas.dynamictrees.api.treedata.ILeavesProperties) TreeFamily(com.ferreusveritas.dynamictrees.trees.TreeFamily) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) Species(com.ferreusveritas.dynamictrees.trees.Species) Cell(com.ferreusveritas.dynamictrees.util.SimpleVoxmap.Cell) MapSignal(com.ferreusveritas.dynamictrees.api.network.MapSignal)

Example 2 with NodeCollector

use of com.ferreusveritas.dynamictrees.systems.nodemappers.NodeCollector in project DynamicTrees by DynamicTreesTeam.

the class ChunkTreeHelper method removeOrphanedBranchNodes.

/**
 * Removes floating little bits of tree that have somehow lost
 * connection with their parent root system.
 *
 * @param world The world
 * @param cPos The chunk position where the effect is intended
 * @param radius Radius of effect in chunk width units
 */
public static void removeOrphanedBranchNodes(World world, ChunkPos cPos, int radius) {
    if (cPos == null) {
        // Who would be so unkind?
        throw new NullPointerException("Null Chunk Position");
    }
    // This is used to track branches that are already proven
    Set<BlockPos> found = new HashSet<>();
    BlockBounds bounds = getEffectiveBlockBounds(world, cPos, radius);
    for (MutableBlockPos pos : bounds.iterate()) {
        if (found.contains(pos)) {
            // Block was already proven to be part of a valid tree structure
            continue;
        }
        // Test if there's a branch block at this position
        IBlockState state = world.getBlockState(pos);
        Optional<BlockBranch> branchBlock = TreeHelper.getBranchOpt(state);
        if (!branchBlock.isPresent()) {
            // No branch block found at this position.  Move on
            continue;
        }
        // Test if the branch has a root node attached to it
        BlockPos rootPos = TreeHelper.findRootNode(world, pos);
        if (rootPos == BlockPos.ORIGIN) {
            // If the root position is the ORIGIN object it means that no root block was found
            // If the root node isn't found then all nodes are orphan.  Destroy the entire network.
            doTreeDestroy(world, branchBlock, rootPos);
            continue;
        }
        // There is at least one root block in the network
        IBlockState rootyState = world.getBlockState(rootPos);
        Optional<BlockRooty> rootyBlock = TreeHelper.getRootyOpt(rootyState);
        if (!rootyBlock.isPresent()) {
            // This theoretically shouldn't ever happen
            continue;
        }
        // Rooty block confirmed, build details about the trunk coming out of it
        EnumFacing trunkDir = rootyBlock.get().getTrunkDirection(world, rootPos);
        BlockPos trunkPos = rootPos.offset(trunkDir);
        IBlockState trunkState = world.getBlockState(trunkPos);
        Optional<BlockBranch> trunk = TreeHelper.getBranchOpt(trunkState);
        if (!trunk.isPresent()) {
            // This theoretically shouldn't ever happen
            continue;
        }
        // There's a trunk coming out of the rooty block, that's kinda expected.  But is it the only rooty block in the network?
        MapSignal signal = new MapSignal();
        signal.destroyLoopedNodes = false;
        trunk.get().analyse(trunkState, world, trunkPos, null, signal);
        if (signal.multiroot || signal.overflow) {
            // We found multiple root nodes.  This can't be resolved. Destroy the entire network
            doTreeDestroy(world, branchBlock, pos);
            continue;
        } else {
            // Tree appears healthy with only a single attached root block
            trunk.get().analyse(trunkState, world, trunkPos, null, new MapSignal(new NodeCollector(found)));
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) EnumFacing(net.minecraft.util.EnumFacing) BlockRooty(com.ferreusveritas.dynamictrees.blocks.BlockRooty) BlockBranch(com.ferreusveritas.dynamictrees.blocks.BlockBranch) NodeCollector(com.ferreusveritas.dynamictrees.systems.nodemappers.NodeCollector) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) MutableBlockPos(net.minecraft.util.math.BlockPos.MutableBlockPos) HashSet(java.util.HashSet) MapSignal(com.ferreusveritas.dynamictrees.api.network.MapSignal)

Aggregations

MapSignal (com.ferreusveritas.dynamictrees.api.network.MapSignal)2 BlockBranch (com.ferreusveritas.dynamictrees.blocks.BlockBranch)2 NodeCollector (com.ferreusveritas.dynamictrees.systems.nodemappers.NodeCollector)2 IBlockState (net.minecraft.block.state.IBlockState)2 BlockPos (net.minecraft.util.math.BlockPos)2 MutableBlockPos (net.minecraft.util.math.BlockPos.MutableBlockPos)2 ILeavesProperties (com.ferreusveritas.dynamictrees.api.treedata.ILeavesProperties)1 BlockDynamicLeaves (com.ferreusveritas.dynamictrees.blocks.BlockDynamicLeaves)1 BlockRooty (com.ferreusveritas.dynamictrees.blocks.BlockRooty)1 Species (com.ferreusveritas.dynamictrees.trees.Species)1 TreeFamily (com.ferreusveritas.dynamictrees.trees.TreeFamily)1 SimpleVoxmap (com.ferreusveritas.dynamictrees.util.SimpleVoxmap)1 Cell (com.ferreusveritas.dynamictrees.util.SimpleVoxmap.Cell)1 HashSet (java.util.HashSet)1 EnumFacing (net.minecraft.util.EnumFacing)1