Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/quest/Quest.cc @ 3008

Last change on this file since 3008 was 2963, checked in by dafrick, 16 years ago

Some Quest stuff, and the rest for the GUIOverlay I failed to commit before.

File size: 13.3 KB
RevLine 
[1992]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 */
[2261]28 
29/**
[2911]30    @file Quest.cc
[2662]31    @brief Implementation of the Quest class.
[2261]32*/
[1992]33
[2105]34#include "OrxonoxStableHeaders.h"
35#include "Quest.h"
36
[1992]37#include "core/CoreIncludes.h"
[2021]38
[2261]39#include "orxonox/objects/infos/PlayerInfo.h"
[2068]40#include "QuestManager.h"
[2095]41#include "QuestDescription.h"
42#include "QuestHint.h"
43#include "QuestEffect.h"
[2662]44#include "QuestListener.h"
[1992]45
[2662]46namespace orxonox
47{
[2261]48    /**
49    @brief
50        Constructor. Registers and initializes object.
51    */
[2092]52    Quest::Quest(BaseObject* creator) : QuestItem(creator)
[2021]53    {
[2092]54        RegisterObject(Quest);
55
[2261]56        this->parentQuest_ = NULL;
[2021]57    }
[2092]58
[1992]59    /**
60    @brief
61        Destructor.
62    */
63    Quest::~Quest()
64    {
[2092]65
[1992]66    }
[2092]67
[2261]68    /**
69    @brief
70        Method for creating a Quest object through XML.
71    */
[2076]72    void Quest::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(Quest, XMLPort, xmlelement, mode);
[2092]75
[2261]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);
[2092]80
[2911]81        QuestManager::getInstance().registerQuest(this); //!<Registers the Quest with the QuestManager.
[2076]82    }
[2092]83
[1992]84    /**
85    @brief
[2261]86        Sets the parentquest of the Quest.
[1996]87    @param quest
[2261]88        A pointer to the Quest to be set as parentquest.
[2068]89    @return
[2261]90        Returns true if the parentquest could be set.
[1996]91    */
[2021]92    bool Quest::setParentQuest(Quest* quest)
[1996]93    {
[2261]94        if(quest == NULL) //!< We don't want to set NULL-Pointers.
[2068]95        {
96            COUT(2) << "The parentquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
97            return false;
98        }
[2092]99
[1996]100        this->parentQuest_ = quest;
[2092]101
[2081]102        COUT(3) << "Parent Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
[1996]103        return true;
104    }
[2092]105
[1996]106    /**
107    @brief
[2261]108        Adds a subquest to the Quest.
[1996]109    @param quest
[2261]110        A pointer to the Quest to be set as subquest.
[2068]111    @return
[2261]112        Returns true if the subquest could be set.
[1996]113    */
[2021]114    bool Quest::addSubQuest(Quest* quest)
[1996]115    {
[2261]116        if(quest == NULL) //!< We don't want to set NULL-Pointers.
[2068]117        {
118            COUT(2) << "The subquest to be added to quest {" << this->getId() << "} was NULL." << std::endl;
119            return false;
120        }
[2092]121
[2261]122        quest->setParentQuest(this); //!< Sets the currentQuest (this) as parentquest for the added subquest.
123        this->subQuests_.push_back(quest); //!< Adds the Quest to the end of the list of subquests.
[2092]124
[2081]125        COUT(3) << "Sub Quest {" << quest->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
[1996]126        return true;
127    }
[2092]128
129
[2068]130    /**
131    @brief
[2261]132        Adds a QuestHint to the list of QuestHints
[2076]133    @param hint
[2261]134        The QuestHint that should be added to the list of QuestHints.
[2076]135    @return
136        Returns true if the hint was successfully added.
137    */
138    bool Quest::addHint(QuestHint* hint)
139    {
[2261]140        if(hint == NULL) //!< We don't want to set NULL-Pointers. Seriously!
[2076]141        {
142            COUT(2) << "A NULL-QuestHint was trying to be added." << std::endl;
143            return false;
144        }
[2092]145
[2261]146        hint->setQuest(this); //!< Sets the current Quest (this) as Quest for the added QuestHint.
147        this->hints_.push_back(hint); //!< Adds the QuestHint to the end of the list of QuestHints.
[2092]148
[2093]149        COUT(3) << "QuestHint {" << hint->getId() << "} was added to Quest {" << this->getId() << "}." << std::endl;
150        return true;
[2076]151    }
[2092]152
[2076]153    /**
154    @brief
[2261]155        Adds an QuestEffect to the list of fail QuestEffects.
156    @param effect
157        The QuestEffect to be added.
158    @return
159        Returns true if successful.
[2076]160    */
161    bool Quest::addFailEffect(QuestEffect* effect)
162    {
[2261]163        if(effect == NULL) //!< We don't want to set NULL-Pointers.
[2076]164        {
165            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
166            return false;
167        }
[2092]168
[2261]169        this->failEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of fail QuestEffects.
[2092]170
[2081]171        COUT(3) << "A FailEffect was added to Quest {" << this->getId() << "}." << std::endl;
[2076]172        return true;
173    }
[2092]174
[2076]175    /**
176    @brief
[2261]177        Adds an QuestEffect to the list of complete QuestEffects.
178    @param effect
179        The QuestEffect to be added.
180    @return
181        Returns true if successful.
[2076]182    */
183    bool Quest::addCompleteEffect(QuestEffect* effect)
184    {
[2261]185        if(effect == NULL) //!< We don't want to set NULL-Pointers.
[2076]186        {
187            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
188            return false;
189        }
[2092]190
[2261]191        this->completeEffects_.push_back(effect); //!< Adds the QuestEffect to the end of the list of complete QuestEffects.
[2092]192
[2081]193        COUT(3) << "A CompleteEffect was added to Quest {" << this->getId() << "}." << std::endl;
[2076]194        return true;
195    }
[2092]196
[2076]197    /**
198    @brief
[2261]199        Returns the subquest at the given index.
200    @param
201        The index.
202    @return
203        Returns a pointer to the subquest at the given index. NULL if there is no element at the given index.
[2076]204    */
[2261]205    const Quest* Quest::getSubQuest(unsigned int index) const
[2076]206    {
207        int i = index;
[2261]208       
209        //! Iterate through all subquests.
[2076]210        for (std::list<Quest*>::const_iterator subQuest = this->subQuests_.begin(); subQuest != this->subQuests_.end(); ++subQuest)
[2093]211        {
[2261]212            if(i == 0) //!< We're counting down...
[2093]213            {
214               return *subQuest;
215            }
216            i--;
217        }
[2261]218       
219        return NULL; //!< If the index is greater than the number of elements in the list.
[2076]220    }
[2092]221
[2076]222    /**
223    @brief
[2261]224        Returns the QuestHint at the given index.
225    @param
226        The index.
227    @return
228        Returns a pointer to the QuestHint at the given index. NULL if there is no element at the given index.
[2076]229    */
[2261]230    const QuestHint* Quest::getHint(unsigned int index) const
[2076]231    {
232        int i = index;
[2261]233       
234        //! Iterate through all QuestHints.
[2076]235        for (std::list<QuestHint*>::const_iterator hint = this->hints_.begin(); hint != this->hints_.end(); ++hint)
[2093]236        {
[2261]237            if(i == 0) //!< We're counting down...
[2093]238            {
239               return *hint;
240            }
241            i--;
242        }
[2261]243        return NULL; //!< If the index is greater than the number of elements in the list.
[2076]244    }
[2092]245
[2076]246    /**
247    @brief
[2261]248        Returns the fail QuestEffect at the given index.
249    @param
250        The index.
251    @return
252        Returns a pointer to the fail QuestEffect at the given index. NULL if there is no element at the given index.
[2076]253    */
[2261]254    const QuestEffect* Quest::getFailEffect(unsigned int index) const
[2076]255    {
256        int i = index;
[2261]257       
258        //! Iterate through all fail QuestEffects.
[2076]259        for (std::list<QuestEffect*>::const_iterator effect = this->failEffects_.begin(); effect != this->failEffects_.end(); ++effect)
[2093]260        {
[2261]261            if(i == 0) //!< We're counting down...
[2093]262            {
263               return *effect;
264            }
265            i--;
266        }
[2261]267        return NULL; //!< If the index is greater than the number of elements in the list.
[2076]268    }
[2092]269
[2076]270    /**
271    @brief
[2261]272        Returns the complete QuestEffect at the given index.
273    @param
274        The index.
275    @return
276        Returns a pointer to the complete QuestEffect at the given index. NULL if there is no element at the given index.
[2076]277    */
[2261]278    const QuestEffect* Quest::getCompleteEffect(unsigned int index) const
[2076]279    {
280        int i = index;
[2261]281       
282        //! Iterate through all complete QuestEffects.
[2076]283        for (std::list<QuestEffect*>::const_iterator effect = this->completeEffects_.begin(); effect != this->completeEffects_.end(); ++effect)
[2093]284        {
[2261]285            if(i == 0) //!< We're counting down...
[2093]286            {
287               return *effect;
288            }
289            i--;
290        }
[2261]291        return NULL; //!< If the index is greater than the number of elements in the list.
[2076]292    }
[2092]293
[2076]294    /**
295    @brief
[2068]296        Returns true if the quest status for the specific player is 'inactive'.
297    @param player
298        The player.
299    @return
300        Returns true if the quest status for the specific player is 'inactive'.
301    @throws
302        Throws an exception if getStatus throws one.
303    */
[2261]304    bool Quest::isInactive(const PlayerInfo* player) const
[2068]305    {
306        return this->getStatus(player) == questStatus::inactive;
307    }
[2092]308
[2068]309    /**
310    @brief
311        Returns true if the quest status for the specific player is 'active'.
312    @param player
313        The player.
314    @return
315        Returns true if the quest status for the specific player is 'active'.
316    @throws
317        Throws an exception if getStatus throws one.
318    */
[2261]319    bool Quest::isActive(const PlayerInfo* player) const
[2068]320    {
[1996]321
[2068]322        return this->getStatus(player) == questStatus::active;
323    }
[2092]324
[1996]325    /**
326    @brief
[2068]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    */
[2261]335    bool Quest::isFailed(const PlayerInfo* player) const
[2068]336    {
337        return this->getStatus(player) == questStatus::failed;
338    }
[2092]339
[2068]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    */
[2261]350    bool Quest::isCompleted(const PlayerInfo* player) const
[2068]351    {
352        return this->getStatus(player) == questStatus::completed;
353    }
[2662]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();
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();
390        return true;
391    }
[2092]392
[1992]393    /**
394    @brief
[2261]395        Starts the Quest for an input player.
[1992]396    @param player
397        The player.
[1996]398    @return
[2261]399        Returns true if the Quest could be started, false if not.
[1992]400    */
[2261]401    bool Quest::start(PlayerInfo* player)
[1992]402    {
[2662]403        if(!this->isStartable(player)) //!< Checks whether the quest can be started.
[1992]404        {
[2662]405            COUT(4) << "A non-startable quest was trying to be started." << std::endl;
406            return false;
[1992]407        }
[2261]408       
[2662]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();
416        return true;
[1992]417    }
[2662]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        if(listener == NULL)
430        {
431            COUT(2) << "A NULL-QuestListener was trying to be added to a Quests listeners." << std::endl;
432            return false;
433        }
434       
435        this->listeners_.push_back(listener);
436        return true;
437    }
[2092]438
[1992]439}
Note: See TracBrowser for help on using the repository browser.