Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I've managed to analyse the vanilla cave generation code and identify the significance of almost all variables involved, but I've encountered a bizarre bug that seems to occur no matter what I do (suggesting strongly that there's something outside the cave generation code itself responsible). I've added/removed qualifiers, I've imported cave generation code from other mods almost entirely wholesale, rebuilt from scratch twice, and still the same bug.

 

Normal cave generation:

p2xYXgD.png

My cave generation:

B5Ck3CQ.png


In short, what appears to happen is that instead of generating whole snake-like caves as normal, each chunk uses a seed value so distinct from its neighbours that no generated cave successfully exits the chunk it started in.

import java.util.Random;

import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.chunk.ChunkPrimer;
import net.minecraft.world.gen.MapGenBase;

public class MapGenCavesWater extends MapGenBase
{
    protected void recursiveGenerate(World worldIn, int chunkX, int chunkZ, int originalX, int originalZ, ChunkPrimer chunkPrimerIn)
    {
        int tunnelCount = this.rand.nextInt(this.rand.nextInt(this.rand.nextInt(10) + 1) + 1);
        if(this.rand.nextInt(5) != 0) tunnelCount = 0;
        for(int j = 0; j < tunnelCount; ++j)
        {
            double xPos = (double)(chunkX * 16 + this.rand.nextInt(16));
            double yPos = (double)this.rand.nextInt(255);
            double zPos = (double)(chunkZ * 16 + this.rand.nextInt(16));
            int length = 1;
            
            // 1 in 4 chance of adding a room, increment length
            if(this.rand.nextInt(4) == 0)
            {
                this.addRoom(this.rand.nextLong(), originalX, originalZ, chunkPrimerIn, xPos, yPos, zPos);
                length += this.rand.nextInt(4);
            }
            
            // Generate tunnel to length
            for(int l = 0; l < length; ++l)
            {
                float f = this.rand.nextFloat() * ((float)Math.PI * 2F);
                float f1 = (this.rand.nextFloat() - 0.5F) * 2.0F / 8.0F;
                float f2 = this.rand.nextFloat() * 2.0F + this.rand.nextFloat();
                this.addTunnel(this.rand.nextLong(), originalX, originalZ, chunkPrimerIn, xPos, yPos, zPos, f2 * 2.0F, f, f1, 0, 0, 0.5D);
            }
        }
    }
    
    protected void addRoom(long randSeed, int chunkX, int chunkZ, ChunkPrimer primer, double xPos, double yPos, double zPos)
    {
        this.addTunnel(randSeed, chunkX, chunkZ, primer, xPos, yPos, zPos, 1.0F + this.rand.nextFloat() * 6.0F, 0.0F, 0.0F, -1, -1, 0.5D);
    }
    
    protected void addTunnel(long randSeed, int chunkX, int chunkZ, ChunkPrimer primer, double xPos, double yPos, double zPos, float size, float yaw, float pitch, int par11Int, int par12Int, double heightMap)
    {
        double chunkMidX = (double)(chunkX * 16 + 8);
        double chunkMidZ = (double)(chunkZ * 16 + 8);
        float f = 0.0F;
        float f1 = 0.0F;
        Random random = new Random(randSeed);
        if(par12Int <= 0)
        {
            int i = this.range * 16 - 16;
            par12Int = i - random.nextInt(i / 4);
        }
        
        boolean singleGen = false;
        if (par11Int == -1)
        {
            par11Int = par12Int / 2;
            singleGen = true;
        }
        
        int j = random.nextInt(par12Int / 2) + par12Int / 4;
        for(boolean flag = random.nextInt(6) == 0; par11Int < par12Int; ++par11Int)
        {
            double d2 = 1.5D + (double)(MathHelper.sin((float)par11Int * (float)Math.PI / (float)par12Int) * size);
            double d3 = d2 * heightMap;
            float f2 = MathHelper.cos(pitch);
            float f3 = MathHelper.sin(pitch);
            xPos += (double)(MathHelper.cos(yaw) * f2);
            yPos += (double)f3;
            zPos += (double)(MathHelper.sin(yaw) * f2);
            
            if(flag) pitch = pitch * 0.92F;
            else pitch = pitch * 0.7F;
            
            pitch = pitch + f1 * 0.1F;
            yaw += f * 0.1F;
            f1 = f1 * 0.9F;
            f = f * 0.75F;
            f1 = f1 + (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 2.0F;
            f = f + (random.nextFloat() - random.nextFloat()) * random.nextFloat() * 4.0F;
            
            if(!singleGen && par11Int == j && size > 1.0F)
            {
                this.addTunnel(random.nextLong(), chunkX, chunkZ, primer, xPos, yPos, zPos, random.nextFloat() * 0.5F + 0.5F, yaw - ((float)Math.PI / 2F), pitch / 3.0F, par11Int, par12Int, 1.0D);
                this.addTunnel(random.nextLong(), chunkX, chunkZ, primer, xPos, yPos, zPos, random.nextFloat() * 0.5F + 0.5F, yaw + ((float)Math.PI / 2F), pitch / 3.0F, par11Int, par12Int, 1.0D);
                return;
            }
            
            if(singleGen || random.nextInt(4) != 0)
            {
                double d4 = xPos - chunkMidX;
                double d5 = zPos - chunkMidZ;
                double d6 = (double)(par12Int - par11Int);
                double d7 = (double)(size + 2.0F + 16.0F);
                
                if(d4 * d4 + d5 * d5 - d6 * d6 > d7 * d7) return;
                
                if(
                		xPos >= chunkMidX - 16.0D - d2 * 2.0D && 
                		zPos >= chunkMidZ - 16.0D - d2 * 2.0D && 
                		xPos <= chunkMidX + 16.0D + d2 * 2.0D && 
                		zPos <= chunkMidZ + 16.0D + d2 * 2.0D
                	)
                {
                    int minX = Math.max(0, MathHelper.floor(xPos - d2) - chunkX * 16 - 1);
                    int maxX = Math.min(16, MathHelper.floor(xPos + d2) - chunkX * 16 + 1);
                    
                    int minY = Math.max(0, MathHelper.floor(yPos - d3) - 1);
                    int maxY = Math.min(255, MathHelper.floor(yPos + d3) + 1);
                    
                    int minZ = Math.max(0, MathHelper.floor(zPos - d2) - chunkZ * 16 - 1);
                    int maxZ = Math.min(16, MathHelper.floor(zPos + d2) - chunkZ * 16 + 1);
                    
                    for(int blockX = minX; blockX < maxX; ++blockX)
                    {
                        double d10 = ((double)(blockX + chunkX * 16) + 0.5D - xPos) / d2;
                        for(int blockZ = minZ; blockZ < maxZ; ++blockZ)
                        {
                            double d8 = ((double)(blockZ + chunkZ * 16) + 0.5D - zPos) / d2;
                            for(int blockY = maxY; blockY > minY; --blockY)
                            {
                                double d9 = ((double)(blockY - 1) + 0.5D - yPos) / d3;
                                if(d9 > -0.7D && d10 * d10 + d9 * d9 + d8 * d8 < 1.0D)
                                {
                                    IBlockState blockAtPos = primer.getBlockState(blockX, blockY, blockZ);
                                    if(blockAtPos.getBlock() == Blocks.WATER)
                                    {
                                        primer.setBlockState(blockX, blockY, blockZ, Blocks.SANDSTONE.getDefaultState());
                                    }
                                }
                            }
                        }
                    }
                    if(singleGen) break;
                }
            }
        }
    }
}

At this point my only meaningful guess is that there's something to do with chunk generation order resulting in the seeds being so different, but I can't find anywhere to remedy that.

Edited by Lyinginbedmon

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.