88using MineCase . Algorithm ;
99using MineCase . Algorithm . Noise ;
1010using MineCase . Server . World . Biomes ;
11+ using MineCase . Server . World . Layer ;
12+ using MineCase . Server . World . Mine ;
1113using Newtonsoft . Json ;
1214using Orleans ;
1315using Orleans . Concurrency ;
@@ -37,6 +39,8 @@ internal class ChunkGeneratorOverWorldGrain : Grain, IChunkGeneratorOverworld
3739
3840 private Biome [ , ] _biomesForGeneration ; // 10x10 or 16 x 16
3941
42+ private GenLayer _genlayer ;
43+
4044 public override Task OnActivateAsync ( )
4145 {
4246 _densityMap = new float [ 5 , 33 , 5 ] ;
@@ -64,6 +68,10 @@ public override Task OnActivateAsync()
6468 }
6569 }
6670
71+ _biomesForGeneration = new Biome [ 16 , 16 ] ;
72+
73+ _genlayer = GenLayer . InitAllLayer ( seed ) ;
74+
6775 return Task . CompletedTask ;
6876 }
6977
@@ -80,15 +88,29 @@ public async Task<ChunkColumnStorage> Generate(IWorld world, int x, int z, Gener
8088
8189 public async Task GenerateChunk ( IWorld world , ChunkColumnStorage chunk , int x , int z , GeneratorSettings settings )
8290 {
83- // GetBiomesForGeneration(_biomesForGeneration, x * 4 - 2, z * 4 - 2, 10, 10);
8491 // 生物群系生成
85- // 生物群系先暂时初始化成这样,以后修改
86- _biomesForGeneration = new Biome [ 16 , 16 ] ;
92+ // 获取生物群系
93+ int [ , ] biomeIds = _genlayer . GetInts ( x * 16 - 8 , z * 16 - 8 , 32 , 32 ) ;
94+
95+ for ( int i = 0 ; i < 10 ; ++ i )
96+ {
97+ for ( int j = 0 ; j < 10 ; ++ j )
98+ {
99+ _biomesForGeneration [ j , i ] = Biome . GetBiome ( biomeIds [ ( int ) ( 0.861111F * j * 4 ) , ( int ) ( 0.861111F * i * 4 ) ] , settings ) ;
100+ }
101+ }
102+
103+ // 基本地形生成
104+ await GenerateBasicTerrain ( chunk , x , z , settings ) ;
105+
106+ // 获取生物群系
107+ biomeIds = _genlayer . GetInts ( x * 16 , z * 16 , 16 , 16 ) ;
108+
87109 for ( int i = 0 ; i < 16 ; ++ i )
88110 {
89111 for ( int j = 0 ; j < 16 ; ++ j )
90112 {
91- _biomesForGeneration [ i , j ] = new BiomePlains ( new BiomeProperties { BiomeName = "plains" } , settings ) ;
113+ _biomesForGeneration [ j , i ] = Biome . GetBiome ( biomeIds [ j , i ] , settings ) ;
92114 }
93115 }
94116
@@ -97,17 +119,20 @@ public async Task GenerateChunk(IWorld world, ChunkColumnStorage chunk, int x, i
97119 {
98120 for ( int j = 0 ; j < 16 ; ++ j )
99121 {
100- chunk . Biomes [ i * 16 + j ] = ( byte ) _biomesForGeneration [ i , j ] . GetBiomeId ( ) ;
122+ chunk . Biomes [ j * 16 + i ] = ( byte ) _biomesForGeneration [ j , i ] . GetBiomeId ( ) ;
101123 }
102124 }
103125
104- // 基本地形生成
105- await GenerateBasicTerrain ( chunk , x , z , settings ) ;
106-
107126 // 添加生物群系特有方块
108127 await ReplaceBiomeBlocks ( settings , x , z , chunk , _biomesForGeneration ) ;
109128
110129 // Todo genrate structure
130+ // 生成洞穴
131+ if ( settings . UseCaves )
132+ {
133+ CavesGenerator generator = new CavesGenerator ( world ) ;
134+ await generator . Generate ( world , x , z , chunk ) ;
135+ }
111136
112137 // 计算skylight
113138 await GenerateSkylightMap ( chunk ) ;
@@ -124,48 +149,43 @@ public async Task PopulateChunk(IWorld world, ChunkColumnStorage chunk, int x, i
124149
125150 private async Task GenerateBasicTerrain ( ChunkColumnStorage chunk , int x , int z , GeneratorSettings settings )
126151 {
127- // this.biomesForGeneration = this.world.getBiomeProvider().getBiomesForGeneration(this.biomesForGeneration, x * 4 - 2, z * 4 - 2, 10, 10);
152+ // 产生高度图
128153 await GenerateDensityMap ( _densityMap , x * 4 , 0 , z * 4 , settings ) ;
129154
155+ // 进行线性插值
130156 for ( int xHigh = 0 ; xHigh < 4 ; ++ xHigh )
131157 {
132- // int xPart1 = xHigh * 5;
133- // int xPart2 = (xHigh + 1) * 5;
134158 for ( int zHigh = 0 ; zHigh < 4 ; ++ zHigh )
135159 {
136- // int zPart11 = (xPart1 + zHigh) * 33;
137- // int zPart12 = (xPart1 + zHigh + 1) * 33;
138- // int zPart21 = (xPart2 + zHigh) * 33;
139- // int zPart22 = (xPart2 + zHigh + 1) * 33;
140160 for ( int yHigh = 0 ; yHigh < 32 ; ++ yHigh )
141161 {
142162 double yPart111 = _densityMap [ xHigh , yHigh , zHigh ] ;
143163 double yPart121 = _densityMap [ xHigh , yHigh , zHigh + 1 ] ;
144164 double yPart211 = _densityMap [ xHigh + 1 , yHigh , zHigh ] ;
145165 double yPart221 = _densityMap [ xHigh + 1 , yHigh , zHigh + 1 ] ;
146- double yDensityDif11 = ( _densityMap [ xHigh , yHigh + 1 , zHigh ] - yPart111 ) * 0.125 ;
147- double yDensityDif12 = ( _densityMap [ xHigh , yHigh + 1 , zHigh + 1 ] - yPart121 ) * 0.125 ;
148- double yDensityDif21 = ( _densityMap [ xHigh + 1 , yHigh + 1 , zHigh ] - yPart211 ) * 0.125 ;
149- double yDensityDif22 = ( _densityMap [ xHigh + 1 , yHigh + 1 , zHigh + 1 ] - yPart221 ) * 0.125 ;
166+ double yDensityStep11 = ( _densityMap [ xHigh , yHigh + 1 , zHigh ] - yPart111 ) * 0.125 ;
167+ double yDensityStep12 = ( _densityMap [ xHigh , yHigh + 1 , zHigh + 1 ] - yPart121 ) * 0.125 ;
168+ double yDensityStep21 = ( _densityMap [ xHigh + 1 , yHigh + 1 , zHigh ] - yPart211 ) * 0.125 ;
169+ double yDensityStep22 = ( _densityMap [ xHigh + 1 , yHigh + 1 , zHigh + 1 ] - yPart221 ) * 0.125 ;
150170
151171 for ( int yLow = 0 ; yLow < 8 ; ++ yLow )
152172 {
153173 double density111 = yPart111 ;
154174 double density121 = yPart121 ;
155- double xDensityDif11 = ( yPart211 - yPart111 ) * 0.25 ;
156- double xDensityDif21 = ( yPart221 - yPart121 ) * 0.25 ;
175+ double xDensityStep11 = ( yPart211 - yPart111 ) * 0.25 ;
176+ double xDensityStep21 = ( yPart221 - yPart121 ) * 0.25 ;
157177
158178 for ( int xLow = 0 ; xLow < 4 ; ++ xLow )
159179 {
160- double zDensityDif11 = ( density121 - density111 ) * 0.25 ;
161- double blockValue = density111 - zDensityDif11 ;
180+ double zDensityStep11 = ( density121 - density111 ) * 0.25 ;
181+ double blockValue = density111 - zDensityStep11 ;
162182
163183 for ( int zLow = 0 ; zLow < 4 ; ++ zLow )
164184 {
165185 int posX = xHigh * 4 + xLow ;
166186 int posY = yHigh * 8 + yLow ;
167187 int posZ = zHigh * 4 + zLow ;
168- if ( ( blockValue += zDensityDif11 ) > 0.0 )
188+ if ( ( blockValue += zDensityStep11 ) > 0.0 )
169189 {
170190 chunk [ posX , posY , posZ ] = BlockStates . Stone ( ) ;
171191 }
@@ -179,14 +199,14 @@ private async Task GenerateBasicTerrain(ChunkColumnStorage chunk, int x, int z,
179199 }
180200 }
181201
182- density111 += xDensityDif11 ;
183- density121 += xDensityDif21 ;
202+ density111 += xDensityStep11 ;
203+ density121 += xDensityStep21 ;
184204 }
185205
186- yPart111 += yDensityDif11 ;
187- yPart121 += yDensityDif12 ;
188- yPart211 += yDensityDif21 ;
189- yPart221 += yDensityDif22 ;
206+ yPart111 += yDensityStep11 ;
207+ yPart121 += yDensityStep12 ;
208+ yPart211 += yDensityStep21 ;
209+ yPart221 += yDensityStep22 ;
190210 }
191211 }
192212 }
@@ -238,14 +258,14 @@ private Task GenerateDensityMap(float[,,] densityMap, int xOffset, int yOffset,
238258 float totalWeight = 0.0F ;
239259
240260 // 中心点生物群系
241- Biome centerBiome = _biomesForGeneration [ x1 + 2 , z1 + 2 ] ;
261+ Biome centerBiome = _biomesForGeneration [ z1 + 2 , x1 + 2 ] ;
242262
243263 // 求scale和groundYOffset的加权平均值
244264 for ( int x2 = 0 ; x2 < 5 ; ++ x2 )
245265 {
246266 for ( int z2 = 0 ; z2 < 5 ; ++ z2 )
247267 {
248- Biome biome = _biomesForGeneration [ x1 + x2 , z1 + z2 ] ;
268+ Biome biome = _biomesForGeneration [ z1 + z2 , x1 + x2 ] ;
249269 float curGroundYOffset = settings . BiomeDepthOffSet + biome . GetBaseHeight ( ) * settings . BiomeDepthWeight ; // biomeDepthOffSet=0
250270 float curScale = settings . BiomeScaleOffset + biome . GetHeightVariation ( ) * settings . BiomeScaleWeight ; // biomeScaleOffset=0
251271
@@ -269,7 +289,7 @@ private Task GenerateDensityMap(float[,,] densityMap, int xOffset, int yOffset,
269289 groundYOffset = ( groundYOffset * 4.0F - 1.0F ) / 8.0F ;
270290
271291 // 取一个-0.36~0.125的随机数,这个随机数决定了起伏的地表
272- float random = ( _depthMap [ x1 , 0 , z1 ] - 0.5F ) * 160000 / 8000.0F ;
292+ float random = ( _depthMap [ x1 , 0 , z1 ] - 0.5F ) * 2 / 8000.0F ;
273293 if ( random < 0.0F )
274294 {
275295 random = - random * 0.3F ;
@@ -304,10 +324,9 @@ private Task GenerateDensityMap(float[,,] densityMap, int xOffset, int yOffset,
304324 // groundYOffset有-0.072~0.025的变动量
305325 groundYOffset1 = groundYOffset1 + random * 0.2F ;
306326 groundYOffset1 = groundYOffset1 * settings . BaseSize / 8.0F ;
307- float groundY = settings . BaseSize + groundYOffset1 * 4.0F ;
308327
309- // 这个是大概的地面y坐标,实际上也没有保证不会出现浮空岛...
310- // float groundY = settings.BaseSize * (1.0F + groundYOffset1 / 2 .0F) ; // baseSize=8.5,应该代表了平均地表高度68
328+ // 这个是大概的地面y坐标
329+ float groundY = settings . BaseSize + groundYOffset1 * 4 .0F; // baseSize=8.5,应该代表了平均地表高度68
311330
312331 // 注意这个y*8才是最终的y坐标
313332 for ( int y = 0 ; y < 33 ; ++ y )
@@ -356,8 +375,8 @@ private Task ReplaceBiomeBlocks(GeneratorSettings settings, int x, int z, ChunkC
356375 {
357376 for ( int z1 = 0 ; z1 < 16 ; ++ z1 )
358377 {
359- Biome biome = biomesIn [ x1 , z1 ] ;
360- biome . GenerateBiomeTerrain ( settings . SeaLevel , _random , chunk , x , z , x1 , z1 , _surfaceMap [ x1 , 0 , z1 ] ) ;
378+ Biome biome = biomesIn [ z1 , x1 ] ;
379+ biome . GenerateBiomeTerrain ( settings . SeaLevel , _random , chunk , x , z , x1 , z1 , ( _surfaceMap [ x1 , 0 , z1 ] - 0.5 ) * 2 ) ;
361380 }
362381 }
363382
0 commit comments