Beispiel #1
0
    def generate_name_force_numbered(self):
        suffix = '_1'

        grammar = Grammar(options={'allowed_variables_numbering': True})
        name, adj, noun = grammar.generate_name(
            'object', 'vault', False, exclude=ContainsEveryObjectContainer())
        self.assertTrue(name.endswith(suffix),
                        'Checking name ends with suffix')
        self.assertTrue(adj.endswith(suffix), 'Checking adj ends with suffix')
        self.assertTrue(noun.endswith(suffix),
                        'Checking noun ends with suffix')
Beispiel #2
0
 def split_name_adj_noun_fail(self):
     """
     What happens if we have too many separators?
     """
     grammar = Grammar()
     try:
         grammar.split_name_adj_noun('A|B|C', True)
         self.assertTrue(
             False, "We should have errored about too many separators")
     except ValueError:
         pass
Beispiel #3
0
 def generate_name_fail(self):
     grammar = Grammar()
     try:
         grammar.generate_name('object',
                               'vault',
                               False,
                               exclude=ContainsEveryObjectContainer())
         self.assertTrue(
             False,
             "We should have errored about an impossible object name")
     except ValueError:
         pass
Beispiel #4
0
def generate_text_from_grammar(game, grammar: Grammar):
    # Assign a specific room type and name to our rooms
    for room in game.world.rooms:
        # First, generate a unique roomtype and name from the grammar
        if game.infos[room.id].room_type is None and grammar.has_tag(
                "#room_type#"):
            game.infos[room.id].room_type = grammar.expand("#room_type#")

        assign_name_to_object(room, grammar, game.infos)

        # Next, assure objects contained in a room must have the same room type
        for obj in game.world.get_all_objects_in(room):
            if game.infos[obj.id].room_type is None:
                game.infos[obj.id].room_type = game.infos[room.id].room_type

    # Objects in inventory can be of any room type.
    for obj in game.world.get_objects_in_inventory():
        if game.infos[obj.id].room_type is None and grammar.has_tag(
                "#room_type#"):
            game.infos[obj.id].room_type = grammar.expand("#room_type#")

    # Assign name and description to objects.
    for obj in game.world.objects:
        if obj.type in ["I", "P"]:
            continue

        assign_name_to_object(obj, grammar, game.infos)
        assign_description_to_object(obj, grammar, game.infos)

    # Generate the room descriptions.
    for room in game.world.rooms:
        if game.infos[
                room.
                id].desc is None:  # Skip rooms which already have a description.
            game.infos[room.id].desc = assign_description_to_room(
                room, game, grammar)

    # Generate the instructions.
    for quest in game.quests:
        if quest.desc is None:  # Skip quests which already have a description.
            quest.desc = assign_description_to_quest(quest, game, grammar)

    # Generate the instructions for the main quest, if needed.
    if game.main_quest and game.main_quest.desc is None:
        game.main_quest.desc = assign_description_to_quest(
            game.main_quest, game, grammar)

    return game
Beispiel #5
0
 def test_grammar_get_random_expansion_fail(self):
     """
     Tests failure when getting a non-existent flag
     (unsuccess used in names as _fail is a special flag in nosetests)
     """
     grammar = Grammar()
     bad_tag_name = 'fahawagads'
     try:
         grammar.get_random_expansion(tag=bad_tag_name)
         self.assertTrue(
             False,
             "We should have errored with a value error about a non-existent tag"
         )
     except ValueError as e:
         self.assertTrue(bad_tag_name in str(e),
                         "Tag name does not occur in error message")
Beispiel #6
0
def make_grammar(options: Mapping = {}, rng: Optional[RandomState] = None) -> Grammar:
    rng = g_rng.next() if rng is None else rng
    grammar = Grammar(options, rng)
    grammar.check()
    return grammar
Beispiel #7
0
def describe_event(event: Event, game: Game, grammar: Grammar) -> str:
    """
    Assign a descripton to a quest.
    """
    # We have to "count" all the adj/noun/types in the world
    # This is important for using "unique" but abstracted references to objects
    counts = OrderedDict()
    counts["adj"] = CountOrderedDict()
    counts["noun"] = CountOrderedDict()
    counts["type"] = CountOrderedDict()

    # Assign name and description to objects.
    for obj in game.world.objects:
        if obj.type in ["I", "P"]:
            continue

        obj_infos = game.infos[obj.id]
        counts['adj'][obj_infos.adj] += 1
        counts['noun'][obj_infos.noun] += 1
        counts['type'][obj.type] += 1

    if len(event.actions) == 0:
        # We don't need to say anything if the quest is empty
        quest_desc = "Choose your own adventure!"
    # If quest has description already, use it
    elif desc:
        quest_desc = desc
    elif not grammar:
        quest_desc = "No grammar. Supply manual description"
    else:
        # Generate a description for either the last, or all commands
        if grammar.options.only_last_action:
            actions_desc, _ = generate_instruction(event.actions[-1], grammar,
                                                   game.infos, game.world,
                                                   counts)
            only_one_action = True
        else:
            actions_desc_list = []
            # Decide if we blend instructions together or not
            if grammar.options.blend_instructions:
                instructions = get_action_chains(event.actions, grammar,
                                                 game.infos)
            else:
                instructions = event.actions

            only_one_action = len(instructions) < 2
            for c in instructions:
                desc, separator = generate_instruction(c, grammar, game.infos,
                                                       game.world, counts)
                actions_desc_list.append(desc)
                if c != instructions[-1] and len(separator) > 0:
                    actions_desc_list.append(separator)
            actions_desc = " ".join(actions_desc_list)

        if only_one_action:
            quest_tag = grammar.get_random_expansion("#quest_one_action#")
            quest_tag = quest_tag.replace("(action)", actions_desc.strip())

        else:
            quest_tag = grammar.get_random_expansion("#quest#")
            quest_tag = quest_tag.replace("(list_of_actions)",
                                          actions_desc.strip())

        event_desc = grammar.expand(quest_tag)
        event_desc = re.sub(
            r"(^|(?<=[?!.]))\s*([a-z])",
            lambda pat: pat.group(1) + ' ' + pat.group(2).upper(), event_desc)

    return event_desc
Beispiel #8
0
 def test_get_all_expansions_for_tag(self):
     grammar = Grammar()
     result = grammar.get_all_expansions_for_tag('#clean_(r)#')
     self.assertNotEqual(len(result), 0,
                         'No expansions for library tag found')
Beispiel #9
0
 def test_get_all_expansions_for_tag_not_existing(self):
     grammar = Grammar()
     result = grammar.get_all_expansions_for_tag('fahawagads')
     self.assertEqual(len(result), 0, 'Result is not empty')
Beispiel #10
0
 def test_grammar_eq2(self):
     grammar = Grammar()
     grammar2 = Grammar(options={'theme': 'something'})
     self.assertNotEqual(
         grammar, grammar2,
         "Testing two different grammar files are not equal")
Beispiel #11
0
 def test_grammar_eq(self):
     grammar = Grammar()
     grammar2 = Grammar()
     self.assertEqual(grammar, grammar2,
                      "Testing two grammar files are equivalent")
Beispiel #12
0
 def test_get_all_nouns_for_type(self):
     grammar = Grammar()
     result = grammar.get_all_nouns_for_type('d')
     self.assertNotEqual(len(result), 0, 'No nouns for door type found')
Beispiel #13
0
 def test_get_all_adj_for_type(self):
     grammar = Grammar()
     result = grammar.get_all_adjective_for_type('d')
     self.assertNotEqual(len(result), 0,
                         'No adjectives for door type found')