source: trunk/game/scripts/quest_engine.py @ 359

Revision 350, 7.8 KB checked in by orlandov, 10 years ago (diff)

Patch by Vaporice and or1andov:

  • add a quest model to the dialogue demo
  • refactor zenbit's dialogue tree to use quest engine
Line 
1#!/usr/bin/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
18import yaml
19
20class Quest(object):
21    def __init__(self, quest_id, quest_giver_id, quest_name, description,
22                 variables):
23        """This holds all the quest information"""
24        self.quest_id = quest_id
25        self.quest_giver_id = quest_giver_id
26        self.quest_name = quest_name
27        self.description = description
28        self.quest_variables = variables
29
30    def setValue(self, variable_name, value):
31        """Set the value of a quest variable
32           @param variable_name: the name of the variable to set
33           @param value: the value you want to assign to the variable
34           @return: True on success
35           @return: False when it failes"""
36
37        if self.quest_variables.has_key(variable_name):
38            self.quest_variables[variable_name]["value"] = value
39            return True
40        else:
41            return False
42
43    def getValue(self, variable_name):
44        """Get the value of a quest_variable
45           @param variable_name: the name of the variable to set
46           @return: the value of the quest_variable"""
47        if self.quest_variables.has_key(variable_name):
48            return self.quest_variables[variable_name]["value"]
49        else:
50            return False
51
52    def getGoalValue(self, variable_name):
53        """Get the goal value of a quest_variable
54           @param variable_name: the name of the variable to set
55           @return: the goal value of the quest variable"""
56        if self.quest_variables.has_key(variable_name):
57            return self.quest_variables[variable_name]["goal_value"]
58        else:
59            return False
60
61    def increaseValue(self, variable_name, value):
62        """Increase a variable by a specified value
63           @param variable_name: the name of the variable to set
64           @param value: the value you want to increase the variable with
65           @return: True on success
66           @return: False when it failes"""
67        if self.quest_variables.has_key(variable_name):
68            self.quest_variables[variable_name]["value"] += value
69            return True
70        else:
71            return False
72
73    def decreaseValue(self, variable_name, value):
74        """Decrease a variable by a specified value
75           @param variable_name: the name of the variable to set
76           @param value: the value you want to decrease the variable with
77           @return: True on success
78           @return: False when it failes"""
79        if self.quest_variables.has_key(variable_name):
80            self.quest_variables[variable_name]["value"] -= value
81            return True
82        else:
83            return False
84
85    def isGoalValue(self, variable_name):
86        """Check if the variable has reached it's goal value
87           @param variable_name: the name of the variable to check
88           @return: True when the variable has reached the goal value
89           @return: False when it has not reached the goal value"""
90        if self.quest_variables.has_key(variable_name):
91            return self.quest_variables[variable_name]["value"] == \
92                    self.quest_variables[variable_name]["goal_value"]
93        else:
94            return False
95
96    def isEqualOrBiggerThanGoalValue(self, variable_name):
97        """Check if the variable is equil or bigger then it's goal value
98           @param variable_name: the name of the variable to set
99           @return: True when it has reached or exceeded the goal value
100           @return: False when it has not reached or exceeded the goal value
101           """
102        if variable_name in self.quest_variables:
103            return self.quest_variables[variable_name]["value"] >= \
104                             self.quest_variables[variable_name]["goal_value"]
105        else:
106            return False
107
108
109class QuestEngine(dict):
110    def __init__(self):
111        """Create a quest engine object"""
112        dict.__init__(self)
113
114        self.empty_quest = Quest(None,None,None,None,{})
115        self.active_quests = {}
116        self.finished_quests = {}
117
118    def __str__(self):
119        return self.active_quests.__str__()
120
121    def __getitem__(self, key):
122        try:
123            return self.active_quests.__getitem__(key)
124        except KeyError:
125            return self.empty_quest
126
127    def items(self):
128        return self.active_quests.items()
129
130    def values(self):
131        return self.active_quests.values()
132
133    def keys(self):
134        return self.active_quests.keys()
135
136    def addQuest(self, quest):
137        """Add a quest to the quest log
138           @param quest: the quest file or tree to add to the quest log
139           @return: True if succesfully added
140           @return: False if failed to add"""
141
142        if isinstance(quest, str):
143            #Convert YAML to Quest
144            tree = yaml.load(file(quest))
145
146        if isinstance(quest, dict):
147            tree = quest
148
149        quest_properties = tree["QUEST_PROPERTIES"]
150        variable_defines = tree["DEFINES"]
151
152        self.active_quests[quest_properties["quest_id"]] = \
153                              Quest(quest_properties["quest_id"],
154                                    quest_properties["quest_giver_id"],
155                                    quest_properties["quest_name"],
156                                    quest_properties["description"],
157                                    variable_defines)
158        return True
159
160    def finishQuest(self, quest_id):
161        """Move a quest to the finished quests log
162           @param quest_id: The id of the quest you want to move
163           @return: True on success
164           @return: False when it failes
165        """
166        if self.active_quests.has_key(quest_id):
167            #Transfer to finished list
168            self.finished_quests[quest_id] = self.active_quests[quest_id]
169            del self.active_quests[quest_id]
170            return True
171        elif self.finished_quests.has_key(quest_id):
172            #Temporary fix
173            return True
174
175        else:
176            return False
177
178    def hasQuest(self, quest_id):
179        """Check wether a quest is in the quest log
180           @param quest_id: ID of the quest you want to check
181           @return: True on when the quest is in the quest log
182           @return: False when it's not in the quest log"""
183        if quest_id in self.active_quests.keys():
184            return True
185        else:
186            return False
187
188    def hasFinishedQuest(self, quest_id):
189        """Check wether a quest is in the finished quests log
190           @param quest_id: ID of the quest you want to check
191           @return: True on when the quest is in the fisnished quests log
192           @return: False when it's not in the finished quests log"""
193        if quest_id in self.finished_quests.keys():
194            return True
195        else:
196            return False
197
198    def deleteQuest(self, quest_id):
199        """Delete a quest
200           @param quest_id: ID of the quest you want to delete
201           @return: True on success
202           @return: False when it failes"""
203        if quest_id in self.active_quests.keys():
204            del self.active_quests[quest_id]
205            return True
206        elif quest_id in self.finished_quests.keys():
207            del self.finished_quests[quest_id]
208            return True
209        return False
Note: See TracBrowser for help on using the repository browser.