Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/quest/GlobalQuest.cc @ 3149

Last change on this file since 3149 was 3149, checked in by rgrieder, 15 years ago

Extracted OrxAssert from Exception.h to OrxAssert.h since it doesn't really have anything to do with exceptions.

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