comparison quest_engine.py @ 0:7a89ea5404b1

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children 06145a6ee387
comparison
equal deleted inserted replaced
-1:000000000000 0:7a89ea5404b1
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
16 import yaml
17 from parpg.common.utils import locateFiles
18 import os.path
19
20 class Quest(object):
21 """Class that holds the information for a quest"""
22 def __init__(self, quest_id, quest_giver_id, quest_name, description,
23 variables):
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 if variable_name in self.quest_variables:
102 return self.quest_variables[variable_name]["value"] >= \
103 self.quest_variables[variable_name]["goal_value"]
104 else:
105 return False
106
107 def restartQuest(self):
108 """Restarts the quest. This sets all values to the reset values,
109 if there is a reset value present """
110 for variable in self.quest_variables.itervalues():
111 if variable.has_key("reset_value"):
112 variable["value"] = variable["reset_value"]
113
114 class QuestEngine(dict):
115 def __init__(self, quest_dir):
116 """Create a quest engine object"""
117 dict.__init__(self)
118 self.empty_quest = Quest(None, None, None, None, {})
119 self.quests = {}
120 self.active_quests = []
121 self.finished_quests = []
122 self.failed_quests = []
123 self.quest_dir = quest_dir
124
125 def __str__(self):
126 return self.quests.__str__()
127
128 def __getitem__(self, key):
129 try:
130 return self.quests.__getitem__(key)
131 except KeyError:
132 return self.empty_quest
133
134 def items(self):
135 return self.quests.items()
136
137 def values(self):
138 return self.quests.values()
139
140 def keys(self):
141 return self.quests.keys()
142
143 def readQuests(self):
144 """Reads in the quests in the quest directory"""
145 files = locateFiles("*.yaml", self.quest_dir)
146 self.quests = {}
147 self.active_quests = []
148 self.finished_quests = []
149 self.failed_quests = []
150 for quest_file in files:
151 quest_file = os.path.relpath(quest_file).replace("\\", "/")
152 tree = yaml.load(open(quest_file))
153 quest_properties = tree["QUEST_PROPERTIES"]
154 variable_defines = tree["DEFINES"]
155
156 self.quests[quest_properties["quest_id"]] = \
157 Quest(quest_properties["quest_id"],
158 quest_properties["quest_giver_id"],
159 quest_properties["quest_name"],
160 quest_properties["description"],
161 variable_defines)
162
163 def activateQuest(self, quest_id):
164 """Add a quest to the quest log
165 @param quest: the quest id of the quest to add to the quest log
166 @return: True if succesfully added
167 @return: False if failed to add"""
168
169 if quest_id in self.quests \
170 and not (quest_id in self.active_quests \
171 or quest_id in self.finished_quests):
172 self.active_quests.append(quest_id)
173 return True
174 return False
175
176 def finishQuest(self, quest_id):
177 """Move a quest to the finished quests log
178 @param quest_id: The id of the quest you want to move
179 @return: True on success
180 @return: False when it failes"""
181 if quest_id in self.active_quests:
182 self.finished_quests.append(quest_id)
183 self.active_quests.remove(quest_id)
184 return True
185 return False
186
187 def restartQuest(self, quest_id):
188 """Restart a quest
189 @param quest_id: ID of the quest you want to restart
190 @return: True on success
191 @return: False when it failes"""
192 if quest_id in self.active_quests:
193 self.quests[quest_id].restartQuest()
194
195 def failQuest(self, quest_id):
196 """Set a quest to failed
197 @param quest_id: ID of the quest you want to fail
198 @return: True on success
199 @return: False when it failes"""
200 if quest_id in self.active_quests:
201 self.failed_quests.append(quest_id)
202 self.active_quests.remove(quest_id)
203 return True
204 return False
205
206 def hasQuest(self, quest_id):
207 """Check whether a quest is present in the quest_list.
208 It doesn't matter which state the quest is, or even if its
209 started.
210 @param quest_id: ID of the quest you want to check
211 @return: True on when the quest is in the quest log
212 @return: False when it's not in the quest log"""
213 return quest_id in self.quests
214
215 def hasActiveQuest(self, quest_id):
216 """Check whether a quest is in the quest log
217 @param quest_id: ID of the quest you want to check
218 @return: True on when the quest is in the quest log
219 @return: False when it's not in the quest log"""
220 return quest_id in self.active_quests
221
222 def hasFinishedQuest(self, quest_id):
223 """Check whether a quest is in the finished quests log
224 @param quest_id: ID of the quest you want to check
225 @return: True on when the quest is in the finished quests log
226 @return: False when it's not in the finished quests log"""
227 return quest_id in self.finished_quests
228
229 def hasFailedQuest(self, quest_id):
230 """Check whether a quest is in the failed quests log
231 @param quest_id: ID of the quest you want to check
232 @return: True on when the quest is in the failed quests log
233 @return: False when it's not in the failed quests log"""
234 return quest_id in self.failed_quests
235
236 def getStateForSaving(self):
237 """Prepares state for saving
238 @type state: dictionary
239 @param state: State of the object"""
240 ret_dict = {}
241 variables_dict = ret_dict["Variables"] = {}
242 for quest in self.quests.itervalues():
243 quest_dict = variables_dict[quest.quest_id] = {}
244 for variable, data in quest.quest_variables.iteritems():
245 quest_dict[variable] = data["value"]
246 ret_dict["ActiveQuests"] = self.active_quests
247 ret_dict["FinishedQuests"] = self.finished_quests
248 ret_dict["FailedQuests"] = self.failed_quests
249 return ret_dict
250
251 def restoreFromState(self, state):
252 """Restores the state"""
253 variables_dict = state["Variables"]
254 for quest_id, variables in variables_dict.iteritems():
255 for variable, value in variables.iteritems():
256 self.quests[quest_id].setValue(variable, value)
257 self.active_quests = state["ActiveQuests"]
258 self.finished_quests = state["FinishedQuests"]
259 self.failed_quests = state["FailedQuests"]