未连接的块

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

Chunks not connected

问题

这是我的代码:

def generate(octaves):
    全局变量 world, xpix, chunkSize # 设置全局变量(用于脚本的其余部分中的其他引用等)
    chunkSize = (12, 12)
    xpix, ypix = chunkSize[0], chunkSize[1]
    world = []
    noise1 = PerlinNoise(octaves=octaves, seed=seed) # 生成噪声
    
    for i in range(xpix): # 生成用于绘制的列表
= []
        for j in range(ypix):
            noise_val = noise1([(i / xpix) + chunkCoordX * xpix, (j / ypix) + chunkCoordY * ypix])

            if noise_val <= .05:
                tiletoplace = tileclassdata.water
            elif noise_val <= .13:
                tiletoplace = tileclassdata.sand
            else:
                tiletoplace = tileclassdata.grass
            placed_tile = classes.tile(tiletoplace, i, j)
.append(placed_tile)
        world.append(行)

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

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

未连接的块

未连接的块

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

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

英文:

Here is my code:

def generate(octaves):
    global world, xpix, chunkSize #set globals (for other references in the rest of the script etc)
    chunkSize = (12, 12)
    xpix, ypix = chunkSize[0], chunkSize[1]
    world = []
    noise1 = PerlinNoise(octaves=octaves, seed=seed) #make noise
    
    for i in range(xpix): # make list for drawer to use
        row = []
        for j in range(ypix):
            noise_val = noise1([(i / xpix) + chunkCoordX * xpix, (j / ypix) + chunkCoordY * ypix])

            if noise_val <= .05:
                tiletoplace = tileclassdata.water
            elif noise_val <= .13:
                tiletoplace = tileclassdata.sand
            else:
                tiletoplace = tileclassdata.grass
            placed_tile = classes.tile(tiletoplace, i, j)
            row.append(placed_tile)
        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

以下是代码部分的翻译:

import numpy as np
from scipy import ndimage
import seaborn as sns

def generate_landscape(field_size: int, flatness: int, magnitude=255):
    """ 生成一个类似岛屿的地形给定参数:
    
    Args:
        field_size: 结果numpy数组的大小
        flatness: 很大的数字将导致完全平坦的地形
            1将导致每个点都随机绘制
    """
    field = np.random.binomial(1, 1 / flatness, size=(field_size, field_size)) \
        * np.random.randint(low=0, high=magnitude, size=(field_size, field_size))
    mask_size = max(int(flatness ** 0.5), 1)
    return ndimage.gaussian_filter(field, mask_size)

sns.heatmap(generate_landscape(32, 10))

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

英文:

Quick and dirty example of the idea in the comments:

import numpy as np
from scipy import ndimage
import seaborn as sns

def generate_landscape(field_size: int, flatness: int, magnitude=255):
    """ Generates an islandish landscape, given:

    Args:
        field_size: size of the resulting numpy array
        flatness: very large number will result in a completely flat terrain;
            1 will result in every spot being drawn at random
    """
    field = np.random.binomial(1, 1 / flatness, size=(field_size, field_size)) \
        * np.random.randint(low=0, high=magnitude, size=(field_size, field_size))
    mask_size = max(int(flatness ** 0.5), 1)
    return ndimage.gaussian_filter(field, mask_size)

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:

确定