Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai2/src/modules/questsystem/QuestManager.cc @ 8843

Last change on this file since 8843 was 8843, checked in by dafrick, 13 years ago

Fixing bug.

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