source: branches/active/character_customization/game/parpg/quest_engine.py @ 758

Revision 736, 10.4 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

  • renamed scripts package to parpg
  • renamed parpg module to application
  • removed packaging and other related files (kept locally for reference, will reintroduce similar scripts to resolve bug #275
  • updated all import statements to respect changes above
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/env 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
19from parpg.common.utils import locateFiles
20import os.path
21
22class Quest(object):
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
45    def getValue(self, variable_name):
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
54    def getGoalValue(self, variable_name):
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
63    def increaseValue(self, variable_name, value):
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
75    def decreaseValue(self, variable_name, value):
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
87    def isGoalValue(self, variable_name):
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
98    def isEqualOrBiggerThanGoalValue(self, variable_name):
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   
109    def restartQuest(self):
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
116class QuestEngine(dict):
117    def __init__(self, quest_dir):
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
127    def __str__(self):
128        return self.quests.__str__()
129
130    def __getitem__(self, key):
131        try:
132            return self.quests.__getitem__(key)
133        except KeyError:
134            return self.empty_quest
135
136    def items(self):
137        return self.quests.items()
138
139    def values(self):
140        return self.quests.values()
141
142    def keys(self):
143        return self.quests.keys()
144   
145    def readQuests(self):
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
165    def activateQuest(self, quest_id):
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
178    def finishQuest(self, quest_id):
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   
189    def restartQuest(self, quest_id):
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   
197    def failQuest(self, quest_id):
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           
208    def hasQuest(self, quest_id):
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
217    def hasActiveQuest(self, quest_id):
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
224    def hasFinishedQuest(self, quest_id):
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   
231    def hasFailedQuest(self, quest_id):
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   
238    def getStateForSaving(self):
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
253    def restoreFromState(self, state):
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"]
Note: See TracBrowser for help on using the repository browser.