def deserialize(cls, data: Mapping) -> "Game": """ Creates a `Game` from serialized data. Args: data: Serialized data with the needed information to build a `Game` object. """ version = data.get("version", cls._SERIAL_VERSION) if version != cls._SERIAL_VERSION: raise ValueError( "Cannot deserialize a TextWorld version {} game, expected version {}" .format(version, cls._SERIAL_VERSION)) world = World.deserialize(data["world"]) game = cls(world) game.grammar = Grammar(data["grammar"]) game.quests = tuple([Quest.deserialize(d) for d in data["quests"]]) game._infos = {k: EntityInfo.deserialize(v) for k, v in data["infos"]} game.kb = KnowledgeBase.deserialize(data["KB"]) game.metadata = data.get("metadata", {}) game._objective = data.get("objective", None) game.extras = data.get("extras", {}) if "main_quest" in data: game.main_quest = Quest.deserialize(data["main_quest"]) return game
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
def deserialize(cls, data: Mapping) -> "Game": """ Creates a `Game` from serialized data. Args: data: Serialized data with the needed information to build a `Game` object. """ world = World.deserialize(data["world"]) game = cls(world) game.grammar = Grammar(data["grammar"]) game.quests = tuple([Quest.deserialize(d) for d in data["quests"]]) game._infos = {k: EntityInfo.deserialize(v) for k, v in data["infos"]} game.kb = KnowledgeBase.deserialize(data["KB"]) game.metadata = data.get("metadata", {}) game._objective = data.get("objective", None) return game
def deserialize(cls, data: Mapping) -> "Game": """ Creates a `Game` from serialized data. Args: data: Serialized data with the needed information to build a `Game` object. """ world = World.deserialize(data["world"]) grammar = None if "grammar" in data: grammar = Grammar(data["grammar"]) quests = [Quest.deserialize(d) for d in data["quests"]] game = cls(world, grammar, quests) game._infos = {k: EntityInfo.deserialize(v) for k, v in data["infos"]} game.state = State.deserialize(data["state"]) game._rules = {k: Rule.deserialize(v) for k, v in data["rules"]} game._types = VariableTypeTree.deserialize(data["types"]) game.metadata = data.get("metadata", {}) return game
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