Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/questsystem/QuestManager.cc @ 12162

Last change on this file since 12162 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 12.9 KB
RevLine 
[1996]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
[2261]29/**
[7456]30    @file QuestManager.cc
[2662]31    @brief Implementation of the QuestManager class.
[2261]32*/
33
[2105]34#include "QuestManager.h"
35
[3196]36#include "util/Exception.h"
[8891]37#include "util/OrxAssert.h"
[10624]38#include "core/singleton/ScopedSingletonIncludes.h"
[7456]39#include "core/command/ConsoleCommand.h"
[5693]40#include "core/GUIManager.h"
[5755]41#include "core/LuaState.h"
[7456]42
[5748]43#include "infos/PlayerInfo.h"
[2105]44
[2095]45#include "Quest.h"
46#include "QuestHint.h"
[5748]47#include "QuestItem.h"
[1996]48
[2662]49namespace orxonox
50{
[10624]51    ManageScopedSingleton(QuestManager, ScopeID::ROOT, false);
[1996]52
[2261]53    /**
54    @brief
55        Constructor. Registers the object.
[2911]56    @todo
57        Is inheriting from BaseObject proper?
[2261]58    */
[2911]59    QuestManager::QuestManager()
[1996]60    {
[8858]61        orxout(internal_info, context::quests) << "QuestManager created." << endl;
[1996]62    }
[2092]63
[2261]64    /**
65    @brief
66        Destructor.
67    */
[1996]68    QuestManager::~QuestManager()
69    {
[8858]70        orxout(internal_info, context::quests) << "QuestManager destroyed." << endl;
[1996]71    }
72
73    /**
74    @brief
[5745]75        Retreive all Quests.
76    @return
77        Returns a map with all Quests indexed by their id's.
78    */
79    std::map<std::string, Quest*> & QuestManager::getQuests(void)
80    {
81        return this->questMap_;
82    }
83
84    /**
85    @brief
[8079]86        Registers a Quest with the QuestManager to make it globally accessible.
[2261]87        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]88    @param quest
[2261]89        The Quest that is to be registered.
[1996]90    @return
91        Returns true if successful, false if not.
92    */
[2911]93    bool QuestManager::registerQuest(Quest* quest)
[1996]94    {
[11071]95        if(quest == nullptr)
[8891]96        {
[11071]97            orxout(internal_error, context::quests) << "Quest pointer is nullptr." << endl;
[8891]98            return false;
99        }
[2092]100
[2261]101        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[7456]102        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); // Inserting the Quest.
[2092]103
[7456]104        if(result.second) // If inserting was a success.
[2068]105        {
[7163]106            quest->setRegistered();
[8858]107            orxout(verbose, context::quests) << "Quest with questId {" << quest->getId() << "} successfully inserted." << endl;
[2068]108            return true;
[2093]109        }
110        else
111        {
[8858]112           orxout(internal_warning, context::quests) << "Quest with the same id was already present." << endl;
[2093]113           return false;
114        }
[1996]115    }
[2092]116
[1996]117    /**
118    @brief
[7163]119        Unregisters a Quest in the QuestManager.
120    */
121    bool QuestManager::unregisterQuest(Quest* quest)
122    {
123        return this->questMap_.erase(quest->getId()) == 1;
124    }
125
126    /**
127    @brief
[8079]128        Registers a QuestHint with the QuestManager to make it globally accessible.
[2261]129        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]130    @param hint
131        The QuestHint to be registered.
132    @return
133        Returns true if successful, false if not.
134    */
[2911]135    bool QuestManager::registerHint(QuestHint* hint)
[1996]136    {
[11071]137        if(hint == nullptr)
[8891]138        {
[11071]139            orxout(internal_error, context::quests) << "Quest pointer is nullptr." << endl;
[8891]140            return false;
141        }
[2092]142
[2261]143        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[7456]144        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); // Inserting the QuestHSint.
[2092]145
[7456]146        if(result.second) // If inserting was a success.
[2068]147        {
[7163]148            hint->setRegistered();
[8858]149            orxout(verbose, context::quests) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << endl;
[2068]150            return true;
[2093]151        }
152        else
153        {
[8858]154           orxout(internal_warning, context::quests) << "QuestHint with the same id was already present." << endl;
[2093]155           return false;
156        }
[1996]157    }
[2092]158
[1996]159    /**
160    @brief
[7163]161        Unregisters a QuestHint in the QuestManager.
162    */
163    bool QuestManager::unregisterHint(QuestHint* hint)
164    {
165        return this->hintMap_.erase(hint->getId()) == 1;
166    }
167
168    /**
169    @brief
[2261]170        Finds a Quest with the given id.
[1996]171    @param questId
[2261]172        The id of the Quest sought for.
[1996]173    @return
[2261]174        Returns a pointer to the Quest with the input id.
[11071]175        Returns nullptr if there is no Quest with the given questId.
[2068]176    @throws
177        Throws an exception if the given questId is invalid.
[1996]178    */
[2911]179    Quest* QuestManager::findQuest(const std::string & questId)
[1996]180    {
[8079]181        if(questId == "") // Check for validity of the given id.
[2068]182            ThrowException(Argument, "Invalid questId.");
[2092]183
[1996]184        Quest* quest;
[2911]185        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
[7456]186        if (it != this->questMap_.end()) // If the Quest is registered.
[2093]187            quest = it->second;
188        else
189        {
[11071]190           quest = nullptr;
[8858]191           orxout(internal_warning, context::quests) << "The quest with id {" << questId << "} is nowhere to be found." << endl;
[2093]192        }
[2092]193
[2093]194        return quest;
[1996]195    }
[2092]196
[1996]197    /**
198    @brief
[2261]199        Finds a QuestHint with the given id.
[1996]200    @param hintId
[2261]201        The id of the QuestHint sought for.
[1996]202    @return
[2261]203        Returns a pointer to the QuestHint with the input id.
[11071]204        Returns nullptr if there is no QuestHint with the given hintId.
[2068]205    @throws
206        Throws an exception if the given hintId is invalid.
[1996]207    */
[2911]208    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]209    {
[8079]210        if(hintId == "") // Check for validity of the given id.
[2068]211            ThrowException(Argument, "Invalid hintId.");
[2092]212
[1996]213        QuestHint* hint;
[2911]214        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
[7456]215        if (it != this->hintMap_.end()) // If the QuestHint is registered.
[2093]216            hint = it->second;
217        else
218        {
[11071]219           hint = nullptr;
[8858]220           orxout(internal_warning, context::quests) << "The hint with id {" << hintId << "} is nowhere to be found." << endl;
[2093]221        }
[2092]222
[2093]223        return hint;
[1996]224    }
225
[7456]226    /**
227    @brief
228        Get the number of Quests the input player has, that are root quests.
229    @param player
230        The player.
231    @return
232        Returns the number of Quests the input player has, that are root quests.
233    */
234    int QuestManager::getNumRootQuests(PlayerInfo* player)
[2963]235    {
[7163]236        int numQuests = 0;
[11071]237        for(const auto& mapEntry : this->questMap_)
[7163]238        {
[11071]239            if(mapEntry.second->getParentQuest() == nullptr && !mapEntry.second->isInactive(player))
[7163]240                numQuests++;
241        }
242        return numQuests;
243    }
[5693]244
[7456]245    /**
246    @brief
247        Get the index-th root quest of the input player.
248    @param player
249        The player.
250    @param index
251        The index of the root quest.
252    @return
253        Returns the index-th root quest of the input player.
254    */
255    Quest* QuestManager::getRootQuest(PlayerInfo* player, int index)
[7163]256    {
[11071]257        for(const auto& mapEntry : this->questMap_)
[7163]258        {
[11071]259            if(mapEntry.second->getParentQuest() == nullptr && !mapEntry.second->isInactive(player) && index-- == 0)
260                return mapEntry.second;
[7163]261        }
[11071]262        return nullptr;
[7163]263    }
[5693]264
[7456]265    /**
266    @brief
267        Get the number of sub-quest of an input Quest for the input player.
268    @param quest
269        The quest to get the sub-quests of.
270    @param player
271        The player.
272    @return
273        Returns the number of sub-quest of an input Quest for the input player.
274    */
[7163]275    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
276    {
[11071]277        if(quest == nullptr)
[8706]278            return this->getNumRootQuests(player);
279
[7163]280        std::list<Quest*> quests = quest->getSubQuestList();
281        int numQuests = 0;
[11071]282        for(Quest* subquest : quests)
[7163]283        {
[11071]284            if(!subquest->isInactive(player))
[7163]285                numQuests++;
286        }
287        return numQuests;
[2963]288    }
289
[7456]290    /**
291    @brief
292        Get the index-th sub-quest of the input Quest for the input player.
293    @param quest
294        The Quest to get the sub-quest of.
295    @param player
296        The player.
297    @param index
298        The index of the sub-quest.
299    */
[7163]300    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
301    {
[11071]302        if(quest == nullptr)
[8706]303            return this->getRootQuest(player, index);
304
[7163]305        std::list<Quest*> quests = quest->getSubQuestList();
[11071]306        for(Quest* subquest : quests)
[7163]307        {
[11071]308            if(!subquest->isInactive(player) && index-- == 0)
309                return quest;
[7163]310        }
[11071]311        return nullptr;
[7163]312    }
313
[7456]314    /**
315    @brief
316        Get the number of QuestHints of the input Quest for the input player.
317    @param quest
318        The quest to get the hints of.
319    @param player
320        The player.
[8706]321    @return
322        Returns the number of QuestHints of the input Quest for the input player.
[7456]323    */
[7163]324    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
325    {
326        std::list<QuestHint*> hints = quest->getHintsList();
327        int numHints = 0;
[11071]328        for(QuestHint* hint : hints)
[7163]329        {
[11071]330            if(hint->isActive(player))
[7163]331                numHints++;
332        }
333        return numHints;
334    }
335
[7456]336    /**
337    @brief
338        Get the index-th QuestHint of the input Quest for the input player.
339    @param quest
340        The Quest to get the QuestHint of.
341    @param player
342        The player.
343    @param index
344        The index of the QuestHint.
[8706]345    @return
346        Returns a pointer to the index-th QuestHint of the input Quest for the input player.
[7456]347    */
[7163]348    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
349    {
350        std::list<QuestHint*> hints = quest->getHintsList();
[11071]351        for(QuestHint* hint : hints)
[7163]352        {
[11071]353            if(hint->isActive(player) && index-- == 0)
354                return hint;
[7163]355        }
[11071]356        return nullptr;
[7163]357    }
358
[7456]359    /**
360    @brief
[8706]361        Get the parent-quest of the input Quest.
362    @param quest
363        The Quest to get the parent-quest of.
364    @return
365        Returns a pointer to the parent-quest of the input Quest.
366    */
367    Quest* QuestManager::getParentQuest(Quest* quest)
368    {
[11071]369        OrxAssert(quest, "The input Quest is nullptr.");
[8706]370        return quest->getParentQuest();
371    }
372
373    /**
374    @brief
[7456]375        Get the QuestDescription of the input Quest.
376    @param item
377        The Quest to get the QuestDescription of.
378    @return
379        Return a pointer ot the QuestDescription of the input Quest.
380    */
[7163]381    QuestDescription* QuestManager::getDescription(Quest* item)
382    {
[11071]383        OrxAssert(item, "The input Quest is nullptr.");
[7163]384        return item->getDescription();
385    }
386
[7456]387    /**
388    @brief
389        Get the QuestDescription of the input QuestHint.
390    @param item
391        The QuestHint to get the QuestDescription of.
392    @return
393        Returns a pointer to the QuestDescription of the input QuestHint.
394    */
[7163]395    QuestDescription* QuestManager::getDescription(QuestHint* item)
396    {
[11071]397        OrxAssert(item, "The input QuestHint is nullptr.");
[7163]398        return item->getDescription();
399    }
400
[2993]401    /**
402    @brief
[8706]403        Get the id of the input Quest.
404    @param item
405        The Quest to get the id of.
406    @return
407        Returns the id of the input Quest.
408    */
409    const std::string QuestManager::getId(Quest* item) const
410    {
[11071]411        OrxAssert(item, "The input Quest is nullptr.");
[8706]412        return item->getId();
413    }
414
415    /**
416    @brief
417        Get the id of the input QuestHint.
418    @param item
419        The QuestHint to get the id of.
420    @return
421        Returns the id of the input QuestHint.
422    */
423    const std::string QuestManager::getId(QuestHint* item) const
424    {
[11071]425        OrxAssert(item, "The input QuestHint is nullptr.");
[8706]426        return item->getId();
427    }
428
429    /**
430    @brief
[5745]431        Retrieve the player for a certain GUI.
432    @param guiName
433        The name of the GUI the player is retrieved for.
[2993]434    @return
[5745]435        Returns the player.
436    @todo
437        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]438    */
[6536]439    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]440    {
[5745]441        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
[11071]442        if(player == nullptr)
[2963]443        {
[8858]444            orxout(internal_error, context::quests) << "GUIOverlay with name '" << guiName << "' has no player." << endl;
[11071]445            return nullptr;
[2993]446        }
[5693]447
[5745]448        return player;
[2963]449    }
450
[1996]451}
Note: See TracBrowser for help on using the repository browser.