示例#1
0
    def __instantiate_entities(self):
        """
        For each entity, select an instance from the object class it belongs to,
        after which its properties are set.
        The entities should have been set in _configure()
        """
        Y, X = self.get_dims()

        maze = spanning_tree_maze_generator(X, Y)
        blocks = [(j, i) for i, m in enumerate(maze) for j, b in enumerate(m)
                  if b == '#']

        ## maybe not all blocks of the maze will be used later
        random.shuffle(blocks)

        ## first remove all maze blocks from the available set
        for b in blocks:
            if b in self.available_grids:
                self.available_grids.remove(b)

        ## select a random img path for each entity
        for i, e in enumerate(self.entities):
            self.set_property(e)

        ## add back some empty grids
        self.available_grids += blocks[len(self.get_blocks()):]

        ## use the remaining blocks
        for e in self.get_blocks():
            assert blocks, "too many blocks for a valid maze"
            e.loc = blocks.pop()
示例#2
0
    def __instantiate_entities(self):
        """
        For each entity, select an instance from the object class it belongs to,
        after which its properties are set.
        The entities should have been set in _configure()
        """
        if self.maze_generation:
            Y, X = self.get_dims()
            maze = spanning_tree_maze_generator(X, Y)
            blocks = [(j, i, 0) for i, m in enumerate(maze)
                      for j, b in enumerate(m) if b == '#']

            ## maybe not all blocks of the maze will be used later
            random.shuffle(blocks)

            ## first remove all maze blocks from the available set
            ## do not only remove part of them, because agent/goal might get stuck in a closed room
            for b in blocks:
                if b in self.available_grids:
                    self.available_grids.remove(b)

            ## instantiate properties for each entity
            for e in self.entities:
                if e.loc is not None:
                    warnings.warn(
                        "Maze generation is on! Overwriting pre-specified location %s!"
                        % (e.loc, ))
                    e.loc = None  # remove the pre-set location when maze_generation is on
                ## skip setting loc for block here and set it later
                if e.type != "block":
                    ## if non-block, randomize the yaw, scale, and offset
                    self.set_property(e,
                                      property_value_dict={
                                          "yaw": None,
                                          "scale": None,
                                          "offset": None
                                      })
                else:
                    assert blocks, "too many blocks for a valid maze"
                    e.loc = blocks.pop()
                    self.set_property(e)  ## still need to set other properties

            ## add back the unused grids
            self.available_grids += blocks
        else:
            ## instantiate properties for each entity
            for i, e in enumerate(self.entities):
                self.set_property(e)
示例#3
0
    def __instantiate_entities(self):
        """
        For each entity, select an instance from the object class it belongs to,
        after which its properties are set.
        The entities should have been set in _configure()
        """
        Y, X = self.get_dims()

        maze = spanning_tree_maze_generator(X, Y)
        blocks = [(j, i, 0) for i, m in enumerate(maze)
                  for j, b in enumerate(m) if b == '#']

        ## maybe not all blocks of the maze will be used later
        random.shuffle(blocks)

        ## first remove all maze blocks from the available set
        for b in blocks:
            if b in self.available_grids:
                self.available_grids.remove(b)

        ## select a random object path for each non-block entity
        for i, e in enumerate(self.entities):
            if e.name is None:
                e.name = random.choice(self.get_all_possible_names(e.type))
            e.id = "%s_%d" % (e.name, i)
            if e.asset_path is None:
                icons = self.items[e.type][e.name]
                e.asset_path = random.choice(icons)
            e.color = self.color_table[e.asset_path]
            if e.loc is None and e.type != "block":
                assert len(self.available_grids) > 0
                e.loc = self.available_grids.pop()
            if e.type == "agent":
                e.yaw = random.uniform(-self.PI, self.PI)
            elif e.type == "goal":
                e.yaw = random.randint(-1, 2) * self.PI_2

        ## add back some empty grids
        self.available_grids += blocks[len(self.get_blocks()):]

        ## use the remaining blocks
        for e in self.get_blocks():
            assert blocks, "too many blocks for a valid maze"
            e.loc = blocks.pop()
示例#4
0
    def __instantiate_entities(self):
        """
        For each entity, select an instance from the object class it belongs to,
        after which its properties are set.
        The entities should have been set in _configure()
        """
        if self.maze_generation:
            Y, X = self.get_dims()
            maze = spanning_tree_maze_generator(X, Y)
            blocks = [(j, i, 0) for i, m in enumerate(maze)
                      for j, b in enumerate(m) if b == '#']

            ## maybe not all blocks of the maze will be used later
            random.shuffle(blocks)

            ## remove locations of all blocks from available grid set
            for b in blocks:
                if b in self.available_grids:
                    self.available_grids.remove(b)
            ## instantiate properties for each entity
            for i, e in enumerate(self.entities):
                if e.loc is not None:
                    warnings.warn(
                        "Maze generation is on! Overwriting pre-specified location %s!"
                        % (e.loc, ))
                    e.loc = None  # remove the pre-set location when maze_generation is on
                # skip setting loc for block here and set it later
                if e.type != "block":
                    self.set_property(e)
            ## add back some empty grids
            self.available_grids += blocks[len(self.get_blocks()):]

            ## use the remaining blocks
            assert len(self.get_blocks()) <= len(
                blocks), "too many blocks for a valid maze"
            for e in self.get_blocks():
                e.loc = blocks.pop()
                self.set_property(e)
        else:
            ## instantiate properties for each entity
            for i, e in enumerate(self.entities):
                self.set_property(e)