ChunkGeneratorOverworldGrain.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Numerics;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Microsoft.Extensions.Logging;
  8. using MineCase.Algorithm;
  9. using MineCase.Algorithm.Noise;
  10. using MineCase.Algorithm.World;
  11. using MineCase.Algorithm.World.Biomes;
  12. using MineCase.Algorithm.World.Layer;
  13. using MineCase.Algorithm.World.Mine;
  14. using MineCase.Server.World.Decoration.Biomes;
  15. using MineCase.World;
  16. using MineCase.World.Biomes;
  17. using MineCase.World.Generation;
  18. using Newtonsoft.Json;
  19. using Orleans;
  20. using Orleans.Concurrency;
  21. using Orleans.Runtime;
  22. namespace MineCase.Server.World.Generation
  23. {
  24. [StatelessWorker]
  25. internal class ChunkGeneratorOverWorldGrain : Grain, IChunkGeneratorOverworld
  26. {
  27. private float[,,] _densityMap;
  28. private float[,,] _depthMap;
  29. private float[,,] _mainNoiseMap;
  30. private float[,,] _minLimitMap;
  31. private float[,,] _maxLimitMap;
  32. private float[,,] _surfaceMap;
  33. private OctavedNoise<PerlinNoise> _depthNoise;
  34. private OctavedNoise<PerlinNoise> _mainNoise;
  35. private OctavedNoise<PerlinNoise> _maxNoise;
  36. private OctavedNoise<PerlinNoise> _minNoise;
  37. private OctavedNoise<PerlinNoise> _surfaceNoise;
  38. private int _seed;
  39. private Random _random;
  40. private float[,] _biomeWeights;
  41. private Biome[,] _biomesForGeneration; // 10x10 or 16 x 16
  42. private GenLayer _genlayer;
  43. public override Task OnActivateAsync()
  44. {
  45. _densityMap = new float[5, 33, 5];
  46. _depthMap = new float[5, 1, 5];
  47. _mainNoiseMap = new float[5, 33, 5];
  48. _minLimitMap = new float[5, 33, 5];
  49. _maxLimitMap = new float[5, 33, 5];
  50. _surfaceMap = new float[16, 1, 16];
  51. _seed = (int)this.GetPrimaryKeyLong();
  52. _random = new Random(_seed);
  53. _depthNoise = new OctavedNoise<PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F);
  54. _mainNoise = new OctavedNoise<PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F);
  55. _maxNoise = new OctavedNoise<PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F);
  56. _minNoise = new OctavedNoise<PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F);
  57. _surfaceNoise = new OctavedNoise<PerlinNoise>(new PerlinNoise(_random.Next()), 8, 0.5F);
  58. _biomeWeights = new float[5, 5];
  59. for (int i = -2; i <= 2; ++i)
  60. {
  61. for (int j = -2; j <= 2; ++j)
  62. {
  63. float f = 10.0F / (float)Math.Sqrt((i * i + j * j) + 0.2D);
  64. _biomeWeights[i + 2, j + 2] = f;
  65. }
  66. }
  67. _biomesForGeneration = new Biome[16, 16];
  68. _genlayer = GenLayer.InitAllLayer(_seed);
  69. return Task.CompletedTask;
  70. }
  71. public async Task<ChunkColumnCompactStorage> Generate(IWorld world, int x, int z, GeneratorSettings settings)
  72. {
  73. var chunkColumn = new ChunkColumnStorage();
  74. for (int i = 0; i < chunkColumn.Sections.Length; ++i)
  75. chunkColumn.Sections[i] = new ChunkSectionStorage(true);
  76. var info = new MapGenerationInfo
  77. {
  78. Seed = await world.GetSeed()
  79. };
  80. GenerateChunk(info, chunkColumn, x, z, settings);
  81. // PopulateChunk(world, chunkColumn, x, z, settings);
  82. return chunkColumn.Compact();
  83. }
  84. public async Task Populate(IWorld world, int x, int z, GeneratorSettings settings)
  85. {
  86. var chunkColumnKey = world.MakeAddressByPartitionKey(new ChunkWorldPos { X = x, Z = z });
  87. ChunkColumnCompactStorage chunkColumn = await GrainFactory.GetGrain<IChunkColumn>(chunkColumnKey).GetStateUnsafe();
  88. Biome chunkBiome = Biome.GetBiome(chunkColumn.Biomes[7 * 16 + 7], settings);
  89. if (chunkBiome.GetBiomeId() == BiomeId.Plains)
  90. {
  91. var decorator = GrainFactory.GetGrain<IBiomePlainsDecorator>((long)BiomeId.Plains);
  92. await decorator.Decorate(world, new ChunkWorldPos(x, z));
  93. // decorator.SpawnMob(world, chunk, new ChunkWorldPos(x, z), new BlockWorldPos { X = blockX, Z = blockZ });
  94. }
  95. else if (chunkBiome.GetBiomeId() == BiomeId.Forest)
  96. {
  97. var decorator = GrainFactory.GetGrain<IBiomeForestDecorator>((long)BiomeId.Forest);
  98. await decorator.Decorate(world, new ChunkWorldPos(x, z));
  99. }
  100. else if (chunkBiome.GetBiomeId() == BiomeId.Taiga)
  101. {
  102. var decorator = GrainFactory.GetGrain<IBiomeTaigaDecorator>((long)BiomeId.Taiga);
  103. await decorator.Decorate(world, new ChunkWorldPos(x, z));
  104. }
  105. }
  106. private void GenerateChunk(MapGenerationInfo info, ChunkColumnStorage chunk, int x, int z, GeneratorSettings settings)
  107. {
  108. // 生物群系生成
  109. // 获取生物群系
  110. int[,] biomeIds = _genlayer.GetInts(x * 16 - 8, z * 16 - 8, 32, 32);
  111. for (int i = 0; i < 10; ++i)
  112. {
  113. for (int j = 0; j < 10; ++j)
  114. {
  115. _biomesForGeneration[j, i] = Biome.GetBiome(biomeIds[(int)(0.861111F * j * 4), (int)(0.861111F * i * 4)], settings);
  116. }
  117. }
  118. // 基本地形生成
  119. GenerateBasicTerrain(chunk, x, z, settings);
  120. // 获取生物群系
  121. biomeIds = _genlayer.GetInts(x * 16, z * 16, 16, 16);
  122. for (int i = 0; i < 16; ++i)
  123. {
  124. for (int j = 0; j < 16; ++j)
  125. {
  126. _biomesForGeneration[j, i] = Biome.GetBiome(biomeIds[j, i], settings);
  127. }
  128. }
  129. // 设置生物群系
  130. for (int i = 0; i < 16; ++i)
  131. {
  132. for (int j = 0; j < 16; ++j)
  133. {
  134. chunk.Biomes[j * 16 + i] = (byte)_biomesForGeneration[j, i].GetBiomeId();
  135. }
  136. }
  137. // 添加生物群系特有方块
  138. ReplaceBiomeBlocks(settings, x, z, chunk, _biomesForGeneration);
  139. // Todo genrate structure
  140. // 生成洞穴
  141. if (settings.UseCaves)
  142. {
  143. CavesGenerator generator = new CavesGenerator(info);
  144. generator.Generate(info, x, z, chunk, _biomesForGeneration[8, 8]);
  145. }
  146. // 计算skylight
  147. GenerateSkylightMap(chunk);
  148. }
  149. public void PopulateChunk(IWorld world, ChunkColumnCompactStorage chunk, int x, int z, GeneratorSettings settings)
  150. {
  151. }
  152. private void GenerateBasicTerrain(ChunkColumnStorage chunk, int x, int z, GeneratorSettings settings)
  153. {
  154. // 产生高度图
  155. GenerateDensityMap(_densityMap, x * 4, 0, z * 4, settings);
  156. // 进行线性插值
  157. for (int xHigh = 0; xHigh < 4; ++xHigh)
  158. {
  159. for (int zHigh = 0; zHigh < 4; ++zHigh)
  160. {
  161. for (int yHigh = 0; yHigh < 32; ++yHigh)
  162. {
  163. double yPart111 = _densityMap[xHigh, yHigh, zHigh];
  164. double yPart121 = _densityMap[xHigh, yHigh, zHigh + 1];
  165. double yPart211 = _densityMap[xHigh + 1, yHigh, zHigh];
  166. double yPart221 = _densityMap[xHigh + 1, yHigh, zHigh + 1];
  167. double yDensityStep11 = (_densityMap[xHigh, yHigh + 1, zHigh] - yPart111) * 0.125;
  168. double yDensityStep12 = (_densityMap[xHigh, yHigh + 1, zHigh + 1] - yPart121) * 0.125;
  169. double yDensityStep21 = (_densityMap[xHigh + 1, yHigh + 1, zHigh] - yPart211) * 0.125;
  170. double yDensityStep22 = (_densityMap[xHigh + 1, yHigh + 1, zHigh + 1] - yPart221) * 0.125;
  171. for (int yLow = 0; yLow < 8; ++yLow)
  172. {
  173. double density111 = yPart111;
  174. double density121 = yPart121;
  175. double xDensityStep11 = (yPart211 - yPart111) * 0.25;
  176. double xDensityStep21 = (yPart221 - yPart121) * 0.25;
  177. for (int xLow = 0; xLow < 4; ++xLow)
  178. {
  179. double zDensityStep11 = (density121 - density111) * 0.25;
  180. double blockValue = density111 - zDensityStep11;
  181. for (int zLow = 0; zLow < 4; ++zLow)
  182. {
  183. int posX = xHigh * 4 + xLow;
  184. int posY = yHigh * 8 + yLow;
  185. int posZ = zHigh * 4 + zLow;
  186. if ((blockValue += zDensityStep11) > 0.0)
  187. {
  188. chunk[posX, posY, posZ] = BlockStates.Stone();
  189. }
  190. else if (posY < settings.SeaLevel)
  191. {
  192. chunk[posX, posY, posZ] = BlockStates.Water();
  193. }
  194. else
  195. {
  196. chunk[posX, posY, posZ] = BlockStates.Air();
  197. }
  198. }
  199. density111 += xDensityStep11;
  200. density121 += xDensityStep21;
  201. }
  202. yPart111 += yDensityStep11;
  203. yPart121 += yDensityStep12;
  204. yPart211 += yDensityStep21;
  205. yPart221 += yDensityStep22;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. private void GenerateDensityMap(float[,,] densityMap, int xOffset, int yOffset, int zOffset, GeneratorSettings settings)
  212. {
  213. _depthNoise.Noise(
  214. _depthMap,
  215. new Vector3(xOffset + 0.1f, 0.0f, zOffset + 0.1f),
  216. new Vector3(settings.DepthNoiseScaleX, 1.0f, settings.DepthNoiseScaleZ));
  217. float coordinateScale = settings.CoordinateScale;
  218. float heightScale = settings.HeightScale;
  219. // 生成3个5*5*33的噪声
  220. _mainNoise.Noise(
  221. _mainNoiseMap,
  222. new Vector3(xOffset, yOffset, zOffset),
  223. new Vector3(
  224. coordinateScale / settings.MainNoiseScaleX,
  225. heightScale / settings.MainNoiseScaleY,
  226. coordinateScale / settings.MainNoiseScaleZ));
  227. _minNoise.Noise(
  228. _minLimitMap,
  229. new Vector3(xOffset, yOffset, zOffset),
  230. new Vector3(
  231. coordinateScale,
  232. heightScale,
  233. coordinateScale));
  234. _maxNoise.Noise(
  235. _maxLimitMap,
  236. new Vector3(xOffset, yOffset, zOffset),
  237. new Vector3(
  238. coordinateScale,
  239. heightScale,
  240. coordinateScale));
  241. // chunk遍历
  242. for (int x1 = 0; x1 < 5; ++x1)
  243. {
  244. for (int z1 = 0; z1 < 5; ++z1)
  245. {
  246. float scale = 0.0F;
  247. float groundYOffset = 0.0F;
  248. float totalWeight = 0.0F;
  249. // 中心点生物群系
  250. Biome centerBiome = _biomesForGeneration[z1 + 2, x1 + 2];
  251. // 求scale和groundYOffset的加权平均值
  252. for (int x2 = 0; x2 < 5; ++x2)
  253. {
  254. for (int z2 = 0; z2 < 5; ++z2)
  255. {
  256. Biome biome = _biomesForGeneration[z1 + z2, x1 + x2];
  257. float curGroundYOffset = settings.BiomeDepthOffSet + biome.GetBaseHeight() * settings.BiomeDepthWeight; // biomeDepthOffSet=0
  258. float curScale = settings.BiomeScaleOffset + biome.GetHeightVariation() * settings.BiomeScaleWeight; // biomeScaleOffset=0
  259. // parabolicField为 10 / √(该点到中心点的距离^2 + 0.2)
  260. float weight = _biomeWeights[z2, x2] / (curGroundYOffset + 2.0F);
  261. if (biome.GetBaseHeight() > centerBiome.GetBaseHeight())
  262. {
  263. weight /= 2.0F;
  264. }
  265. scale += curScale * weight;
  266. groundYOffset += curGroundYOffset * weight;
  267. totalWeight += weight;
  268. }
  269. }
  270. scale = scale / totalWeight;
  271. groundYOffset = groundYOffset / totalWeight;
  272. scale = scale * 0.9F + 0.1F;
  273. groundYOffset = (groundYOffset * 4.0F - 1.0F) / 8.0F;
  274. // 取一个-0.36~0.125的随机数,这个随机数决定了起伏的地表
  275. float random = (_depthMap[x1, 0, z1] - 0.5F) * 2 / 8000.0F;
  276. if (random < 0.0F)
  277. {
  278. random = -random * 0.3F;
  279. }
  280. random = random * 3.0F - 2.0F;
  281. if (random < 0.0)
  282. {
  283. random = random / 2.0F;
  284. if (random < -1.0)
  285. {
  286. random = -1.0F;
  287. }
  288. random = random / 1.4F;
  289. random = random / 2.0F;
  290. }
  291. else
  292. {
  293. if (random > 1.0F)
  294. {
  295. random = 1.0F;
  296. }
  297. random = random / 8.0F;
  298. }
  299. float groundYOffset1 = groundYOffset;
  300. float scale1 = scale;
  301. // groundYOffset有-0.072~0.025的变动量
  302. groundYOffset1 = groundYOffset1 + random * 0.2F;
  303. groundYOffset1 = groundYOffset1 * settings.BaseSize / 8.0F;
  304. // 这个是大概的地面y坐标
  305. float groundY = settings.BaseSize + groundYOffset1 * 4.0F; // baseSize=8.5,应该代表了平均地表高度68
  306. // 注意这个y*8才是最终的y坐标
  307. for (int y = 0; y < 33; ++y)
  308. {
  309. // result偏移量,这个是负数则趋向固体,是正数则趋向液体和空气
  310. float offset = (y - groundY) * settings.StretchY * 128.0F / 256.0F / scale1; // scale大概在0.1~0.2这样...
  311. if (offset < 0.0F)
  312. {
  313. offset *= 4.0F;
  314. }
  315. // 并不保证lowerLimit < upperLimit,不过没有影响
  316. float lowerLimit = (_minLimitMap[x1, y, z1] - 0.5F) * 160000 / settings.LowerLimitScale; // lowerLimitScale=512
  317. float upperLimit = (_maxLimitMap[x1, y, z1] - 0.5F) * 160000 / settings.UpperLimitScale; // upperLimitScale=512
  318. float t = ((_mainNoiseMap[x1, y, z1] - 0.5F) * 160000 / 10.0F + 1.0F) / 2.0F;
  319. // 这个函数t < 0则取lowerLimit,t > 1则取upperLimit,否则以t为参数在上下限间线性插值
  320. float result = MathHelper.DenormalizeClamp(lowerLimit, upperLimit, t) - offset;
  321. // y = 30~32
  322. if (y > 29)
  323. {
  324. // 在原result和-10之间线性插值,这样y > 240的方块就会越来越少,最后全变成空气
  325. float t2 = (float)(y - 29) / 3.0F;
  326. result = result * (1.0F - t2) + -10.0F * t2;
  327. }
  328. _densityMap[x1, y, z1] = (float)result;
  329. }
  330. }
  331. }
  332. densityMap = _densityMap;
  333. }
  334. private void ReplaceBiomeBlocks(GeneratorSettings settings, int x, int z, ChunkColumnStorage chunk, Biome[,] biomesIn)
  335. {
  336. _surfaceNoise.Noise(
  337. _surfaceMap,
  338. new Vector3(x * 16 + 0.1F, 0, z * 16 + 0.1F),
  339. new Vector3(0.0625F, 1.0F, 0.0625F));
  340. for (int x1 = 0; x1 < 16; ++x1)
  341. {
  342. for (int z1 = 0; z1 < 16; ++z1)
  343. {
  344. Biome biome = biomesIn[z1, x1];
  345. biome.GenerateBiomeTerrain(settings.SeaLevel, _random, chunk, x, z, x1, z1, (_surfaceMap[x1, 0, z1] - 0.5) * 2);
  346. }
  347. }
  348. }
  349. private void GenerateSkylightMap(ChunkColumnStorage chunk)
  350. {
  351. for (int i = 0; i < ChunkConstants.SectionsPerChunk; ++i)
  352. {
  353. var skyLight = chunk.Sections[i].SkyLight;
  354. for (int y = 0; y < ChunkConstants.BlockEdgeWidthInSection; y++)
  355. {
  356. for (int z = 0; z < ChunkConstants.BlockEdgeWidthInSection; z++)
  357. {
  358. for (int x = 0; x < ChunkConstants.BlockEdgeWidthInSection; x++)
  359. {
  360. skyLight[x, y, z] = 0xF;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. private int GetDensityMapIndex(int x, int y, int z)
  367. {
  368. return (x * 5 + z) * 33 + y;
  369. }
  370. private double GetDensityMapValue(double[] densityMap, int x, int y, int z)
  371. {
  372. return densityMap[(x * 5 + z) * 33 + y];
  373. }
  374. }
  375. }