Пример #1
0
    def build(self, validate: bool = True) -> Game:
        """ Create a `Game` instance given the defined facts.

        Parameters
        ----------
        validate : optional
            If True, check if the game is valid, i.e. respects all constraints.

        Returns
        -------
            Generated game.
        """
        if validate:
            self.validate()  # Validate the state of the world.

        world = World.from_facts(self.facts, kb=self._kb)
        game = Game(world, quests=self.quests)

        # Keep names and descriptions that were manually provided.
        for k, var_infos in game.infos.items():
            if k in self._entities:
                game.infos[k] = self._entities[k].infos

        # Use text grammar to generate name and description.
        grammar = Grammar(self.options.grammar,
                          rng=np.random.RandomState(
                              self.options.seeds["grammar"]))
        game.change_grammar(grammar)
        game.metadata["desc"] = "Generated with textworld.GameMaker."

        self._game = game  # Keep track of previous build.
        return self._game
Пример #2
0
    def generate_random_quests(self,
                               nb_quests=1,
                               length: int = 1,
                               breadth: int = 1) -> List[Quest]:
        """ Generates random quests for the game.

        .. warning:: This method overrides any previous quests the game had.

        Args:
            nb_quests: Number of parallel quests, i.e. not sharing a common goal.
            length: Number of actions that need to be performed to complete the game.
            breadth: Number of subquests per independent quest. It controls how nonlinear
                     a quest can be (1: linear).

        Returns:
            The generated quests.
        """
        options = self.options.copy()
        options.nb_parallel_quests = nb_quests
        options.quest_length = length
        options.quest_breadth = breadth
        options.chaining.rng = options.rngs['quest']

        world = World.from_facts(self.facts)
        self.quests = textworld.generator.make_quest(world, options)

        # Calling build will generate the description for the quest.
        self.build()
        return self.quests
Пример #3
0
    def add_distractors(self, nb_distractors: int) -> None:
        """ Adds a number of distractors - random objects.

        Args:
            nb_distractors: The number of distractors to add.
        """
        self._distractors_facts = []
        world = World.from_facts(self.facts)
        self._distractors_facts = world.populate(nb_distractors)
Пример #4
0
    def generate_distractors(self, nb_distractors: int) -> None:
        """ Generates a number of distractors - random objects.

        Args:
            nb_distractors: The number of distractors to game will contain.
        """
        self._distractors_facts = []
        world = World.from_facts(self.facts)
        self._distractors_facts = world.populate(nb_distractors)
Пример #5
0
def make_world_with(rooms, rng=None):
    """ Make a world that contains the given rooms.

    Parameters
    ----------
    rooms : list of textworld.logic.Variable
        Rooms in the map. Variables must have type 'r'.
    """
    map = make_map(n_rooms=len(rooms), rng=rng)
    for (n, d), room in zip(map.nodes.items(), rooms):
        d["name"] = room.name

    world = World.from_map(map)
    world.set_player_room()
    return world
Пример #6
0
    def new_quest(self, max_length: int) -> Quest:
        """ Generates a random quest for the game.

        Calling this method replaced all previous quests.

        Args:
            max_length: The maximum length of the quest to generate.

        Returns:
            The generated quest.
        """
        world = World.from_facts(self.facts)
        self._quests = [textworld.generator.make_quest(world, max_length)]

        # Calling build will generate the description for the quest.
        self.build()
        return self._quests[0]
Пример #7
0
def make_world(world_size, nb_objects=0, rngs=None):
    """ Make a world (map + objects).

    Parameters
    ----------
    world_size : int
        Number of rooms in the world.
    nb_objects : int
        Number of objects in the world.
    """
    if rngs is None:
        rngs = {}
        rng = g_rng.next()
        rngs['map'] = RandomState(rng.randint(65635))
        rngs['objects'] = RandomState(rng.randint(65635))

    map_ = make_map(n_rooms=world_size, rng=rngs['map'])
    world = World.from_map(map_)
    world.set_player_room()
    world.populate(nb_objects=nb_objects, rng=rngs['objects'])
    return world
Пример #8
0
    def build(self, validate: bool = True) -> Game:
        """ Create a `Game` instance given the defined facts.

        Parameters
        ----------
        validate : optional
            If True, check if the game is valid, i.e. respects all constraints.

        Returns
        -------
            Generated game.
        """
        if validate:
            self.validate()  # Validate the state of the world.

        world = World.from_facts(self.facts)
        game = Game(world, quests=self._quests)

        # Keep names and descriptions that were manually provided.
        for k, var_infos in game.infos.items():
            if k in self._entities:
                var_infos.name = self._entities[k].name
                var_infos.desc = self._entities[k].desc

            # If we can, reuse information generated during last build.
            if self._game is not None and k in self._game.infos:
                # var_infos.desc = self._game.infos[k].desc
                var_infos.name = self._game.infos[k].name
                var_infos.adj = self._game.infos[k].adj
                var_infos.noun = self._game.infos[k].noun
                var_infos.room_type = self._game.infos[k].room_type

        # Generate text for recently added objects.
        game.change_grammar(self.grammar)
        game.metadata["desc"] = "Generated with textworld.GameMaker."

        self._game = game  # Keep track of previous build.
        return self._game