Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/GlobalQuest.cc @ 7480

Last change on this file since 7480 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: 8.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 GlobalQuest.cc
[2662]31    @brief Implementation of the GlobalQuest class.
[2261]32*/
[1992]33
[2105]34#include "GlobalQuest.h"
35
[1992]36#include "core/CoreIncludes.h"
[3196]37#include "core/XMLPort.h"
[7456]38
[2261]39#include "QuestEffect.h"
40
[2662]41namespace orxonox
42{
[1992]43    CreateFactory(GlobalQuest);
44
[2068]45    /**
46    @brief
[2261]47        Constructor. Registers the object.
[2068]48    */
[2092]49    GlobalQuest::GlobalQuest(BaseObject* creator) : Quest(creator)
[2021]50    {
[2092]51        RegisterObject(GlobalQuest);
[2021]52    }
[2092]53
[1992]54    /**
55    @brief
56        Destructor.
57    */
58    GlobalQuest::~GlobalQuest()
59    {
[2092]60
[1992]61    }
[6417]62
[2261]63    /**
64    @brief
65        Method for creating a GlobalQuest object through XML.
66    */
[2076]67    void GlobalQuest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
68    {
69        SUPER(GlobalQuest, XMLPort, xmlelement, mode);
[6417]70
[2261]71        XMLPortObject(GlobalQuest, QuestEffect, "reward-effects", addRewardEffect, getRewardEffects, xmlelement, mode);
[2076]72
[7163]73        COUT(4) << "New GlobalQuest {" << this->getId() << "} created." << std::endl;
[2076]74    }
[6417]75
[2261]76    /**
77    @brief
78        Fails the Quest for all players.
79        Invokes the fail QuestEffects on all the players possessing this Quest.
80    @param player
81        The player failing it.
82    @return
83        Returns true if the Quest could be failed, false if not.
84    */
85    bool GlobalQuest::fail(PlayerInfo* player)
86    {
[7456]87        if(!this->isFailable(player)) // Check whether the Quest can be failed.
[2261]88        {
[2662]89            COUT(4) << "A non-completable quest was trying to be failed." << std::endl;
90            return false;
[2261]91        }
[6417]92
[2662]93        Quest::fail(player);
[6417]94
[7456]95        // Iterate through all players possessing this Quest.
96        for(std::set<PlayerInfo*>::const_iterator it = players_.begin(); it != players_.end(); it++)
97            QuestEffect::invokeEffects(*it, this->getFailEffectList());
[2261]98
[7456]99        return true;
[2662]100    }
101
[2261]102    /**
103    @brief
104        Completes the Quest for all players.
105        Invokes the complete QuestEffects on all the players possessing this Quest.
106        Invokes the reward QuestEffects on the player completing the Quest.
107    @param player
108        The player completing it.
109    @return
110        Returns true if the Quest could be completed, false if not.
111    */
112    bool GlobalQuest::complete(PlayerInfo* player)
[2068]113    {
[7456]114        if(!this->isCompletable(player)) // Check whether the Quest can be completed.
[2261]115        {
[2662]116            COUT(4) << "A non-completable quest was trying to be completed." << std::endl;
117            return false;
[2261]118        }
[6417]119
[7456]120        // Iterate through all players possessing the Quest.
[2662]121        for(std::set<PlayerInfo*>::const_iterator it = players_.begin(); it != players_.end(); it++)
122            QuestEffect::invokeEffects(*it, this->getCompleteEffectList());
[6417]123
[2662]124        Quest::complete(player);
[6417]125
[7456]126        QuestEffect::invokeEffects(player, this->rewards_); // Invoke reward QuestEffects on the player completing the Quest.
[2662]127        return true;
[2068]128    }
[2092]129
[1996]130    /**
131    @brief
[2261]132        Checks whether the Quest can be started.
[1996]133    @param player
134        The player for whom is to be checked.
135    @return
136        Returns true if the quest can be started, false if not.
[2068]137    @throws
138        Throws an exception if either isInactive() of isActive() throws one.
[1996]139    */
[2261]140    bool GlobalQuest::isStartable(const PlayerInfo* player) const
[1996]141    {
[2261]142        if(!(this->getParentQuest() == NULL || this->getParentQuest()->isActive(player)))
143            return false;
[7456]144
[3280]145        return (this->isInactive(player) && !(this->status_ == QuestStatus::Completed || this->status_ == QuestStatus::Failed));
[1996]146    }
[2092]147
[1996]148    /**
149    @brief
[2261]150        Checks whether the Quest can be failed.
[1996]151    @param player
152        The player for whom is to be checked.
153    @return
[2261]154        Returns true if the Quest can be failed, false if not.
[2068]155    @throws
156        Throws an Exception if isActive() throws one.
[1996]157    */
[2261]158    bool GlobalQuest::isFailable(const PlayerInfo* player) const
[1996]159    {
160        return this->isActive(player);
[2068]161
[1996]162    }
[2092]163
[1996]164    /**
165    @brief
[2261]166        Checks whether the Quest can be completed.
[1996]167    @param player
168        The player for whom is to be checked.
169    @return
[2261]170        Returns true if the Quest can be completed, false if not.
[2068]171    @throws
172        Throws an Exception if isActive() throws one.
[1996]173    */
[2261]174    bool GlobalQuest::isCompletable(const PlayerInfo* player) const
[1996]175    {
176        return this->isActive(player);
177    }
[1992]178
179    /**
180    @brief
[2261]181        Returns the status of the Quest for a specific player.
[1992]182    @param player
183        The player.
[2068]184    @throws
185        Throws an Exception if player is NULL.
[1992]186    */
[3280]187    QuestStatus::Value GlobalQuest::getStatus(const PlayerInfo* player) const
[1992]188    {
[7456]189        //TODO: Replace with assert.
190        if(player == NULL) // We don't want NULL-Pointers!
[2261]191            ThrowException(Argument, "The input PlayerInfo* is NULL.");
[2092]192
[7456]193        // Find the player.
[2261]194        std::set<PlayerInfo*>::const_iterator it = this->players_.find((PlayerInfo*)(void*)player);
[7456]195        if (it != this->players_.end()) // If the player was found.
[2093]196            return this->status_;
[1992]197
[3280]198        return QuestStatus::Inactive;
[1992]199    }
[2092]200
[1992]201    /**
202    @brief
203        Sets the status for a specific player.
[2093]204        But be careful wit this one, the status will just be set without checking for its validity. You have to know what you're doing.
[1992]205    @param player
206        The player.
207    @param status
208        The status to be set.
[2068]209    @return
210        Returns false if player is NULL.
[1992]211    */
[3280]212    bool GlobalQuest::setStatus(PlayerInfo* player, const QuestStatus::Value & status)
[1992]213    {
[7456]214        //TODO: Replace with assert.
215        if(player == NULL) // We don't want NULL-Pointers!
[2068]216            return false;
[2092]217
[7456]218        // Find the player.
[2261]219        std::set<PlayerInfo*>::const_iterator it = this->players_.find(player);
[7456]220        if (it == this->players_.end()) // Player is not yet in the list.
221            this->players_.insert(player); // Add the player to the set.
[6417]222
[7456]223        this->status_ = status; // Set the status, which is global, remember...?
[2093]224        return true;
[1992]225    }
[6417]226
[2261]227    /**
228    @brief
229        Adds a reward QuestEffect to the list of reward QuestEffects.
230    @param effect
231        The QuestEffect to be added.
232    @return
233        Returns true if successful.
234    */
235    bool GlobalQuest::addRewardEffect(QuestEffect* effect)
236    {
[7456]237        //TODO: Replace with assert?
238        if(effect == NULL) // We don't want NULL-Pointers!
[2261]239        {
240            COUT(2) << "The reward effect to be added to quest {" << this->getId() << "} was NULL." << std::endl;
241            return false;
242        }
[1992]243
[7456]244        this->rewards_.push_back(effect); // Add the QuestEffect to the list.
[1996]245
[7163]246        COUT(4) << "Reward effect was added to Quest {" << this->getId() << "}." << std::endl;
[2261]247        return true;
248    }
[6417]249
[2261]250    /**
251    @brief
252        Returns the reward QuestEffect at the given index.
253    @param index
254        The index.
255    @return
256        Returns the QuestEffect at the given index.
257    */
258    const QuestEffect* GlobalQuest::getRewardEffects(unsigned int index) const
259    {
260        int i = index;
261        for (std::list<QuestEffect*>::const_iterator effect = this->rewards_.begin(); effect != this->rewards_.end(); ++effect)
262        {
263            if(i == 0)
264               return *effect;
[7456]265
[2261]266            i--;
267        }
268        return NULL;
269    }
270
[1992]271}
Note: See TracBrowser for help on using the repository browser.