1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import yaml
19 from scripts.common.utils import locateFiles
20 import os.path
21
23 """Class that holds the information for a quest"""
24 - def __init__(self, quest_id, quest_giver_id, quest_name, description,
25 variables):
26 self.quest_id = quest_id
27 self.quest_giver_id = quest_giver_id
28 self.quest_name = quest_name
29 self.description = description
30 self.quest_variables = variables
31
32 - def setValue(self, variable_name, value):
33 """Set the value of a quest variable
34 @param variable_name: the name of the variable to set
35 @param value: the value you want to assign to the variable
36 @return: True on success
37 @return: False when it failes"""
38
39 if self.quest_variables.has_key(variable_name):
40 self.quest_variables[variable_name]["value"] = value
41 return True
42 else:
43 return False
44
46 """Get the value of a quest_variable
47 @param variable_name: the name of the variable to set
48 @return: the value of the quest_variable"""
49 if self.quest_variables.has_key(variable_name):
50 return self.quest_variables[variable_name]["value"]
51 else:
52 return False
53
55 """Get the goal value of a quest_variable
56 @param variable_name: the name of the variable to set
57 @return: the goal value of the quest variable"""
58 if self.quest_variables.has_key(variable_name):
59 return self.quest_variables[variable_name]["goal_value"]
60 else:
61 return False
62
64 """Increase a variable by a specified value
65 @param variable_name: the name of the variable to set
66 @param value: the value you want to increase the variable with
67 @return: True on success
68 @return: False when it fails"""
69 if self.quest_variables.has_key(variable_name):
70 self.quest_variables[variable_name]["value"] += value
71 return True
72 else:
73 return False
74
76 """Decrease a variable by a specified value
77 @param variable_name: the name of the variable to set
78 @param value: the value you want to decrease the variable with
79 @return: True on success
80 @return: False when it failes"""
81 if self.quest_variables.has_key(variable_name):
82 self.quest_variables[variable_name]["value"] -= value
83 return True
84 else:
85 return False
86
88 """Check if the variable has reached it's goal value
89 @param variable_name: the name of the variable to check
90 @return: True when the variable has reached the goal value
91 @return: False when it has not reached the goal value"""
92 if self.quest_variables.has_key(variable_name):
93 return self.quest_variables[variable_name]["value"] == \
94 self.quest_variables[variable_name]["goal_value"]
95 else:
96 return False
97
99 """Check if the variable is equil or bigger then it's goal value
100 @param variable_name: the name of the variable to set
101 @return: True when it has reached or exceeded the goal value
102 @return: False when it has not reached or exceeded the goal value """
103 if variable_name in self.quest_variables:
104 return self.quest_variables[variable_name]["value"] >= \
105 self.quest_variables[variable_name]["goal_value"]
106 else:
107 return False
108
110 """Restarts the quest. This sets all values to the reset values,
111 if there is a reset value present """
112 for variable in self.quest_variables.itervalues():
113 if variable.has_key("reset_value"):
114 variable["value"] = variable["reset_value"]
115
118 """Create a quest engine object"""
119 dict.__init__(self)
120 self.empty_quest = Quest(None, None, None, None, {})
121 self.quests = {}
122 self.active_quests = []
123 self.finished_quests = []
124 self.failed_quests = []
125 self.quest_dir = quest_dir
126
129
131 try:
132 return self.quests.__getitem__(key)
133 except KeyError:
134 return self.empty_quest
135
137 return self.quests.items()
138
140 return self.quests.values()
141
143 return self.quests.keys()
144
146 """Reads in the quests in the quest directory"""
147 files = locateFiles("*.yaml", self.quest_dir)
148 self.quests = {}
149 self.active_quests = []
150 self.finished_quests = []
151 self.failed_quests = []
152 for quest_file in files:
153 quest_file = os.path.relpath(quest_file).replace("\\", "/")
154 tree = yaml.load(open(quest_file))
155 quest_properties = tree["QUEST_PROPERTIES"]
156 variable_defines = tree["DEFINES"]
157
158 self.quests[quest_properties["quest_id"]] = \
159 Quest(quest_properties["quest_id"],
160 quest_properties["quest_giver_id"],
161 quest_properties["quest_name"],
162 quest_properties["description"],
163 variable_defines)
164
166 """Add a quest to the quest log
167 @param quest: the quest id of the quest to add to the quest log
168 @return: True if succesfully added
169 @return: False if failed to add"""
170
171 if quest_id in self.quests \
172 and not (quest_id in self.active_quests \
173 or quest_id in self.finished_quests):
174 self.active_quests.append(quest_id)
175 return True
176 return False
177
179 """Move a quest to the finished quests log
180 @param quest_id: The id of the quest you want to move
181 @return: True on success
182 @return: False when it failes"""
183 if quest_id in self.active_quests:
184 self.finished_quests.append(quest_id)
185 self.active_quests.remove(quest_id)
186 return True
187 return False
188
190 """Restart a quest
191 @param quest_id: ID of the quest you want to restart
192 @return: True on success
193 @return: False when it failes"""
194 if quest_id in self.active_quests:
195 self.quests[quest_id].restartQuest()
196
198 """Set a quest to failed
199 @param quest_id: ID of the quest you want to fail
200 @return: True on success
201 @return: False when it failes"""
202 if quest_id in self.active_quests:
203 self.failed_quests.append(quest_id)
204 self.active_quests.remove(quest_id)
205 return True
206 return False
207
209 """Check whether a quest is present in the quest_list.
210 It doesn't matter which state the quest is, or even if its
211 started.
212 @param quest_id: ID of the quest you want to check
213 @return: True on when the quest is in the quest log
214 @return: False when it's not in the quest log"""
215 return quest_id in self.quests
216
218 """Check whether a quest is in the quest log
219 @param quest_id: ID of the quest you want to check
220 @return: True on when the quest is in the quest log
221 @return: False when it's not in the quest log"""
222 return quest_id in self.active_quests
223
225 """Check whether a quest is in the finished quests log
226 @param quest_id: ID of the quest you want to check
227 @return: True on when the quest is in the finished quests log
228 @return: False when it's not in the finished quests log"""
229 return quest_id in self.finished_quests
230
232 """Check whether a quest is in the failed quests log
233 @param quest_id: ID of the quest you want to check
234 @return: True on when the quest is in the failed quests log
235 @return: False when it's not in the failed quests log"""
236 return quest_id in self.failed_quests
237
239 """Prepares state for saving
240 @type state: dictionary
241 @param state: State of the object"""
242 ret_dict = {}
243 variables_dict = ret_dict["Variables"] = {}
244 for quest in self.quests.itervalues():
245 quest_dict = variables_dict[quest.quest_id] = {}
246 for variable, data in quest.quest_variables.iteritems():
247 quest_dict[variable] = data["value"]
248 ret_dict["ActiveQuests"] = self.active_quests
249 ret_dict["FinishedQuests"] = self.finished_quests
250 ret_dict["FailedQuests"] = self.failed_quests
251 return ret_dict
252
254 """Restores the state"""
255 variables_dict = state["Variables"]
256 for quest_id, variables in variables_dict.iteritems():
257 for variable, value in variables.iteritems():
258 self.quests[quest_id].setValue(variable, value)
259 self.active_quests = state["ActiveQuests"]
260 self.finished_quests = state["FinishedQuests"]
261 self.failed_quests = state["FailedQuests"]
262