use of ivorius.reccomplex.world.gen.feature.structure.Environment 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.getStructureGenerations(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.Environment in project RecurrentComplex by Ivorforce.
the class WorldScriptMazeGenerator method getPlacedRooms.
public List<PlacedMazeComponent<MazeComponentStructure<Connector>, Connector>> getPlacedRooms(Random random, AxisAlignedTransform2D transform, Environment environment) {
if (mazeComponent.rooms.isEmpty())
return null;
ConnectorFactory factory = new ConnectorFactory();
Connector wallConnector = factory.get(ConnectorStrategy.DEFAULT_WALL);
Connector defaultConnector = mazeComponent.defaultConnector.toConnector(factory);
// TODO Make configurable
Set<Connector> blockedConnections = Collections.singleton(wallConnector);
int[] boundsHigher = mazeComponent.rooms.boundsHigher();
int[] boundsLower = mazeComponent.rooms.boundsLower();
int[] oneArray = new int[boundsHigher.length];
Arrays.fill(oneArray, 1);
final int[] outsideBoundsHigher = IvVecMathHelper.add(boundsHigher, oneArray);
final int[] outsideBoundsLower = IvVecMathHelper.sub(boundsLower, oneArray);
List<MazeComponentStructure<Connector>> transformedComponents = StructureRegistry.INSTANCE.getStructuresInMaze(mazeID).flatMap(pair -> pair.getLeft().declaredVariables().omega(environment, true).flatMap(domain -> WorldGenMaze.transforms(pair.getLeft(), pair.getRight(), transform, factory, environment.copy(domain), blockedConnections))).collect(Collectors.toList());
MorphingMazeComponent<Connector> maze = new SetMazeComponent<>();
WorldScriptMazeGenerator.enclose(maze, new MazeRoom(outsideBoundsLower), new MazeRoom(outsideBoundsHigher), defaultConnector);
WorldScriptMazeGenerator.blockRooms(maze, mazeComponent.rooms.compile(false).keySet(), defaultConnector);
WorldGenMaze.buildExitPaths(environment, factory, mazeComponent.exitPaths, maze.rooms()).forEach(path -> maze.exits().put(path.getKey(), path.getValue()));
// Add reachability outside the maze
maze.reachability().putAll(WorldGenMaze.addExternalReachability(ImmutableListMultimap.builder(), maze.exits(), blockedConnections).build());
// Add reachability between exits
maze.reachability().putAll(mazeComponent.reachability.build(ImmutableListMultimap.builder(), AxisAlignedTransform2D.ORIGINAL, mazeComponent.boundsSize(), SavedMazeReachability.notBlocked(blockedConnections, maze.exits()), maze.exits().keySet()).build());
ConnectorStrategy connectorStrategy = new ConnectorStrategy();
List<MazePredicate<Connector>> predicates = rules.stream().map(r -> r.build(this, blockedConnections, factory, transformedComponents, connectorStrategy)).filter(Objects::nonNull).collect(Collectors.toCollection(ArrayList::new));
predicates.add(new LimitAABBStrategy<>(outsideBoundsHigher));
predicates.add(new BlockedConnectorStrategy<>(blockedConnections));
int totalRooms = mazeComponent.rooms.compile(true).size();
return MazeComponentConnector.connect(maze, transformedComponents, connectorStrategy, new MazePredicateMany<>(predicates), random, RCConfig.mazePlacementReversesPerRoom >= 0 ? MathHelper.floor(totalRooms * RCConfig.mazePlacementReversesPerRoom + 0.5f) : MazeComponentConnector.INFINITE_REVERSES);
}
use of ivorius.reccomplex.world.gen.feature.structure.Environment in project RecurrentComplex by Ivorforce.
the class TransformerAbstractCloud method buildCloud.
public TObjectDoubleMap<BlockPos> buildCloud(S instanceData, IvWorldData worldData, StructurePrepareContext context, TransformerMulti transformer, TransformerMulti.InstanceData transformerInstanceData) {
Random random = context.random;
Environment environment = context.environment;
BlockPos lowerCoord = StructureBoundingBoxes.min(context.boundingBox);
int[] strucSize = new int[] { worldData.blockCollection.width, worldData.blockCollection.height, worldData.blockCollection.length };
TObjectDoubleMap<BlockPos> cloud = new TObjectDoubleHashMap<>();
BlurredValueField blurredValueField = new BlurredValueField(strucSize);
int gridCoords = 1;
for (int d : strucSize) gridCoords *= d;
int values = MathHelper.floor(gridCoords * (1f / 25f) + 0.5f);
for (int i = 0; i < values; i++) blurredValueField.addValue(1 + (random.nextFloat() - random.nextFloat()) * (float) cloudExpansionRandomization() / 100f, random);
BlockAreas.mutablePositions(worldData.blockCollection.area()).forEach(pos -> {
IBlockState state = worldData.blockCollection.getBlockState(pos);
BlockPos worldCoord = context.transform.apply(pos, strucSize).add(lowerCoord);
if (matches(instanceData, state) && canPenetrate(environment, worldData, worldCoord, 1, transformer, transformerInstanceData))
cloud.put(pos.toImmutable(), 1);
});
double expansionDistance = cloudExpansionDistance();
BlockPos.MutableBlockPos sidePos = new BlockPos.MutableBlockPos();
BlockPos.MutableBlockPos sideWorldCoord = new BlockPos.MutableBlockPos();
if (expansionDistance > 0.000001) {
// The code below will be called _often_, so let's cache the divisions
double[] sideFalloffs = new double[6];
List<EnumFacing> checkSides = new ArrayList<>();
for (EnumFacing side : EnumFacing.values()) {
double sideExpansionDistance = cloudExpansionDistance(side);
double sideFalloff = sideExpansionDistance > 0.000001 ? 1.0f / sideExpansionDistance / expansionDistance : 0;
if (sideFalloff > 0) {
checkSides.add(side);
sideFalloffs[side.getIndex()] = sideFalloff;
}
}
visitRecursively(Sets.newHashSet(cloud.keySet()), (changed, pos) -> {
double density = cloud.get(pos);
for (EnumFacing side : checkSides) {
double sideFalloff = sideFalloffs[side.getIndex()];
IvMutableBlockPos.offset(pos, sidePos, side);
double sideDensity = density - sideFalloff * blurredValueField.getValue(sidePos.getX(), sidePos.getY(), sidePos.getZ());
if (sideDensity <= 0 || cloud.get(sidePos) >= sideDensity - 0.00001)
continue;
IvMutableBlockPos.add(context.transform.applyOn(sidePos, sideWorldCoord, strucSize), lowerCoord);
if (!canPenetrate(environment, worldData, sideWorldCoord, sideDensity, transformer, transformerInstanceData))
continue;
BlockPos immutableSidePos = sidePos.toImmutable();
cloud.put(immutableSidePos, sideDensity);
changed.add(immutableSidePos);
}
return true;
});
}
return cloud;
}
Aggregations