Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

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