def add_blockers(self): # TODO make noise scale and f constructor parameters/random pnoise = PerlinNoise(scale=30, octaves=1) f = 0.8 # Blockers amount density = pnoise.noise_grid(range(self.padded_size), range(self.padded_size)) + 0.5 density = f * (density + self.symmetric_array(density)) / 2 rs = rand(self.padded_size, self.padded_size) rs = (rs + self.symmetric_array(rs)) / 2 # plt.matshow(density) # plt.colorbar() self.padded_tiles[np.logical_and( rs < density, self.padded_tiles == 0)] = tiles["blocker"].index
def __init__(self, size=45, padded_size=79, symmetry="central", min_starting_dist=15, seed=None): if seed is None: seed = randint(2**31) self.seed = seed np.random.seed(seed) self.to_quadrant_funcs = dict(central=self.to_upper_triangle, diagonal=self.to_upper_triangle, horizontal=self.to_upper_half) self.reflect_point_funcs = dict(central=self.reflect_central) self.to_quadrant = self.to_quadrant_funcs[symmetry] self.reflect_point = self.reflect_point_funcs[symmetry] self.symmetry = symmetry self.symmetric_array = flip_funcs[symmetry] self.size = size self.padded_size = padded_size pad_before = (padded_size - size) // 2 pad_after = padded_size - size - pad_before instart = pad_before instop = pad_before + size self.instart = instart self.instop = instop self.inslice = slice(instart, instop) self.padded_tiles = np.zeros((self.padded_size, self.padded_size), dtype=int) self.min_starting_dist = min_starting_dist self.ownerships = -2 * np.ones( (self.padded_size, self.padded_size), dtype=int) pnoise = PerlinNoise(scale=20, octaves=2) # Take info from constructor elevations = pnoise.noise_grid(range(size), range(size)) + 0.5 elevations += self.symmetric_array(elevations) elevations = np.array(np.round(3 / 2 * elevations), dtype=int) self.padded_elevations = np.zeros((self.padded_size, self.padded_size), dtype=int) self.padded_elevations[self.inslice, self.inslice] = elevations for k in range(pad_before): self.padded_elevations[k, self.inslice] = elevations[0, :] self.padded_elevations[self.inslice, k] = elevations[:, 0] for k in range(pad_after): self.padded_elevations[-(k + 1), self.inslice] = elevations[-1, :] self.padded_elevations[self.inslice, -(k + 1)] = elevations[:, -1] self.padded_elevations[:instart, :instart] = elevations[ 0, 0] * np.ones((pad_before, pad_before)) self.padded_elevations[:instart, instop:] = elevations[0, -1] * np.ones( (pad_before, pad_after)) self.padded_elevations[ instop:, :instart] = elevations[-1, 0] * np.ones( (pad_after, pad_before)) self.padded_elevations[instop:, instop:] = elevations[-1, -1] * np.ones( (pad_after, pad_after)) water = self.padded_elevations == 0 self.terraform() self.waypoint_mask = np.ones_like(self.padded_tiles, dtype=bool) self.waypoint_mask[self.inslice, self.inslice] = False self.add_blockers() self.add_waypoints() self.mills = [] self.add_mills() self.remove_head_to_head() water = np.logical_and(self.padded_elevations < 3, water) water = np.logical_and(np.logical_not(self.waypoint_mask), water) self.padded_tiles[water] = tiles["water"].index self.padded_elevations[water] = 2 self.padded_elevations -= 2 cliffs = self.padded_elevations % 2 == 1 self.padded_tiles[cliffs] = tiles["cliff"].index self.add_roads() self.close_map()