def test_antialias(self): original = [[0.5, 0.12, 0.7, 0.15, 0.0], [0.0, 0.12, 0.7, 0.7, 8.0], [0.2, 0.12, 0.7, 0.7, 4.0]] antialiased = anti_alias(original, 1) self.assertAlmostEquals(1.2781818181818183, antialiased[0][0]) self.assertAlmostEquals(0.4918181818181818, antialiased[1][2]) original = [[0.8]] antialiased = anti_alias(original, 10) self.assertAlmostEquals(0.8, antialiased[0][0])
def test_antialias(self): original = numpy.array([[0.5, 0.12, 0.7, 0.15, 0.0], [0.0, 0.12, 0.7, 0.7, 8.0], [0.2, 0.12, 0.7, 0.7, 4.0]]) antialiased = anti_alias(original, 1) self.assertAlmostEquals(1.2781818181818183, antialiased[0][0]) self.assertAlmostEquals(0.4918181818181818, antialiased[1][2]) original = numpy.array([[0.8]]) antialiased = anti_alias(original, 10) self.assertAlmostEquals(0.8, antialiased[0][0])
def sea_depth(world, sea_level): # a dynamic programming approach to gather how far the next land is # from a given coordinate up to a maximum distance of max_radius # result is 0 for land coordinates and -1 for coordinates further than # max_radius away from land # there might be even faster ways but it does the trick def next_land_dynamic(ocean, max_radius=5): next_land = numpy.full(ocean.shape, -1, int) # non ocean tiles are zero distance away from next land next_land[numpy.logical_not(ocean)]=0 height, width = ocean.shape for dist in range(max_radius): indices = numpy.transpose(numpy.where(next_land==dist)) for y, x in indices: for dy in range(-1, 2): ny = y + dy if 0 <= ny < height: for dx in range(-1, 2): nx = x + dx if 0 <= nx < width: if next_land[ny,nx] == -1: next_land[ny,nx] = dist + 1 return next_land # We want to multiply the raw sea_depth by one of these factors # depending on the distance from the next land # possible TODO: make this a parameter factors = [0.0, 0.3, 0.5, 0.7, 0.9] next_land = next_land_dynamic(world.layers['ocean'].data) sea_depth = sea_level - world.layers['elevation'].data for y in range(world.height): for x in range(world.width): dist_to_next_land = next_land[y,x] if dist_to_next_land > 0: sea_depth[y,x]*=factors[dist_to_next_land-1] sea_depth = anti_alias(sea_depth, 10) min_depth = sea_depth.min() max_depth = sea_depth.max() sea_depth = (sea_depth - min_depth) / (max_depth - min_depth) return sea_depth
def sea_depth(world, sea_level): # a dynamic programming approach to gather how far the next land is # from a given coordinate up to a maximum distance of max_radius # result is 0 for land coordinates and -1 for coordinates further than # max_radius away from land # there might be even faster ways but it does the trick def next_land_dynamic(ocean, max_radius=5): next_land = numpy.full(ocean.shape, -1, int) # non ocean tiles are zero distance away from next land next_land[numpy.logical_not(ocean)] = 0 height, width = ocean.shape for dist in range(max_radius): indices = numpy.transpose(numpy.where(next_land == dist)) for y, x in indices: for dy in range(-1, 2): ny = y + dy if 0 <= ny < height: for dx in range(-1, 2): nx = x + dx if 0 <= nx < width: if next_land[ny, nx] == -1: next_land[ny, nx] = dist + 1 return next_land # We want to multiply the raw sea_depth by one of these factors # depending on the distance from the next land # possible TODO: make this a parameter factors = [0.0, 0.3, 0.5, 0.7, 0.9] next_land = next_land_dynamic(world.layers['ocean'].data) sea_depth = sea_level - world.layers['elevation'].data for y in range(world.height): for x in range(world.width): dist_to_next_land = next_land[y, x] if dist_to_next_land > 0: sea_depth[y, x] *= factors[dist_to_next_land - 1] sea_depth = anti_alias(sea_depth, 10) min_depth = sea_depth.min() max_depth = sea_depth.max() sea_depth = (sea_depth - min_depth) / (max_depth - min_depth) return sea_depth
def test_sea_depth(self): ocean_level = 1.0 extent = 11 w = World("sea_depth", Size(extent, extent), 0, GenerationParameters(0, ocean_level, 0), None) ocean = numpy.full([extent, extent], True) ocean[5, 5] = False elevation = numpy.zeros([extent, extent], float) elevation[5, 5] = 2.0 t = numpy.zeros([extent, extent]) w.elevation = (elevation, t) w.ocean = ocean desired_result = numpy.asarray([0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, \ 0.9, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, -1.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.9, \ 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]) desired_result = desired_result.reshape([extent, extent]) # this part is verbatim from the function. It's not part of the test # Some refactoring is in order to increase test quality desired_result = anti_alias(desired_result, 10) min_depth = desired_result.min() max_depth = desired_result.max() desired_result = (desired_result - min_depth) / (max_depth - min_depth) # end of verbatim part result = sea_depth(w, ocean_level) for y in range(extent): for x in range(extent): self.assertAlmostEqual(desired_result[y, x], result[y, x])
def test_sea_depth(self): ocean_level = 1.0 extent = 11 w = World("sea_depth", Size(extent,extent), 0, GenerationParameters(0, ocean_level, 0), None) ocean = numpy.full([extent,extent], True) ocean[5,5]=False elevation = numpy.zeros([extent,extent], float) elevation[5,5] = 2.0 t = numpy.zeros([extent, extent]) w.elevation = (elevation, t) w.ocean = ocean desired_result = numpy.asarray([0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, \ 0.9, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, -1.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.0, 0.0, 0.0, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.3, 0.3, 0.3, 0.3, 0.3, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.7, 0.9, \ 0.9, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.9, \ 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9]) desired_result = desired_result.reshape([extent,extent]) # this part is verbatim from the function. It's not part of the test # Some refactoring is in order to increase test quality desired_result = anti_alias(desired_result, 10) min_depth = desired_result.min() max_depth = desired_result.max() desired_result = (desired_result - min_depth) / (max_depth - min_depth) # end of verbatim part result = sea_depth(w, ocean_level) for y in range(extent): for x in range(extent): self.assertAlmostEqual(desired_result[y,x], result[y,x])
def sea_depth(world, sea_level): sea_depth = sea_level - world.layers['elevation'].data for y in range(world.height): for x in range(world.width): if world.tiles_around((x, y), radius=1, predicate=world.is_land): sea_depth[y, x] = 0 elif world.tiles_around((x, y), radius=2, predicate=world.is_land): sea_depth[y, x] *= 0.3 elif world.tiles_around((x, y), radius=3, predicate=world.is_land): sea_depth[y, x] *= 0.5 elif world.tiles_around((x, y), radius=4, predicate=world.is_land): sea_depth[y, x] *= 0.7 elif world.tiles_around((x, y), radius=5, predicate=world.is_land): sea_depth[y, x] *= 0.9 sea_depth = anti_alias(sea_depth, 10) min_depth = sea_depth.min() max_depth = sea_depth.max() sea_depth = (sea_depth - min_depth) / (max_depth - min_depth) return sea_depth
def sea_depth(world, sea_level): sea_depth = sea_level - world.elevation['data'] for y in range(world.height): for x in range(world.width): if world.tiles_around((x, y), radius=1, predicate=world.is_land): sea_depth[y, x] = 0 elif world.tiles_around((x, y), radius=2, predicate=world.is_land): sea_depth[y, x] *= 0.3 elif world.tiles_around((x, y), radius=3, predicate=world.is_land): sea_depth[y, x] *= 0.5 elif world.tiles_around((x, y), radius=4, predicate=world.is_land): sea_depth[y, x] *= 0.7 elif world.tiles_around((x, y), radius=5, predicate=world.is_land): sea_depth[y, x] *= 0.9 sea_depth = anti_alias(sea_depth, 10) min_depth = sea_depth.min() max_depth = sea_depth.max() sea_depth = (sea_depth - min_depth) / (max_depth - min_depth) return sea_depth
def sea_depth(world, sea_level): sea_depth = [[sea_level - world.elevation['data'][y][x] for x in range(world.width)] for y in range(world.height)] for y in range(world.height): for x in range(world.width): if world.tiles_around((x, y), radius=1, predicate=world.is_land): sea_depth[y][x] = 0 elif world.tiles_around((x, y), radius=2, predicate=world.is_land): sea_depth[y][x] *= 0.3 elif world.tiles_around((x, y), radius=3, predicate=world.is_land): sea_depth[y][x] *= 0.5 elif world.tiles_around((x, y), radius=4, predicate=world.is_land): sea_depth[y][x] *= 0.7 elif world.tiles_around((x, y), radius=5, predicate=world.is_land): sea_depth[y][x] *= 0.9 sea_depth = anti_alias(sea_depth, 10) min_depth, max_depth = matrix_min_and_max(sea_depth) sea_depth = [[rescale_value(sea_depth[y][x], min_depth, max_depth, 0.0, 1.0) for x in range(world.width)] for y in range(world.height)] return sea_depth
def sea_depth(world, sea_level): sea_depth = [[ sea_level - world.elevation['data'][y][x] for x in range(world.width) ] for y in range(world.height)] for y in range(world.height): for x in range(world.width): if world.tiles_around((x, y), radius=1, predicate=world.is_land): sea_depth[y][x] = 0 elif world.tiles_around((x, y), radius=2, predicate=world.is_land): sea_depth[y][x] *= 0.3 elif world.tiles_around((x, y), radius=3, predicate=world.is_land): sea_depth[y][x] *= 0.5 elif world.tiles_around((x, y), radius=4, predicate=world.is_land): sea_depth[y][x] *= 0.7 elif world.tiles_around((x, y), radius=5, predicate=world.is_land): sea_depth[y][x] *= 0.9 sea_depth = anti_alias(sea_depth, 10) min_depth, max_depth = matrix_min_and_max(sea_depth) sea_depth = [[ rescale_value(sea_depth[y][x], min_depth, max_depth, 0.0, 1.0) for x in range(world.width) ] for y in range(world.height)] return sea_depth