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

Revision 476, 7.8 KB checked in by maximinus_parpg, 10 years ago (diff)

Edited and tidied up files.
Generally a mix of spelling mistakes, non pep-8 code
and other things.

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 fails"""
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
108class QuestEngine(dict):
109    def __init__(self):
110        """Create a quest engine object"""
111        dict.__init__(self)
112        self.empty_quest = Quest(None,None,None,None,{})
113        self.active_quests = {}
114        self.finished_quests = {}
115
116    def __str__(self):
117        return self.active_quests.__str__()
118
119    def __getitem__(self, key):
120        try:
121            return self.active_quests.__getitem__(key)
122        except KeyError:
123            return self.empty_quest
124
125    def items(self):
126        return self.active_quests.items()
127
128    def values(self):
129        return self.active_quests.values()
130
131    def keys(self):
132        return self.active_quests.keys()
133
134    def addQuest(self, quest):
135        """Add a quest to the quest log
136           @param quest: the quest file or tree to add to the quest log
137           @return: True if succesfully added
138           @return: False if failed to add"""
139
140        if isinstance(quest, str):
141            # Convert YAML to Quest
142            tree = yaml.load(file(quest))
143
144        if isinstance(quest, dict):
145            tree = quest
146
147        quest_properties = tree["QUEST_PROPERTIES"]
148        variable_defines = tree["DEFINES"]
149
150        self.active_quests[quest_properties["quest_id"]] = \
151                              Quest(quest_properties["quest_id"],
152                                    quest_properties["quest_giver_id"],
153                                    quest_properties["quest_name"],
154                                    quest_properties["description"],
155                                    variable_defines)
156        return True
157
158    def finishQuest(self, quest_id):
159        """Move a quest to the finished quests log
160           @param quest_id: The id of the quest you want to move
161           @return: True on success
162           @return: False when it failes"""
163        if self.active_quests.has_key(quest_id):
164            #Transfer to finished list
165            self.finished_quests[quest_id] = self.active_quests[quest_id]
166            del self.active_quests[quest_id]
167            return True
168        elif self.finished_quests.has_key(quest_id):
169            #Temporary fix
170            return True
171        else:
172            return False
173
174    def hasQuest(self, quest_id):
175        """Check wether a quest is in the quest log
176           @param quest_id: ID of the quest you want to check
177           @return: True on when the quest is in the quest log
178           @return: False when it's not in the quest log"""
179        if quest_id in self.active_quests.keys():
180            return True
181        else:
182            return False
183
184    def hasFinishedQuest(self, quest_id):
185        """Check wether a quest is in the finished quests log
186           @param quest_id: ID of the quest you want to check
187           @return: True on when the quest is in the fisnished quests log
188           @return: False when it's not in the finished quests log"""
189        if quest_id in self.finished_quests.keys():
190            return True
191        else:
192            return False
193
194    def deleteQuest(self, quest_id):
195        """Delete a quest
196           @param quest_id: ID of the quest you want to delete
197           @return: True on success
198           @return: False when it failes"""
199        if quest_id in self.active_quests.keys():
200            del self.active_quests[quest_id]
201            return True
202        elif quest_id in self.finished_quests.keys():
203            del self.finished_quests[quest_id]
204            return True
205        return False
Note: See TracBrowser for help on using the repository browser.