示例#1
0
    def place_content(self, content, retries=0):
        """Places content in a valid location"""
        plan_label = get_polygon_label(content)

        log.debug(f"Placing {plan_label}...")

        # TODO: add capability for Space to remember failed attempts to reduce
        # amount of tries needed; possibly keep a list of failed locations
        retries = 0
        while retries < self.max_retries:
            if retries < self.max_retries // 2:
                # attempt to locate object at the corners with the first half
                # of tries
                potential_location = self.corner_locate(content)
            else:
                # attempt to locate object at the edge with the rest of the
                # tries
                potential_location = self.edge_locate(content)

            # create a new Polygon-class object at the potential_location
            # this new object will be used to represent the original content
            if content.__class__ == Polygon:
                # handle input for shapely Polygon class because parameters
                # are expected to be different
                place = Polygon(shell=potential_location)
            else:  # handle input for RBC Polygon subclass objects
                respective_points = [Point(x, y)
                                     for (x, y) in potential_location]

                # TODO Creating a new object shuffles the contents. This
                # behavior should be prevented so that child polygons are not
                # shuffled by default.
                place = content.__class__(points=respective_points,
                                          name=content.name,
                                          contents=content.contents,
                                          exist_sp=content)

                # Make sure room_type is preserved if it exists
                if hasattr(content, 'room_type'):
                    place.room_type = content.room_type

            if self.validate_place(place):
                # object's location is valid; add object to self.plan dict
                self.plan[plan_label] = place
                return
            else:  # location is not valid; retry
                log.info(f"Failed to place {plan_label}. Retrying...")
                retries += 1

        log.info(f"Reached max retries for {plan_label}. Skipping...")
        # max retries reached; continue finishing up Space construction
        # without content
        return