def test_get_and_set_verbose(self): self.assertEqual(False, get_verbose(), "By default verbose should be set to False") set_verbose(True) self.assertEqual(True, get_verbose()) set_verbose(False) self.assertEqual(False, get_verbose())
def center_land(world): """Translate the map horizontally and vertically to put as much ocean as possible at the borders. It operates on elevation and plates map""" y_sums = world.layers['elevation'].data.sum(1) # 1 == sum along x-axis y_with_min_sum = y_sums.argmin() if get_verbose(): print("geo.center_land: height complete") x_sums = world.layers['elevation'].data.sum(0) # 0 == sum along y-axis x_with_min_sum = x_sums.argmin() if get_verbose(): print("geo.center_land: width complete") latshift = 0 world.layers['elevation'].data = numpy.roll(numpy.roll( world.layers['elevation'].data, -y_with_min_sum + latshift, axis=0), -x_with_min_sum, axis=1) world.layers['plates'].data = numpy.roll(numpy.roll( world.layers['plates'].data, -y_with_min_sum + latshift, axis=0), -x_with_min_sum, axis=1) if get_verbose(): print("geo.center_land: width complete")
def generate_world(w, step): if isinstance(step, str): step = Step.get_by_name(step) if not step.include_precipitations: return w # Prepare sufficient seeds for the different steps of the generation rng = numpy.random.RandomState(w.seed) # create a fresh RNG in case the global RNG is compromised (i.e. has been queried an indefinite amount of times before generate_world() was called) sub_seeds = rng.randint(0, numpy.iinfo(numpy.int32).max, size=100) # choose lowest common denominator (32 bit Windows numpy cannot handle a larger value) seed_dict = { 'PrecipitationSimulation': sub_seeds[ 0], # after 0.19.0 do not ever switch out the seeds here to maximize seed-compatibility 'ErosionSimulation': sub_seeds[ 1], 'WatermapSimulation': sub_seeds[ 2], 'IrrigationSimulation': sub_seeds[ 3], 'TemperatureSimulation': sub_seeds[ 4], 'HumiditySimulation': sub_seeds[ 5], 'PermeabilitySimulation': sub_seeds[ 6], 'BiomeSimulation': sub_seeds[ 7], 'IcecapSimulation': sub_seeds[ 8], '': sub_seeds[99] } TemperatureSimulation().execute(w, seed_dict['TemperatureSimulation']) # Precipitation with thresholds PrecipitationSimulation().execute(w, seed_dict['PrecipitationSimulation']) if not step.include_erosion: return w ErosionSimulation().execute(w, seed_dict['ErosionSimulation']) # seed not currently used if get_verbose(): print("...erosion calculated") WatermapSimulation().execute(w, seed_dict['WatermapSimulation']) # seed not currently used # FIXME: create setters IrrigationSimulation().execute(w, seed_dict['IrrigationSimulation']) # seed not currently used HumiditySimulation().execute(w, seed_dict['HumiditySimulation']) # seed not currently used PermeabilitySimulation().execute(w, seed_dict['PermeabilitySimulation']) cm, biome_cm = BiomeSimulation().execute(w, seed_dict['BiomeSimulation']) # seed not currently used for cl in cm.keys(): count = cm[cl] if get_verbose(): print("%s = %i" % (str(cl), count)) if get_verbose(): print('') # empty line print('Biome obtained:') for cl in biome_cm.keys(): count = biome_cm[cl] if get_verbose(): print(" %30s = %7i" % (str(cl), count)) IcecapSimulation().execute(w, seed_dict['IcecapSimulation']) # makes use of temperature-map return w
def execute(self, world, seed): if get_verbose(): start_time = time.time() pre_calculated = self._calculate(seed, world.width, world.height) ths = [('low', find_threshold_f(pre_calculated, 0.75, world.ocean)), ('med', find_threshold_f(pre_calculated, 0.3, world.ocean)), ('hig', None)] world.set_precipitation(pre_calculated, ths) if get_verbose(): elapsed_time = time.time() - start_time print("...precipitations calculated. Elapsed time %f seconds." % elapsed_time)
def execute(self, world, seed): if get_verbose(): start_time = time.time() pre_calculated = self._calculate(seed, world) ths = [ ('low', find_threshold_f(pre_calculated, 0.75, world.ocean)), ('med', find_threshold_f(pre_calculated, 0.3, world.ocean)), ('hig', None) ] world.set_precipitation(pre_calculated, ths) if get_verbose(): elapsed_time = time.time() - start_time print( "...precipitations calculated. Elapsed time %f seconds." % elapsed_time)
def execute(self, world, seed): if get_verbose(): start_time = time.time() pre_calculated = self._calculate(seed, world) ocean = world.layers['ocean'].data ths = [ ('low', find_threshold_f(pre_calculated, 0.75, ocean)), ('med', find_threshold_f(pre_calculated, 0.3, ocean)), ('hig', None) ] world.precipitation = (pre_calculated, ths) if get_verbose(): elapsed_time = time.time() - start_time print( "...precipitations calculated. Elapsed time %f seconds." % elapsed_time)
def operation_ancient_map(world, map_filename, resize_factor, sea_color, draw_biome, draw_rivers, draw_mountains, draw_outer_land_border): draw_ancientmap_on_file(world, map_filename, resize_factor, sea_color, draw_biome, draw_rivers, draw_mountains, draw_outer_land_border, get_verbose()) print("+ ancient map generated in '%s'" % map_filename)
def center_land(world): """Translate the map horizontally and vertically to put as much ocean as possible at the borders. It operates on elevation and plates map""" y_sums = world.layers['elevation'].data.sum(1) # 1 == sum along x-axis y_with_min_sum = y_sums.argmin() if get_verbose(): print("geo.center_land: height complete") x_sums = world.layers['elevation'].data.sum(0) # 0 == sum along y-axis x_with_min_sum = x_sums.argmin() if get_verbose(): print("geo.center_land: width complete") latshift = 0 world.layers['elevation'].data = numpy.roll(numpy.roll(world.layers['elevation'].data, -y_with_min_sum + latshift, axis=0), - x_with_min_sum, axis=1) world.layers['plates'].data = numpy.roll(numpy.roll(world.layers['plates'].data, -y_with_min_sum + latshift, axis=0), - x_with_min_sum, axis=1) if get_verbose(): print("geo.center_land: width complete")
def center_land(world): """Translate the map horizontally and vertically to put as much ocean as possible at the borders. It operates on elevation and plates map""" min_sum_on_y = None y_with_min_sum = None latshift = 0 for y in range(world.height): sum_on_y = 0 for x in range(world.width): sum_on_y += world.elevation['data'][y][x] if min_sum_on_y is None or sum_on_y < min_sum_on_y: min_sum_on_y = sum_on_y y_with_min_sum = y if get_verbose(): print("geo.center_land: height complete") min_sum_on_x = None x_with_min_sum = None for x in range(world.width): sum_on_x = 0 for y in range(world.height): sum_on_x += world.elevation['data'][y][x] if min_sum_on_x is None or sum_on_x < min_sum_on_x: min_sum_on_x = sum_on_x x_with_min_sum = x if get_verbose(): print("geo.center_land: width complete") new_elevation_data = [] new_plates = [] for y in range(world.height): new_elevation_data.append([]) new_plates.append([]) src_y = (y_with_min_sum + y - latshift) % world.height for x in range(world.width): src_x = (x_with_min_sum + x) % world.width new_elevation_data[y].append(world.elevation['data'][src_y][src_x]) new_plates[y].append(world.plates[src_y][src_x]) world.elevation['data'] = new_elevation_data world.plates = new_plates if get_verbose(): print("geo.center_land: width complete")
def generate_world(w, step): if isinstance(step, str): step = Step.get_by_name(step) seed = w.seed if not step.include_precipitations: return w # Precipitation with thresholds PrecipitationSimulation().execute(w, seed) if not step.include_erosion: return w ErosionSimulation().execute(w, seed) if get_verbose(): print("...erosion calculated") WatermapSimulation().execute(w, seed) # FIXME: create setters IrrigationSimulation().execute(w, seed) TemperatureSimulation().execute(w, seed) HumiditySimulation().execute(w, seed) PermeabilitySimulation().execute(w, seed) cm, biome_cm = BiomeSimulation().execute(w, seed) for cl in cm.keys(): count = cm[cl] if get_verbose(): print("%s = %i" % (str(cl), count)) if get_verbose(): print('') # empty line print('Biome obtained:') for cl in biome_cm.keys(): count = biome_cm[cl] if get_verbose(): print(" %30s = %7i" % (str(cl), count)) return w
def center_land(world): """Translate the map horizontally and vertically to put as much ocean as possible at the borders. It operates on elevation and plates map""" min_sum_on_y = None y_with_min_sum = None for y in range(world.height): sum_on_y = 0 for x in range(world.width): sum_on_y += world.elevation['data'][y][x] if min_sum_on_y is None or sum_on_y < min_sum_on_y: min_sum_on_y = sum_on_y y_with_min_sum = y if get_verbose(): print("geo.center_land: height complete") min_sum_on_x = None x_with_min_sum = None for x in range(world.width): sum_on_x = 0 for y in range(world.height): sum_on_x += world.elevation['data'][y][x] if min_sum_on_x is None or sum_on_x < min_sum_on_x: min_sum_on_x = sum_on_x x_with_min_sum = x if get_verbose(): print("geo.center_land: width complete") new_elevation_data = [] new_plates = [] for y in range(world.height): new_elevation_data.append([]) new_plates.append([]) src_y = (y_with_min_sum + y) % world.height for x in range(world.width): src_x = (x_with_min_sum + x) % world.width new_elevation_data[y].append(world.elevation['data'][src_y][src_x]) new_plates[y].append(world.plates[src_y][src_x]) world.elevation['data'] = new_elevation_data world.plates = new_plates if get_verbose(): print("geo.center_land: width complete")
def generate_world(w, step): if isinstance(step, str): step = Step.get_by_name(step) seed = w.seed if not step.include_precipitations: return w # Precipitation with thresholds PrecipitationSimulation().execute(w, seed) if not step.include_erosion: return w ErosionSimulation().execute(w, seed) if get_verbose(): print("...erosion calculated") WatermapSimulation().execute(w, seed) # FIXME: create setters IrrigationSimulation().execute(w, seed) HumiditySimulation().execute(w, seed) TemperatureSimulation().execute(w, seed) PermeabilitySimulation().execute(w, seed) cm, biome_cm = BiomeSimulation().execute(w, seed) for cl in cm.keys(): count = cm[cl] if get_verbose(): print("%s = %i" % (str(cl), count)) if get_verbose(): print('') # empty line print('Biome obtained:') for cl in biome_cm.keys(): count = biome_cm[cl] if get_verbose(): print(" %30s = %7i" % (str(cl), count)) return w
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), draw_biome = True, draw_rivers = True, draw_mountains = True, draw_outer_land_border = False, verbose=get_verbose()): rng = numpy.random.RandomState(world.seed) # create our own random generator if verbose: start_time = time.time() land_color = ( 181, 166, 127, 255) # TODO: Put this in the argument list too?? borders = _find_land_borders(world, resize_factor) if draw_outer_land_border: outer_borders = _find_outer_borders(world, resize_factor, borders) outer_borders = _find_outer_borders(world, resize_factor, outer_borders) if draw_mountains: # TODO: numpy offers masked arrays - maybe they can be leveraged for all this? mountains_mask = _find_mountains_mask(world, resize_factor) if draw_biome: boreal_forest_mask = _find_boreal_forest_mask(world, resize_factor) temperate_forest_mask = _find_temperate_forest_mask(world, resize_factor) warm_temperate_forest_mask = \ _find_warm_temperate_forest_mask(world, resize_factor) tropical_dry_forest_mask = _find_tropical_dry_forest_mask(world, resize_factor) # jungle is actually Tropical Rain Forest and Tropical Seasonal Forest jungle_mask = _mask(world, world.is_jungle, resize_factor) tundra_mask = _mask(world, world.is_tundra, resize_factor) # savanna is actually Tropical semi-arid savanna_mask = _mask(world, world.is_savanna, resize_factor) cold_parklands_mask = _mask(world, world.is_cold_parklands, resize_factor) steppe_mask = _mask(world, world.is_steppe, resize_factor) cool_desert_mask = _mask(world, world.is_cool_desert, resize_factor) chaparral_mask = _mask(world, world.is_chaparral, resize_factor) hot_desert_mask = _mask(world, world.is_hot_desert, resize_factor) rock_desert_mask = _mask(world, world.is_hot_desert, resize_factor) # TODO: add is_desert_mask def unset_mask(pos): x, y = pos mountains_mask[y, x] = 0 def unset_boreal_forest_mask(pos): x, y = pos boreal_forest_mask[y, x] = 0 def unset_temperate_forest_mask(pos): x, y = pos temperate_forest_mask[y, x] = 0 def unset_warm_temperate_forest_mask(pos): x, y = pos warm_temperate_forest_mask[y, x] = 0 def unset_tropical_dry_forest_mask(pos): x, y = pos tropical_dry_forest_mask[y, x] = 0 def unset_jungle_mask(pos): x, y = pos jungle_mask[y, x] = 0 def unset_tundra_mask(pos): x, y = pos tundra_mask[y, x] = 0 def unset_savanna_mask(pos): x, y = pos savanna_mask[y, x] = 0 def unset_hot_desert_mask(pos): x, y = pos hot_desert_mask[y, x] = 0 def unset_rock_desert_mask(pos): x, y = pos rock_desert_mask[y, x] = 0 def unset_cold_parklands_mask(pos): x, y = pos cold_parklands_mask[y, x] = 0 def unset_steppe_mask(pos): x, y = pos steppe_mask[y, x] = 0 def unset_cool_desert_mask(pos): x, y = pos cool_desert_mask[y, x] = 0 def unset_chaparral_mask(pos): x, y = pos chaparral_mask[y, x] = 0 def on_border(pos): x, y = pos return borders[y, x] if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() border_color = (0, 0, 0, 255) outer_border_color = gradient(0.5, 0, 1.0, rgba_to_rgb(border_color), rgba_to_rgb(sea_color)) for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): xf = int(x / resize_factor) yf = int(y / resize_factor) if borders[y, x]: target.set_pixel(x, y, border_color) elif draw_outer_land_border and outer_borders[y, x]: target.set_pixel(x, y, outer_border_color) elif world.ocean[yf, xf]: target.set_pixel(x, y, sea_color) else: target.set_pixel(x, y, land_color) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() def anti_alias(steps): def _anti_alias_step(): for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): _anti_alias_point(x, y) def _anti_alias_point(x, y): n = 2 tot_r = target[y, x][0] * 2 tot_g = target[y, x][1] * 2 tot_b = target[y, x][2] * 2 for dy in range(-1, +2): py = y + dy if py > 0 and py < resize_factor * world.height: for dx in range(-1, +2): px = x + dx if px > 0 and px < resize_factor * world.width: n += 1 tot_r += target[py, px][0] tot_g += target[py, px][1] tot_b += target[py, px][2] r = int(tot_r / n) g = int(tot_g / n) b = int(tot_b / n) target[y, x] = (r, g, b, 255) for i in range(steps): _anti_alias_step() anti_alias(1) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw glacier if draw_biome: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y, x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw tundra if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tundra_mask[y, x] > 0: _draw_tundra(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw tundra " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw cold parklands for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cold_parklands_mask[y, x] > 0: _draw_cold_parklands(target, x, y) # Draw steppes for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if steppe_mask[y, x] > 0: _draw_steppe(target, x, y) # Draw chaparral for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if chaparral_mask[y, x] > 0: _draw_chaparral(target, x, y) # Draw savanna for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if savanna_mask[y, x] > 0: _draw_savanna(target, x, y) # Draw cool desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cool_desert_mask[y, x] > 0: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_cool_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_cool_desert_mask) # Draw hot desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if hot_desert_mask[y, x] > 0: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_hot_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_hot_desert_mask) # Draw boreal forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if boreal_forest_mask[y, x] > 0: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_boreal_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_boreal_forest_mask) # Draw temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if temperate_forest_mask[y, x] > 0: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: if rng.random_sample() <= .5: _draw_temperate_forest1(target, x, y, w=w, h=h) else: _draw_temperate_forest2(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_temperate_forest_mask) # Draw warm temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if warm_temperate_forest_mask[y, x] > 0: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_warm_temperate_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_warm_temperate_forest_mask) # Draw dry tropical forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tropical_dry_forest_mask[y, x] > 0: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_tropical_dry_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_tropical_dry_forest_mask) # Draw jungle for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if jungle_mask[y, x] > 0: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_jungle(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_jungle_mask) if draw_rivers: draw_rivers_on_image(world, target, resize_factor) # Draw mountains if draw_mountains: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y, x] > 0: w = mountains_mask[y, x] h = 3 + int(world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_a_mountain(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_mask) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), draw_biome=True, draw_rivers=True, draw_mountains=True, draw_outer_land_border=False, verbose=get_verbose()): rng = numpy.random.RandomState( world.seed) # create our own random generator if verbose: start_time = time.time() land_color = (181, 166, 127, 255 ) # TODO: Put this in the argument list too?? scaled_ocean = world.ocean.repeat(resize_factor, 0).repeat(resize_factor, 1) borders = numpy.zeros( (resize_factor * world.height, resize_factor * world.width), bool) borders[count_neighbours(scaled_ocean) > 0] = True borders[scaled_ocean] = False # cache neighbours count at different radii border_neighbours = {} border_neighbours[6] = numpy.rint(count_neighbours(borders, 6)) border_neighbours[9] = numpy.rint(count_neighbours(borders, 9)) if draw_outer_land_border: inner_borders = borders outer_borders = None for i in range(2): _outer_borders = numpy.zeros( (resize_factor * world.height, resize_factor * world.width), bool) _outer_borders[count_neighbours(inner_borders) > 0] = True _outer_borders[inner_borders] = False _outer_borders[numpy.logical_not(scaled_ocean)] = False outer_borders = _outer_borders inner_borders = outer_borders if draw_mountains: mountains_mask = _find_mountains_mask(world, resize_factor) if draw_biome: biome_masks = _build_biome_group_masks(world, resize_factor) def _draw_biome(name, _func, w, h, r, _alt_func=None): if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks[name][y, x] > 0: if r == 0 or border_neighbours[r][y, x] <= 2: if _alt_func is not None and rng.random_sample( ) > .5: _alt_func(target, x, y, w, h) else: _func(target, x, y, w, h) biome_masks[name][y - r:y + r + 1, x - r:x + r + 1] = 0.0 if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_ancientmap: " + name + " Elapsed time " + str(elapsed_time) + " seconds.") if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() border_color = (0, 0, 0, 255) outer_border_color = gradient(0.5, 0, 1.0, rgba_to_rgb(border_color), rgba_to_rgb(sea_color)) # start in low resolution num_channels = 4 channels = numpy.zeros((num_channels, world.height, world.width), int) for c in range(num_channels): channels[c] = land_color[c] channels[c][world.ocean] = sea_color[c] # now go full resolution channels = channels.repeat(resize_factor, 1).repeat(resize_factor, 2) if draw_outer_land_border: for c in range(num_channels): channels[c][outer_borders] = outer_border_color[c] for c in range(num_channels): channels[c][borders] = border_color[c] if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() # don't anti-alias the alpha channel for c in range(num_channels - 1): channels[c] = anti_alias_channel(channels[c], 1) # switch from channel major storage to pixel major storage for c in range(num_channels): target[:, :, c] = channels[c, :, :] if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") if draw_biome: # Draw glacier if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y, x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") _draw_biome('tundra', _draw_tundra, 0, 0, 0) _draw_biome('cold parklands', _draw_cold_parklands, 0, 0, 0) _draw_biome('steppe', _draw_steppe, 0, 0, 0) _draw_biome('chaparral', _draw_chaparral, 0, 0, 0) _draw_biome('savanna', _draw_savanna, 0, 0, 0) _draw_biome('cool desert', _draw_cool_desert, 8, 2, 9) _draw_biome('hot desert', _draw_hot_desert, 8, 2, 9) _draw_biome('boreal forest', _draw_boreal_forest, 4, 5, 6) _draw_biome('cool temperate forest', _draw_temperate_forest1, 4, 5, 6, _draw_temperate_forest2) _draw_biome('warm temperate forest', _draw_warm_temperate_forest, 4, 5, 6) _draw_biome('tropical dry forest group', _draw_tropical_dry_forest, 4, 5, 6) _draw_biome('jungle', _draw_jungle, 4, 5, 6) # TODO: there was a stub for a rock desert biome group # it should be super easy to introduce that group with the new # biome group concept but since it did nothing I removed the stub if draw_rivers: draw_rivers_on_image(world, target, resize_factor) # Draw mountains if draw_mountains: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y, x] > 0: w = mountains_mask[y, x] h = 3 + int( world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if r not in border_neighbours: border_neighbours[r] = numpy.rint( count_neighbours(borders, r)) if border_neighbours[r][y, x] <= 2: _draw_a_mountain(target, x, y, w=w, h=h) mountains_mask[y - r:y + r + 1, x - r:x + r + 1] = 0.0 if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), draw_biome = True, draw_rivers = True, draw_mountains = True, draw_outer_land_border = False, verbose=get_verbose()): rng = numpy.random.RandomState(world.seed) # create our own random generator if verbose: start_time = time.time() land_color = ( 181, 166, 127, 255) # TODO: Put this in the argument list too?? scaled_ocean = world.ocean.repeat(resize_factor, 0).repeat(resize_factor, 1) borders = numpy.zeros((resize_factor * world.height, resize_factor * world.width), bool) borders[count_neighbours(scaled_ocean) > 0] = True borders[scaled_ocean] = False # cache neighbours count at different radii border_neighbours = {} border_neighbours[6] = numpy.rint(count_neighbours(borders, 6)) border_neighbours[9] = numpy.rint(count_neighbours(borders, 9)) if draw_outer_land_border: inner_borders = borders outer_borders = None for i in range(2): _outer_borders = numpy.zeros((resize_factor * world.height, resize_factor * world.width), bool) _outer_borders[count_neighbours(inner_borders) > 0] = True _outer_borders[inner_borders] = False _outer_borders[numpy.logical_not(scaled_ocean)] = False outer_borders = _outer_borders inner_borders = outer_borders if draw_mountains: mountains_mask = _find_mountains_mask(world, resize_factor) if draw_biome: biome_masks = _build_biome_group_masks(world, resize_factor) def _draw_biome(name, _func, w, h, r, _alt_func = None): if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks[name][y, x] > 0: if r == 0 or border_neighbours[r][y,x] <= 2: if _alt_func is not None and rng.random_sample() > .5: _alt_func(target, x, y, w, h) else: _func(target, x, y, w, h) biome_masks[name][y-r:y+r+1,x-r:x+r+1] = 0.0 if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_ancientmap: " + name + " Elapsed time " + str(elapsed_time) + " seconds.") if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() border_color = (0, 0, 0, 255) outer_border_color = gradient(0.5, 0, 1.0, rgba_to_rgb(border_color), rgba_to_rgb(sea_color)) # start in low resolution num_channels = 4 channels = numpy.zeros((num_channels, world.height, world.width), int) for c in range(num_channels): channels[c] = land_color[c] channels[c][world.ocean] = sea_color[c] # now go full resolution channels = channels.repeat(resize_factor, 1).repeat(resize_factor, 2) if draw_outer_land_border: for c in range(num_channels): channels[c][outer_borders] = outer_border_color[c] for c in range(num_channels): channels[c][borders] = border_color[c] if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() # don't anti-alias the alpha channel for c in range(num_channels-1): channels[c] = anti_alias_channel(channels[c], 1) # switch from channel major storage to pixel major storage for c in range(num_channels): target[:,:,c] = channels[c,:,:] if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") if draw_biome: # Draw glacier if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y, x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") _draw_biome('tundra', _draw_tundra, 0, 0, 0) _draw_biome('cold parklands', _draw_cold_parklands, 0, 0, 0) _draw_biome('steppe', _draw_steppe, 0, 0, 0) _draw_biome('chaparral', _draw_chaparral, 0, 0, 0) _draw_biome('savanna', _draw_savanna, 0, 0, 0) _draw_biome('cool desert', _draw_cool_desert, 8, 2, 9) _draw_biome('hot desert', _draw_hot_desert, 8, 2, 9) _draw_biome('boreal forest', _draw_boreal_forest, 4, 5, 6) _draw_biome('cool temperate forest', _draw_temperate_forest1, 4, 5, 6, _draw_temperate_forest2) _draw_biome('warm temperate forest', _draw_warm_temperate_forest, 4, 5, 6) _draw_biome('tropical dry forest group', _draw_tropical_dry_forest, 4, 5, 6) _draw_biome('jungle', _draw_jungle, 4, 5, 6) # TODO: there was a stub for a rock desert biome group # it should be super easy to introduce that group with the new # biome group concept but since it did nothing I removed the stub if draw_rivers: draw_rivers_on_image(world, target, resize_factor) # Draw mountains if draw_mountains: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y, x] > 0: w = mountains_mask[y, x] h = 3 + int(world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if r not in border_neighbours: border_neighbours[r] = numpy.rint(count_neighbours(borders, r)) if border_neighbours[r][y,x] <= 2: _draw_a_mountain(target, x, y, w=w, h=h) mountains_mask[y-r:y+r+1,x-r:x+r+1] = 0.0 if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")
def generate_world(w, step): if isinstance(step, str): step = Step.get_by_name(step) if not step.include_precipitations: return w # Prepare sufficient seeds for the different steps of the generation rng = numpy.random.RandomState( w.seed ) # create a fresh RNG in case the global RNG is compromised (i.e. has been queried an indefinite amount of times before generate_world() was called) sub_seeds = rng.randint( 0, numpy.iinfo(numpy.int32).max, size=100 ) # choose lowest common denominator (32 bit Windows numpy cannot handle a larger value) seed_dict = { 'PrecipitationSimulation': sub_seeds[ 0], # after 0.19.0 do not ever switch out the seeds here to maximize seed-compatibility 'ErosionSimulation': sub_seeds[1], 'WatermapSimulation': sub_seeds[2], 'IrrigationSimulation': sub_seeds[3], 'TemperatureSimulation': sub_seeds[4], 'HumiditySimulation': sub_seeds[5], 'PermeabilitySimulation': sub_seeds[6], 'BiomeSimulation': sub_seeds[7], 'IcecapSimulation': sub_seeds[8], '': sub_seeds[99] } TemperatureSimulation().execute(w, seed_dict['TemperatureSimulation']) # Precipitation with thresholds PrecipitationSimulation().execute(w, seed_dict['PrecipitationSimulation']) if not step.include_erosion: return w ErosionSimulation().execute( w, seed_dict['ErosionSimulation']) # seed not currently used if get_verbose(): print("...erosion calculated") WatermapSimulation().execute( w, seed_dict['WatermapSimulation']) # seed not currently used # FIXME: create setters IrrigationSimulation().execute( w, seed_dict['IrrigationSimulation']) # seed not currently used HumiditySimulation().execute( w, seed_dict['HumiditySimulation']) # seed not currently used PermeabilitySimulation().execute(w, seed_dict['PermeabilitySimulation']) cm, biome_cm = BiomeSimulation().execute( w, seed_dict['BiomeSimulation']) # seed not currently used for cl in cm.keys(): count = cm[cl] if get_verbose(): print("%s = %i" % (str(cl), count)) if get_verbose(): print('') # empty line print('Biome obtained:') for cl in biome_cm.keys(): count = biome_cm[cl] if get_verbose(): print(" %30s = %7i" % (str(cl), count)) IcecapSimulation().execute( w, seed_dict['IcecapSimulation']) # makes use of temperature-map return w
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), verbose=get_verbose()): random.seed(world.seed * 11) if verbose: start_time = time.time() land_color = ( 181, 166, 127, 255) # TODO: Put this in the argument list too?? borders = _find_land_borders(world, resize_factor) mountains_mask = _find_mountains_mask(world, resize_factor) boreal_forest_mask = _find_boreal_forest_mask(world, resize_factor) temperate_forest_mask = _find_temperate_forest_mask(world, resize_factor) warm_temperate_forest_mask = \ _find_warm_temperate_forest_mask(world, resize_factor) tropical_dry_forest_mask = _find_tropical_dry_forest_mask(world, resize_factor) # jungle is actually Tropical Rain Forest and Tropical Seasonal Forest jungle_mask = _mask(world, world.is_jungle, resize_factor) tundra_mask = _mask(world, world.is_tundra, resize_factor) # savanna is actually Tropical semi-arid savanna_mask = _mask(world, world.is_savanna, resize_factor) cold_parklands_mask = _mask(world, world.is_cold_parklands, resize_factor) steppe_mask = _mask(world, world.is_steppe, resize_factor) cool_desert_mask = _mask(world, world.is_cool_desert, resize_factor) chaparral_mask = _mask(world, world.is_chaparral, resize_factor) hot_desert_mask = _mask(world, world.is_hot_desert, resize_factor) rock_desert_mask = _mask(world, world.is_hot_desert, resize_factor) # TODO: add is_desert_mask def unset_mask(pos): x, y = pos mountains_mask[y][x] = False def unset_boreal_forest_mask(pos): x, y = pos boreal_forest_mask[y][x] = False def unset_temperate_forest_mask(pos): x, y = pos temperate_forest_mask[y][x] = False def unset_warm_temperate_forest_mask(pos): x, y = pos warm_temperate_forest_mask[y][x] = False def unset_tropical_dry_forest_mask(pos): x, y = pos tropical_dry_forest_mask[y][x] = False def unset_jungle_mask(pos): x, y = pos jungle_mask[y][x] = False def unset_tundra_mask(pos): x, y = pos tundra_mask[y][x] = False def unset_savanna_mask(pos): x, y = pos savanna_mask[y][x] = False def unset_hot_desert_mask(pos): x, y = pos hot_desert_mask[y][x] = False def unset_rock_desert_mask(pos): x, y = pos rock_desert_mask[y][x] = False def unset_cold_parklands_mask(pos): x, y = pos cold_parklands_mask[y][x] = False def unset_steppe_mask(pos): x, y = pos steppe_mask[y][x] = False def unset_cool_desert_mask(pos): x, y = pos cool_desert_mask[y][x] = False def unset_chaparral_mask(pos): x, y = pos chaparral_mask[y][x] = False def on_border(pos): x, y = pos return borders[y][x] if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() min_elev = None max_elev = None for y in range(world.height): for x in range(world.width): e = world.elevation['data'][y][x] if min_elev is None or e < min_elev: min_elev = e if max_elev is None or e > max_elev: max_elev = e # elev_delta = max_elev - min_elev # TODO: no longer used? if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: max, min elevation " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): xf = int(x / resize_factor) yf = int(y / resize_factor) if borders[y][x]: target.set_pixel(x, y, (0, 0, 0, 255)) elif world.ocean[yf][xf]: target.set_pixel(x, y, sea_color) else: target.set_pixel(x, y, land_color) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() def anti_alias(steps): def _anti_alias_step(): for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): _anti_alias_point(x, y) def _anti_alias_point(x, y): n = 2 tot_r = target[x, y][0] * 2 tot_g = target[x, y][1] * 2 tot_b = target[x, y][2] * 2 for dy in range(-1, +2): py = y + dy if py > 0 and py < resize_factor * world.height: for dx in range(-1, +2): px = x + dx if px > 0 and px < resize_factor * world.width: n += 1 tot_r += target[px, py][0] tot_g += target[px, py][1] tot_b += target[px, py][2] r = int(tot_r / n) g = int(tot_g / n) b = int(tot_b / n) target[x, y] = (r, g, b, 255) for i in range(steps): _anti_alias_step() anti_alias(1) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw glacier if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y][x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw tundra if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tundra_mask[y][x]: _draw_tundra(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw tundra " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw cold parklands for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cold_parklands_mask[y][x]: _draw_cold_parklands(target, x, y) # Draw steppes for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if steppe_mask[y][x]: _draw_steppe(target, x, y) # Draw chaparral for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if chaparral_mask[y][x]: _draw_chaparral(target, x, y) # Draw savanna for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if savanna_mask[y][x]: _draw_savanna(target, x, y) # Draw cool desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cool_desert_mask[y][x]: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_cool_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_cool_desert_mask) # Draw hot desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if hot_desert_mask[y][x]: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_hot_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_hot_desert_mask) # Draw boreal forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if boreal_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_boreal_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_boreal_forest_mask) # Draw temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if temperate_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: if random.random() <= .5: _draw_temperate_forest1(target, x, y, w=w, h=h) else: _draw_temperate_forest2(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_temperate_forest_mask) # Draw warm temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if warm_temperate_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_warm_temperate_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_warm_temperate_forest_mask) # Draw dry tropical forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tropical_dry_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_tropical_dry_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_tropical_dry_forest_mask) # Draw jungle for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if jungle_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_jungle(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_jungle_mask) draw_rivers_on_image(world, target, resize_factor) # Draw mountains if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y][x]: w = mountains_mask[y][x] h = 3 + int(world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_a_mountain(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_mask) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), draw_biome=True, draw_rivers=True, draw_mountains=True, draw_outer_land_border=False, verbose=get_verbose()): rng = numpy.random.RandomState( world.seed) # create our own random generator if verbose: start_time = time.time() land_color = (181, 166, 127, 255 ) # TODO: Put this in the argument list too?? borders = _find_land_borders(world, resize_factor) if draw_outer_land_border: outer_borders = _find_outer_borders(world, resize_factor, borders) outer_borders = _find_outer_borders(world, resize_factor, outer_borders) if draw_mountains: # TODO: numpy offers masked arrays - maybe they can be leveraged for all this? mountains_mask = _find_mountains_mask(world, resize_factor) if draw_biome: biome_masks = _build_biome_group_masks(world, resize_factor) # TODO: there was a stub for a rock desert biome group here # it should be super easy to introduce that group with the new # biome group concept but since it did nothing I removed the stub def unset_mask(pos): x, y = pos mountains_mask[y, x] = 0 def unset_boreal_forest_mask(pos): x, y = pos biome_masks['boreal forest'][y, x] = 0 def unset_temperate_forest_mask(pos): x, y = pos biome_masks['cool temperate forest'][y, x] = 0 def unset_warm_temperate_forest_mask(pos): x, y = pos biome_masks['warm temperate forest'][y, x] = 0 def unset_tropical_dry_forest_mask(pos): x, y = pos biome_masks['tropical dry forest group'][y, x] = 0 def unset_jungle_mask(pos): x, y = pos biome_masks['jungle'][y, x] = 0 def unset_hot_desert_mask(pos): x, y = pos biome_masks['hot desert'][y, x] = 0 def unset_cool_desert_mask(pos): x, y = pos biome_masks['cool desert'][y, x] = 0 def on_border(pos): x, y = pos return borders[y, x] if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() border_color = (0, 0, 0, 255) outer_border_color = gradient(0.5, 0, 1.0, rgba_to_rgb(border_color), rgba_to_rgb(sea_color)) for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): xf = int(x / resize_factor) yf = int(y / resize_factor) if borders[y, x]: target.set_pixel(x, y, border_color) elif draw_outer_land_border and outer_borders[y, x]: target.set_pixel(x, y, outer_border_color) elif world.is_ocean((xf, yf)): target.set_pixel(x, y, sea_color) else: target.set_pixel(x, y, land_color) if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() def anti_alias(steps): def _anti_alias_step(): for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): _anti_alias_point(x, y) def _anti_alias_point(x, y): n = 2 tot_r = target[y, x][0] * 2 tot_g = target[y, x][1] * 2 tot_b = target[y, x][2] * 2 for dy in range(-1, +2): py = y + dy if py > 0 and py < resize_factor * world.height: for dx in range(-1, +2): px = x + dx if px > 0 and px < resize_factor * world.width: n += 1 tot_r += target[py, px][0] tot_g += target[py, px][1] tot_b += target[py, px][2] r = int(tot_r / n) g = int(tot_g / n) b = int(tot_b / n) target[y, x] = (r, g, b, 255) for i in range(steps): _anti_alias_step() anti_alias(1) if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw glacier if draw_biome: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y, x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw tundra if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['tundra'][y, x] > 0: _draw_tundra(target, x, y) if verbose: elapsed_time = time.time() - start_time print("...drawing_functions.draw_oldmap_on_pixel: draw tundra " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw cold parklands for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['cold parklands'][y, x] > 0: _draw_cold_parklands(target, x, y) # Draw steppes for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['steppe'][y, x] > 0: _draw_steppe(target, x, y) # Draw chaparral for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['chaparral'][y, x] > 0: _draw_chaparral(target, x, y) # Draw savanna for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['savanna'][y, x] > 0: _draw_savanna(target, x, y) # Draw cool desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['cool desert'][y, x] > 0: w = 8 h = 2 r = 9 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_cool_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_cool_desert_mask) # Draw hot desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['hot desert'][y, x] > 0: w = 8 h = 2 r = 9 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_hot_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_hot_desert_mask) # Draw boreal forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['boreal forest'][y, x] > 0: w = 4 h = 5 r = 6 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_boreal_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_boreal_forest_mask) # Draw temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['cool temperate forest'][y, x] > 0: w = 4 h = 5 r = 6 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: if rng.random_sample() <= .5: _draw_temperate_forest1(target, x, y, w=w, h=h) else: _draw_temperate_forest2(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_temperate_forest_mask) # Draw warm temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['warm temperate forest'][y, x] > 0: w = 4 h = 5 r = 6 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_warm_temperate_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_warm_temperate_forest_mask) # Draw dry tropical forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['tropical dry forest group'][y, x] > 0: w = 4 h = 5 r = 6 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_tropical_dry_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_tropical_dry_forest_mask) # Draw jungle for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if biome_masks['jungle'][y, x] > 0: w = 4 h = 5 r = 6 if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_jungle(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_jungle_mask) if draw_rivers: draw_rivers_on_image(world, target, resize_factor) # Draw mountains if draw_mountains: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y, x] > 0: w = mountains_mask[y, x] h = 3 + int( world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if len( world.tiles_around_factor( resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_a_mountain(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_mask) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")
def draw_ancientmap(world, target, resize_factor=1, sea_color=(212, 198, 169, 255), draw_biome = True, draw_rivers = True, draw_mountains = True, draw_outer_land_border = False, verbose=get_verbose()): random.seed(world.seed * 11) if verbose: start_time = time.time() land_color = ( 181, 166, 127, 255) # TODO: Put this in the argument list too?? borders = _find_land_borders(world, resize_factor) if draw_outer_land_border: outer_borders = _find_outer_borders(world, resize_factor, borders) outer_borders = _find_outer_borders(world, resize_factor, outer_borders) if draw_mountains: mountains_mask = _find_mountains_mask(world, resize_factor) if draw_biome: boreal_forest_mask = _find_boreal_forest_mask(world, resize_factor) temperate_forest_mask = _find_temperate_forest_mask(world, resize_factor) warm_temperate_forest_mask = \ _find_warm_temperate_forest_mask(world, resize_factor) tropical_dry_forest_mask = _find_tropical_dry_forest_mask(world, resize_factor) # jungle is actually Tropical Rain Forest and Tropical Seasonal Forest jungle_mask = _mask(world, world.is_jungle, resize_factor) tundra_mask = _mask(world, world.is_tundra, resize_factor) # savanna is actually Tropical semi-arid savanna_mask = _mask(world, world.is_savanna, resize_factor) cold_parklands_mask = _mask(world, world.is_cold_parklands, resize_factor) steppe_mask = _mask(world, world.is_steppe, resize_factor) cool_desert_mask = _mask(world, world.is_cool_desert, resize_factor) chaparral_mask = _mask(world, world.is_chaparral, resize_factor) hot_desert_mask = _mask(world, world.is_hot_desert, resize_factor) rock_desert_mask = _mask(world, world.is_hot_desert, resize_factor) # TODO: add is_desert_mask def unset_mask(pos): x, y = pos mountains_mask[y][x] = False def unset_boreal_forest_mask(pos): x, y = pos boreal_forest_mask[y][x] = False def unset_temperate_forest_mask(pos): x, y = pos temperate_forest_mask[y][x] = False def unset_warm_temperate_forest_mask(pos): x, y = pos warm_temperate_forest_mask[y][x] = False def unset_tropical_dry_forest_mask(pos): x, y = pos tropical_dry_forest_mask[y][x] = False def unset_jungle_mask(pos): x, y = pos jungle_mask[y][x] = False def unset_tundra_mask(pos): x, y = pos tundra_mask[y][x] = False def unset_savanna_mask(pos): x, y = pos savanna_mask[y][x] = False def unset_hot_desert_mask(pos): x, y = pos hot_desert_mask[y][x] = False def unset_rock_desert_mask(pos): x, y = pos rock_desert_mask[y][x] = False def unset_cold_parklands_mask(pos): x, y = pos cold_parklands_mask[y][x] = False def unset_steppe_mask(pos): x, y = pos steppe_mask[y][x] = False def unset_cool_desert_mask(pos): x, y = pos cool_desert_mask[y][x] = False def unset_chaparral_mask(pos): x, y = pos chaparral_mask[y][x] = False def on_border(pos): x, y = pos return borders[y][x] if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: init Elapsed time " + str(elapsed_time) + " seconds.") sys.stdout.flush() if verbose: start_time = time.time() min_elev = None max_elev = None for y in range(world.height): for x in range(world.width): e = world.elevation['data'][y][x] if min_elev is None or e < min_elev: min_elev = e if max_elev is None or e > max_elev: max_elev = e # elev_delta = max_elev - min_elev # TODO: no longer used? if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: max, min elevation " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() border_color = (0, 0, 0, 255) outer_border_color = gradient(0.5, 0, 1.0, rgba_to_rgb(border_color), rgba_to_rgb(sea_color)) for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): xf = int(x / resize_factor) yf = int(y / resize_factor) if borders[y][x]: target.set_pixel(x, y, border_color) elif draw_outer_land_border and outer_borders[y][x]: target.set_pixel(x, y, outer_border_color) elif world.ocean[yf][xf]: target.set_pixel(x, y, sea_color) else: target.set_pixel(x, y, land_color) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: color ocean " + "Elapsed time " + str(elapsed_time) + " seconds.") if verbose: start_time = time.time() def anti_alias(steps): def _anti_alias_step(): for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): _anti_alias_point(x, y) def _anti_alias_point(x, y): n = 2 tot_r = target[x, y][0] * 2 tot_g = target[x, y][1] * 2 tot_b = target[x, y][2] * 2 for dy in range(-1, +2): py = y + dy if py > 0 and py < resize_factor * world.height: for dx in range(-1, +2): px = x + dx if px > 0 and px < resize_factor * world.width: n += 1 tot_r += target[px, py][0] tot_g += target[px, py][1] tot_b += target[px, py][2] r = int(tot_r / n) g = int(tot_g / n) b = int(tot_b / n) target[x, y] = (r, g, b, 255) for i in range(steps): _anti_alias_step() anti_alias(1) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: anti alias " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw glacier if draw_biome: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if not borders[y][x] and world.is_iceland( (int(x / resize_factor), int(y / resize_factor))): _draw_glacier(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw glacier " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw tundra if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tundra_mask[y][x]: _draw_tundra(target, x, y) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw tundra " + "Elapsed time " + str(elapsed_time) + " seconds.") # Draw cold parklands for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cold_parklands_mask[y][x]: _draw_cold_parklands(target, x, y) # Draw steppes for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if steppe_mask[y][x]: _draw_steppe(target, x, y) # Draw chaparral for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if chaparral_mask[y][x]: _draw_chaparral(target, x, y) # Draw savanna for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if savanna_mask[y][x]: _draw_savanna(target, x, y) # Draw cool desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if cool_desert_mask[y][x]: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_cool_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_cool_desert_mask) # Draw hot desert for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if hot_desert_mask[y][x]: w = 8 h = 2 r = 9 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_hot_desert(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_hot_desert_mask) # Draw boreal forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if boreal_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_boreal_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_boreal_forest_mask) # Draw temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if temperate_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: if random.random() <= .5: _draw_temperate_forest1(target, x, y, w=w, h=h) else: _draw_temperate_forest2(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_temperate_forest_mask) # Draw warm temperate forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if warm_temperate_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_warm_temperate_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_warm_temperate_forest_mask) # Draw dry tropical forest for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if tropical_dry_forest_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_tropical_dry_forest(target, x, y, w=w, h=h) world.on_tiles_around_factor( resize_factor, (x, y), radius=r, action=unset_tropical_dry_forest_mask) # Draw jungle for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if jungle_mask[y][x]: w = 4 h = 5 r = 6 if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_jungle(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_jungle_mask) if draw_rivers: draw_rivers_on_image(world, target, resize_factor) # Draw mountains if draw_mountains: if verbose: start_time = time.time() for y in range(resize_factor * world.height): for x in range(resize_factor * world.width): if mountains_mask[y][x]: w = mountains_mask[y][x] h = 3 + int(world.level_of_mountain( (int(x / resize_factor), int(y / resize_factor)))) r = max(int(w / 3 * 2), h) if len(world.tiles_around_factor(resize_factor, (x, y), radius=r, predicate=on_border)) <= 2: _draw_a_mountain(target, x, y, w=w, h=h) world.on_tiles_around_factor(resize_factor, (x, y), radius=r, action=unset_mask) if verbose: elapsed_time = time.time() - start_time print( "...drawing_functions.draw_oldmap_on_pixel: draw mountains " + "Elapsed time " + str(elapsed_time) + " seconds.")