Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/GlobalQuest.cc @ 3132

Last change on this file since 3132 was 3110, checked in by rgrieder, 16 years ago

Removed old msvc specific support for precompiled header files.

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