Permalink
Browse files

Added thexa4 Perlin terrain generation function

  • Loading branch information...
1 parent 60be40b commit e7d33fed3e68c790959ea2be7722fffc86acff89 @phreeza committed Jun 6, 2010
Showing with 36 additions and 1 deletion.
  1. +5 −1 cells.py
  2. +31 −0 terrain/generator.py
View
@@ -81,7 +81,7 @@ def __init__(self, bounds, mind_list, symmetric, max_time):
self.max_time = max_time
self.tic = time.time()
self.terr = ScalarMapLayer(self.size)
- self.terr.set_simple(10)
+ self.terr.set_perlin(10)
self.minds = [m[1].AgentMind for m in mind_list]
self.energy_map = ScalarMapLayer(self.size)
@@ -356,6 +356,10 @@ def set_streak(self, range):
def set_simple(self, range):
self.values = terrain_generator().create_simple(self.size, range, symmetric)
+
+ def set_perlin(self, range):
+ self.values = terrain_generator().create_perlin(self.size, range, symmetric)
+
def change(self, x, y, val):
self.values[x, y] += val
View
@@ -1,5 +1,6 @@
import numpy
import random
+import math
class terrain_generator():
def create_random(self, size, range, symmetric=False):
@@ -131,6 +132,36 @@ def create_simple(self, size, range, symmetric=False):
if symmetric:
ret = self.make_symmetric(ret)
return numpy.array(ret)
+
+ def create_perlin(self, size, roughness, symmetric = False):
+ (width, height) = size
+ values = numpy.zeros(size)
+ noise = numpy.random.random_sample((width+1, height+1))
+ octaves = (256, 8, 2)
+ for y in range(height):
+ for x in range(width):
+ if symmetric and x < y:
+ values[x][y] = values[y][x]
+ continue
+ nr = 1
+ for i in octaves:
+ top = y/i
+ left = x/i
+ my = float(y % i) / i
+ mx = float(x % i) / i
+ values[x][y] += self.interpolate(noise[top][left], noise[top][left+1], noise[top+1][left], noise[top+1][left+1], mx, my) * math.pow(0.5, nr)
+ nr += 1
+ values[x][y] = int(values[x][y] * roughness)
+ return numpy.array(values,dtype=int)
+
+ #Some helper functions.
+ def interpolate(self, p1, p2, p3, p4, x, y):
+ top = self.interpolate1d(p1, p2, x)
+ bottom = self.interpolate1d(p3, p4, x)
+ return self.interpolate1d(top, bottom, y)
+
+ def interpolate1d(self, p1, p2, mu):
+ return p1*(1-mu)+p2*mu
def add_random_range(self, x, rand_min, rand_max):
"""Returns a number that is between x + rand_min and x + rand_max (inclusive)"""

0 comments on commit e7d33fe

Please sign in to comment.