Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem/src/orxonox/objects/Quest.cc @ 2138

Last change on this file since 2138 was 2081, checked in by dafrick, 16 years ago

Ready for merge! (It won't compile after the merge, though ;) )

File size: 9.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "core/CoreIncludes.h"
30
31#include "Quest.h"
32#include "QuestManager.h"
33
34namespace orxonox {
35
36    Quest::Quest() : QuestItem()
37    {
38        this->initialize();
39    }
40   
41    /**
42    @brief
43        Destructor.
44    */
45    Quest::~Quest()
46    {
47       
48    }
49   
50    void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
51    {
52        SUPER(Quest, XMLPort, xmlelement, mode);
53       
54        XMLPortObject(Quest, Quest, "", addSubQuest, getSubQuests, xmlelement, mode);
55        XMLPortObject(Quest, QuestHint, "", addHint, getHints, xmlelement, mode);
56        XMLPortObject(Quest, QuestEffect, "fail-effects", addFailEffect, getFailEffects, xmlelement, mode);
57        XMLPortObject(Quest, QuestEffect, "complete-effects", addCompleteEffect, getCompleteEffects, xmlelement, mode);
58       
59        QuestManager::registerQuest(this); //Registers the quest with the QuestManager.
60    }
61   
62    /**
63    @brief
64        Initializes the object. Needs to be called first in every constructor of this class.
65    */
66    void Quest::initialize(void)
67    {
68        RegisterObject(Quest);
69       
70        this->parentQuest_ = NULL;
71    }
72
73    /**
74    @brief
75        Sets the parent quest of the quest.
76    @param quest
77        A pointer to the quest to be set as parent quest.
78    @return
79        Returns true if the parentQuest could be set.
80    */
81    bool Quest::setParentQuest(Quest* quest)
82    {
83        if(quest == NULL)
84        {
85            COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
86            return false;
87        }
88       
89        this->parentQuest_ = quest;
90       
91        COUT(3) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
92        return true;
93    }
94   
95    /**
96    @brief
97        Adds a sub quest to the quest.
98    @param quest
99        A pointer to the quest to be set as sub quest.
100    @return
101        Returns true if the subQuest vould be set.
102    */
103    bool Quest::addSubQuest(Quest* quest)
104    {
105        if(quest == NULL)
106        {
107            COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
108            return false;
109        }
110       
111        quest->setParentQuest(this);
112        this->subQuests_.push_back(quest);
113       
114        COUT(3) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
115        return true;
116    }
117   
118   
119    /**
120    @brief
121        Adds a Hint to the list of hints
122    @param hint
123        The hint that should be added to the list of hints.
124    @return
125        Returns true if the hint was successfully added.
126    */
127    bool Quest::addHint(QuestHint* hint)
128    {
129        if(hint == NULL)
130        {
131            COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl;
132            return false;
133        }
134       
135        this->hints_.push_back(hint);
136        hint->setQuest(this);
137       
138        COUT(3) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
139        return true;
140    }
141   
142    /**
143    @brief
144       
145    */
146    bool Quest::addFailEffect(QuestEffect* effect)
147    {
148        if(effect == NULL)
149        {
150            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
151            return false;
152        }
153       
154        this->failEffects_.push_back(effect);
155       
156        COUT(3) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl;
157        return true;
158    }
159   
160    /**
161    @brief
162       
163    */
164    bool Quest::addCompleteEffect(QuestEffect* effect)
165    {
166        if(effect == NULL)
167        {
168            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
169            return false;
170        }
171       
172        this->completeEffects_.push_back(effect);
173       
174        COUT(3) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl;
175        return true;
176    }
177   
178    /**
179    @brief
180       
181    */
182    const Quest* Quest::getParentQuest(void)
183    {
184        return this->parentQuest_;
185    }
186   
187    /**
188    @brief
189       
190    */
191    const Quest* Quest::getSubQuests(unsigned int index) const
192    {
193        int i = index;
194        for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest)
195        {
196            if(i == 0)
197            {
198               return *subQuest;
199            }
200            i--;
201        }
202        return NULL;
203    }
204   
205    /**
206    @brief
207       
208    */
209    const QuestHint* Quest::getHints(unsigned int index) const
210    {
211        int i = index;
212        for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint)
213        {
214            if(i == 0)
215            {
216               return *hint;
217            }
218            i--;
219        }
220        return NULL;
221    }
222   
223    /**
224    @brief
225       
226    */
227    const QuestEffect* Quest::getFailEffects(unsigned int index) const
228    {
229        int i = index;
230        for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect)
231        {
232            if(i == 0)
233            {
234               return *effect;
235            }
236            i--;
237        }
238        return NULL;
239    }
240   
241    /**
242    @brief
243       
244    */
245    const QuestEffect* Quest::getCompleteEffects(unsigned int index) const
246    {
247        int i = index;
248        for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect)
249        {
250            if(i == 0)
251            {
252               return *effect;
253            }
254            i--;
255        }
256        return NULL;
257    }
258   
259    /**
260    @brief
261        Returns true if the quest status for the specific player is 'inactive'.
262    @param player
263        The player.
264    @return
265        Returns true if the quest status for the specific player is 'inactive'.
266    @throws
267        Throws an exception if getStatus throws one.
268    */
269    bool Quest::isInactive(const Player* player) const
270    {
271        return this->getStatus(player) == questStatus::inactive;
272    }
273   
274    /**
275    @brief
276        Returns true if the quest status for the specific player is 'active'.
277    @param player
278        The player.
279    @return
280        Returns true if the quest status for the specific player is 'active'.
281    @throws
282        Throws an exception if getStatus throws one.
283    */
284    bool Quest::isActive(const Player* player) const
285    {
286
287        return this->getStatus(player) == questStatus::active;
288    }
289   
290    /**
291    @brief
292        Returns true if the quest status for the specific player is 'failed'.
293    @param player
294        The player.
295    @return
296        Returns true if the quest status for the specific player is 'failed'.
297    @throws
298        Throws an exception if getStatus throws one.
299    */
300    bool Quest::isFailed(const Player* player) const
301    {
302        return this->getStatus(player) == questStatus::failed;
303    }
304   
305    /**
306    @brief
307        Returns true if the quest status for the specific player is 'completed'.
308    @param player
309        The player.
310    @return
311        Returns true if the quest status for the specific player is 'completed'.
312    @throws
313        Throws an exception if getStatus throws one.
314    */
315    bool Quest::isCompleted(const Player* player) const
316    {
317        return this->getStatus(player) == questStatus::completed;
318    }
319   
320    /**
321    @brief
322        Starts the quest.
323    @param player
324        The player.
325    @return
326        Returns true if the quest could be started, false if not.
327    */
328    bool Quest::start(Player* player)
329    {
330        if(this->isStartable(player))
331        {
332            this->setStatus(player, questStatus::active);
333            return true;
334        }
335        COUT(2) << "A non-startable quest was trying to be started." << std::endl;
336        return false;
337    }
338   
339    /**
340    @brief
341        Fails the quest.
342    @param player
343        The player.
344    @return
345        Returns true if the quest could be failed, false if not.
346    */
347    bool Quest::fail(Player* player)
348    {
349        if(this->isFailable(player))
350        {
351            this->setStatus(player, questStatus::failed);
352            QuestEffect::invokeEffects(player, this->failEffects_);
353            return true;
354        }
355        COUT(2) << "A non-failable quest was trying to be failed." << std::endl;
356        return false;
357    }
358   
359    /**
360    @brief
361        Completes the quest.
362    @param player
363        The player.
364    @return
365        Returns true if the quest could be completed, false if not.
366    */
367    bool Quest::complete(Player* player)
368    {
369        if(this->isCompletable(player))
370        {
371            this->setStatus(player, questStatus::completed);
372            QuestEffect::invokeEffects(player, this->completeEffects_);
373            return true;
374        }
375        COUT(2) << "A non-completable quest was trying to be completed." << std::endl;
376        return false;
377    }
378
379}
Note: See TracBrowser for help on using the repository browser.