Skip to content

Commit 3ed501f

Browse files
jstzwjsunnycase
authored andcommitted
More biomes (#52)
* Set seed from setting file * Add Ocean * Add DoublePlants generator * Add Double grass generator * Test CactiGenerator * Caves Generator(without test) * Add Caves with test * Add biome generation * Add Layer Generator * Fix a bug in GenlayerZoom * Better biome parameters * Fix cavegen bug * Remove useless code * Remove old blockaccessor
1 parent 0db646b commit 3ed501f

29 files changed

+1485
-361
lines changed

src/MineCase.Server.Grains/User/UserGrain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public async Task JoinGame()
8888
// _worldTimeSyncTimer = RegisterTimer(OnSyncWorldTime, null, TimeSpan.Zero, )
8989

9090
// 设置出生点
91-
await _player.Spawn(this.GetPrimaryKey(), new Vector3(0, 200, 0), 0.0f, 0.0f);
91+
await _player.Spawn(this.GetPrimaryKey(), new Vector3(1000, 200, 1000), 0.0f, 0.0f);
9292
}
9393

9494
private async Task SendTimeUpdate()

src/MineCase.Server.Grains/World/BlockAccessorGrain.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/MineCase.Server.Grains/World/ChunkColumnGrain.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading.Tasks;
77
using MineCase.Server.Game.Entities;
8+
using MineCase.Server.Settings;
89
using MineCase.Server.World.Generation;
910
using Orleans;
1011

@@ -109,22 +110,33 @@ public Task UpdatePlayerPosition(IPlayer player, Vector3 position)
109110

110111
private async Task EnsureChunkGenerated()
111112
{
112-
/*
113-
var generator = GrainFactory.GetGrain<IChunkGeneratorFlat>(1);
114-
GeneratorSettings settings = new GeneratorSettings
113+
var serverSetting = GrainFactory.GetGrain<IServerSettings>(0);
114+
string worldType = (await serverSetting.GetSettings()).LevelType;
115+
if (worldType == "DEFAULT" || worldType == "default")
115116
{
116-
FlatGeneratorInfo = new FlatGeneratorInfo
117+
var generator = GrainFactory.GetGrain<IChunkGeneratorOverworld>(await _world.GetSeed());
118+
GeneratorSettings settings = new GeneratorSettings
119+
{
120+
};
121+
_state = await generator.Generate(_world, _chunkX, _chunkZ, settings);
122+
}
123+
else if (worldType == "FLAT" || worldType == "flat")
124+
{
125+
var generator = GrainFactory.GetGrain<IChunkGeneratorFlat>(await _world.GetSeed());
126+
GeneratorSettings settings = new GeneratorSettings
117127
{
118128
FlatBlockId = new BlockState?[] { BlockStates.Stone(), BlockStates.Dirt(), BlockStates.Grass() }
119-
}
120-
};
121-
_state = await generator.Generate(_chunkX, _chunkZ, settings);
122-
*/
123-
var generator = GrainFactory.GetGrain<IChunkGeneratorOverworld>(1);
124-
GeneratorSettings settings = new GeneratorSettings
129+
};
130+
_state = await generator.Generate(_world, _chunkX, _chunkZ, settings);
131+
}
132+
else
125133
{
126-
};
127-
_state = await generator.Generate(_world, _chunkX, _chunkZ, settings);
134+
var generator = GrainFactory.GetGrain<IChunkGeneratorOverworld>(await _world.GetSeed());
135+
GeneratorSettings settings = new GeneratorSettings
136+
{
137+
};
138+
_state = await generator.Generate(_world, _chunkX, _chunkZ, settings);
139+
}
128140
}
129141
}
130142
}

src/MineCase.Server.Grains/World/Generation/ChunkGeneratorOverworldGrain.cs

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using MineCase.Algorithm;
99
using MineCase.Algorithm.Noise;
1010
using MineCase.Server.World.Biomes;
11+
using MineCase.Server.World.Layer;
12+
using MineCase.Server.World.Mine;
1113
using Newtonsoft.Json;
1214
using Orleans;
1315
using 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

src/MineCase.Server.Grains/World/WorldGrain.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ internal class WorldGrain : Grain, IWorld
1616
private Dictionary<uint, IEntity> _entities;
1717
private uint _nextAvailEId;
1818
private long _worldAge;
19-
private IBlockAccessor _blockAccessor;
2019
private GeneratorSettings _genSettings; // 生成设置
2120
private string _seed; // 世界种子
2221

@@ -25,7 +24,6 @@ public override async Task OnActivateAsync()
2524
IServerSettings serverSettings = GrainFactory.GetGrain<IServerSettings>(0);
2625
_nextAvailEId = 0;
2726
_entities = new Dictionary<uint, IEntity>();
28-
_blockAccessor = GrainFactory.GetGrain<IBlockAccessor>(this.GetPrimaryKeyString());
2927
_genSettings = new GeneratorSettings();
3028
await InitGeneratorSettings(_genSettings);
3129
_seed = (await serverSettings.GetSettings()).LevelSeed;
@@ -64,11 +62,6 @@ public Task OnGameTick(TimeSpan deltaTime)
6462

6563
public Task<long> GetAge() => Task.FromResult(_worldAge);
6664

67-
public Task<IBlockAccessor> GetBlockAccessor()
68-
{
69-
return Task.FromResult(_blockAccessor);
70-
}
71-
7265
public Task<int> GetSeed()
7366
{
7467
int result = 0;

0 commit comments

Comments
 (0)