source: branches/active/character_customization/game/parpg/dialogueactions.py @ 794

Revision 774, 12.9 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites

  • removed shebang from all python files except launcher scripts
  • added shebang to pychan_designer
  • Property svn:mime-type set to text/plain
Line 
1#   This file is part of PARPG.
2#
3#   PARPG is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7#
8#   PARPG is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12#
13#   You should have received a copy of the GNU General Public License
14#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
15"""
16Provides classes used to implement dialogue logic and allow dialogues to have
17external effects on the game state.
18"""
19import logging
20
21def setupLogging():
22    """Set various logging parameters for this module."""
23    module_logger = logging.getLogger('dialogueaction')
24    if __debug__:
25        module_logger.setLevel(logging.DEBUG)
26setupLogging()
27
28class DialogueAction(object):
29    """
30    Abstract base class for subclasses that represent dialogue actions embedded
31    within a DialogueSection or DialogueResponse.
32   
33    Subclasses must define the keyword class variable and implement both the
34    __init__ and __call__ methods.
35   
36    @cvar keyword: keyword used by the L{DialogueParser} to recognize the
37        L{DialogueAction} in serialized L{Dialogues<Dialogues>}.
38    @type keyword: basestring
39    """
40    logger = logging.getLogger('dialogueaction.DialogueAction')
41    registered_actions = {}
42   
43    @classmethod
44    def registerAction(cls, dialogue_action_type):
45        """
46        Register a L{DialogueAction} subclass for easy reference.
47       
48        @param dialogue_action_type: dialogue action to register.
49        @type dialogue_action_type: L{DialogueAction} subclass
50        """
51        cls.registered_actions[dialogue_action_type.keyword] = \
52            dialogue_action_type
53   
54    def __init__(self, *args, **kwargs):
55        """
56        Initialize a new L{DialogueAction} instance.
57       
58        @param args: positional arguments passed by the L{DialogueParser} after
59            reading a serialized L{Dialogue}.
60        @type args: list of objects
61        @param kwargs: keyword arguments passed by the L{DialogueParser} after
62            reading a serialized L{Dialogue}.
63        @type kwargs: dict of objects
64        """
65        if (not hasattr(type(self), 'keyword')):
66            raise AttributeError('DialogueAction subclasses must define the '
67                                 'keyword class variable.')
68        self.arguments = (args, kwargs)
69   
70    def __call__(self, game_state):
71        """
72        Execute the L{DialogueAction}.
73       
74        @param game_state: variables and functions that make up the current
75            game state.
76        @type game_state: dict of objects
77        """
78        raise NotImplementedError('subclasses of DialogueAction must '
79                                  'override __call__')
80
81
82class MeetAction(DialogueAction):
83    """
84    L{DialogueAction} that adds an NPC to the list of NPCs known by the player.
85    """
86    keyword = 'meet'
87   
88    def __init__(self, *args, **kwargs):
89        """
90        Initialize a new L{MeetAction} instance.
91       
92        @param args: positional arguments.
93        @type args: list of objects
94        @param npc_id: identifier of the NPC that the player has met.
95        @type npc_id: basestring
96        @param kwargs: keyword arguments (not used).
97        @type kwargs: dict of objects
98        """
99        DialogueAction.__init__(self, *args, **kwargs)
100        self.npc_id = args[0]
101   
102    def __call__(self, game_state):
103        """
104        Add an NPC to the list of NPCs known by the player.
105       
106        @param game_state: variables and functions that make up the current
107            game state.
108        @type game_state: dict of objects
109        """
110        npc_id = self.npc_id
111        # NOTE Technomage 2010-11-13: This print statement seems overly
112        #     verbose, so I'm logging it as an INFO message instead.
113#        print("You've met {0}!".format(npc_id))
114        self.logger.info("You've met {0}!".format(npc_id))
115        game_state['pc'].meet(npc_id)
116DialogueAction.registerAction(MeetAction)
117
118
119class InventoryAction(DialogueAction):
120    """
121    Abstract base class for L{DialogueActions<DialogueAction>} used to
122    manipulate the NPC's and the player's inventory.
123    """
124    def __init__(self, *args, **kwargs):
125        """
126        Initialize a new L{InventoryAction} instance.
127       
128        @param args: positional arguments.
129        @type args: list of objects
130        @param item_types: item types that should be manipulated.
131        @type item_types: list of basestrings
132        @param kwargs: keyword arguments.
133        @type kwargs: dict of objects
134        """
135        DialogueAction.__init__(self, *args, **kwargs)
136        self.item_types = args
137
138
139class TakeStuffAction(InventoryAction):
140    """
141    L{InventoryAction} used to move items from the NPC's inventory to the
142    player's inventory.
143    """
144    keyword = 'take_stuff'
145   
146    def __call__(self, game_state):
147        """
148        Move items from the NPC's inventory to the player's inventory.
149       
150        @param game_state: variables and functions that make up the current
151            game state.
152        @type game_state: dict of objects
153        """
154        item_types = self.item_types
155        for item_type in item_types:
156            item = game_state['npc'].inventory.findItem(item_type=item_type)
157            if (item):
158                game_state['npc'].give(item, game_state['pc'])
159                print("{0} gave you the {1}".format(game_state['npc'].name,
160                                                    item_type))
161            else:
162                print("{0} doesn't have the {1}".format(game_state['npc'].name,
163                                                        item_type))
164DialogueAction.registerAction(TakeStuffAction)
165
166
167class GiveStuffAction(InventoryAction):
168    """
169    L{InventoryAction} used to move items from the player's inventory to the
170    NPC's inventory.
171    """
172    keyword = 'give_stuff'
173   
174    def __call__(self, game_state):
175        """
176        Move items from the player's inventory to the NPC's inventory.
177       
178        @param game_state: variables and functions that make up the current
179            game state.
180        @type game_state: dict of objects
181        """
182        item_types = self.item_types
183        for item_type in item_types:
184            item = game_state['npc'].inventory.findItem(item_type = item_type)
185            if (item):
186                game_state['pc'].give(item, game_state['npc'])
187                print("You give the {0} to {1}".format(item_type,
188                                                       game_state['npc'].name))
189            else:
190                print("You don't have the {0}".format(item_type))
191DialogueAction.registerAction(GiveStuffAction)
192
193
194class QuestAction(DialogueAction):
195    """
196    Abstract base class for quest-related L{DialogueActions<DialogueAction>}.
197    """
198    def __init__(self, *args, **kwargs):
199        """
200        Initialize a new L{QuestAction} instance.
201       
202        @param args: positional arguments.
203        @type args: list of objects
204        @param quest_id: ID of the quest to manipulate.
205        @type quest_id: basestring
206        @param kwargs: keyword arguments (not used).
207        @type kwargs: dict of objects
208        """
209        DialogueAction.__init__(self, *args, **kwargs)
210        self.quest_id = kwargs['quest'] if 'quest' in kwargs else args[0]
211
212
213class StartQuestAction(QuestAction):
214    """L{QuestAction} used to activate a quest."""
215    keyword = 'start_quest'
216   
217    def __call__(self, game_state):
218        """
219        Activate a quest.
220       
221        @param game_state: variables and functions that make up the current
222            game state.
223        @type game_state: dict of objects
224        """
225        quest_id = self.quest_id
226        print("You've picked up the \"{0}\" quest!".format(quest_id))
227        game_state['quest'].activateQuest(quest_id)
228DialogueAction.registerAction(StartQuestAction)
229
230
231class CompleteQuestAction(QuestAction):
232    """
233    L{QuestAction} used to mark a quest as successfully finished/completed.
234    """
235    keyword = 'complete_quest'
236   
237    def __call__(self, game_state):
238        """
239        Successfully complete a quest.
240       
241        @param game_state: variables and functions that make up the current
242            game state.
243        @type game_state: dict of objects
244        """
245        quest_id = self.quest_id
246        print("You've finished the \"{0}\" quest".format(quest_id))
247        game_state['quest'].finishQuest(quest_id)
248DialogueAction.registerAction(CompleteQuestAction)
249
250
251class FailQuestAction(QuestAction):
252    """L{QuestAction} used to fail an active quest."""
253    keyword = 'fail_quest'
254   
255    def __call__(self, game_state):
256        """
257        Fail an active quest.
258       
259        @param game_state: variables and functions that make up the current
260            game state.
261        @type game_state: dict of objects
262        """
263        quest_id = self.quest_id
264        print("You've failed the \"{0}\" quest".format(quest_id))
265        game_state['quest'].failQuest(quest_id)
266DialogueAction.registerAction(FailQuestAction)
267
268
269class RestartQuestAction(QuestAction):
270    """L{QuestAction} used to restart an active quest."""
271    keyword = 'restart_quest'
272   
273    def __call__(self, game_state):
274        """
275        Restart an active quest.
276       
277        @param game_state: variables and functions that make up the current
278            game state.
279        @type game_state: dict of objects
280        """
281        quest_id = self.quest_id
282        print("You've restarted the \"{0}\" quest".format(quest_id))
283        game_state['quest'].restartQuest(quest_id)
284DialogueAction.registerAction(RestartQuestAction)
285
286
287class QuestVariableAction(QuestAction):
288    """
289    Base class for L{QuestActions<QuestAction>} that modify quest
290    variables.
291    """
292    def __init__(self, *args, **kwargs):
293        """
294        Initialize a new L{QuestVariableAction} instance.
295       
296        @param args: positional arguments (not used).
297        @type args: list of objects
298        @param kwargs: keyword arguments.
299        @type kwargs: dict of objects
300        @keyword quest: ID of the quest whose variable should be modified.
301        @type quest: basestring
302        @keyword variable: name of the quest variable to modify.
303        @type variable: basestring
304        @keyword value: new value that should be used to modify the quest
305            variable.
306        @type value: object
307        """
308        QuestAction.__init__(self, *args, **kwargs)
309        self.variable_name = kwargs['variable']
310        self.value = kwargs['value']
311
312
313class IncreaseQuestVariableAction(QuestVariableAction):
314    """
315    L{QuestVariableAction} used to increase the value of a quest variable by a
316    set amount.
317    """
318    keyword = 'increase_quest_variable'
319   
320    def __call__(self, game_state):
321        """
322        Increase a quest variable by a set amount.
323       
324        @param game_state: variables and functions that make up the current
325            game state.
326        @type game_state: dict of objects
327        """
328        quest_id = self.quest_id
329        variable_name = self.variable_name
330        value = self.value
331        print('Increased {0} by {1}'.format(variable_name, value))
332        game_state['quest'][quest_id].increaseValue(variable_name, value)
333DialogueAction.registerAction(IncreaseQuestVariableAction)
334
335
336class DecreaseQuestVariableAction(QuestVariableAction):
337    """
338    L{QuestVariableAction} used to decrease the value of a quest variable by a
339    set amount.
340    """
341    keyword = 'decrease_quest_variable'
342   
343    def __call__(self, game_state):
344        """
345        Decrease a quest variable by a set amount.
346       
347        @param game_state: variables and functions that make up the current
348            game state.
349        @type game_state: dict of objects
350        """
351        quest_id = self.quest_id
352        variable_name = self.variable_name
353        value = self.value
354        print('Decreased {0} by {1}'.format(variable_name, value))
355        game_state['quest'][quest_id].decreaseValue(variable_name, value)
356DialogueAction.registerAction(DecreaseQuestVariableAction)
357
358
359class SetQuestVariableAction(QuestVariableAction):
360    """
361    L{QuestVariableAction} used to set the value of a quest variable.
362    """
363    keyword = 'set_quest_variable'
364   
365    def __call__(self, game_state):
366        """
367        Set the value of a quest variable.
368       
369        @param game_state: variables and functions that make up the current
370            game state.
371        @type game_state: dict of objects
372        """
373        quest_id = self.quest_id
374        variable_name = self.variable_name
375        value = self.value
376        print('Set {0} to {1}'.format(variable_name, value))
377        game_state['quest'][quest_id].setValue(variable_name, value)
378DialogueAction.registerAction(SetQuestVariableAction)
Note: See TracBrowser for help on using the repository browser.