Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7293 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 9.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/**
[3196]30    @file
[2662]31    @brief Implementation of the QuestManager class.
[2261]32*/
33
[2105]34#include "QuestManager.h"
35
[5748]36#include <CEGUIWindow.h>
37
[3196]38#include "util/Exception.h"
[7284]39#include "util/ScopedSingletonManager.h"
[1996]40#include "core/CoreIncludes.h"
[5693]41#include "core/GUIManager.h"
[5755]42#include "core/LuaState.h"
[7284]43#include "core/command/ConsoleCommand.h"
[5748]44#include "infos/PlayerInfo.h"
[5745]45#include "overlays/GUIOverlay.h"
[2105]46
[5755]47#include "ToluaBindQuestsystem.h"
[2095]48#include "Quest.h"
49#include "QuestHint.h"
[5748]50#include "QuestItem.h"
[1996]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);
[1996]68    }
[2092]69
[2261]70    /**
71    @brief
72        Destructor.
73    */
[1996]74    QuestManager::~QuestManager()
75    {
[7163]76
[1996]77    }
78
79    /**
80    @brief
[5745]81        Retreive all Quests.
82    @return
83        Returns a map with all Quests indexed by their id's.
84    */
85    std::map<std::string, Quest*> & QuestManager::getQuests(void)
86    {
87        return this->questMap_;
88    }
89
90    /**
91    @brief
[2261]92        Registers a Quest with the QuestManager to make it globally accessable.
93        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]94    @param quest
[2261]95        The Quest that is to be registered.
[1996]96    @return
97        Returns true if successful, false if not.
98    */
[2911]99    bool QuestManager::registerQuest(Quest* quest)
[1996]100    {
[2261]101        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
[2068]102        {
103            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
104            return false;
[2093]105        }
[2092]106
[2261]107        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[2911]108        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
[2092]109
[2261]110        if(result.second) //!< If inserting was a success.
[2068]111        {
[7163]112            quest->setRegistered();
113            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
[2068]114            return true;
[2093]115        }
116        else
117        {
118           COUT(2) << "Quest with the same id was already present." << std::endl;
119           return false;
120        }
[1996]121    }
[2092]122
[1996]123    /**
124    @brief
[7163]125        Unregisters a Quest in the QuestManager.
126    */
127    bool QuestManager::unregisterQuest(Quest* quest)
128    {
129        return this->questMap_.erase(quest->getId()) == 1;
130    }
131
132    /**
133    @brief
[1996]134        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]135        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]136    @param hint
137        The QuestHint to be registered.
138    @return
139        Returns true if successful, false if not.
140    */
[2911]141    bool QuestManager::registerHint(QuestHint* hint)
[1996]142    {
[2261]143        if(hint == NULL) //!< Still not liking NULL-pointers.
[2068]144        {
145            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
146            return false;
147        }
[2092]148
[2261]149        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[2911]150        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
[2092]151
[2261]152        if(result.second) //!< If inserting was a success.
[2068]153        {
[7163]154            hint->setRegistered();
155            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
[2068]156            return true;
[2093]157        }
158        else
159        {
160           COUT(2) << "QuestHint with the same id was already present." << std::endl;
161           return false;
162        }
[1996]163    }
[2092]164
[1996]165    /**
166    @brief
[7163]167        Unregisters a QuestHint in the QuestManager.
168    */
169    bool QuestManager::unregisterHint(QuestHint* hint)
170    {
171        return this->hintMap_.erase(hint->getId()) == 1;
172    }
173
174    /**
175    @brief
[2261]176        Finds a Quest with the given id.
[1996]177    @param questId
[2261]178        The id of the Quest sought for.
[1996]179    @return
[2261]180        Returns a pointer to the Quest with the input id.
181        Returns NULL if there is no Quest with the given questId.
[2068]182    @throws
183        Throws an exception if the given questId is invalid.
[1996]184    */
[2911]185    Quest* QuestManager::findQuest(const std::string & questId)
[1996]186    {
[7163]187        if(questId.compare(BLANKSTRING) == 1) //!< Check vor validity of the given id.
[2093]188        {
[2068]189            ThrowException(Argument, "Invalid questId.");
[2093]190        }
[2092]191
[1996]192        Quest* quest;
[2911]193        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
194        if (it != this->questMap_.end()) //!< If the Quest is registered.
[2093]195        {
196            quest = it->second;
197        }
198        else
199        {
200           quest = NULL;
201           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
202        }
[2092]203
[2093]204        return quest;
[1996]205
206    }
[2092]207
[1996]208    /**
209    @brief
[2261]210        Finds a QuestHint with the given id.
[1996]211    @param hintId
[2261]212        The id of the QuestHint sought for.
[1996]213    @return
[2261]214        Returns a pointer to the QuestHint with the input id.
215        Returns NULL if there is no QuestHint with the given hintId.
[2068]216    @throws
217        Throws an exception if the given hintId is invalid.
[1996]218    */
[2911]219    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]220    {
[7163]221        if(hintId.compare(BLANKSTRING) == 1) //!< Check vor validity of the given id.
[2093]222        {
[2068]223            ThrowException(Argument, "Invalid hintId.");
[2093]224        }
[2092]225
[1996]226        QuestHint* hint;
[2911]227        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
228        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
[2093]229        {
230            hint = it->second;
231        }
232        else
233        {
234           hint = NULL;
235           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
236        }
[2092]237
[2093]238        return hint;
[1996]239
240    }
241
[7163]242    int QuestManager::getNumParentQuests(PlayerInfo* player)
[2963]243    {
[7163]244        int numQuests = 0;
245        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
246        {
247            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player))
248                numQuests++;
249        }
250        return numQuests;
251    }
[5693]252
[7163]253    Quest* QuestManager::getParentQuest(PlayerInfo* player, int index)
254    {
255        for(std::map<std::string, Quest*>::iterator it = this->questMap_.begin(); it != this->questMap_.end(); it++)
256        {
257            if(it->second->getParentQuest() == NULL && !it->second->isInactive(player) && index-- == 0)
258                return it->second;
259        }
260        return NULL;
261    }
[5693]262
[7163]263    int QuestManager::getNumSubQuests(Quest* quest, PlayerInfo* player)
264    {
265        std::list<Quest*> quests = quest->getSubQuestList();
266        int numQuests = 0;
267        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
268        {
269            if(!(*it)->isInactive(player))
270                numQuests++;
271        }
272        return numQuests;
[2963]273    }
274
[7163]275    Quest* QuestManager::getSubQuest(Quest* quest, PlayerInfo* player, int index)
276    {
277        std::list<Quest*> quests = quest->getSubQuestList();
278        for(std::list<Quest*>::iterator it = quests.begin(); it != quests.end(); it++)
279        {
280            if(!(*it)->isInactive(player) && index-- == 0)
281                return *it;
282        }
283        return NULL;
284    }
285
286    int QuestManager::getNumHints(Quest* quest, PlayerInfo* player)
287    {
288        std::list<QuestHint*> hints = quest->getHintsList();
289        int numHints = 0;
290        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
291        {
292            if((*it)->isActive(player))
293                numHints++;
294        }
295        return numHints;
296    }
297
298    QuestHint* QuestManager::getHints(Quest* quest, PlayerInfo* player, int index)
299    {
300        std::list<QuestHint*> hints = quest->getHintsList();
301        for(std::list<QuestHint*>::iterator it = hints.begin(); it != hints.end(); it++)
302        {
303            if((*it)->isActive(player) && index-- == 0)
304                return *it;
305        }
306        return NULL;
307    }
308
309    QuestDescription* QuestManager::getDescription(Quest* item)
310    {
311        return item->getDescription();
312    }
313
314    QuestDescription* QuestManager::getDescription(QuestHint* item)
315    {
316        return item->getDescription();
317    }
318
[2993]319    /**
320    @brief
[5745]321        Retrieve the player for a certain GUI.
322    @param guiName
323        The name of the GUI the player is retrieved for.
[2993]324    @return
[5745]325        Returns the player.
326    @todo
327        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]328    */
[6536]329    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]330    {
[5745]331        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
332        if(player == NULL)
[2963]333        {
[5745]334            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
[2993]335            return NULL;
336        }
[5693]337
[5745]338        return player;
[2963]339    }
340
[1996]341}
Note: See TracBrowser for help on using the repository browser.