Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Resolving some TODOs and doing some additional cleanup. Almost done now…

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