Search in sources :

Example 1 with NBTStorable

use of ivorius.reccomplex.nbt.NBTStorable in project RecurrentComplex by Ivorforce.

the class WorldGenMaze method place.

@Nullable
public static PlacedStructure place(Random random, Environment environment, BlockPos shift, int[] roomSize, PlacedMazeComponent<MazeComponentStructure<Connector>, Connector> placedComponent, BlockPos pos, AxisAlignedTransform2D transform) {
    MazeComponentStructure<Connector> componentInfo = placedComponent.component();
    Structure<?> structure = StructureRegistry.INSTANCE.get(componentInfo.structureID);
    if (structure == null) {
        RecurrentComplex.logger.error(String.format("Could not find structure '%s' for maze", componentInfo.structureID));
        return null;
    }
    BlockPos compLowerPos = getBoundingBox(roomSize, placedComponent, structure, componentInfo.transform).add(shift);
    NBTStorable instanceData = new StructureGenerator<>(structure).seed(random.nextLong()).environment(environment.copy(componentInfo.variableDomain)).transform(Transforms.apply(componentInfo.transform, transform)).lowerCoord(lowerCoord(structure, compLowerPos, componentInfo.transform, pos, transform)).structureID(componentInfo.structureID).instanceData().orElse(null);
    return new PlacedStructure(componentInfo.structureID, componentInfo.structureID, componentInfo.transform, componentInfo.variableDomain, compLowerPos, instanceData.writeToNBT());
}
Also used : BlockPos(net.minecraft.util.math.BlockPos) NBTStorable(ivorius.reccomplex.nbt.NBTStorable) Nullable(javax.annotation.Nullable)

Example 2 with NBTStorable

use of ivorius.reccomplex.nbt.NBTStorable in project RecurrentComplex by Ivorforce.

the class GenericStructure method generate.

@Override
public void generate(@Nonnull final StructureSpawnContext context, @Nonnull InstanceData instanceData, @Nonnull TransformerMulti foreignTransformer) {
    WorldServer world = context.environment.world;
    IvWorldData worldData = constructWorldData();
    boolean asSource = context.generateAsSource;
    RunTransformer transformer = getRunTransformer(instanceData, foreignTransformer, asSource);
    instanceData.variableDomain.fill(context.environment.variables);
    // The world initializes the block event array after it generates the world - in the constructor
    // This hackily sets the field to a temporary value. Yay.
    // Hax
    RCAccessorWorldServer.ensureBlockEventArray(world);
    IvBlockCollection blockCollection = worldData.blockCollection;
    int[] areaSize = new int[] { blockCollection.width, blockCollection.height, blockCollection.length };
    BlockPos origin = StructureBoundingBoxes.min(context.boundingBox);
    Map<BlockPos, GeneratingTileEntity<?>> generatingTEs = new HashMap<>();
    Map<BlockPos, NBTTagCompound> tileEntityCompounds = new HashMap<>();
    for (NBTTagCompound tileEntityCompound : worldData.tileEntities) {
        BlockPos src = RCMover.getTileEntityPos(tileEntityCompound);
        tileEntityCompounds.put(src, tileEntityCompound);
        asGeneratingTileEntity(context, world, areaSize, origin, tileEntityCompound, generatingTEs::put);
    }
    if (transformer != null)
        transformer.transformer.transform(transformer.instanceData, Transformer.Phase.BEFORE, context, worldData, transformer);
    StructureBoundingBox relevantSourceArea = context.intersection(BlockAreas.toBoundingBox(blockCollection.area()));
    if (// Why did we get asked to generate again?
    relevantSourceArea != null) {
        BlockPos.MutableBlockPos worldPos = new BlockPos.MutableBlockPos();
        for (int pass = 0; pass < 2; pass++) {
            for (BlockPos sourcePos : RCStructureBoundingBoxes.mutablePositions(relevantSourceArea)) {
                IvMutableBlockPos.add(context.transform.applyOn(sourcePos, worldPos, areaSize), origin);
                if (context.includesComplex(worldPos)) {
                    IBlockState state = PosTransformer.transformBlockState(blockCollection.getBlockState(sourcePos), context.transform);
                    if (RecurrentComplex.specialRegistry.isSafe(state.getBlock()) && pass == getPass(state) && (transformer == null || !transformer.transformer.skipGeneration(transformer.instanceData, context, worldPos, state, worldData, sourcePos))) {
                        GeneratingTileEntity<?> generatingTE = generatingTEs.get(sourcePos);
                        //noinspection unchecked
                        if (asSource || generatingTE == null || ((GeneratingTileEntity) generatingTE).shouldPlaceInWorld(context, instanceData.tileEntities.get(sourcePos)))
                            setBlock(context, areaSize, worldPos, state, () -> tileEntityCompounds.get(sourcePos));
                        else
                            // Replace with air
                            context.setBlock(worldPos, Blocks.AIR.getDefaultState(), 2);
                    }
                }
            }
        }
    }
    if (transformer != null)
        transformer.transformer.transform(transformer.instanceData, Transformer.Phase.AFTER, context, worldData, transformer);
    for (NBTTagCompound entityCompound : worldData.entities) {
        double[] transformedEntityPos = context.transform.applyOn(getEntityPos(entityCompound), areaSize);
        if (context.includes(new Vec3i(transformedEntityPos[0] + origin.getX(), transformedEntityPos[1] + origin.getY(), transformedEntityPos[2] + origin.getZ()))) {
            Entity entity = EntityList.createEntityFromNBT(entityCompound, world);
            if (entity != null) {
                PosTransformer.transformEntityPos(entity, context.transform, areaSize);
                Mover.moveEntity(entity, origin);
                RCAccessorEntity.setEntityUniqueID(entity, UUID.randomUUID());
                generateEntityContents(context, entity);
                world.spawnEntity(entity);
            } else {
                RecurrentComplex.logger.error("Error loading entity with ID '" + entityCompound.getString("id") + "'");
            }
        }
    }
    if (!asSource) {
        if (context.generationLayer < MAX_GENERATING_LAYERS) {
            generatingTEs.entrySet().forEach(entry -> {
                NBTStorable teData = instanceData.tileEntities.get(entry.getKey());
                if (teData != null)
                    ((GeneratingTileEntity) entry.getValue()).generate(context, teData);
            });
        } else {
            RecurrentComplex.logger.warn("Structure generated with over " + MAX_GENERATING_LAYERS + " layers; most likely infinite loop!");
        }
    }
}
Also used : Vec3i(net.minecraft.util.math.Vec3i) GeneratingTileEntity(ivorius.reccomplex.block.GeneratingTileEntity) RCAccessorEntity(ivorius.reccomplex.utils.accessor.RCAccessorEntity) Entity(net.minecraft.entity.Entity) TileEntity(net.minecraft.tileentity.TileEntity) StructureBoundingBox(net.minecraft.world.gen.structure.StructureBoundingBox) IBlockState(net.minecraft.block.state.IBlockState) IvWorldData(ivorius.ivtoolkit.tools.IvWorldData) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) WorldServer(net.minecraft.world.WorldServer) RCAccessorWorldServer(ivorius.reccomplex.utils.accessor.RCAccessorWorldServer) IvBlockCollection(ivorius.ivtoolkit.blocks.IvBlockCollection) RunTransformer(ivorius.reccomplex.world.gen.feature.structure.generic.transformers.RunTransformer) IvMutableBlockPos(ivorius.ivtoolkit.blocks.IvMutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) NBTStorable(ivorius.reccomplex.nbt.NBTStorable) GeneratingTileEntity(ivorius.reccomplex.block.GeneratingTileEntity) IvMutableBlockPos(ivorius.ivtoolkit.blocks.IvMutableBlockPos)

Example 3 with NBTStorable

use of ivorius.reccomplex.nbt.NBTStorable in project RecurrentComplex by Ivorforce.

the class GenericStructure method prepareInstanceData.

@Nullable
@Override
public InstanceData prepareInstanceData(@Nonnull StructurePrepareContext context, @Nonnull TransformerMulti foreignTransformer) {
    InstanceData instanceData = new InstanceData();
    if (!context.generateAsSource) {
        IvWorldData worldData = constructWorldData();
        IvBlockCollection blockCollection = worldData.blockCollection;
        int[] areaSize = new int[] { blockCollection.width, blockCollection.height, blockCollection.length };
        BlockPos origin = StructureBoundingBoxes.min(context.boundingBox);
        variableDomain.fill(instanceData.variableDomain, context.environment, context.random);
        instanceData.variableDomain.fill(context.environment.variables);
        instanceData.transformerData = this.transformer.prepareInstanceData(context, worldData);
        instanceData.foreignTransformerData = foreignTransformer.prepareInstanceData(context, worldData);
        TransformerMulti transformer = TransformerMulti.fuse(Arrays.asList(this.transformer, foreignTransformer));
        TransformerMulti.InstanceData cInstanceData = transformer.fuseDatas(Arrays.asList(instanceData.transformerData, instanceData.foreignTransformerData));
        RunTransformer runTransformer = new RunTransformer(transformer, cInstanceData);
        transformer.configureInstanceData(cInstanceData, context, worldData, runTransformer);
        if (context.generateMaturity.isSuggest() && !transformer.mayGenerate(cInstanceData, context, worldData, runTransformer))
            return null;
        worldData.tileEntities.forEach(tileEntityCompound -> {
            asGeneratingTileEntity(context, IvTileEntityHelper.getAnyWorld(), areaSize, origin, tileEntityCompound, (src, tileEntity) -> {
                NBTStorable tileEntityInstanceData = (NBTStorable) tileEntity.prepareInstanceData(context);
                if (tileEntityInstanceData != null)
                    instanceData.tileEntities.put(src, tileEntityInstanceData);
            });
        });
    }
    return instanceData;
}
Also used : RunTransformer(ivorius.reccomplex.world.gen.feature.structure.generic.transformers.RunTransformer) IvWorldData(ivorius.ivtoolkit.tools.IvWorldData) TransformerMulti(ivorius.reccomplex.world.gen.feature.structure.generic.transformers.TransformerMulti) IvMutableBlockPos(ivorius.ivtoolkit.blocks.IvMutableBlockPos) BlockPos(net.minecraft.util.math.BlockPos) NBTStorable(ivorius.reccomplex.nbt.NBTStorable) IvBlockCollection(ivorius.ivtoolkit.blocks.IvBlockCollection) Nullable(javax.annotation.Nullable)

Example 4 with NBTStorable

use of ivorius.reccomplex.nbt.NBTStorable in project RecurrentComplex by Ivorforce.

the class GenericVillagePiece method prepare.

public void prepare(WorldServer world) {
    Structure<?> structure = StructureRegistry.INSTANCE.get(structureID);
    if (structure == null)
        return;
    GenerationType generationType = structure.generationType(generationID);
    if (!(generationType instanceof VanillaGeneration))
        return;
    VanillaGeneration vanillaGenInfo = (VanillaGeneration) generationType;
    AxisAlignedTransform2D transform = getTransform(vanillaGenInfo.front, mirrorX, getCoordBaseMode().getOpposite());
    instanceData = new StructureGenerator<>(structure).seed(seed).environment(environment(world, generationType)).transform(transform).boundingBox(boundingBox).instanceData().map(NBTStorable::writeToNBT).orElse(null);
}
Also used : StructureGenerator(ivorius.reccomplex.world.gen.feature.StructureGenerator) AxisAlignedTransform2D(ivorius.ivtoolkit.math.AxisAlignedTransform2D) NBTStorable(ivorius.reccomplex.nbt.NBTStorable) VanillaGeneration(ivorius.reccomplex.world.gen.feature.structure.generic.generation.VanillaGeneration) GenerationType(ivorius.reccomplex.world.gen.feature.structure.generic.generation.GenerationType)

Aggregations

NBTStorable (ivorius.reccomplex.nbt.NBTStorable)4 BlockPos (net.minecraft.util.math.BlockPos)3 IvBlockCollection (ivorius.ivtoolkit.blocks.IvBlockCollection)2 IvMutableBlockPos (ivorius.ivtoolkit.blocks.IvMutableBlockPos)2 IvWorldData (ivorius.ivtoolkit.tools.IvWorldData)2 RunTransformer (ivorius.reccomplex.world.gen.feature.structure.generic.transformers.RunTransformer)2 Nullable (javax.annotation.Nullable)2 AxisAlignedTransform2D (ivorius.ivtoolkit.math.AxisAlignedTransform2D)1 GeneratingTileEntity (ivorius.reccomplex.block.GeneratingTileEntity)1 RCAccessorEntity (ivorius.reccomplex.utils.accessor.RCAccessorEntity)1 RCAccessorWorldServer (ivorius.reccomplex.utils.accessor.RCAccessorWorldServer)1 StructureGenerator (ivorius.reccomplex.world.gen.feature.StructureGenerator)1 GenerationType (ivorius.reccomplex.world.gen.feature.structure.generic.generation.GenerationType)1 VanillaGeneration (ivorius.reccomplex.world.gen.feature.structure.generic.generation.VanillaGeneration)1 TransformerMulti (ivorius.reccomplex.world.gen.feature.structure.generic.transformers.TransformerMulti)1 IBlockState (net.minecraft.block.state.IBlockState)1 Entity (net.minecraft.entity.Entity)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 TileEntity (net.minecraft.tileentity.TileEntity)1 Vec3i (net.minecraft.util.math.Vec3i)1