有了地形接下来该生成结构和建筑了,由于这部分代码很长,本篇只研究生成洞穴
完整的provideChunk:
public Chunk provideChunk(int x, int z)
{
this.rand.setSeed((long)x * 341873128712L + (long)z * 132897987541L);
ChunkPrimer chunkprimer = new ChunkPrimer();
// 生成基本地形(没有洞穴等建筑)
this.setBlocksInChunk(x, z, chunkprimer);
// 生成16*16的生物群系数组
this.biomesForGeneration = this.worldObj.getWorldChunkManager().loadBlockGeneratorData(this.biomesForGeneration, x * 16, z * 16, 16, 16);
// 把石头替换成生物群系相应的方块
this.replaceBlocksForBiome(x, z, chunkprimer, this.biomesForGeneration);
// 生成洞穴、村庄、要塞等结构和建筑
if (this.settings.useCaves)
{
this.caveGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useRavines)
{
this.ravineGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useMineShafts && this.mapFeaturesEnabled)
{
this.mineshaftGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useVillages && this.mapFeaturesEnabled)
{
this.villageGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useStrongholds && this.mapFeaturesEnabled)
{
this.strongholdGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useTemples && this.mapFeaturesEnabled)
{
this.scatteredFeatureGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
if (this.settings.useMonuments && this.mapFeaturesEnabled)
{
this.oceanMonumentGenerator.generate(this, this.worldObj, x, z, chunkprimer);
}
// 转为Chunk对象
Chunk chunk = new Chunk(this.worldObj, chunkprimer, x, z);
byte[] abyte = chunk.getBiomeArray();
// 设置生物群系ID
for (int i = 0; i < abyte.length; ++i)
{
abyte[i] = (byte)this.biomesForGeneration[i].biomeID;
}
// 生成光照图,这里不讨论
chunk.generateSkylightMap();
return chunk;
}
生成洞穴
类名net.minecraft.world.gen.MapGenBase
public void generate(IChunkProvider chunkProviderIn, World worldIn, int chunkX, int chunkZ, ChunkPrimer chunkPrimerIn)
{
int range = this.range;
this.worldObj = worldIn;
this.rand.setSeed(worldIn.getSeed());
long rand1 = this.rand.nextLong();
long rand2 = this.rand.nextLong();
// 遍历周围(range*2+1)*(range*2+1)的区块,默认range=8
for (int x = chunkX - range; x <= chunkX + range; ++x)
{
for (int z = chunkZ - range; z <= chunkZ + range; ++z)
{
long j1 = (long)x * rand1;
long k1 = (long)z * rand2;
this.rand.setSeed(j1 ^ k1 ^ worldIn.getSeed());
// 这个由子类实现
this.recursiveGenerate(worldIn, x, z, chunkX, chunkZ, chunkPrimerIn);
}
}
}
类名net.minecraft.world.gen.MapGenCaves
public class MapGenCaves extends MapGenBase
{
// 挖洞,默认参数
protected void