Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _compile_test_game(game, options: GameOptions):
grammar_flags = {
"theme": "house",
"include_adj": False,
"only_last_action": True,
"blend_instructions": True,
"blend_descriptions": True,
"refer_by_name_only": True,
"instruction_extension": []
}
rng_grammar = np.random.RandomState(1234)
grammar = textworld.generator.make_grammar(grammar_flags, rng=rng_grammar)
game.change_grammar(grammar)
game_file = textworld.generator.compile_game(game, options)
return game_file
Parameters
----------
path :
Path where to save the generated game.
Returns
-------
game_file
Path to the game file.
"""
self._working_game = self.build()
options = textworld.GameOptions()
options.path = path
options.force_recompile = True
game_file = textworld.generator.compile_game(self._working_game, options)
return game_file
options.seeds = g_rng.seed
options.nb_rooms = args.nb_rooms
options.nb_objects = args.nb_objects
options.quest_length = args.quest_length
options.quest_breadth = args.quest_breadth
game = textworld.generator.make_game(options)
if args.no_quest:
game.quests = []
game_name = "neverending"
path = pjoin(args.output, game_name + ".ulx")
options = textworld.GameOptions()
options.path = path
options.force_recompile = True
game_file = textworld.generator.compile_game(game, options)
return game_file
rng_quest = rngs['quest']
rng_grammar = rngs['grammar']
modes = ["easy", "medium", "hard"]
if mode == "easy":
door_states = None
n_distractors = 0
elif mode == "medium":
door_states = ["open", "closed"]
n_distractors = 10
elif mode == "hard":
door_states = ["open", "closed", "locked"]
n_distractors = 20
# Generate map.
map_ = textworld.generator.make_map(n_rooms=options.nb_rooms, rng=rng_map,
possible_door_states=door_states)
assert len(map_.nodes()) == options.nb_rooms
world = World.from_map(map_)
# Randomly place the player.
starting_room = None
if len(world.rooms) > 1:
starting_room = rng_map.choice(world.rooms)
world.set_player_room(starting_room)
# Add object the player has to pick up.
types_counts = kb.types.count(world.state)
obj_type = kb.types.sample(parent_type='o', rng=rng_objects, include_parent=True)
var_id = get_new(obj_type, types_counts)
right_obj = Variable(var_id, obj_type)
def add_random_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.append(textworld.generator.make_quest(world, max_length))
# Calling build will generate the description for the quest.
self.build()
return self.quests[-1]
def __init__(self, kb: Optional[KnowledgeBase] = None) -> None:
"""
Creates an empty world, with a player and an empty inventory.
"""
self._entities = {}
self._named_entities = {}
self.quests = []
self.rooms = []
self.paths = []
self._kb = kb or KnowledgeBase.default()
self._types_counts = self._kb.types.count(State(self._kb.logic))
self.player = self.new(type='P')
self.inventory = self.new(type='I')
self.nowhere = []
self.grammar = textworld.generator.make_grammar()
self._game = None
self._distractors_facts = []
# Add object the player should not pick up.
types_counts = kb.types.count(world.state)
obj_type = kb.types.sample(parent_type='o', rng=rng_objects, include_parent=True)
var_id = get_new(obj_type, types_counts)
wrong_obj = Variable(var_id, obj_type)
# Place it anywhere in the world.
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