Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/quest/GlobalQuest.cc @ 2191

Last change on this file since 2191 was 2191, checked in by dafrick, 15 years ago

Just changed some small stuff…

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