Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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