Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/masterserver/src/modules/questsystem/GlobalQuest.cc @ 7559

Last change on this file since 7559 was 7456, checked in by dafrick, 14 years ago

Reviewing documentation fo Questsystem, moving documentation fully into doxygen.
Added some files to modules they belong to.

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