Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6372 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

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