Пример #1
0
 def __init__(self, maps):
     # type: (Maps) -> FlatSettlement
     self._maps = maps  # type: Maps
     self.__origin = maps.box.origin
     self.limits = TransformBox(maps.box)  # type: TransformBox
     self.limits.translate(
         dx=-self.__origin.x,
         dz=-self.__origin.z)  # use coordinates relative to BuildingBox
     self._road_network = maps.road_network  # type: RoadNetwork
     self._center = None  # type: Point2D
     self._village_skeleton = None  # type: VillageSkeleton
     self._parcels = []  # type: List[Parcel]
Пример #2
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    box = TransformBox(box)
    maps = Maps(level, box)
    gen = ProcHouseGenerator(box)
    palette_str = options["palette"]
    if palette_str == "Oak":
        palette = oak_house_palette1
    elif palette_str == "Birch":
        palette = birch_house_palette1
    elif palette_str == "Dark Oak":
        palette = dark_oak_house_palette1
    elif palette_str == "Spruce":
        palette = spruce_house_palette1
    elif palette_str == "Acacia":
        palette = acacia_house_palette1
    elif palette_str == "Jungle":
        palette = jungle_house_palette1
    elif palette_str == "Sand":
        palette = sand_house_palette1
    elif palette_str == "Red Sand":
        palette = red_sand_house_palette1
    elif palette_str == "Terracotta":
        palette = terracotta_palette1
    else:
        raise ValueError("Unhandled option: {}".format(palette_str))
    gen.generate(level, maps.height_map, palette)
Пример #3
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    box = TransformBox(box)
    maps = Maps(level, box)
    gen = CropGenerator(box)
    if options['type'] in Entity.entityList:
        gen._gen_animal_farm(level, maps.height_map, options['type'])  # don't try this at home
    else:
        gen._gen_crop_v1(level)
Пример #4
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    t0 = time()
    print("Hello Settlers!")
    box = TransformBox(box)
    maps = Maps(level, box)
    settlement = FlatSettlement(maps)
    settlement.init_road_network()  # define outside connections
    settlement.init_town_center(
    )  # define town settlement as point close to roads and geometric center of the box
    settlement.build_skeleton()  # define buildings list and seed them
    settlement.define_parcels()  # define parcels around seeds
    settlement.generate(level)  # build buildings on parcels
    logging.debug('{} seconds of execution'.format(time() - t0))
Пример #5
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    box = TransformBox(box)
    maps = Maps(level, box)
    img_stock = MapStock("water_map", max(maps.width, maps.length))
    alg, kernel, param = options['algorithm'], options['kernel'], options['parameter']

    # maps.fluid_map.detect_sources(level, alg, kernel, param)
    color_map = colors.ListedColormap(['deepskyblue', 'forestgreen', 'darkcyan', 'lightseagreen', 'aquamarine'])
    water_map = Map("water_map_test_{}{}{}".format(alg, kernel, param), max(maps.width, maps.length),
                    maps.fluid_map.water.T, color_map, (-1, 3), ["Unlabeled", "No water", "Ocean", "River", "Swamp"]
                    )
    img_stock.add_map(water_map)

    for matrix, title in zip([maps.fluid_map.river_distance, maps.fluid_map.ocean_distance, maps.fluid_map.lava_distance],
                             ['sweet_water_distance', 'salty_water_distance', 'lava_distance']):
        water_map = Map(title, max(maps.width, maps.length), matrix.T, "jet", (matrix.min(), matrix.max()))
        img_stock.add_map(water_map)
Пример #6
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    box = TransformBox(box)
    maps = Maps(level, box)
    img_stock = MapStock("faster_water_map", max(maps.width, maps.length))
    d = options['max exploration']

    maps.fluid_map.detect_sources(level, max_distance=d)
    color_map = colors.ListedColormap(['deepskyblue', 'forestgreen', 'darkcyan', 'lightseagreen', 'aquamarine'])
    water_map = Map("water_map_test_{}".format(parameters.MAX_WATER_EXPLORATION), max(maps.width, maps.length),
                    maps.fluid_map.water.T, color_map, (-1, 3), ["Unlabeled", "No water", "Ocean", "River", "Swamp"]
                    )
    img_stock.add_map(water_map)

    for matrix, title in zip([maps.fluid_map.river_distance, maps.fluid_map.ocean_distance],
                             ['sweet_water_distance{}'.format(d), 'salty_water_distance{}'.format(d)]):
        water_map = Map(title, max(maps.width, maps.length), matrix.T, "jet", (matrix.min(), matrix.max()))
        img_stock.add_map(water_map)
Пример #7
0
            # Road Creation Process
            self.maps.road_network.connect_to_network(new_parcel.entry_point)

            self.map_log(interest_map, accessibility_map, sociability_map,
                         building_type)


if __name__ == '__main__':

    from utils import TransformBox
    from map import Maps
    from flat_settlement import FlatSettlement

    N = 100

    my_bounding_box = TransformBox((0, 0, 0), (N, 0, N))
    my_maps = Maps(None, my_bounding_box)

    print("Creating Flat map")
    my_flat_settlement = FlatSettlement(my_maps)

    print("Initializing road network")
    my_flat_settlement.init_road_network()

    print("Initializing town center")
    my_flat_settlement.init_town_center()

    print("Building skeleton")
    my_flat_settlement.build_skeleton()

    # N = 50
Пример #8
0
def perform(level, box, options):
    # type: (MCLevel, BoundingBox, dict) -> None
    box = TransformBox(box)
    maps = Maps(level, box)
    gen = WindmillGenerator(box)
    gen.generate(level, maps.height_map)
Пример #9
0
class FlatSettlement:
    """
    Intermediate project: generate a realistic village on a flat terrain
    """
    def __init__(self, maps):
        # type: (Maps) -> FlatSettlement
        self._maps = maps  # type: Maps
        self.__origin = maps.box.origin
        self.limits = TransformBox(maps.box)  # type: TransformBox
        self.limits.translate(
            dx=-self.__origin.x,
            dz=-self.__origin.z)  # use coordinates relative to BuildingBox
        self._road_network = maps.road_network  # type: RoadNetwork
        self._center = None  # type: Point2D
        self._village_skeleton = None  # type: VillageSkeleton
        self._parcels = []  # type: List[Parcel]

    def __random_border_point(self):
        # type: () -> Point2D
        width, length = self.limits.width, self.limits.length
        # random draw to decide on what side the border will be. Favors larger sides
        if bernouilli(width / (width + length)):
            # border point along x border
            x = 0 if bernouilli() else self.limits.width - 1
            z = randint(0, length - 1)
        else:
            # border point along z border
            x = randint(0, width - 1)
            z = 0 if bernouilli() else self.limits.length - 1
        return Point2D(x, z)

    def init_road_network(self):
        out_connections = [self.__random_border_point()]
        max_road_count = max(
            1,
            min(self.limits.width, self.limits.length) //
            MEAN_ROAD_COVERED_SURFACE)
        logging.debug('Max road count: {}'.format(max_road_count))
        road_count = min(geometric(1. / max_road_count),
                         max_road_count * 3 // 2)
        logging.debug(
            'New settlement will have {} roads B)'.format(road_count))
        logging.debug('First border point @{}'.format(str(out_connections[0])))

        for road_id in xrange(road_count):
            min_distance_to_roads = min(self.limits.width,
                                        self.limits.length) / (road_id + 1)
            logging.debug('Generating road #{}'.format(road_id + 1))
            # generate new border point far enough from existing points
            while True:
                new_road_point = self.__random_border_point()
                distances = [
                    euclidean(road_point, new_road_point)
                    for road_point in out_connections
                ]
                distance_to_roads = min(distances)
                log_args = (str(new_road_point), distance_to_roads,
                            min_distance_to_roads)
                if distance_to_roads >= min_distance_to_roads:
                    out_connections.append(new_road_point)
                    logging.debug(
                        '\tSettled on border point {} at {}m >= {}m'.format(
                            *log_args))
                    break
                else:
                    logging.debug(
                        '\tDismissed point {} at {}m < {}m'.format(*log_args))
                    min_distance_to_roads *= 0.9
            # update road network
            if road_id == 0:
                self._road_network.find_road(out_connections[0],
                                             out_connections[1])
            else:
                self._road_network.connect_to_network(out_connections[-1])

    def init_town_center(self):
        while True:
            mean_x = (self.limits.minx + self.limits.maxx) / 2
            dx = normal(mean_x, self.limits.width / 6)
            dx = int(min(self.limits.maxx - 1, max(self.limits.minx, dx)))
            dx -= self.limits.minx

            mean_z = (self.limits.minz + self.limits.maxz) / 2
            dz = normal(mean_z, self.limits.width / 6)
            dz = int(min(self.limits.maxz - 1, max(self.limits.minz, dz)))
            dz -= self.limits.minz

            random_center = Point2D(dx, dz)
            distance = self._road_network.get_distance(random_center)
            if distance <= SETTLEMENT_ACCESS_DIST:
                self._center = random_center
                logging.debug(
                    'Settlement center placed @{}, {}m away from road'.format(
                        str(random_center), distance))
                break

    def build_skeleton(self):
        self._village_skeleton = VillageSkeleton('Flat_scenario', self._maps,
                                                 self.town_center,
                                                 self._parcels)
        self._village_skeleton.grow()

    def define_parcels(self):
        """
        Parcel extension from initialized parcels. Parcels are expended in place
        """
        obs = self._maps.obstacle_map
        obs.map[:] = 0
        for parcel in self._parcels:
            obs.add_parcel_to_obstacle_map(parcel, 1)
        obs.add_network_to_obstacle_map()
        obs.map += self._maps.fluid_map.as_obstacle_array
        expendable_parcels = self._parcels[:]  # type: List[Parcel]
        # most parcels should initially be expendable, except the ghost one
        expendable_parcels = filter(Parcel.is_expendable, expendable_parcels)

        while expendable_parcels:
            # extend expendables parcels while there still are some

            for parcel in expendable_parcels:
                # direction computation
                road_dir_x = parcel.entry_x - parcel.mean_x
                road_dir_z = parcel.entry_z - parcel.mean_z
                road_dir = Direction(road_dir_x, 0, road_dir_z)
                lateral_dir = road_dir.rotate() if bernouilli(
                    0.5) else -road_dir.rotate()

                priority_directions = [
                    road_dir, lateral_dir, -lateral_dir, -road_dir
                ]
                for direction in priority_directions:
                    if parcel.is_expendable(direction):
                        parcel.expand(direction)
                        break

            expendable_parcels = filter(Parcel.is_expendable,
                                        expendable_parcels)

        # set parcels heights
        def define_parcels_heights(parcel):
            # type: (Parcel) -> None
            y = percentile(parcel.height_map, 35)
            d = euclidean(parcel.center, self.town_center)
            h = int(MAX_HEIGHT * exp(-d / BUILDING_HEIGHT_SPREAD))
            parcel.set_height(y, h)

        map(define_parcels_heights, self._parcels)
        # translate all parcels to absolute coordinates
        map(
            lambda _parcel: _parcel.translate_to_absolute_coords(
                self.__origin), self._parcels)

    def generate(self, level):
        height_map = self._maps.height_map
        # todo: replace this
        # crop_town = crop_type.new_instance(self.limits)
        # crop_town.generate(level)

        # single_house_town = house_type.new_instance(self.limits)
        # single_house_town.generate(level)

        self._road_network.generate(level, self.__origin)

        for parcel in self._parcels:  # type: Parcel
            parcel_biome = parcel.biome(level)
            palette = get_biome_palette(parcel_biome)
            parcel.generator.generate(level, parcel.height_map, palette)

        x, z = self.town_center.x, self.town_center.z
        y = self._maps.height_map[x, z]
        self._road_network.generate(level, (x, y, z))

    @property
    def town_center(self):
        return self._center