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
def make_game_with(world, quests=None, grammar=None): game = Game(world, grammar, quests) if grammar is None: for var, var_infos in game.infos.items(): var_infos.name = var.name else: game = generate_text_from_grammar(game, grammar) return game
def make_game(options: GameOptions) -> Game: """ Make a game (map + objects + quest). Arguments: options: For customizing the game generation (see :py:class:`textworld.GameOptions <textworld.generator.game.GameOptions>` for the list of available options). Returns: Generated game. """ rngs = options.rngs # Generate only the map for now (i.e. without any objects) world = make_world(options.nb_rooms, nb_objects=0, rngs=rngs) # Generate quest(s). # By default, exclude quests finishing with: go, examine, look and inventory. exclude = ["go.*", "examine.*", "look.*", "inventory.*"] options.chaining.rules_per_depth = [ options.kb.rules.get_matching(".*", exclude=exclude) ] options.chaining.backward = True options.chaining.create_variables = True options.chaining.rng = rngs['quest'] options.chaining.restricted_types = {"r", "d"} quests = make_quest(world, options) # If needed, add distractors objects (i.e. not related to the quest) to reach options.nb_objects. nb_objects = sum(1 for e in world.entities if e.type not in {'r', 'd', 'I', 'P'}) nb_distractors = options.nb_objects - nb_objects if nb_distractors > 0: world.populate(nb_distractors, rng=rngs['objects']) grammar = make_grammar(options.grammar, rng=rngs['grammar']) game = Game(world, grammar, quests) game.metadata["uuid"] = options.uuid return game
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