Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
path = M.connect(R1.east, R2.west)
path.door = M.new(type='d', name='wooden door')
path.door.add_property("open")
carrot = M.new(type='f', name='carrot')
M.inventory.add(carrot)
# Add a closed chest in R2.
chest = M.new(type='c', name='chest')
chest.add_property("open")
R2.add(chest)
quest1_cmds = ["go east", "insert carrot into chest"]
carrot_in_chest = M.new_event_using_commands(quest1_cmds)
eating_carrot = Event(conditions={M.new_fact("eaten", carrot)})
quest1 = Quest(win_events=[carrot_in_chest],
fail_events=[eating_carrot],
reward=2)
quest2_cmds = quest1_cmds + ["close chest"]
quest2_actions = M.new_event_using_commands(quest2_cmds).actions
chest_closed_with_carrot = Event(
conditions={
M.new_fact("in", carrot, chest),
M.new_fact("closed", chest)
},
actions=quest2_actions)
quest2 = Quest(win_events=[chest_closed_with_carrot],
fail_events=[eating_carrot])
# Add a closed chest in R2.
chest = M.new(type='c', name='chest')
chest.add_property("open")
R2.add(chest)
quest1_cmds = ["go east", "insert carrot into chest"]
carrot_in_chest = M.new_event_using_commands(quest1_cmds)
eating_carrot = Event(conditions={M.new_fact("eaten", carrot)})
quest1 = Quest(win_events=[carrot_in_chest],
fail_events=[eating_carrot],
reward=2)
quest2_cmds = quest1_cmds + ["close chest"]
quest2_actions = M.new_event_using_commands(quest2_cmds).actions
chest_closed_with_carrot = Event(
conditions={
M.new_fact("in", carrot, chest),
M.new_fact("closed", chest)
},
actions=quest2_actions)
quest2 = Quest(win_events=[chest_closed_with_carrot],
fail_events=[eating_carrot])
M.quests = [quest1, quest2]
game = M.build()
game.main_quest = M.new_quest_using_commands(quest2_cmds)
game_file = _compile_test_game(game, options)
return game, game_file
world.populate_with([wrong_obj], rng=rng_objects)
# Generate a quest that finishes by taking something (i.e. the right
# object since it's the only one in the inventory).
options.chaining.rules_per_depth = [kb.rules.get_matching("take.*")]
options.chaining.backward = True
options.chaining.rng = rng_quest
# options.chaining.restricted_types = exceptions
# exceptions = ["r", "c", "s", "d"] if mode == "easy" else ["r"]
chain = textworld.generator.sample_quest(world.state, options.chaining)
# Add objects needed for the quest.
world.state = chain.initial_state
event = Event(chain.actions)
quest = Quest(win_events=[event],
fail_events=[Event(conditions={Proposition("in", [wrong_obj, world.inventory])})])
grammar = textworld.generator.make_grammar(options.grammar, rng=rng_grammar)
game = textworld.generator.make_game_with(world, [quest], grammar)
game.metadata = metadata
mode_choice = modes.index(mode)
uuid = "tw-treasure_hunter-{specs}-{grammar}-{seeds}"
uuid = uuid.format(specs=encode_seeds((mode_choice, options.nb_rooms, options.quest_length)),
grammar=options.grammar.uuid,
seeds=encode_seeds([options.seeds[k] for k in sorted(options.seeds)]))
game.metadata["uuid"] = uuid
return game
quests: The quests to be done in the game.
grammar: The grammar to control the text generation.
"""
self.world = world
self.quests = tuple(quests)
self.metadata = {}
self._objective = None
self._infos = self._build_infos()
self.kb = world.kb
self.extras = {}
# Check if we can derive a global winning policy from the quests.
self.main_quest = None
policy = GameProgression(self).winning_policy
if policy:
win_event = Event(actions=policy)
self.main_quest = Quest(win_events=[win_event])
self.change_grammar(grammar)
recorder = Recorder()
agent = textworld.agents.HumanAgent(autocompletion=True)
textworld.play(game_file, agent=agent, wrappers=[recorder])
# Skip "None" actions.
actions = [action for action in recorder.actions if action is not None]
# Ask the user which quests have important state, if this is set
# (if not, we assume the last action contains all the relevant facts)
winning_facts = None
if ask_for_state and recorder.last_game_state is not None:
winning_facts = [user_query.query_for_important_facts(actions=recorder.actions,
facts=recorder.last_game_state.state.facts,
varinfos=self._working_game.infos)]
event = Event(actions=actions, conditions=winning_facts)
self.quests.append(Quest(win_events=[event]))
# Calling build will generate the description for the quest.
self.build()
return self.quests[-1]
msg = "No quest can be generated with the provided options."
raise NoSuchQuestExistError(msg)
chains.append(chain)
state = chain.initial_state # State might have changed, i.e. options.create_variable is True.
if options.chaining.backward and hasattr(world, "state"):
world.state = state
quests = []
actions = []
for chain in reversed(chains):
for i in range(1, len(chain.nodes)):
actions.append(chain.actions[i - 1])
if chain.nodes[i].breadth != chain.nodes[i - 1].breadth:
event = Event(actions)
quests.append(Quest(win_events=[event]))
actions.append(chain.actions[-1])
event = Event(actions)
quests.append(Quest(win_events=[event]))
return quests
# Skip "None" actions.
actions = [action for action in recorder.actions if action is not None]
# Ask the user which quests have important state, if this is set
# (if not, we assume the last action contains all the relevant facts)
winning_facts = None
if ask_for_state and recorder.last_game_state is not None:
winning_facts = [user_query.query_for_important_facts(actions=recorder.actions,
facts=recorder.last_game_state.state.facts,
varinfos=self._working_game.infos)]
if len(commands) != len(actions):
unrecognized_commands = [c for c, a in zip(commands, recorder.actions) if a is None]
raise QuestError("Some of the actions were unrecognized: {}".format(unrecognized_commands))
event = Event(actions=actions, conditions=winning_facts)
self.quests = [Quest(win_events=[event])]
# Calling build will generate the description for the quest.
self.build()
return self.quests[-1]
def deserialize(cls, data: Mapping) -> "Quest":
""" Creates a `Quest` from serialized data.
Args:
data: Serialized data with the needed information to build a
`Quest` object.
"""
win_events = [Event.deserialize(d) for d in data["win_events"]]
fail_events = [Event.deserialize(d) for d in data["fail_events"]]
commands = data.get("commands", [])
reward = data["reward"]
desc = data["desc"]
return cls(win_events, fail_events, reward, desc, commands)