Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
world.add_fact(Proposition("in", [right_obj, world.inventory]))
# Add containers and supporters to the world.
types_counts = kb.types.count(world.state)
objects = []
distractor_types = uniquify(['c', 's']
+ kb.types.descendants('c')
+ kb.types.descendants('s'))
for i in range(n_distractors):
obj_type = rng_objects.choice(distractor_types)
var_id = get_new(obj_type, types_counts) # This update the types_counts.
objects.append(Variable(var_id, obj_type))
world.populate_with(objects, rng=rng_objects)
lockable_objects.append(s.arguments[0])
if "locked" in obj_propositions or "closed" in obj_propositions:
locked_or_closed_objects.append(s.arguments[0])
object_id = 0
while object_id < nb_objects:
if len(locked_or_closed_objects) > 0:
# Prioritize adding key if there are locked or closed things in the room.
obj_type = "k"
else:
obj_type = self.kb.types.sample(parent_type='t', rng=rng, exceptions=["d", "r"],
include_parent=False, probs=object_types_probs)
if self.kb.types.is_descendant_of(obj_type, "o"):
obj_name = get_new(obj_type, types_counts)
obj = Variable(obj_name, obj_type)
allowed_objects_holder = list(objects_holder)
if obj_type == "k":
if len(locked_or_closed_objects) > 0:
# Look for a *locked* container or a door.
rng.shuffle(locked_or_closed_objects)
locked_or_closed_obj = locked_or_closed_objects.pop()
state.append(Proposition("match", [obj, locked_or_closed_obj]))
lockable_objects.remove(locked_or_closed_obj)
# Do not place the key in its own matching container.
if locked_or_closed_obj in allowed_objects_holder:
allowed_objects_holder.remove(locked_or_closed_obj)
elif len(lockable_objects) > 0:
# Place the object somewhere.
obj_holder = rng.choice(allowed_objects_holder)
if self.kb.types.is_descendant_of(obj_holder.type, "s"):
state.append(Proposition("on", [obj, obj_holder]))
elif self.kb.types.is_descendant_of(obj_holder.type, "c"):
state.append(Proposition("in", [obj, obj_holder]))
elif self.kb.types.is_descendant_of(obj_holder.type, "I"):
state.append(Proposition("in", [obj, obj_holder]))
elif self.kb.types.is_descendant_of(obj_holder.type, "r"):
state.append(Proposition("at", [obj, obj_holder]))
else:
raise ValueError("Unknown type for object holder: {}".format(obj_holder))
elif self.kb.types.is_descendant_of(obj_type, "s"):
supporter_name = get_new(obj_type, types_counts)
supporter = Variable(supporter_name, obj_type)
state.append(Proposition("at", [supporter, room]))
objects_holder.append(supporter)
elif self.kb.types.is_descendant_of(obj_type, "c"):
container_name = get_new(obj_type, types_counts)
container = Variable(container_name, obj_type)
state.append(Proposition("at", [container, room]))
objects_holder.append(container)
container_state = rng.choice(["open", "closed", "locked"])
state.append(Proposition(container_state, [container]))
lockable_objects.append(container)
if container_state in ["locked", "closed"]:
locked_or_closed_objects.append(container)
""" Creates new entity given its type.
Args:
type: The type of the entity.
name: The name of the entity.
desc: The description of the entity.
Returns:
The newly created entity.
* If the `type` is `'r'`, then a `WorldRoom` object is returned.
* Otherwise, a `WorldEntity` is returned.
"""
var_id = type
if not self._kb.types.is_constant(type):
var_id = get_new(type, self._types_counts)
var = Variable(var_id, type)
if type == "r":
entity = WorldRoom(var, name, desc)
self.rooms.append(entity)
else:
entity = WorldEntity(var, name, desc, kb=self._kb)
self._entities[var_id] = entity
if entity.name:
self._named_entities[entity.name] = entity
return entity
state.append(Proposition("in", [obj, obj_holder]))
elif self.kb.types.is_descendant_of(obj_holder.type, "I"):
state.append(Proposition("in", [obj, obj_holder]))
elif self.kb.types.is_descendant_of(obj_holder.type, "r"):
state.append(Proposition("at", [obj, obj_holder]))
else:
raise ValueError("Unknown type for object holder: {}".format(obj_holder))
elif self.kb.types.is_descendant_of(obj_type, "s"):
supporter_name = get_new(obj_type, types_counts)
supporter = Variable(supporter_name, obj_type)
state.append(Proposition("at", [supporter, room]))
objects_holder.append(supporter)
elif self.kb.types.is_descendant_of(obj_type, "c"):
container_name = get_new(obj_type, types_counts)
container = Variable(container_name, obj_type)
state.append(Proposition("at", [container, room]))
objects_holder.append(container)
container_state = rng.choice(["open", "closed", "locked"])
state.append(Proposition(container_state, [container]))
lockable_objects.append(container)
if container_state in ["locked", "closed"]:
locked_or_closed_objects.append(container)
else:
raise ValueError("Unknown object type: {}".format(obj_type))
object_id += 1
# 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)
world.add_fact(Proposition("in", [right_obj, world.inventory]))
# Add containers and supporters to the world.
types_counts = kb.types.count(world.state)
objects = []
distractor_types = uniquify(['c', 's']
+ kb.types.descendants('c')
+ kb.types.descendants('s'))
for i in range(n_distractors):
obj_type = rng_objects.choice(distractor_types)
var_id = get_new(obj_type, types_counts) # This update the types_counts.
objects.append(Variable(var_id, obj_type))
world.populate_with(objects, rng=rng_objects)
# 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