source: trunk/game/dialogue_demo.py @ 366

Revision 353, 4.9 KB checked in by orlandov, 10 years ago (diff)

Ticket by or1andov: Update and clean up the dialogue files

  • Add support for new dialogue directives (meet, take_stuff, get_stuff etc)
  • Commit dialogue schema which I had forgotten to check in before
  • Dialogue files can be verified by running:

kwalify -lf dialogue_schema.yaml dialogue/*.yaml

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