Search in sources :

Example 1 with SaplingGeneration

use of ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration in project RecurrentComplex by Ivorforce.

the class RCSaplingGenerator method findRandomSapling.

@Nullable
public static Pair<Structure<?>, SaplingGeneration> findRandomSapling(WorldServer world, BlockPos pos, Random random, boolean considerVanilla) {
    Environment baseEnv = Environment.inNature(world, new StructureBoundingBox(pos, pos));
    List<Pair<Structure<?>, SaplingGeneration>> applicable = StructureRegistry.INSTANCE.getGenerationTypes(SaplingGeneration.class).stream().filter(pair1 -> pair1.getRight().generatesIn(baseEnv.withGeneration(pair1.getRight()))).collect(Collectors.toCollection(ArrayList::new));
    // Hackily consider big vanilla trees too
    int vanillaComplexity = complexity(world, pos, random, predictors);
    ImmutableMultimap<Integer, Pair<Structure<?>, SaplingGeneration>> groups = IvFunctions.groupMap(applicable, pair -> pair.getRight().pattern.pattern.compile(true).size());
    List<Integer> complexities = Lists.newArrayList(groups.keySet());
    if (vanillaComplexity > 0)
        complexities.add(vanillaComplexity);
    Collections.sort(complexities);
    Pair<Structure<?>, SaplingGeneration> pair = null;
    while (complexities.size() > 0 && pair == null) {
        Integer complexity = complexities.remove(complexities.size() - 1);
        Set<Pair<Structure<?>, SaplingGeneration>> placeable = groups.get(complexity).stream().filter(p -> p.getRight().pattern.canPlace(world, pos, p.getLeft().size(), p.getLeft().isRotatable(), p.getLeft().isMirrorable())).collect(Collectors.toSet());
        double totalWeight = placeable.stream().mapToDouble(RCSaplingGenerator::getSpawnWeight).sum();
        if (complexity == vanillaComplexity && considerVanilla) {
            if (random.nextDouble() * (totalWeight * RCConfig.baseSaplingSpawnWeight + 1) < 1)
                break;
        }
        if (totalWeight > 0)
            pair = WeightedSelector.select(random, placeable, RCSaplingGenerator::getSpawnWeight);
    }
    return pair;
}
Also used : BlockSurfacePos(ivorius.ivtoolkit.blocks.BlockSurfacePos) java.util(java.util) Blocks(net.minecraft.init.Blocks) AxisAlignedTransform2D(ivorius.ivtoolkit.math.AxisAlignedTransform2D) World(net.minecraft.world.World) Structure(ivorius.reccomplex.world.gen.feature.structure.Structure) StructureSpawnContext(ivorius.reccomplex.world.gen.feature.structure.context.StructureSpawnContext) StructureRegistry(ivorius.reccomplex.world.gen.feature.structure.StructureRegistry) StructureGenerator(ivorius.reccomplex.world.gen.feature.StructureGenerator) BlockPos(net.minecraft.util.math.BlockPos) Multimap(com.google.common.collect.Multimap) WeightedSelector(ivorius.ivtoolkit.random.WeightedSelector) RCConfig(ivorius.reccomplex.RCConfig) Collectors(java.util.stream.Collectors) Environment(ivorius.reccomplex.world.gen.feature.structure.Environment) IBlockState(net.minecraft.block.state.IBlockState) IvFunctions(ivorius.ivtoolkit.util.IvFunctions) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) WorldServer(net.minecraft.world.WorldServer) StructureBoundingBox(net.minecraft.world.gen.structure.StructureBoundingBox) Nullable(javax.annotation.Nullable) SaplingGeneration(ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration) StructureBoundingBox(net.minecraft.world.gen.structure.StructureBoundingBox) SaplingGeneration(ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration) Environment(ivorius.reccomplex.world.gen.feature.structure.Environment) Structure(ivorius.reccomplex.world.gen.feature.structure.Structure) Pair(org.apache.commons.lang3.tuple.Pair) Nullable(javax.annotation.Nullable)

Example 2 with SaplingGeneration

use of ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration in project RecurrentComplex by Ivorforce.

the class RCSaplingGenerator method growSapling.

public static void growSapling(WorldServer world, BlockPos pos, Random random, Structure<?> structure, SaplingGeneration saplingGenInfo) {
    int[] strucSize = structure.size();
    Multimap<AxisAlignedTransform2D, BlockPos> placeables = saplingGenInfo.pattern.testAll(world, pos, strucSize, structure.isRotatable(), structure.isMirrorable());
    // Use keys() here to get the correct distribution
    AxisAlignedTransform2D transform = Lists.newArrayList(placeables.keys()).get(random.nextInt(placeables.keys().size()));
    Collection<BlockPos> transformedPositions = placeables.get(transform);
    BlockPos startPos = Lists.newArrayList(transformedPositions).get(random.nextInt(transformedPositions.size()));
    Map<BlockPos, IBlockState> before = new HashMap<>();
    IBlockState air = Blocks.AIR.getDefaultState();
    saplingGenInfo.pattern.copy(transform, strucSize).forEach(i -> i.delete, (p, i) -> {
        BlockPos ePos = p.add(startPos);
        before.put(ePos, world.getBlockState(ePos));
        world.setBlockState(ePos, air, 4);
    });
    BlockPos spawnPos = transform.apply(saplingGenInfo.spawnShift, new int[] { 1, 1, 1 }).add(startPos);
    boolean success = new StructureGenerator<>(structure).world(world).generationInfo(saplingGenInfo).transform(transform).seed(random.nextLong()).maturity(StructureSpawnContext.GenerateMaturity.SUGGEST).memorize(RCConfig.memorizeSaplings).allowOverlaps(true).randomPosition(BlockSurfacePos.from(spawnPos), (context, blockCollection) -> spawnPos.getY()).generate().succeeded();
    if (!success)
        before.forEach((pos1, state) -> world.setBlockState(pos1, state, 4));
}
Also used : BlockSurfacePos(ivorius.ivtoolkit.blocks.BlockSurfacePos) java.util(java.util) Blocks(net.minecraft.init.Blocks) AxisAlignedTransform2D(ivorius.ivtoolkit.math.AxisAlignedTransform2D) World(net.minecraft.world.World) Structure(ivorius.reccomplex.world.gen.feature.structure.Structure) StructureSpawnContext(ivorius.reccomplex.world.gen.feature.structure.context.StructureSpawnContext) StructureRegistry(ivorius.reccomplex.world.gen.feature.structure.StructureRegistry) StructureGenerator(ivorius.reccomplex.world.gen.feature.StructureGenerator) BlockPos(net.minecraft.util.math.BlockPos) Multimap(com.google.common.collect.Multimap) WeightedSelector(ivorius.ivtoolkit.random.WeightedSelector) RCConfig(ivorius.reccomplex.RCConfig) Collectors(java.util.stream.Collectors) Environment(ivorius.reccomplex.world.gen.feature.structure.Environment) IBlockState(net.minecraft.block.state.IBlockState) IvFunctions(ivorius.ivtoolkit.util.IvFunctions) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) WorldServer(net.minecraft.world.WorldServer) StructureBoundingBox(net.minecraft.world.gen.structure.StructureBoundingBox) Nullable(javax.annotation.Nullable) SaplingGeneration(ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration) IBlockState(net.minecraft.block.state.IBlockState) StructureGenerator(ivorius.reccomplex.world.gen.feature.StructureGenerator) AxisAlignedTransform2D(ivorius.ivtoolkit.math.AxisAlignedTransform2D) BlockPos(net.minecraft.util.math.BlockPos)

Aggregations

ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 Lists (com.google.common.collect.Lists)2 Multimap (com.google.common.collect.Multimap)2 BlockSurfacePos (ivorius.ivtoolkit.blocks.BlockSurfacePos)2 AxisAlignedTransform2D (ivorius.ivtoolkit.math.AxisAlignedTransform2D)2 WeightedSelector (ivorius.ivtoolkit.random.WeightedSelector)2 IvFunctions (ivorius.ivtoolkit.util.IvFunctions)2 RCConfig (ivorius.reccomplex.RCConfig)2 StructureGenerator (ivorius.reccomplex.world.gen.feature.StructureGenerator)2 Environment (ivorius.reccomplex.world.gen.feature.structure.Environment)2 Structure (ivorius.reccomplex.world.gen.feature.structure.Structure)2 StructureRegistry (ivorius.reccomplex.world.gen.feature.structure.StructureRegistry)2 StructureSpawnContext (ivorius.reccomplex.world.gen.feature.structure.context.StructureSpawnContext)2 SaplingGeneration (ivorius.reccomplex.world.gen.feature.structure.generic.generation.SaplingGeneration)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 IBlockState (net.minecraft.block.state.IBlockState)2 Blocks (net.minecraft.init.Blocks)2 BlockPos (net.minecraft.util.math.BlockPos)2