Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/Quest.cc @ 7456

Last change on this file since 7456 was 7456, checked in by dafrick, 14 years ago

Reviewing documentation fo Questsystem, moving documentation fully into doxygen.
Added some files to modules they belong to.

  • Property svn:eol-style set to native
File size: 13.3 KB
RevLine 
[1992]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 */
[6417]28
[2261]29/**
[7456]30    @file Quest.cc
[2662]31    @brief Implementation of the Quest class.
[2261]32*/
[1992]33
[2105]34#include "Quest.h"
35
[1992]36#include "core/CoreIncludes.h"
[3196]37#include "core/XMLPort.h"
[7456]38
[2095]39#include "QuestDescription.h"
[7456]40#include "QuestEffect.h"
[2095]41#include "QuestHint.h"
[2662]42#include "QuestListener.h"
[7456]43#include "QuestManager.h"
[1992]44
[2662]45namespace orxonox
46{
[2261]47    /**
48    @brief
49        Constructor. Registers and initializes object.
50    */
[2092]51    Quest::Quest(BaseObject* creator) : QuestItem(creator)
[2021]52    {
[2092]53        RegisterObject(Quest);
54
[2261]55        this->parentQuest_ = NULL;
[2021]56    }
[2092]57
[1992]58    /**
59    @brief
60        Destructor.
61    */
62    Quest::~Quest()
63    {
[7163]64        if(this->isRegistered())
65            QuestManager::getInstance().unregisterQuest(this);
[1992]66    }
[2092]67
[2261]68    /**
69    @brief
70        Method for creating a Quest object through XML.
71    */
[2076]72    void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(Quest, XMLPort, xmlelement, mode);
[2092]75
[2261]76        XMLPortObject(Quest, Quest, "subquests", addSubQuest, getSubQuest, xmlelement, mode);
77        XMLPortObject(Quest, QuestHint, "hints", addHint, getHint, xmlelement, mode);
78        XMLPortObject(Quest, QuestEffect, "fail-effects", addFailEffect, getFailEffect, xmlelement, mode);
79        XMLPortObject(Quest, QuestEffect, "complete-effects", addCompleteEffect, getCompleteEffect, xmlelement, mode);
[2092]80
[7456]81        QuestManager::getInstance().registerQuest(this); // Registers the Quest with the QuestManager.
[2076]82    }
[2092]83
[1992]84    /**
85    @brief
[7456]86        Sets the parent-quest of the Quest.
[1996]87    @param quest
[7456]88        A pointer to the Quest to be set as parent-quest.
[2068]89    @return
[7456]90        Returns true if the parent-quest could be set.
[1996]91    */
[2021]92    bool Quest::setParentQuest(Quest* quest)
[1996]93    {
[7456]94        //TODO: Replace with assert.
95        if(quest == NULL) // We don't want to set NULL-Pointers.
[2068]96        {
97            COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
98            return false;
99        }
[2092]100
[1996]101        this->parentQuest_ = quest;
[2092]102
[7163]103        COUT(4) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
[1996]104        return true;
105    }
[2092]106
[1996]107    /**
108    @brief
[7456]109        Adds a sub-quest to the Quest.
[1996]110    @param quest
[7456]111        A pointer to the Quest to be set as sub-quest.
[2068]112    @return
[7456]113        Returns true if the sub-quest could be set.
[1996]114    */
[2021]115    bool Quest::addSubQuest(Quest* quest)
[1996]116    {
[7456]117        //TODO: Replace with assert.
118        if(quest == NULL) // We don't want to set NULL-Pointers.
[2068]119        {
120            COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
121            return false;
122        }
[2092]123
[7456]124        quest->setParentQuest(this); // Sets the currentQuest (this) as parent-quest for the added sub-quest.
125        this->subQuests_.push_back(quest); // Adds the Quest to the end of the list of sub-quests.
[2092]126
[7163]127        COUT(4) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
[1996]128        return true;
129    }
[2092]130
131
[2068]132    /**
133    @brief
[2261]134        Adds a QuestHint to the list of QuestHints
[2076]135    @param hint
[2261]136        The QuestHint that should be added to the list of QuestHints.
[2076]137    @return
138        Returns true if the hint was successfully added.
139    */
140    bool Quest::addHint(QuestHint* hint)
141    {
[7456]142        //TODO: Replace with assert.
143        if(hint == NULL) // We don't want to set NULL-Pointers. Seriously!
[2076]144        {
145            COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl;
146            return false;
147        }
[2092]148
[7456]149        hint->setQuest(this); // Sets the current Quest (this) as Quest for the added QuestHint.
150        this->hints_.push_back(hint); // Adds the QuestHint to the end of the list of QuestHints.
[2092]151
[7163]152        COUT(4) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
[2093]153        return true;
[2076]154    }
[2092]155
[2076]156    /**
157    @brief
[2261]158        Adds an QuestEffect to the list of fail QuestEffects.
159    @param effect
160        The QuestEffect to be added.
161    @return
162        Returns true if successful.
[2076]163    */
164    bool Quest::addFailEffect(QuestEffect* effect)
165    {
[7456]166        //TODO: Replace with assert.
167        if(effect == NULL) // We don't want to set NULL-Pointers.
[2076]168        {
169            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
170            return false;
171        }
[2092]172
[7456]173        this->failEffects_.push_back(effect); // Adds the QuestEffect to the end of the list of fail QuestEffects.
[2092]174
[7163]175        COUT(4) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl;
[2076]176        return true;
177    }
[2092]178
[2076]179    /**
180    @brief
[2261]181        Adds an QuestEffect to the list of complete QuestEffects.
182    @param effect
183        The QuestEffect to be added.
184    @return
185        Returns true if successful.
[2076]186    */
187    bool Quest::addCompleteEffect(QuestEffect* effect)
188    {
[7456]189        //TODO: Replace with assert.
190        if(effect == NULL) // We don't want to set NULL-Pointers.
[2076]191        {
192            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
193            return false;
194        }
[2092]195
[7456]196        this->completeEffects_.push_back(effect); // Adds the QuestEffect to the end of the list of complete QuestEffects.
[2092]197
[7163]198        COUT(4) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl;
[2076]199        return true;
200    }
[2092]201
[2076]202    /**
203    @brief
[7456]204        Returns the sub-quest at the given index.
[7401]205    @param index
[2261]206        The index.
207    @return
[7456]208        Returns a pointer to the sub-quest at the given index. NULL if there is no element at the given index.
[2076]209    */
[2261]210    const Quest* Quest::getSubQuest(unsigned int index) const
[2076]211    {
212        int i = index;
[6417]213
[7456]214        // Iterate through all subquests.
[2076]215        for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest)
[2093]216        {
[7456]217            if(i == 0) // We're counting down...
[2093]218               return *subQuest;
[7456]219
[2093]220            i--;
221        }
[6417]222
[7456]223        return NULL; // If the index is greater than the number of elements in the list.
[2076]224    }
[2092]225
[2076]226    /**
227    @brief
[2261]228        Returns the QuestHint at the given index.
[7401]229    @param index
[2261]230        The index.
231    @return
232        Returns a pointer to the QuestHint at the given index. NULL if there is no element at the given index.
[2076]233    */
[2261]234    const QuestHint* Quest::getHint(unsigned int index) const
[2076]235    {
236        int i = index;
[6417]237
[7456]238        // Iterate through all QuestHints.
[2076]239        for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint)
[2093]240        {
[7456]241            if(i == 0) // We're counting down...
[2093]242               return *hint;
[7456]243
[2093]244            i--;
245        }
[7456]246        return NULL; // If the index is greater than the number of elements in the list.
[2076]247    }
[2092]248
[2076]249    /**
250    @brief
[2261]251        Returns the fail QuestEffect at the given index.
[7401]252    @param index
[2261]253        The index.
254    @return
255        Returns a pointer to the fail QuestEffect at the given index. NULL if there is no element at the given index.
[2076]256    */
[2261]257    const QuestEffect* Quest::getFailEffect(unsigned int index) const
[2076]258    {
259        int i = index;
[6417]260
[7456]261        // Iterate through all fail QuestEffects.
[2076]262        for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect)
[2093]263        {
[7456]264            if(i == 0) // We're counting down...
[2093]265               return *effect;
[7456]266
[2093]267            i--;
268        }
[7456]269        return NULL; // If the index is greater than the number of elements in the list.
[2076]270    }
[2092]271
[2076]272    /**
273    @brief
[2261]274        Returns the complete QuestEffect at the given index.
[7401]275    @param index
[2261]276        The index.
277    @return
278        Returns a pointer to the complete QuestEffect at the given index. NULL if there is no element at the given index.
[2076]279    */
[2261]280    const QuestEffect* Quest::getCompleteEffect(unsigned int index) const
[2076]281    {
282        int i = index;
[6417]283
[7456]284        // Iterate through all complete QuestEffects.
[2076]285        for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect)
[2093]286        {
[7456]287            if(i == 0) // We're counting down...
[2093]288               return *effect;
[7456]289
[2093]290            i--;
291        }
[7456]292        return NULL; // If the index is greater than the number of elements in the list.
[2076]293    }
[2092]294
[2076]295    /**
296    @brief
[2068]297        Returns true if the quest status for the specific player is 'inactive'.
298    @param player
299        The player.
300    @return
301        Returns true if the quest status for the specific player is 'inactive'.
302    @throws
303        Throws an exception if getStatus throws one.
304    */
[2261]305    bool Quest::isInactive(const PlayerInfo* player) const
[2068]306    {
[3280]307        return this->getStatus(player) == QuestStatus::Inactive;
[2068]308    }
[2092]309
[2068]310    /**
311    @brief
312        Returns true if the quest status for the specific player is 'active'.
313    @param player
314        The player.
315    @return
316        Returns true if the quest status for the specific player is 'active'.
317    @throws
318        Throws an exception if getStatus throws one.
319    */
[2261]320    bool Quest::isActive(const PlayerInfo* player) const
[2068]321    {
[3280]322        return this->getStatus(player) == QuestStatus::Active;
[2068]323    }
[2092]324
[1996]325    /**
326    @brief
[2068]327        Returns true if the quest status for the specific player is 'failed'.
328    @param player
329        The player.
330    @return
331        Returns true if the quest status for the specific player is 'failed'.
332    @throws
333        Throws an exception if getStatus throws one.
334    */
[2261]335    bool Quest::isFailed(const PlayerInfo* player) const
[2068]336    {
[3280]337        return this->getStatus(player) == QuestStatus::Failed;
[2068]338    }
[2092]339
[2068]340    /**
341    @brief
342        Returns true if the quest status for the specific player is 'completed'.
343    @param player
344        The player.
345    @return
346        Returns true if the quest status for the specific player is 'completed'.
347    @throws
348        Throws an exception if getStatus throws one.
349    */
[2261]350    bool Quest::isCompleted(const PlayerInfo* player) const
[2068]351    {
[3280]352        return this->getStatus(player) == QuestStatus::Completed;
[2068]353    }
[6417]354
[2662]355    /**
356    @brief
357        Fails the Quest for an input player.
358    @param player
359        The player.
360    @return
361        Returns true if the Quest could be failed, false if not.
362    */
363    bool Quest::fail(PlayerInfo* player)
364    {
[7456]365        QuestListener::advertiseStatusChange(this->listeners_, "fail"); // Tells the QuestListeners, that the status has changed to failed.
[3280]366        this->setStatus(player, QuestStatus::Failed);
[6417]367
[2662]368        COUT(4) << "Quest {" << this->getId() << "} is failed for player: " << player << " ." <<std::endl;
[6417]369
[7403]370        this->getDescription()->sendFailQuestNotification(player);
[2662]371        return true;
372    }
[6417]373
[2662]374    /**
375    @brief
376        Completes the Quest for an input player.
377    @param player
378        The player.
379    @return
380        Returns true if the Quest could be completed, false if not.
381    */
382    bool Quest::complete(PlayerInfo* player)
383    {
[7456]384        QuestListener::advertiseStatusChange(this->listeners_, "complete"); // Tells the QuestListeners, that the status has changed to completed.
[3280]385        this->setStatus(player, QuestStatus::Completed);
[6417]386
[2662]387        COUT(4) << "Quest {" << this->getId() << "} is completed for player: " << player << " ." <<std::endl;
[6417]388
[7403]389        this->getDescription()->sendCompleteQuestNotification(player);
[2662]390        return true;
391    }
[2092]392
[1992]393    /**
394    @brief
[2261]395        Starts the Quest for an input player.
[1992]396    @param player
397        The player.
[1996]398    @return
[2261]399        Returns true if the Quest could be started, false if not.
[1992]400    */
[2261]401    bool Quest::start(PlayerInfo* player)
[1992]402    {
[7456]403        if(!this->isStartable(player)) // Checks whether the quest can be started.
[1992]404        {
[2662]405            COUT(4) << "A non-startable quest was trying to be started." << std::endl;
406            return false;
[1992]407        }
[6417]408
[2662]409        COUT(4) << "Quest {" << this->getId() << "} is started for player: " << player << " ." <<std::endl;
[6417]410
[7456]411        QuestListener::advertiseStatusChange(this->listeners_, "start"); // Tells the QuestListeners, that the status has changed to active.
[6417]412
[3280]413        this->setStatus(player, QuestStatus::Active);
[6417]414
[7403]415        this->getDescription()->sendAddQuestNotification(player);
[2662]416        return true;
[1992]417    }
[6417]418
[2662]419    /**
420    @brief
421        Adds a QuestListener to the list of QuestListeners listening to this Quest.
422    @param listener
423        The QuestListener to be added.
424    @return
425        Returns true if successful, false if not.
426    */
427    bool Quest::addListener(QuestListener* listener)
428    {
[7456]429        //TODO: Replace with assert?
[2662]430        if(listener == NULL)
431        {
432            COUT(2) << "A NULL-QuestListener was trying to be added to a Quests listeners." << std::endl;
433            return false;
434        }
[6417]435
[2662]436        this->listeners_.push_back(listener);
437        return true;
438    }
[2092]439
[1992]440}
Note: See TracBrowser for help on using the repository browser.