未连接的块

huangapple go评论97阅读模式
英文:

Chunks not connected

问题

这是我的代码:

  1. def generate(octaves):
  2. 全局变量 world, xpix, chunkSize # 设置全局变量(用于脚本的其余部分中的其他引用等)
  3. chunkSize = (12, 12)
  4. xpix, ypix = chunkSize[0], chunkSize[1]
  5. world = []
  6. noise1 = PerlinNoise(octaves=octaves, seed=seed) # 生成噪声
  7. for i in range(xpix): # 生成用于绘制的列表
  8. = []
  9. for j in range(ypix):
  10. noise_val = noise1([(i / xpix) + chunkCoordX * xpix, (j / ypix) + chunkCoordY * ypix])
  11. if noise_val <= .05:
  12. tiletoplace = tileclassdata.water
  13. elif noise_val <= .13:
  14. tiletoplace = tileclassdata.sand
  15. else:
  16. tiletoplace = tileclassdata.grass
  17. placed_tile = classes.tile(tiletoplace, i, j)
  18. .append(placed_tile)
  19. world.append(行)

我正在尝试使我生成的区块连接起来。例如:

我可以在1,-4处有一个区块,在0,-4处有一个区块。但它们会是这个样子的:

未连接的块

未连接的块

我希望它们连接起来,这样我就可以获得诸如岛屿之类的东西,而不仅仅是在我的游戏地图中随机散布的斑点。

我尝试重构求解柏林噪声地图中区块位置的总和,但我还没有能够设计出一个能解决这个问题的总和。

英文:

Here is my code:

  1. def generate(octaves):
  2. global world, xpix, chunkSize #set globals (for other references in the rest of the script etc)
  3. chunkSize = (12, 12)
  4. xpix, ypix = chunkSize[0], chunkSize[1]
  5. world = []
  6. noise1 = PerlinNoise(octaves=octaves, seed=seed) #make noise
  7. for i in range(xpix): # make list for drawer to use
  8. row = []
  9. for j in range(ypix):
  10. noise_val = noise1([(i / xpix) + chunkCoordX * xpix, (j / ypix) + chunkCoordY * ypix])
  11. if noise_val <= .05:
  12. tiletoplace = tileclassdata.water
  13. elif noise_val <= .13:
  14. tiletoplace = tileclassdata.sand
  15. else:
  16. tiletoplace = tileclassdata.grass
  17. placed_tile = classes.tile(tiletoplace, i, j)
  18. row.append(placed_tile)
  19. world.append(row)

I am trying to make the chunks I generate connect. For example:

I could have a chunk at 1,-4 and one at 0,-4. But this is what they'd look like:

未连接的块

未连接的块

I would like them to connect so that I can get things like islands generating across chunks instead of just have random blobs scattered around my game's map.

I have tried refactoring the sum for working out the chunks position in the perlin-noise map but I have not been able to devise a sum that figures it out.

答案1

得分: 0

以下是代码部分的翻译:

  1. import numpy as np
  2. from scipy import ndimage
  3. import seaborn as sns
  4. def generate_landscape(field_size: int, flatness: int, magnitude=255):
  5. """ 生成一个类似岛屿的地形给定参数:
  6. Args:
  7. field_size: 结果numpy数组的大小
  8. flatness: 很大的数字将导致完全平坦的地形
  9. 1将导致每个点都随机绘制
  10. """
  11. field = np.random.binomial(1, 1 / flatness, size=(field_size, field_size)) \
  12. * np.random.randint(low=0, high=magnitude, size=(field_size, field_size))
  13. mask_size = max(int(flatness ** 0.5), 1)
  14. return ndimage.gaussian_filter(field, mask_size)
  15. sns.heatmap(generate_landscape(32, 10))

请注意,代码部分中的 HTML 实体 """ 已被保留,以确保代码的完整性。

英文:

Quick and dirty example of the idea in the comments:

  1. import numpy as np
  2. from scipy import ndimage
  3. import seaborn as sns
  4. def generate_landscape(field_size: int, flatness: int, magnitude=255):
  5. """ Generates an islandish landscape, given:
  6. Args:
  7. field_size: size of the resulting numpy array
  8. flatness: very large number will result in a completely flat terrain;
  9. 1 will result in every spot being drawn at random
  10. """
  11. field = np.random.binomial(1, 1 / flatness, size=(field_size, field_size)) \
  12. * np.random.randint(low=0, high=magnitude, size=(field_size, field_size))
  13. mask_size = max(int(flatness ** 0.5), 1)
  14. return ndimage.gaussian_filter(field, mask_size)
  15. sns.heatmap(generate_landscape(32, 10))

Example output:

未连接的块

After that, I'd take a desired water/sand %%, calculate quantiles and apply the threshold.

huangapple
  • 本文由 发表于 2023年7月11日 00:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定