Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem3/src/orxonox/objects/quest/GlobalQuest.cc @ 2328

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