Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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