Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Started implementation of QuestEffectBeacon.
Done some documentation of QuestItem and subclasses.

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