source: trunk/game/dialogue_demo.py @ 343

Revision 343, 4.3 KB checked in by zenbitz, 10 years ago (diff)

New drunkard.yaml and edits to fguard.yaml. Some parts not tested due to lack of API functions. dialogue_demo.py converted to *nix friendly

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
18from scripts import dialogue
19import yaml
20import sys
21
22"""
23A very simple demonstration of the dialogue engine in effect. It mocks up a
24simple user object and allows the dialogue engine to interact with it via
25callbacks we provide.
26"""
27
28class Player(object):
29    """
30    Mock player object that always has complete quests
31    """
32    def __init__(self):
33        self.current_quests = set()
34        self.finished_quests = set()
35        self.inventory = set()
36        self.peopleIknow = set()
37
38    def canAcceptQuest(self, quest):
39        return     quest not in self.finished_quests \
40               and quest not in self.current_quests
41
42    def hasSatisfiedQuest(self, quest):
43        return quest in self.finished_quests
44   
45    def questIsCurrent(self,quest):
46        return quest in self.current_quests
47
48    def startQuest(self, quest):
49        if quest in self.current_quests:
50            raise RuntimeError("Already have quest, %s" % quest)
51        self.current_quests.add(quest)
52
53    def completeQuest(self, quest):
54        self.finished_quests.add(quest)
55        self.current_quests.remove(quest)
56   
57    def meet(self,npc):
58        if npc in self.peopleIknow:
59            raise RuntimeError("I already know %s" % npc)
60        self.peopleIknow.add(npc)
61       
62    def met(self,npc):
63        return npc in self.peopleIknow
64       
65class Box(object):
66    """
67    Mock box object than can be opened or closed
68    This would normally be a container
69    """
70    def __init__(self):
71        self.state = 'closed'
72       
73    def isOpen(self):
74        if self.state == 'open':
75            return self.state
76        else:
77            return False
78   
79    def isClosed(self):
80        if self.state == 'closed':
81             return self.state
82        else:
83             return False
84       
85    def open(self):
86        self.state = 'open'
87        return self.state
88   
89    def close(self):
90        self.state = 'closed'
91        return self.state
92   
93def main(dialogue_file):
94
95    # set up some closures
96    def say_cb(state, text):
97        print "NPC says:", text
98
99    def get_reply(responses):
100        for i, response in enumerate(responses):
101            print "%d. %s" % (i, response)
102        print "\nChoose a response: ",
103        val = int(sys.stdin.readline().strip())
104        print "you picked %s" % (val,)
105        return val
106
107    def start_quest(state, quest):
108        print "You've picked up the '%s' quest!" % quest
109        state['pc'].startQuest(quest)
110
111    def complete_quest(state, quest):
112        print "You've finished the '%s' quest" % quest
113        state['pc'].completeQuest(quest)
114 
115    def meet(state, npc):
116        print "You've met %s!" % npc
117        state['pc'].meet(npc)
118       
119    def get_stuff(state, thing):
120        if thing not in state['pc'].inventory:
121            state['pc'].inventory.add(thing)
122            print "You've now have the %s" % thing
123   
124    def take_stuff(state,thing):
125        if thing in state['pc'].inventory:
126            state['pc'].inventory.remove(thing)
127            print "You no longer have the %s" % thing
128
129    callbacks = {
130        "say": say_cb,
131        "start_quest": start_quest,
132        "complete_quest": complete_quest,
133        "meet": meet,
134        "get_stuff" : get_stuff,
135        "take_stuff" : take_stuff
136    }
137
138    pc = Player()
139    box = Box()
140   
141    state = {
142        'quests': {},
143        'pc': pc,
144        'box': box
145    }
146
147    dialog = dialogue.DialogueEngine(dialogue_file, callbacks, state)
148    responses = dialog.run()
149    while responses:
150        choice = get_reply(responses)
151        responses = dialog.reply(choice)
152
153if __name__ == "__main__":
154    dialogue_file = 'dialogue/sample.yaml'
155    if len(sys.argv) > 1:
156        dialogue_file = sys.argv[1]
157       
158    main(dialogue_file)
Note: See TracBrowser for help on using the repository browser.