2

我发现了一篇关于世界生成的好帖子,可以在这里找到。

它很好地描述了需要做什么,但我无法弄清楚如何在 python 中做到这一点。我相信它是在 as3 中完成的,但我不确定。无论如何,我还发现了一个针对 perlin 噪声的 libtcod 函数:

noise2d=libtcod.noise_new(2)
value = libtcod.noise_turbulence_perlin(noise2d,[0.5,0.7],32.0)

我真的不知道如何实现这一点,然后根据高度分配字符。

如果有人可以帮助我将文章中的步骤翻译成 python,我将非常感激。谢谢

4

1 回答 1

2

这是我几年前写的一个类,它为我的 roguelike 原型DomeriaRL创建了一张世界地图。相关部分在__generate方法中。

它将噪声应用于高度图,对其进行归一化,并根据高度图的值创建图块。

我正在使用颜色图来创建从一种瓷砖到另一种瓷砖的平滑过渡。

from constants.constant import *
from world import World
from worldtile import WorldTile

class WorldGenerator(object):
  """Randomly generates a new world with terrain and objects"""

  def regular(self):
    """Randomly generate a new world with some water, swamps, hills, some objects etc"""

    idx = [ 0 , 15, 75, 90, 101 ] # indexes of the keys 
    col = [ T.Color(0,100,100),
            T.Color(0,75,0), 
            T.Color(50,150,0), 
            T.Color(150,120,80), 
            T.Color(180,180,180)]

    map=T.color_gen_map(col,idx)

    tiles = zip(idx, [[SWAMP, PLAINS],
                      [PLAINS, FOREST], 
                      [HILLS, FOREST],
                      [HILLS, HILLS, MOUNTAINS],
                      [HILLS, MOUNTAINS,MOUNTAINS]])

    world = self.__generate(map, tiles)
    return world

  def __generate(self, colormap, mtiles, noise_zoom=1, noise_octaves=10):
    hm = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
    hm1 = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)
    hm2 = T.heightmap_new(WORLD_WIDTH, WORLD_HEIGHT)

    noise = T.noise_new(2)
    T.heightmap_add_fbm(hm1, noise, noise_zoom, noise_zoom, 0.0, 0.0, noise_octaves, 0.0, 1.0)
    T.heightmap_add_fbm(hm2, noise, noise_zoom*2, noise_zoom*2, 0.0, 0.0, noise_octaves/2, 0.0, 1.0)

    T.heightmap_multiply_hm(hm1, hm2, hm)
    T.heightmap_normalize(hm, mi=0.0, ma=1)

    tiles = {}

    # 0...100 -> value from noised heightmap
    for x in xrange(0, 101):
      lower = [c for c in mtiles if c[0] <= x][-1] # tile is between lower and upper color
      upper = [c for c in mtiles if c[0] > x][0]

      # calculating percentage
      lower_c = x - lower[0]
      upper_c = upper[0] - x
      count = lower_c + upper_c

      tiles[x] = colormap[x], lower[1], int(upper_c * 1.0 / count * 100), upper[1] 

    # generate world grid
    grid = []
    for x in xrange(WORLD_WIDTH):
      grid.append([])
      for y in xrange(WORLD_HEIGHT):
        hm_v = T.heightmap_get_value(hm, x, y)
        grid[x].append(WorldTile(*tiles[int(hm_v * 100)]))

    T.heightmap_delete(hm)
    T.heightmap_delete(hm1)
    T.heightmap_delete(hm2)

    objects = []
    while len(objects) < 5:
      for x in xrange(WORLD_WIDTH):
        for y in xrange(WORLD_HEIGHT):
          r = random.randrange(0, 10000)
          pos = [wo for wo in WORLDOBJECTS if wo.chance >= r and grid[x][y].tile in wo.tiles]
          if pos:
            o = random.choice(pos).create(x, y)
            objects.append(o)

    return World(grid, objects)

示例世界地图如下所示:

在此处输入图像描述

请注意,此游戏使用 libtcod 1.5.1b1,并且在较新版本中更改了一些函数名称。

于 2013-05-08T14:02:54.820 回答