def expand_clean_replace(symbol, grammar, obj, game_infos): """ Return a cleaned/keyword replaced symbol. """ obj_infos = game_infos[obj.id] phrase = grammar.expand(symbol) phrase = phrase.replace("(obj)", obj_infos.id) phrase = phrase.replace("(name)", obj_infos.name) phrase = phrase.replace("(name-n)", obj_infos.noun if obj_infos.adj is not None else obj_infos.name) phrase = phrase.replace("(name-adj)", obj_infos.adj if obj_infos.adj is not None else grammar.expand("#ordinary_adj#")) if obj.type != "": phrase = phrase.replace("(name-t)", KnowledgeBase.default().types.get_description(obj.type)) else: assert False, "Does this even happen?" return fix_determinant(phrase)
def assign_description_to_room(room, game, grammar): """ Assign a descripton to a room. """ # Add the decorative text room_desc = expand_clean_replace("#dec#\n\n", grammar, room, game.infos) # Convert the objects into groupings based on adj/noun/type objs = [o for o in room.content if KnowledgeBase.default().types.is_descendant_of(o.type, KnowledgeBase.default().types.CLASS_HOLDER)] groups = OrderedDict() groups["adj"] = OrderedDict() groups["noun"] = OrderedDict() for obj in objs: obj_infos = game.infos[obj.id] adj, noun = obj_infos.adj, obj_infos.noun # get all grouped adjectives and nouns groups['adj'][adj] = list(filter(lambda x: game.infos[x.id].adj == adj, objs)) groups['noun'][noun] = list(filter(lambda x: game.infos[x.id].noun == noun, objs)) # Generate the room description, prioritizing group descriptions where possible ignore = [] for obj in objs: if obj.id in ignore: continue # Skip that object. obj_infos = game.infos[obj.id] adj, noun = obj_infos.adj, obj_infos.noun if grammar.options.blend_descriptions: found = False for type in ["noun", "adj"]: group_filt = [] if getattr(obj_infos, type) != "": group_filt = list(filter(lambda x: x.id not in ignore, groups[type][getattr(obj_infos, type)])) if len(group_filt) > 1: found = True desc = replace_num(grammar.expand("#room_desc_group#"), len(group_filt)) if type == "noun": desc = desc.replace("(val)", "{}s".format(getattr(obj_infos, type))) desc = desc.replace("(name)", obj_list_to_prop_string(group_filt, "adj", game.infos, det_type="one")) elif type == "adj": _adj = getattr(obj_infos, type) if getattr(obj_infos, type) is not None else "" desc = desc.replace("(val)", "{}things".format(_adj)) desc = desc.replace("(name)", obj_list_to_prop_string(group_filt, "noun", game.infos)) for o2 in group_filt: ignore.append(o2.id) if KnowledgeBase.default().types.is_descendant_of(o2.type, KnowledgeBase.default().types.CLASS_HOLDER): for vtype in [o2.type] + KnowledgeBase.default().types.get_ancestors(o2.type): tag = "#room_desc_({})_multi_{}#".format(vtype, "adj" if type == "noun" else "noun") if grammar.has_tag(tag): desc += expand_clean_replace(" " + tag, grammar, o2, game.infos) break room_desc += " {}".format(fix_determinant(desc)) break if found: continue if obj.type not in ["P", "I", "d"]: for vtype in [obj.type] + KnowledgeBase.default().types.get_ancestors(obj.type): tag = "#room_desc_({})#".format(vtype) if grammar.has_tag(tag): room_desc += expand_clean_replace(" " + tag, grammar, obj, game.infos) break room_desc += "\n\n" # Look for potential exit directions. exits_with_open_door = [] exits_with_closed_door = [] exits_without_door = [] for dir_ in sorted(room.exits.keys()): if dir_ in room.doors: door_obj = room.doors[dir_] attributes_names = [attr.name for attr in door_obj.get_attributes()] if "open" in attributes_names: exits_with_open_door.append((dir_, door_obj)) else: exits_with_closed_door.append((dir_, door_obj)) else: exits_without_door.append(dir_) exits_desc = [] # Describing exits with door. if grammar.options.blend_descriptions and len(exits_with_closed_door) > 1: dirs, door_objs = zip(*exits_with_closed_door) e_desc = grammar.expand("#room_desc_doors_closed#") e_desc = replace_num(e_desc, len(door_objs)) e_desc = e_desc.replace("(dir)", list_to_string(dirs, False)) e_desc = clean_replace_objs(grammar, e_desc, door_objs, game.infos) e_desc = repl_sing_plur(e_desc, len(door_objs)) exits_desc.append(e_desc) else: for dir_, door_obj in exits_with_closed_door: d_desc = expand_clean_replace(" #room_desc_(d)#", grammar, door_obj, game.infos) d_desc = d_desc.replace("(dir)", dir_) exits_desc.append(d_desc) if grammar.options.blend_descriptions and len(exits_with_open_door) > 1: dirs, door_objs = zip(*exits_with_open_door) e_desc = grammar.expand("#room_desc_doors_open#") e_desc = replace_num(e_desc, len(door_objs)) e_desc = e_desc.replace("(dir)", list_to_string(dirs, False)) e_desc = clean_replace_objs(grammar, e_desc, door_objs, game.infos) e_desc = repl_sing_plur(e_desc, len(door_objs)) exits_desc.append(e_desc) else: for dir_, door_obj in exits_with_open_door: d_desc = expand_clean_replace(" #room_desc_(d)#", grammar, door_obj, game.infos) d_desc = d_desc.replace("(dir)", dir_) exits_desc.append(d_desc) # Describing exits without door. if grammar.options.blend_descriptions and len(exits_without_door) > 1: e_desc = grammar.expand("#room_desc_exits#").replace("(dir)", list_to_string(exits_without_door, False)) e_desc = repl_sing_plur(e_desc, len(exits_without_door)) exits_desc.append(e_desc) else: for dir_ in exits_without_door: e_desc = grammar.expand("#room_desc_(dir)#").replace("(dir)", dir_) exits_desc.append(e_desc) room_desc += " ".join(exits_desc) # Finally, set the description return fix_determinant(room_desc)