def __init__(self): self.x = SCREEN_WIDTH / 2 self.y = SCREEN_HEIGHT / 2 self.tx = 0 self.ty = 0 self.noiseX = PerlinNoise(octaves=10, seed=1) self.noiseY = PerlinNoise(octaves=10, seed=2)
def noiseMountain(oldpic, mountHeight, size): noise1 = PerlinNoise(octaves=20) print('noise1') noise2 = PerlinNoise(octaves=21) print('noise2') noise3 = PerlinNoise(octaves=22) print('noise3') noise4 = PerlinNoise(octaves=23) print('noise4') xpix, ypix = size, size pic = [] newnoise = noise(size) for i in range(xpix): row = [] for j in range(ypix): noise_val = noise1([i / xpix, j / ypix]) noise_val += 0.5 * noise2([i / xpix, j / ypix]) noise_val += 0.25 * noise3([i / xpix, j / ypix]) noise_val += 0.125 * noise4([i / xpix, j / ypix]) print(i, j) noise_val -= oldpic[i][j] * mountHeight noise_val += newnoise[i][j] * mountHeight row.append(noise_val) pic.append(row) print(pic) return pic
def _generate_map(self): map_margin = 32 mid_y = int(MAP_H / 2) noise1 = PerlinNoise(octaves=4) noise2 = PerlinNoise(octaves=12) noise3 = PerlinNoise(octaves=16) self.collision_noise = [0 for x in range(MAP_W)] for x in range(MAP_W): n = noise1([0, x/MAP_W]) + \ 0.5 * noise2([0, x/MAP_W]) + \ 0.25 * noise3([0, x/MAP_W]) y2 = clamp(mid_y + n * 200, map_margin, MAP_H - map_margin) self.collision_noise[x] = int(y2)
def make_perlin_mountains(self,num): #processo (lento) di creazione mappe con PerlinNoise print "[WORLD MAKER]: generation of perlin_maps\t" for k in xrange(num): print "%3.2f %%" % (float(k*100.0/num)),"\r", matr = [] P = PerlinNoise() matr = P.run(W_WORLD, H_WORLD, 0.25,[0.065,0.125,0.25,0.5,0.7,1,1.6,1.8,2],D_WORLD+randint(-2,2)) t = '' for i in range(W_WORLD): for j in range(H_WORLD): t += str(matr[i+j*W_WORLD]).ljust(2,' ')+ ' ' t += '\n' with open(r"lib_grows/perlin_maps/collina"+str(k)+".txt","w") as out: out.write(t)
def test_perlin_noise_works_in_2D(): """Check value in [-1, 1] range.""" noise = PerlinNoise(octaves=13) lim = 100 for i in range(lim): for j in range(lim): assert -1 <= noise([i / lim, j / lim]) <= 1
def perlin(terrain: np.ndarray, amplitude: float, octaves: float, seed: int): """ Apply the Perlin noise on a terrain. Parameters: ----------- terrain: numpy.ndarray, shape (M, N) Terrain to modify. amplitude: float Maximum noise amplitude. octaves: float Number of sub rectangles in each range [0, 1]. seed: int Specific seed you want to initialize the random generator with. """ p_noise = PerlinNoise(octaves=octaves, seed=seed) rows, cols = terrain.shape # Calculate the noise. noise = np.zeros((rows, cols)) for i in range(rows): for j in range(cols): noise[i][j] = p_noise([i / rows, j / cols]) # Apply the noise to the terrain. terrain += amplitude * (noise - np.min(noise))
def noise(size): noise = PerlinNoise(octaves=10, seed=uuid.uuid1().int >> 64) xpix, ypix = size, size pic = [[noise([i / xpix, j / ypix]) for j in range(xpix)] for i in range(ypix)] print(len(pic)) return pic
def naturaLine(octaves=9, y=200,scale=60): '''Makes a HORIZONTAL naturalistic line, like a mountain range or river using perlin nosie. For more riverlike look, increase the number of octaves.)''' noise = PerlinNoise(octaves=octaves) x_pix = w # width of line outline = turtle.Turtle() outline.up() outline.color('gray40') outline.goto(-int(w/2),y-30) # start lower to fill in below the peaks outline.begin_fill() outline.goto(-int(w/2),y) for i in range(w): yval = noise(i/int(x_pix)) outline.goto(i-int(w/2),yval*scale+y) outline.down() panel.update() # animates the line being drawn. outline.goto(int(w/2),y-30) # move down to fill in below the peaks outline.end_fill() outline.ht() panel.update()
def __init__(self, pos, size, color): new_pos = (pos[0] + random.randint(-size * 2, size * 2), pos[1] + random.randint(-size * 3, size)) super(FireParticle, self).__init__(new_pos, size, color) self.noise = PerlinNoise() self.start_size = size + random.randint(-10, 10) / 2 self.lifespan = 1 self.set_acceleration((0, -1))
def create_plants_perlin(flat_terrain_2d: MapgenTerrain, plants_profile: PerlinPlantProfile): octaves = math.sqrt( max(flat_terrain_2d.size_x, flat_terrain_2d.size_z) / 2) print('perlin octaves: ' + str(octaves)) perlin = PerlinNoise(octaves) for pp in plants_profile.plant_distributions: print(pp) create_plants_perlin_sub(flat_terrain_2d, pp, perlin)
def test_perlin_noise_works_in_1D(): """Check if values in -1 1 range and changes slowly""" p_noise = PerlinNoise() lim = 2000 prev = 0 for i in range(lim): noise = p_noise([i / lim]) assert -1 < noise < 1 assert 0 <= abs(noise - prev) < 0.001 prev = noise
def __init__(self, tcp_clients, noise, logs): self.noise = noise self.tcp_clients = tcp_clients self.chunks = {} self.entities = {} self.logs = logs self.chunks_indexes = {} self.last_id = 0 self.noise2 = PerlinNoise(octaves=6, seed=SEED) self.charge_from_file()
def __init__(self, x, y): global map_size # self.x = random.choice([random.randint(25,50),random.randint(map_size-50,map_size-25)]) # self.y = random.choice([random.randint(25,50),random.randint(map_size-50,map_size-25)]) self.x = x self.y = y self.noise = PerlinNoise(octaves=10, seed=random.randint(1, 100)) self.noise_resolution = 100 # self.noise_map = [[self.noise([i/noise_resolution,j/noise_resolution]) for i in range(1,noise_resolution)] for j in range(1,noise_resolution)] # self.noise_map = [[self.noise([i/self.noise_resolution, i/self.noise_resolution]) for i in range(self.noise_resolution)]] self.noise_index = 0
def test_perlin_noise_works_in_high_dimensions(): """Checks if values in -1 1 in high dimensions.""" n_dims = 10 noise = PerlinNoise(octaves=3) vec = [] n_passes = 100 for check in range(n_passes): vec = [] for dim in range(n_dims): vec.append(random.uniform(-10, 10)) assert -1 <= noise(vec) <= 1
def get_price(self, time): noise = PerlinNoise(octaves=10, seed=self.s + 1) s = (noise(time * 4) - 0.5) * 2 s *= (self.v * self.i) * 0.5 self.c = self.i + s #self.c = (self.i * 2) * (sum([PerlinNoise(octaves=8**j,seed=j + 69)(i/100)/(3**j) for j in range(10)]) + 0.5) if self.c < 1: self.c = 1 return self.c
def setup(self): #create the block terrain using perlin noise noise = PerlinNoise(octaves=self.noiseOctaves, seed=self.noiseSeed) self.blocks =[[0 for j in range(self.size)] for i in range(self.size)] for i in range(self.size): for j in range(self.size): value = noise([(i+self.noiseX)/self.zoom, (j+self.noiseY)/self.zoom]) if value <= self.landCutoff: #water self.blocks[i][j] = 0 else: self.blocks[i][j] = 1
def generate_terrain(self, seed=123): bkg_res = 10 for chunk in self.chunks: for j in range(bkg_res): for k in range(bkg_res): pattern = random.randint(0, 30) if pattern == 9: chunk.backgrounds.append( Background( [ chunk.rect.x + j * int(500 / bkg_res), chunk.rect.y + k * int(500 / bkg_res) ], "./img/background/ground_grass_flower_1.png", size=[int(500 / bkg_res), int(500 / bkg_res)], random_rotation=True)) elif pattern == 8 or pattern == 7: chunk.backgrounds.append( Background( [ chunk.rect.x + j * int(500 / bkg_res), chunk.rect.y + k * int(500 / bkg_res) ], "./img/background/ground_grass_leave_1.png", size=[int(500 / bkg_res), int(500 / bkg_res)], random_rotation=True)) # else: # chunk.backgrounds.append(Background([chunk.rect.x + j*int(500/bkg_res), chunk.rect.y + k*int(500/bkg_res)], # "./img/background/ground_grass_1.png", # size=[int(500/bkg_res),int(500/bkg_res)])) #ToDo: add procedural generation noise = PerlinNoise() for i in range(1000): pos = random_in_rect(self.rect) chance = noise( [pos[0] / self.rect.width, pos[1] / self.rect.height]) + 0.5 if chance > 0.2: self.add_entity(Oak(pos, size=[64, 64])) pos = random_in_rect(self.rect) chance = noise( [pos[0] / self.rect.width, pos[1] / self.rect.height]) + 0.5 if chance > 0.2: self.add_entity(Pine(pos, size=[64, 64])) #self.add_entity(Oak(random_in_rect(self.rect), size=[64,64])) #self.add_entity(Pine(random_in_rect(self.rect), size=[64,64])) if i % 4 == 0: self.add_entity(Rock(random_in_rect(self.rect), size=[32, 32]))
def get_perlin_line2(density: int, num_points: int, amplitude: int = 75) -> np.array: """ This one's unpredictable but has done some cool stuff """ noise1 = PerlinNoise(octaves=4) noise2 = PerlinNoise(octaves=8) noise3 = PerlinNoise(octaves=12) noise4 = PerlinNoise(octaves=16) pts = np.zeros((num_points, 2)) for i in range(num_points): noise_val = noise1(i / num_points) noise_val += 0.5 * noise2(i / num_points) noise_val += 0.25 * noise3(i / num_points) noise_val += 0.125 * noise4(i / num_points) noise_val *= amplitude pts[i] = np.array([0 + noise_val, i / density]) return pts
def __init__(self, width: int, height: int, amplitudes: list, zoomed: bool = False, elevation: float = 1): """ To simplify things, the map will me created blank. The user needs to refer to static methods to add and tweak features. :param width: :param height: :param amplitudes: related to the octaves """ self.width = int(width) self.height = int(height) amplitudes.sort(key=lambda x: abs(x)) noise = [] for amplitude in amplitudes: noise.append( PerlinNoise(octaves=amplitude, seed=int(datetime.timestamp(datetime.now())))) pic = [] for i in range(self.width): row = [] for j in range(self.height): noise_val = 0 for octave, amplitude in zip(noise, amplitudes): ''' Multiplying by 1/amplitude yields a zoomed in map, while amplitude/max(amplitudes) yields a very sparse ocean-filled map. It may be interesting to let the user play with this. ''' if zoomed: weight = (1 / amplitude) else: weight = amplitude / max(amplitudes) noise_val += weight * octave( [i / self.width, j / self.height]) row.append(noise_val) pic.append(row) self.map = np.array(pic) / sum(amplitudes) self.normalize_map() if elevation != 1: self.transform_elevation(elevation)
def __init__(self, pos, size=[16, 16], transparency=128, update_rate=1): super(Light, self).__init__() self.rect = pygame.Rect(pos[0], pos[1], size[0], size[1]) self.size = size self.update_rate = update_rate self.alpha = transparency self.images = [] self.image = None self.counter = 0 self.index = 0 self.noise = PerlinNoise(octaves=30, seed=777) self.is_active = True
def random_terrain(height, width=None): '''Generate a random terrain Returns a matrix of size (height, width), where the value of each grid point is determined using Perlin noise. ''' if width == None: width = height noise = PerlinNoise(octaves=10) grid = np.matrix([[noise([i / height, j / width]) for j in range(width)] for i in range(height)]) grid = (grid - grid.min()) / (grid.max() - grid.min()) start = (0, 0) goal = (height - 1, width - 1) return grid, start, goal
def makeExits(graph, width, height): noisey = PerlinNoise() width = (width) / pi height = (height) / pi leftNoise = (noisey([0, height / 2])) botNoise = noisey([width / 2, 0]) topNoise = noisey([width / 2, height]) rightNoise = noisey([width, height / 2]) bigy = 100 leftNoise = int(abs(leftNoise * bigy) % height) botNoise = int(abs(botNoise * bigy) % width) topNoise = int(abs(topNoise * bigy) % width) rightNoise = int(abs(rightNoise * bigy) % height) print(leftNoise, botNoise, topNoise, rightNoise)
def steps(cls, rows: int, cols: int, width: float, height: float, seed: int) -> np.ndarray: """ Generate a cubes terrain. Parameters: ----------- rows: int Number of rows of the terrain. cols: int Number of columns of the terrain. width: float Width and length of the cubes. height: float Maximum height of the cubes. seed: int Specific seed you want to initialize the random generator with. Return: ------- np.ndarray Resulting terrain. """ width = int(width / SCALE) # Generate the terrain terrain = cls.terrain(rows, cols) p_noise = PerlinNoise(octaves=STEPS_FREQUENCY, seed=seed) # Calculate the Perlin noise noise = np.zeros((rows, cols)) for i in range(0, rows, width): for j in range(0, cols, width): noise[i][j] = p_noise([i / rows, j / cols]) noise[i][j] += uniform(-STEPS_NOISE, STEPS_NOISE) # Add the blocks following the Perlin noise min_noise = np.min(noise) for i in range(0, rows, width): for j in range(0, cols, width): cls.step(terrain, i, j, width, width, height * (noise[i][j] - min_noise)) return terrain
def generate_heights(self, detail=1, freqmult=0.5): # Creates the random noise used to define the heights on the map noisemaps = [] start_time = time.time() print("Generating noise:") percentage = 100 // detail for i in range(1, detail + 1): noisemaps.append(PerlinNoise(2 ** i, random.randint(1, 1000))) for k in range(len(noisemaps)): self.height_values += freqmult ** k * np.array( [[noisemaps[k]([i / self.N, j / self.N]) for j in range(self.N)] for i in range(self.N)]) print("(" + str(percentage * (k + 1)) + "%" + " " + str(time.time() - start_time) + "s)", end=" ") sys.stdout.flush() print("Noise generated!") self.normalise_map(self.height_values) self.map_exists = True
def get_perlin_line(density: int, num_points: int, octaves: int = 4, amplitude: int = 75) -> np.array: """ Returns perlin noise based on given level of stochasticity Octaves: Number of layered noise functions being used to create the perlin noise. More octaves means more stochasticty and more jaggedness """ noise = PerlinNoise(octaves=octaves) pts = np.zeros((num_points, 2)) for i in range(num_points): noise_val = noise(i / num_points) noise_val += 0.25 * noise((i - 1) / num_points) noise_val += 0.125 * noise((i + 1) / num_points) noise_val *= amplitude pts[i] = np.array([0 + noise_val, i / density]) return pts