Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/Quest.cc @ 7456

Last change on this file since 7456 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: 13.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 Quest.cc
31    @brief Implementation of the Quest class.
32*/
33
34#include "Quest.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39#include "QuestDescription.h"
40#include "QuestEffect.h"
41#include "QuestHint.h"
42#include "QuestListener.h"
43#include "QuestManager.h"
44
45namespace orxonox
46{
47    /**
48    @brief
49        Constructor. Registers and initializes object.
50    */
51    Quest::Quest(BaseObject* creator) : QuestItem(creator)
52    {
53        RegisterObject(Quest);
54
55        this->parentQuest_ = NULL;
56    }
57
58    /**
59    @brief
60        Destructor.
61    */
62    Quest::~Quest()
63    {
64        if(this->isRegistered())
65            QuestManager::getInstance().unregisterQuest(this);
66    }
67
68    /**
69    @brief
70        Method for creating a Quest object through XML.
71    */
72    void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(Quest, XMLPort, xmlelement, mode);
75
76        XMLPortObject(Quest, Quest, "subquests", addSubQuest, getSubQuest, xmlelement, mode);
77        XMLPortObject(Quest, QuestHint, "hints", addHint, getHint, xmlelement, mode);
78        XMLPortObject(Quest, QuestEffect, "fail-effects", addFailEffect, getFailEffect, xmlelement, mode);
79        XMLPortObject(Quest, QuestEffect, "complete-effects", addCompleteEffect, getCompleteEffect, xmlelement, mode);
80
81        QuestManager::getInstance().registerQuest(this); // Registers the Quest with the QuestManager.
82    }
83
84    /**
85    @brief
86        Sets the parent-quest of the Quest.
87    @param quest
88        A pointer to the Quest to be set as parent-quest.
89    @return
90        Returns true if the parent-quest could be set.
91    */
92    bool Quest::setParentQuest(Quest* quest)
93    {
94        //TODO: Replace with assert.
95        if(quest == NULL) // We don't want to set NULL-Pointers.
96        {
97            COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
98            return false;
99        }
100
101        this->parentQuest_ = quest;
102
103        COUT(4) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
104        return true;
105    }
106
107    /**
108    @brief
109        Adds a sub-quest to the Quest.
110    @param quest
111        A pointer to the Quest to be set as sub-quest.
112    @return
113        Returns true if the sub-quest could be set.
114    */
115    bool Quest::addSubQuest(Quest* quest)
116    {
117        //TODO: Replace with assert.
118        if(quest == NULL) // We don't want to set NULL-Pointers.
119        {
120            COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
121            return false;
122        }
123
124        quest->setParentQuest(this); // Sets the currentQuest (this) as parent-quest for the added sub-quest.
125        this->subQuests_.push_back(quest); // Adds the Quest to the end of the list of sub-quests.
126
127        COUT(4) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
128        return true;
129    }
130
131
132    /**
133    @brief
134        Adds a QuestHint to the list of QuestHints
135    @param hint
136        The QuestHint that should be added to the list of QuestHints.
137    @return
138        Returns true if the hint was successfully added.
139    */
140    bool Quest::addHint(QuestHint* hint)
141    {
142        //TODO: Replace with assert.
143        if(hint == NULL) // We don't want to set NULL-Pointers. Seriously!
144        {
145            COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl;
146            return false;
147        }
148
149        hint->setQuest(this); // Sets the current Quest (this) as Quest for the added QuestHint.
150        this->hints_.push_back(hint); // Adds the QuestHint to the end of the list of QuestHints.
151
152        COUT(4) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
153        return true;
154    }
155
156    /**
157    @brief
158        Adds an QuestEffect to the list of fail QuestEffects.
159    @param effect
160        The QuestEffect to be added.
161    @return
162        Returns true if successful.
163    */
164    bool Quest::addFailEffect(QuestEffect* effect)
165    {
166        //TODO: Replace with assert.
167        if(effect == NULL) // We don't want to set NULL-Pointers.
168        {
169            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
170            return false;
171        }
172
173        this->failEffects_.push_back(effect); // Adds the QuestEffect to the end of the list of fail QuestEffects.
174
175        COUT(4) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl;
176        return true;
177    }
178
179    /**
180    @brief
181        Adds an QuestEffect to the list of complete QuestEffects.
182    @param effect
183        The QuestEffect to be added.
184    @return
185        Returns true if successful.
186    */
187    bool Quest::addCompleteEffect(QuestEffect* effect)
188    {
189        //TODO: Replace with assert.
190        if(effect == NULL) // We don't want to set NULL-Pointers.
191        {
192            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
193            return false;
194        }
195
196        this->completeEffects_.push_back(effect); // Adds the QuestEffect to the end of the list of complete QuestEffects.
197
198        COUT(4) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl;
199        return true;
200    }
201
202    /**
203    @brief
204        Returns the sub-quest at the given index.
205    @param index
206        The index.
207    @return
208        Returns a pointer to the sub-quest at the given index. NULL if there is no element at the given index.
209    */
210    const Quest* Quest::getSubQuest(unsigned int index) const
211    {
212        int i = index;
213
214        // Iterate through all subquests.
215        for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest)
216        {
217            if(i == 0) // We're counting down...
218               return *subQuest;
219
220            i--;
221        }
222
223        return NULL; // If the index is greater than the number of elements in the list.
224    }
225
226    /**
227    @brief
228        Returns the QuestHint at the given index.
229    @param index
230        The index.
231    @return
232        Returns a pointer to the QuestHint at the given index. NULL if there is no element at the given index.
233    */
234    const QuestHint* Quest::getHint(unsigned int index) const
235    {
236        int i = index;
237
238        // Iterate through all QuestHints.
239        for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint)
240        {
241            if(i == 0) // We're counting down...
242               return *hint;
243
244            i--;
245        }
246        return NULL; // If the index is greater than the number of elements in the list.
247    }
248
249    /**
250    @brief
251        Returns the fail QuestEffect at the given index.
252    @param index
253        The index.
254    @return
255        Returns a pointer to the fail QuestEffect at the given index. NULL if there is no element at the given index.
256    */
257    const QuestEffect* Quest::getFailEffect(unsigned int index) const
258    {
259        int i = index;
260
261        // Iterate through all fail QuestEffects.
262        for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect)
263        {
264            if(i == 0) // We're counting down...
265               return *effect;
266
267            i--;
268        }
269        return NULL; // If the index is greater than the number of elements in the list.
270    }
271
272    /**
273    @brief
274        Returns the complete QuestEffect at the given index.
275    @param index
276        The index.
277    @return
278        Returns a pointer to the complete QuestEffect at the given index. NULL if there is no element at the given index.
279    */
280    const QuestEffect* Quest::getCompleteEffect(unsigned int index) const
281    {
282        int i = index;
283
284        // Iterate through all complete QuestEffects.
285        for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect)
286        {
287            if(i == 0) // We're counting down...
288               return *effect;
289
290            i--;
291        }
292        return NULL; // If the index is greater than the number of elements in the list.
293    }
294
295    /**
296    @brief
297        Returns true if the quest status for the specific player is 'inactive'.
298    @param player
299        The player.
300    @return
301        Returns true if the quest status for the specific player is 'inactive'.
302    @throws
303        Throws an exception if getStatus throws one.
304    */
305    bool Quest::isInactive(const PlayerInfo* player) const
306    {
307        return this->getStatus(player) == QuestStatus::Inactive;
308    }
309
310    /**
311    @brief
312        Returns true if the quest status for the specific player is 'active'.
313    @param player
314        The player.
315    @return
316        Returns true if the quest status for the specific player is 'active'.
317    @throws
318        Throws an exception if getStatus throws one.
319    */
320    bool Quest::isActive(const PlayerInfo* player) const
321    {
322        return this->getStatus(player) == QuestStatus::Active;
323    }
324
325    /**
326    @brief
327        Returns true if the quest status for the specific player is 'failed'.
328    @param player
329        The player.
330    @return
331        Returns true if the quest status for the specific player is 'failed'.
332    @throws
333        Throws an exception if getStatus throws one.
334    */
335    bool Quest::isFailed(const PlayerInfo* player) const
336    {
337        return this->getStatus(player) == QuestStatus::Failed;
338    }
339
340    /**
341    @brief
342        Returns true if the quest status for the specific player is 'completed'.
343    @param player
344        The player.
345    @return
346        Returns true if the quest status for the specific player is 'completed'.
347    @throws
348        Throws an exception if getStatus throws one.
349    */
350    bool Quest::isCompleted(const PlayerInfo* player) const
351    {
352        return this->getStatus(player) == QuestStatus::Completed;
353    }
354
355    /**
356    @brief
357        Fails the Quest for an input player.
358    @param player
359        The player.
360    @return
361        Returns true if the Quest could be failed, false if not.
362    */
363    bool Quest::fail(PlayerInfo* player)
364    {
365        QuestListener::advertiseStatusChange(this->listeners_, "fail"); // Tells the QuestListeners, that the status has changed to failed.
366        this->setStatus(player, QuestStatus::Failed);
367
368        COUT(4) << "Quest {" << this->getId() << "} is failed for player: " << player << " ." <<std::endl;
369
370        this->getDescription()->sendFailQuestNotification(player);
371        return true;
372    }
373
374    /**
375    @brief
376        Completes the Quest for an input player.
377    @param player
378        The player.
379    @return
380        Returns true if the Quest could be completed, false if not.
381    */
382    bool Quest::complete(PlayerInfo* player)
383    {
384        QuestListener::advertiseStatusChange(this->listeners_, "complete"); // Tells the QuestListeners, that the status has changed to completed.
385        this->setStatus(player, QuestStatus::Completed);
386
387        COUT(4) << "Quest {" << this->getId() << "} is completed for player: " << player << " ." <<std::endl;
388
389        this->getDescription()->sendCompleteQuestNotification(player);
390        return true;
391    }
392
393    /**
394    @brief
395        Starts the Quest for an input player.
396    @param player
397        The player.
398    @return
399        Returns true if the Quest could be started, false if not.
400    */
401    bool Quest::start(PlayerInfo* player)
402    {
403        if(!this->isStartable(player)) // Checks whether the quest can be started.
404        {
405            COUT(4) << "A non-startable quest was trying to be started." << std::endl;
406            return false;
407        }
408
409        COUT(4) << "Quest {" << this->getId() << "} is started for player: " << player << " ." <<std::endl;
410
411        QuestListener::advertiseStatusChange(this->listeners_, "start"); // Tells the QuestListeners, that the status has changed to active.
412
413        this->setStatus(player, QuestStatus::Active);
414
415        this->getDescription()->sendAddQuestNotification(player);
416        return true;
417    }
418
419    /**
420    @brief
421        Adds a QuestListener to the list of QuestListeners listening to this Quest.
422    @param listener
423        The QuestListener to be added.
424    @return
425        Returns true if successful, false if not.
426    */
427    bool Quest::addListener(QuestListener* listener)
428    {
429        //TODO: Replace with assert?
430        if(listener == NULL)
431        {
432            COUT(2) << "A NULL-QuestListener was trying to be added to a Quests listeners." << std::endl;
433            return false;
434        }
435
436        this->listeners_.push_back(listener);
437        return true;
438    }
439
440}
Note: See TracBrowser for help on using the repository browser.