Пример #1
0
    def get_npc_sentences(self, caller, npc):
        """
        Get NPC's sentences that can show to the caller.

        Args:
            caller: (object) the character who want to start a talk.
            npc: (object) the NPC that the character want to talk to.

        Returns:
            sentences: (list) a list of available sentences.
        """
        if not caller:
            return

        if not npc:
            return

        sentences = []

        # Get npc's dialogues.
        for dlg_key in npc.dialogues:
            # Get all dialogues.
            npc_dlg = self.get_dialogue(dlg_key)
            if not npc_dlg:
                continue

            # Match conditions.
            if not STATEMENT_HANDLER.match_condition(npc_dlg["condition"], caller, npc):
                continue

            # Match dependencies.
            match = True
            for dep in npc_dlg["dependencies"]:
                status = QUEST_STATUS_SET.get(dep["type"])
                if not status.match(caller, dep["quest"]):
                    match = False
                    break

            if not match:
                continue

            if npc_dlg["sentences"]:
                # If has sentence, use it.
                sentences.append(npc_dlg["sentences"][0])

        if not sentences:
            # Use default sentences.
            # Default sentences should not have condition and dependencies.
            for dlg_key in npc.default_dialogues:
                npc_dlg = self.get_dialogue(dlg_key)
                if npc_dlg:
                    sentences.append(npc_dlg["sentences"][0])
            
        return sentences
Пример #2
0
    def dialogue_have_quest(self, caller, npc, dialogue):
        """
        Find quests by recursion.
        """
        provide_quest = False
        finish_quest = False

        # check if the dialogue is available
        npc_dlg = self.get_dialogue(dialogue)
        if not npc_dlg:
            return (provide_quest, finish_quest)

        if not STATEMENT_HANDLER.match_condition(npc_dlg["condition"], caller, npc):
            return (provide_quest, finish_quest)

        match = True
        for dep in npc_dlg["dependencies"]:
            status = QUEST_STATUS_SET.get(dep["type"])
            if not status.match(caller, dep["quest"]):
                match = False
                break
        if not match:
            return (provide_quest, finish_quest)

        # find quests in its sentences
        for sen in npc_dlg["sentences"]:
            for quest_key in sen["finish_quest"]:
                if caller.quest_handler.is_accomplished(quest_key):
                    finish_quest = True
                    return (provide_quest, finish_quest)

            if not provide_quest and sen["provide_quest"]:
                for quest_key in sen["provide_quest"]:
                    if caller.quest_handler.can_provide(quest_key):
                        provide_quest = True
                        return (provide_quest, finish_quest)

        for dlg_key in npc_dlg["nexts"]:
            # get next dialogue
            provide, finish = self.dialogue_have_quest(caller, npc, dlg_key)
                
            provide_quest = (provide_quest or provide)
            finish_quest = (finish_quest or finish)

            if finish_quest:
                break

            if not caller.quest_handler.get_accomplished_quests():
                if provide_quest:
                    break

        return (provide_quest, finish_quest)
Пример #3
0
    def __init__(self, *args, **kwargs):
        super(QuestDependenciesForm, self).__init__(*args, **kwargs)

        objects = CM.QUESTS.all_with_base()
        choices = [(obj["key"], obj["name"] + " (" + obj["key"] + ")") for obj in objects]
        self.fields['quest'] = forms.ChoiceField(choices=choices)
        self.fields['dependency'] = forms.ChoiceField(choices=choices)
        
        objects = QUEST_STATUS_SET.all()
        choices = [(obj, obj) for obj in objects]
        self.fields['type'] = forms.ChoiceField(choices=choices)

        localize_form_fields(self)
Пример #4
0
    def match_dependencies(self, quest_key):
        """
        Check quest's dependencies

        Args:
            quest_key: (string) quest's key

        Returns:
            (boolean) result
        """
        for dep in QUEST_DEPENDENCIES.filter(quest_key):
            status = QUEST_STATUS_SET.get(dep.type)
            if not status.match(self.owner, dep.dependency):
                return False
        return True
Пример #5
0
    def get_next_sentences(self, caller, npc, current_dialogue, current_sentence):
        """
        Get current sentence's next sentences.
        
        Args:
            caller: (object) the character who want to start a talk.
            npc: (object) the NPC that the character want to talk to.
            dialogue: (string) the key of the currrent dialogue.
            sentence: (int) the number of current sentence.

        Returns:
            sentences: (list) a list of available sentences.
        """
        if not caller:
            return

        # Get current dialogue.
        dlg = self.get_dialogue(current_dialogue)
        if not dlg:
            return

        sentences = []

        try:
            # If has next sentence, use next sentence.
            sentences.append(dlg["sentences"][current_sentence + 1])
        except Exception, e:
            # Else get next dialogues.
            for dlg_key in dlg["nexts"]:
                # Get next dialogue.
                next_dlg = self.get_dialogue(dlg_key)
                if not next_dlg:
                    continue

                if not next_dlg["sentences"]:
                    continue

                if not STATEMENT_HANDLER.match_condition(next_dlg["condition"], caller, npc):
                    continue

                for dep in next_dlg["dependencies"]:
                    status = QUEST_STATUS_SET.get(dep["type"])
                    if not status.match(caller, dep["quest"]):
                        continue

                sentences.append(next_dlg["sentences"][0])
Пример #6
0
    def __init__(self, *args, **kwargs):
        super(DialogueQuestDependenciesForm, self).__init__(*args, **kwargs)

        objects = CM.DIALOGUES.objects.all()
        choices = [(obj.key, obj.name + " (" + obj.key + ")")
                   for obj in objects]
        self.fields['dialogue'] = forms.ChoiceField(choices=choices)

        objects = CM.QUESTS.objects.all()
        choices = [(obj.key, obj.name + " (" + obj.key + ")")
                   for obj in objects]
        self.fields['dependency'] = forms.ChoiceField(choices=choices)

        objects = QUEST_STATUS_SET.all()
        choices = [obj for obj in objects]
        self.fields['type'] = forms.ChoiceField(choices=choices)

        localize_form_fields(self)