source: trunk/game/dialogue_demo.py @ 673

Revision 673, 3.9 KB checked in by technomage, 9 years ago (diff)

Patch by Technomage

  • Fixed a bug that caused NPCs to attempt to give the player an invalid list of items named with a single letter instead of the correctly named item (e.g. 'Y', 'e', 'a', 's', 't' instead of 'Yeast') in a dialogue.
  • Fixed a bug in the dialogue_demo.py script that caused it to crash with an AttributeError? when a dialogue was loaded.
  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17"""A very simple demonstration of the dialogue engine in effect. It mocks up a
18   simple user object and allows the dialogue engine to interact with it via
19   callbacks we provide."""
20import logging
21import sys
22
23from scripts.dialogueparsers import YamlDialogueParser
24from scripts.dialogueengine import DialogueEngine
25from scripts.quest_engine import QuestEngine
26
27def setup_logging():
28    logging.basicConfig(filename='dialogue_demo.log')
29setup_logging()
30
31class Player(object):
32    """
33    Mock player object that always has complete quests
34    """
35    def __init__(self):
36        self.inventory = set(['beer'])
37        self.people_i_know = set()
38
39    def meet(self,npc):
40        if npc in self.people_i_know:
41            raise RuntimeError("I already know %s" % npc)
42        self.people_i_know.add(npc)
43
44    def met(self,npc):
45        return npc in self.people_i_know
46
47
48class Beer(object):
49    quality = 3
50
51
52class Box(object):
53    """
54    Mock box object than can be opened or closed
55    This would normally be a container
56    """
57    def __init__(self):
58        self.state = 'closed'
59
60    def isOpen(self):
61        if self.state == 'open':
62            return self.state
63        else:
64            return False
65
66    def isClosed(self):
67        if self.state == 'closed':
68            return self.state
69        else:
70            return False
71
72    def open(self):
73        self.state = 'open'
74        return self.state
75
76    def close(self):
77        self.state = 'closed'
78        return self.state
79
80def getReply(dialogue_responses):
81    while (True):
82        print('Available responses:')
83        for index, response in enumerate(dialogue_responses):
84            print('{0} - {1}'.format(index, response.text))
85        try:
86            chosen_response_n = int(raw_input('Choose a response number> '))
87            chosen_response = dialogue_responses[chosen_response_n]
88        except ValueError, IndexError:
89            print('invalid response, please enter an integer between 0 and {0}'
90                  .format(len(dialogue_responses) - 1))
91        else:
92            break
93   
94    return chosen_response
95
96def main(dialogue_filepath):
97    pc = Player()
98    box = Box()
99    quest = QuestEngine('quests')
100    beer = Beer()
101   
102    parser = YamlDialogueParser()
103    with file(dialogue_filepath, 'r') as dialogue_file:
104        dialogue = parser.load(dialogue_file)
105   
106    game_state = {
107        'quest': quest,
108        'pc': pc,
109        'box': box,
110        'beer': beer
111    }
112
113    DialogueEngine.initiateDialogue(dialogue, game_state)
114    npc_name = dialogue.npc_name
115    while DialogueEngine.in_dialogue:
116        responses = DialogueEngine.continueDialogue()
117        current_dialogue_section = DialogueEngine.getCurrentDialogueSection()
118        dialogue_text = current_dialogue_section.text.replace('\n', '\n    ')
119        print('{0}: {1}'.format(npc_name, dialogue_text))
120        chosen_reply = getReply(responses)
121        DialogueEngine.reply(chosen_reply)
122
123if __name__ == "__main__":
124    print "1 - Dialogue sample\n2 - Quest Sample"
125    choice = input("> ")
126    if choice == 1:
127        dialogue_file = 'dialogue/drunkard.yaml'
128    elif choice == 2:
129        dialogue_file = 'dialogue/quest_sample.yaml'
130    if len(sys.argv) > 1:
131        dialogue_file = sys.argv[1]
132
133    main(dialogue_file)
Note: See TracBrowser for help on using the repository browser.