Jump to content

[1.15.2] Structure Gen - Filling air/water blocks underneath structure + other questions


FizixRichard

Recommended Posts

Hey all,

 

I'm currently playing with structure generation in MC 1.15.2 and have largely gotten everything working. My custom structures generate and I can control how they're placed which is cool. However, there are some quirks I'd like to fix but I'm struggling to find answers.

 

The big one is when the structure overhangs surface elevation changes or water (so there are air or water blocks below part of the structure. I've got a method that checks chunk elevation and will only spawn if the elevation is changes within certain bounds, but this still allows for air or water below parts of the structure (unless I tell it to only spawn on a completely flat chunk, which limits spawn possibilities on larger structures). What I would like to do is to fill any air or water blocks underneath the custom structure with a solid block, which I've found code for within some of the vanilla structures, however I cannot get these functions to work within my custom structure piece.

 

When I implement them, they appeared to do nothing, outputting to the console it appears all blocks in the vertical column are air and if I try to replace an air block with sandstone, the generator hangs.

 

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;
    private static IWorld iWorldIn;
    private static int size = 3;

    public static void register()
    {


        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        CE.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(CE.MODID + ":" + pieceLocation), 1)
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );




    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        // Set the world from chunk generator (not sure how else to get world from here)
        iWorldIn = ObfuscationReflectionHelper.getPrivateValue(ChunkGenerator.class, generator, "field_222540_a");

        LOGGER.info("Adding Pieces: " + position);
       
        JigsawManager.addPieces(new ResourceLocation(CE.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece {
    
        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);

                      
            // Fill underside
          	int yStart = 100;
          	int xPos = position.getX();            
            int zPos = position.getZ();            
          
          	int xPosMax = xPos + bounds.getXSize();
            int zPosMax = zPos + bounds.getZSize();
		
            // Tried various ways of getting yStart here, the bounding box returns 0, I fixed to 100 for testing
          
            for(int x = xPos; x < xPosMax; ++x) {
                for(int z = zPos; z < zPosMax; ++z) {
                    //int k = -5;
                    this.replaceAirAndLiquidDownwards(iWorldIn, Blocks.SANDSTONE.getDefaultState(), x,  yStart, z, bounds);
                }
            }


        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);
        }


        /**
         * Replaces air and liquid from given position downwards. Stops when hitting anything else than air or liquid
         */
        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn)
        {            
           int i = this.getXWithOffset(x, z);
           int j = this.getYWithOffset(y);
      		int k = this.getZWithOffset(x, z);
          
          // If I output data to console here, I get 4 blocks inside the bounding box (the structure is 7 high), and everything is air. It appears like the blocks arent set yet???
      		if (boundingboxIn.isVecInside(new BlockPos(i, j, k))) {
         while((worldIn.isAirBlock(new BlockPos(i, j, k)) || worldIn.getBlockState(new BlockPos(i, j, k)).getMaterial().isLiquid()) && j > 1) {
            worldIn.setBlockState(new BlockPos(i, j, k), blockstateIn, 2);
            --j;
         }

      }
        }
     }
  }
}

 

 

Anyone have any idea whats going on and how to get this working?

 

 

My other question is, is it possible to have a structure generate partially underground, for example a house with a basement?

 

 

 

Edited by FizixRichard
Link to comment
Share on other sites

I thought I'd add the full source.

 

Registries:

 

SetupWorldGen

public class setupWorldGen
{
    private static final Logger LOGGER = LogManager.getLogger();


    public static void setupWorldGen()
    {
        for (Biome biome : ForgeRegistries.BIOMES)
        {
            // Add structures ignoring Oceans
            if (!biome.getCategory() == Biome.Category.OCEAN)
            {
                addSurfaceStructure(biome, CeStructures.TEST_STRUCTURE);
            }
        }
    }




    private static void addSurfaceStructure(Biome biome, Structure<NoFeatureConfig> structure) {
        biome.addStructure(structure.withConfiguration(IFeatureConfig.NO_FEATURE_CONFIG));
        biome.addFeature(GenerationStage.Decoration.SURFACE_STRUCTURES, structure.withConfiguration(IFeatureConfig.NO_FEATURE_CONFIG).withPlacement(Placement.NOPE.configure(IPlacementConfig.NO_PLACEMENT_CONFIG)));
    }


}

 

 

CEStructures

public class CeStructures
{
    private static final Logger LOGGER = LogManager.getLogger();


    // Declare structures
    public static final Structure<NoFeatureConfig> TEST_STRUCTURE = create(TestStructure.SHORT_NAME, new TestStructure(NoFeatureConfig::deserialize));


    @SubscribeEvent
    public static void createStructures(RegistryEvent.Register<Feature<?>> event)
    {
        // Register Structure Pieces
        CeStructurePieces.registerPieces();


        // Register Structures
        IForgeRegistry<Feature<?>> registry = event.getRegistry();

        registry.register(TEST_STRUCTURE);

    }


    private static @Nonnull
    <T extends Feature<?>> T create(String name, T feature) {
        feature.setRegistryName(CE.MODID, name.toLowerCase(Locale.ROOT));
        return feature;
    }


}

 

 

CeStructurePieces

public class CeStructurePieces
{

    private static final Logger LOGGER = LogManager.getLogger();


    // Declare Structure Pieces
    public static final IStructurePieceType TEST_STRUCTURE = TestStructurePieces.Piece::new;



    public static void registerPieces()
    {
        // Register The Jigsaw Pieces
        TestStructurePieces.register();



        // Register Structure Pieces
        register(TestStructure.SHORT_NAME, TEST_STRUCTURE);

    }


    @SuppressWarnings("UnusedReturnValue")
    private static @Nonnull
    IStructurePieceType register(String name, IStructurePieceType type) {
        return Registry.register(Registry.STRUCTURE_PIECE, new ResourceLocation(CE.MODID, name.toLowerCase(Locale.ROOT)), type);
    }


}

 

 

Structure and Structure Piece Classes:

 

TestStructure

public class TestStructure extends AbstractStructure
{
    private static final Logger LOGGER = LogManager.getLogger();

    // Properties
    public static final String SHORT_NAME = "test_structure";
    public static final String LONG_NAME = CE.MODID + ":" + SHORT_NAME;
    private static final int SEED_MODIFIER = 165745296;
    private static final int STRUCTURE_SIZE = 3;
    private static final int STRUCTURE_DISTANCE = 10;
    private static final int STRUCTURE_SEPARATION = 5;
    private static final int HEIGHT_DELTA = 3;
    private static final double SPAWN_CHANCE = 0.7;



    public TestStructure(Function<Dynamic<?>, ? extends NoFeatureConfig> configFactoryIn) {
        super(configFactoryIn, SHORT_NAME, STRUCTURE_SIZE, HEIGHT_DELTA);        
    }


    @Override
    protected int getSeedModifier() {
        return SEED_MODIFIER;
    }

    @Override
    public @Nonnull
    String getStructureName() {
        return LONG_NAME;
    }

    @Override
    public int getSize() {
        return STRUCTURE_SIZE;
    }

    @Override
    public int getHeightDelta() {
        return HEIGHT_DELTA;
    }

    @Override
    protected int getFeatureDistance(ChunkGenerator<?> generator) { return STRUCTURE_DISTANCE; }

    @Override
    protected int getFeatureSeparation(ChunkGenerator<?> generator) { return STRUCTURE_SEPARATION; }

    @Override
    protected double getSpawnChance() { return SPAWN_CHANCE; }



    @Override
    public @Nonnull
    IStartFactory getStartFactory() {
        return Start::new;
    }


    public static class Start extends MarginedStructureStart {

        public Start(Structure<?> structure, int chunkX, int chunkZ, MutableBoundingBox bounds, int reference, long seed) {
            super(structure, chunkX, chunkZ, bounds, reference, seed);
        }

        @Override
        public void init(@Nonnull ChunkGenerator<?> chunkGenerator, @Nonnull TemplateManager templateManager, int chunkX, int chunkZ, @Nonnull Biome biome) {
            Rotation rotation = Rotation.values()[this.rand.nextInt(Rotation.values().length)];
            BlockPos position = getSurfaceStructurePosition(chunkGenerator, 3, rotation, chunkX, chunkZ);

            
            TestStructurePieces.create(chunkGenerator, templateManager, position, this.components, this.rand);

            this.recalculateStructureSize();
        }
      
      
      public BlockPos getSurfaceStructurePosition(@Nonnull ChunkGenerator generator, int size, Rotation rotation, int chunkX, int chunkZ) {
            int xOffset = size * 16;
            int zOffset = size * 16;

            int x = (chunkX << 4);
            int z = (chunkZ << 4);

           
            BlockPos adjustedPos = new BlockPos(x, 0, z);

            return adjustedPos;            
        }
    }

}

 

AbstractStruture

public abstract class AbstractStructure extends ScatteredStructure<NoFeatureConfig> {
    private static final Logger LOGGER = LogManager.getLogger();
  
    private final String name;
    private final int size;
    private final int heightdelta;


    public AbstractStructure(Function<Dynamic<?>, ? extends NoFeatureConfig> configFactoryIn, String name, int size, int heightdelta) {
        super(configFactoryIn);
        this.name = name;
        this.size = size;
        this.heightdelta = heightdelta;
    }

    protected abstract double getSpawnChance();

    @Override
    public int getSize() {
        return this.size;
    }


    public int getHeightDelta() {
        return this.heightdelta;
    }

    @Override
    protected int getBiomeFeatureDistance(ChunkGenerator<?> chunkGenerator) {
        return getFeatureDistance(chunkGenerator);
    }

    protected abstract int getFeatureDistance(ChunkGenerator<?> chunkGenerator);

    @Override
    protected int getBiomeFeatureSeparation(ChunkGenerator<?> chunkGenerator) {
        return getFeatureSeparation(chunkGenerator);
    }

    protected abstract int getFeatureSeparation(ChunkGenerator<?> chunkGenerator);


    @Override
    @Nonnull
    public String getStructureName() {
        return new ResourceLocation(CE.MODID, name).toString();
    }


    @Override
    public boolean canBeGenerated(BiomeManager biomeManager, @Nonnull ChunkGenerator<?> chunkGenerator, @Nonnull Random randIn, int chunkX, int chunkZ, @Nonnull Biome biomeIn)
    {

        /* original
        ChunkPos chunkpos = this.getStartPositionForPosition(generatorIn, randIn, chunkX, chunkZ, 0, 0);
      return chunkX == chunkpos.x && chunkZ == chunkpos.z && generatorIn.hasStructure(biomeIn, this);
         */
        

        ChunkPos chunkpos = getStartPositionForPosition(chunkGenerator, randIn, chunkX, chunkZ, 0, 0);

        
        if (chunkX == chunkpos.x && chunkZ == chunkpos.z)
        {
            // Is the surface where we want to generate flat enough
            if (surfaceCheck(chunkGenerator, chunkX, chunkZ))
            {

                
                for (int x = chunkX - getSize(); x <= chunkX + getSize(); x++) {
                    for (int z = chunkZ - getSize(); z <= chunkZ + getSize(); z++)
                    {
                        if (!chunkGenerator.hasStructure(biomeIn, this)) {

                            return false;
                        }
                    }
                }
              

                // Check how far from villages we are            
                for (int x2 = chunkX - 5; x2 <= chunkX + 5; ++x2) {
                    for (int z2 = chunkZ - 5; z2 <= chunkZ + 5; ++z2) {
                        BlockPos position = new BlockPos((x2 << 4) + 9, 0, (z2 << 4) + 9);
                        if (Feature.VILLAGE.canBeGenerated(biomeManager, chunkGenerator, randIn, x2, z2, biomeManager.getBiome(position))) {
                            
                            return false;
                        }
                    }
                }

                // Apply spawn chance               
                int x3 = chunkX >> 4;
                int z3 = chunkZ >> 4;
                randIn.setSeed((long) (x3 ^ z3 << 4) ^ chunkGenerator.getSeed());
                randIn.nextInt();

                Double randVal = randIn.nextDouble();
                boolean chanceCheck = randVal < getSpawnChance();               

                return chanceCheck;

            }
        }

        return false;
    }



    @Override
    @Nonnull
    protected ChunkPos getStartPositionForPosition(ChunkGenerator<?> chunkGenerator, Random random, int x, int z, int spacingOffsetsX, int spacingOffsetsZ)
    {
        
        int featureDistance = this.getBiomeFeatureDistance(chunkGenerator);
        int featureSeparation = this.getBiomeFeatureSeparation(chunkGenerator);
        int xPos = x + featureDistance * spacingOffsetsX;
        int zPos = z + featureDistance * spacingOffsetsZ;
        int i1 = xPos < 0 ? xPos - featureDistance + 1 : xPos;
        int j1 = zPos < 0 ? zPos - featureDistance + 1 : zPos;
        int validChunkX = i1 / featureDistance;
        int validChunkZ = j1 / featureDistance;

        ((SharedSeedRandom) random).setLargeFeatureSeedWithSalt(chunkGenerator.getSeed(), validChunkX, validChunkZ, getSeedModifier());

        validChunkX = validChunkX * featureDistance;
        validChunkZ = validChunkZ * featureDistance;
        validChunkX = validChunkX + random.nextInt(featureDistance - featureSeparation);
        validChunkZ = validChunkZ + random.nextInt(featureDistance - featureSeparation);
        return new ChunkPos(validChunkX, validChunkZ);
    }




    protected boolean surfaceCheck(@Nonnull ChunkGenerator<?> chunkGenerator, int chunkX, int chunkZ)
    {      

        int offset = getSize() * 16;
        int xStart = (chunkX << 4);
        int zStart = (chunkZ << 4);

        
        int c1 = chunkGenerator.func_222531_c(xStart, zStart, Heightmap.Type.WORLD_SURFACE_WG);
        int c2 = chunkGenerator.func_222531_c(xStart, zStart + offset, Heightmap.Type.WORLD_SURFACE_WG);
        int c3 = chunkGenerator.func_222531_c(xStart + offset, zStart, Heightmap.Type.WORLD_SURFACE_WG);
        int c4 = chunkGenerator.func_222531_c(xStart + offset, zStart + offset, Heightmap.Type.WORLD_SURFACE_WG);


        int lowerHeight = Math.min(Math.min(c1, c2), Math.min(c3, c4));
        int upperHeight = Math.max(Math.max(c1, c2), Math.max(c3, c4));

        //LOGGER.info("Area Height Range: " + lowerHeight + " to " + upperHeight);

        boolean isFlat = Math.abs(upperHeight - lowerHeight) <= getHeightDelta();      

        return isFlat;
    }
}

 

 

TestStructurePiece

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;
    private static IWorld iWorldIn;
    private static int size = 3;

    public static void register()
    {


        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        CE.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(CE.MODID + ":" + pieceLocation), 1)
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );




    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        // Set the world from chunk generator (not sure how else to get world from here)
        iWorldIn = ObfuscationReflectionHelper.getPrivateValue(ChunkGenerator.class, generator, "field_222540_a");

        LOGGER.info("Adding Pieces: " + position);
       
        JigsawManager.addPieces(new ResourceLocation(CE.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece {
    
        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);

                      
            // Fill underside
          	int yStart = 100;
          	int xPos = position.getX();            
            int zPos = position.getZ();            
          
          	int xPosMax = xPos + bounds.getXSize();
            int zPosMax = zPos + bounds.getZSize();
		
            // Tried various ways of getting yStart here, the bounding box returns 0, I fixed to 100 for testing
          
            for(int x = xPos; x < xPosMax; ++x) {
                for(int z = zPos; z < zPosMax; ++z) {
                    //int k = -5;
                    this.replaceAirAndLiquidDownwards(iWorldIn, Blocks.SANDSTONE.getDefaultState(), x,  yStart, z, bounds);
                }
            }


        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);
        }


        /**
         * Replaces air and liquid from given position downwards. Stops when hitting anything else than air or liquid
         */
        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn)
        {            
           int i = this.getXWithOffset(x, z);
           int j = this.getYWithOffset(y);
      		int k = this.getZWithOffset(x, z);
          
          // If I output data to console here, I get 4 blocks inside the bounding box (the structure is 7 high), and everything is air. It appears like the blocks arent set yet???
      		if (boundingboxIn.isVecInside(new BlockPos(i, j, k))) {
         while((worldIn.isAirBlock(new BlockPos(i, j, k)) || worldIn.getBlockState(new BlockPos(i, j, k)).getMaterial().isLiquid()) && j > 1) {
            worldIn.setBlockState(new BlockPos(i, j, k), blockstateIn, 2);
            --j;
         }

      }
        }
     }
  }
}

 

 

 

 

 

Link to comment
Share on other sites

Figured this out and I thought I'd update this in case anyone else is trying to figure structure generation stuff out.

 

 

public class TestStructurePieces
{
    private static final Logger LOGGER = LogManager.getLogger();

    private static String pathIn = "test_structure";
    private static String pieceLocation = "test/test_structure";
    private static IStructurePieceType pieceStructureIn = CeStructurePieces.TEST_STRUCTURE;


    public static void register()
    {
        JigsawManager.REGISTRY.register(new JigsawPattern(
                new ResourceLocation(
                        Ce.MODID,
                        pathIn
                ),
                new ResourceLocation("empty"),
                ImmutableList.of(
                                Pair.of(new SingleJigsawPiece(Ce.MODID + ":" + pieceLocation), 1)                         
                                ),
                JigsawPattern.PlacementBehaviour.RIGID)
        );
    }

    public static void create(ChunkGenerator<?> generator, TemplateManager templateManager, BlockPos position, List<StructurePiece> pieces, SharedSeedRandom random) {

        JigsawManager.addPieces(new ResourceLocation(Ce.MODID, pathIn), 7, Piece::new, generator, templateManager, position, pieces, random);
    }




    public static class Piece extends AbstractVillagePiece
    {
        private TemplateManager templateManager;

        public Piece(TemplateManager templateManager, JigsawPiece jigsawPiece, BlockPos position, int groundLevelDelta, Rotation rotation, MutableBoundingBox bounds) {

            super(pieceStructureIn, templateManager, jigsawPiece, position, groundLevelDelta, rotation, bounds);
            this.templateManager = templateManager;
        }

        public Piece(TemplateManager templateManager, CompoundNBT compoundNBT) {
            super(templateManager, compoundNBT, pieceStructureIn);

            this.templateManager = templateManager;
        }


        @Override
        public boolean create(IWorld worldIn, ChunkGenerator<?> chunkGeneratorIn, Random randomIn, MutableBoundingBox mutableBoundingBoxIn, ChunkPos chunkPosIn) {

            BlockPos position = this.getPos();

            

            // Foundation Fill Process
            for(int x = this.boundingBox.minX; x < this.boundingBox.maxX; ++x) {
                for(int z = this.boundingBox.minZ; z < this.boundingBox.maxZ; ++z) {
                    this.replaceAirAndLiquidDownwards(worldIn, Blocks.SANDSTONE.getDefaultState(), x, position.getY() - 1, z, mutableBoundingBoxIn);
                }
            }

            return this.jigsawPiece.place(this.templateManager, worldIn, chunkGeneratorIn, this.pos, this.rotation, mutableBoundingBoxIn, randomIn);
        }


        protected void replaceAirAndLiquidDownwards(IWorld worldIn, BlockState blockstateIn, int x, int y, int z, MutableBoundingBox boundingboxIn) {

            int j = y;

            if (boundingboxIn.isVecInside(new BlockPos(x, j, z)))
            {
                while((worldIn.isAirBlock(new BlockPos(x, j, z)) || worldIn.getBlockState(new BlockPos(x, j, z)).getMaterial().isLiquid()) && j > 1) {
                    worldIn.setBlockState(new BlockPos(x, j, z), blockstateIn, 2);
                    --j;
                }
           }
        }

    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.