Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11426 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 7.1 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{
[9667]43    RegisterClass(GlobalQuest);
[1992]44
[2068]45    /**
46    @brief
[2261]47        Constructor. Registers the object.
[2068]48    */
[9667]49    GlobalQuest::GlobalQuest(Context* context) : Quest(context)
[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
[8858]73        orxout(verbose, context::quests) << "New GlobalQuest {" << this->getId() << "} created." << 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        {
[8858]89            orxout(verbose, context::quests) << "A non-completable quest was trying to be failed." << endl;
[2662]90            return false;
[2261]91        }
[6417]92
[2662]93        Quest::fail(player);
[6417]94
[7456]95        // Iterate through all players possessing this Quest.
[11071]96        for(PlayerInfo* questPlayer : players_)
97            QuestEffect::invokeEffects(questPlayer, 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        {
[8858]116            orxout(verbose, context::quests) << "A non-completable quest was trying to be completed." << endl;
[2662]117            return false;
[2261]118        }
[6417]119
[7456]120        // Iterate through all players possessing the Quest.
[11071]121        for(PlayerInfo* questPlayer : players_)
122            QuestEffect::invokeEffects(questPlayer, 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.
137    */
[2261]138    bool GlobalQuest::isStartable(const PlayerInfo* player) const
[1996]139    {
[11071]140        if(!(this->getParentQuest() == nullptr || this->getParentQuest()->isActive(player)))
[2261]141            return false;
[7456]142
[3280]143        return (this->isInactive(player) && !(this->status_ == QuestStatus::Completed || this->status_ == QuestStatus::Failed));
[1996]144    }
[2092]145
[1996]146    /**
147    @brief
[2261]148        Checks whether the Quest can be failed.
[1996]149    @param player
150        The player for whom is to be checked.
151    @return
[2261]152        Returns true if the Quest can be failed, false if not.
[1996]153    */
[2261]154    bool GlobalQuest::isFailable(const PlayerInfo* player) const
[1996]155    {
156        return this->isActive(player);
[2068]157
[1996]158    }
[2092]159
[1996]160    /**
161    @brief
[2261]162        Checks whether the Quest can be completed.
[1996]163    @param player
164        The player for whom is to be checked.
165    @return
[2261]166        Returns true if the Quest can be completed, false if not.
[1996]167    */
[2261]168    bool GlobalQuest::isCompletable(const PlayerInfo* player) const
[1996]169    {
170        return this->isActive(player);
171    }
[1992]172
173    /**
174    @brief
[2261]175        Returns the status of the Quest for a specific player.
[1992]176    @param player
177        The player.
178    */
[11071]179    QuestStatus GlobalQuest::getStatus(const PlayerInfo* player) const
[1992]180    {
[7552]181        assert(player);
[2092]182
[7456]183        // Find the player.
[2261]184        std::set<PlayerInfo*>::const_iterator it = this->players_.find((PlayerInfo*)(void*)player);
[7456]185        if (it != this->players_.end()) // If the player was found.
[2093]186            return this->status_;
[1992]187
[3280]188        return QuestStatus::Inactive;
[1992]189    }
[2092]190
[1992]191    /**
192    @brief
193        Sets the status for a specific player.
[2093]194        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]195    @param player
196        The player.
197    @param status
198        The status to be set.
[2068]199    @return
[11071]200        Returns false if player is nullptr.
[1992]201    */
[11071]202    bool GlobalQuest::setStatus(PlayerInfo* player, const QuestStatus & status)
[1992]203    {
[7552]204        assert(player);
[2092]205
[7456]206        // Find the player.
[2261]207        std::set<PlayerInfo*>::const_iterator it = this->players_.find(player);
[7456]208        if (it == this->players_.end()) // Player is not yet in the list.
209            this->players_.insert(player); // Add the player to the set.
[6417]210
[7456]211        this->status_ = status; // Set the status, which is global, remember...?
[2093]212        return true;
[1992]213    }
[6417]214
[2261]215    /**
216    @brief
217        Adds a reward QuestEffect to the list of reward QuestEffects.
218    @param effect
219        The QuestEffect to be added.
220    @return
221        Returns true if successful.
222    */
223    bool GlobalQuest::addRewardEffect(QuestEffect* effect)
224    {
[7552]225        assert(effect);
[1992]226
[7456]227        this->rewards_.push_back(effect); // Add the QuestEffect to the list.
[1996]228
[8858]229        orxout(verbose, context::quests) << "Reward effect was added to Quest {" << this->getId() << "}." << endl;
[2261]230        return true;
231    }
[6417]232
[2261]233    /**
234    @brief
235        Returns the reward QuestEffect at the given index.
236    @param index
237        The index.
238    @return
239        Returns the QuestEffect at the given index.
240    */
241    const QuestEffect* GlobalQuest::getRewardEffects(unsigned int index) const
242    {
243        int i = index;
[11071]244        for (QuestEffect* reward : this->rewards_)
[2261]245        {
246            if(i == 0)
[11071]247               return reward;
[7456]248
[2261]249            i--;
250        }
[11071]251        return nullptr;
[2261]252    }
253
[1992]254}
Note: See TracBrowser for help on using the repository browser.