Beispiel #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
Beispiel #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
Beispiel #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)
Beispiel #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)
Beispiel #5
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]
Beispiel #6
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