コード例 #1
0
    def generate_map(self, size=75, freq=20, lloyds=2, sigma=3.15, seed=None):
        """Initializes the map and generates the base noise and voronoi diagram"""
        # make up data points
        size_sqrt = size
        size = size**2

        # compute Voronoi tesselation
        if seed is not None:
            np.random.seed(seed)
        points = np.random.random((size, 2))
        vor = VoronoiDiagram(points)
        vor.generate_voronoi()
        self.voronoi_diagram = vor.relax_points(lloyds)
        regions, vertices = voronoi_finite_polygons_2d(self.voronoi_diagram)

        polygons = sorted(clip(points, regions, vertices),
                          key=lambda x: x[0][0])
        polygons_stripped = [[
            vert for vert in poly if 0 <= vert[0] <= 1 and 0 <= vert[1] <= 1
        ] for poly in polygons]
        self.polygons = [Polygon(i) for i in polygons_stripped]

        # Get noise, turn into 1D array
        noise = Perlin(freq, shuffle_seed=seed)
        noise_img = noise.create_image(save=True, width=200, height=200)
        noise_gauss = noise.gaussian_kernel(noise_img, nsig=sigma)
        noise_gauss.save("images/noise.png")
        noise_resized = noise_gauss.resize((size_sqrt, size_sqrt))
        noise_arr = np.array(noise_resized)

        self._generate_base_terrain(noise_arr)