我发现了一篇关于世界一代的精彩帖子,可以找到here.
它很好地描述了需要做什么,但我在查找如何在python中完成它时遇到了麻烦。我相信它是在as3中完成的,但我不确定。无论如何,我还发现了一个用于perlin噪声的libtcod函数:
noise2d=libtcod.noise_new(2)
value = libtcod.noise_turbulence_perlin(noise2d,[0.5,0.7],32.0)
我真的不知道如何实现这一点,然后根据高度分配字符。
如果有人可以帮我翻译从文章到python的步骤,我真的很感激。感谢
答案 0 :(得分:2)
这是我几年前写的一个课程,它为我的roguelike原型DomeriaRL创建了一个世界地图。相关部分位于__generate
method。
它将噪声应用于高度图,对其进行标准化,并根据高度图的值创建切片。
我正在使用颜色贴图创建从一种图块类型到另一种图块类型的平滑过渡。
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,并且某些功能名称在较新版本中已更改。