Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/Quest.cc @ 2191

Last change on this file since 2191 was 2105, checked in by rgrieder, 16 years ago

updated msvc files and precompiled headers.

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