Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/quest/Quest.cc @ 2146

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

Started implementation of QuestEffectBeacon.
Done some documentation of QuestItem and subclasses.

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