Пример #1
0
    def _build_extra_road_connection(self, building, collector_building):
        collector_coords = set(
            coords
            for coords in self.production_builder.iter_possible_road_coords(
                collector_building.position, collector_building.position))
        destination_coords = set(
            coords
            for coords in self.production_builder.iter_possible_road_coords(
                building.loading_area, building.position))
        pos = building.loading_area
        beacon = Rect.init_from_borders(pos.left - 1, pos.top - 1,
                                        pos.right + 1, pos.bottom + 1)

        path = RoadPlanner()(self.owner.personality_manager.get('RoadPlanner'),
                             collector_coords, destination_coords, beacon,
                             self.production_builder.get_path_nodes())
        if path is None:
            return BUILD_RESULT.IMPOSSIBLE

        cost = self.production_builder.get_road_cost(path)
        for resource_id, amount in cost.items():
            if resource_id == RES.GOLD:
                if self.owner.get_component(
                        StorageComponent).inventory[resource_id] < amount:
                    return BUILD_RESULT.NEED_RESOURCES
            elif self.settlement.get_component(
                    StorageComponent).inventory[resource_id] < amount:
                return BUILD_RESULT.NEED_RESOURCES
        return BUILD_RESULT.OK if self.production_builder.build_road(
            path) else BUILD_RESULT.UNKNOWN_ERROR
Пример #2
0
    def _get_road_to_builder(self, builder):
        """Return a path from the builder to a building with general collectors (None if impossible)."""
        loading_area = builder.get_loading_area()
        collector_coords = set()
        for building in self.collector_buildings:
            if loading_area.distance(building.position) == 1:
                return []
            if loading_area.distance(building.position) > building.radius:
                continue  # the collector building is too far to be useful
            for coords in self.iter_possible_road_coords(
                    building.position, building.position):
                collector_coords.add(coords)

        destination_coords = set(
            self.iter_possible_road_coords(loading_area, builder.position))
        if self is self.settlement_manager.production_builder:
            if not self.settlement_manager.production_builder.road_connectivity_cache.is_connection_possible(
                    collector_coords, destination_coords):
                return None

        blocked_coords = set([
            coords for coords in builder.position.tuple_iter()
        ]).union(self.land_manager.coastline)
        beacon = Rect.init_from_borders(loading_area.left - 1,
                                        loading_area.top - 1,
                                        loading_area.right + 1,
                                        loading_area.bottom + 1)

        return RoadPlanner()(self.owner.personality_manager.get('RoadPlanner'),
                             collector_coords,
                             destination_coords,
                             beacon,
                             self.get_path_nodes(),
                             blocked_coords=blocked_coords)