def _update_diamond(self, terrain, x, y, diamond_len):
"""Update the midpoint of a diamond.
Midpoint becomes average of diamond corners plus a random offset determined by noise.
Args:
terrain (Terrain): Terrain to update.
x (int): X coordinate of center of diamond.
y (int): Y coordinate of center of diamond.
diamond_len (int): Length of one corner of diamond to other.
Returns:
Terrain: New terrain with updated square center.
"""
half_len = diamond_len / 2
# If on edge of terrain, only access 3 neighbours to avoid leaving terrain bounds
neighbours = []
if x != 0:
neighbours.append(terrain[x - half_len, y])
if y != 0:
neighbours.append(terrain[x, y - half_len])
if x != terrain.width - 1:
neighbours.append(terrain[x + half_len, y])
if y != terrain.length - 1:
neighbours.append(terrain[x, y + half_len])
mean_height = sum(neighbours) / float(len(neighbours))
frequency = terrain.length / diamond_len
offset = (random.random() - 0.5) * self.amp_from_freq(frequency)
if not 0 <= mean_height + offset <= 1:
if mean_height + offset > 1:
terrain[x, y] = 1
else:
terrain[x, y] = 0
else:
terrain[x, y] = mean_height + offset
return terrain
评论列表
文章目录