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;
}
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));
}
Aggregations