Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/modules/questsystem/QuestManager.cc @ 6957

Last change on this file since 6957 was 6945, checked in by dafrick, 15 years ago

A lot of cleanup, mostly increasing output levels, which means, that the console is no longer cluttered by lots and lots of Quest-stuff (the log file still is, but that should be ok…).
Also some possible bugs (or let's say pitfalls) removed, which have been around for a long time and didn't cause any problems so far. Now they never will.
Also, regarding my previous commit. Quests seem tu work just fine, even the second time the level is loaded, which is awesome.

Ergo: Questsystem/Notificationsystem segfault upon loading a level with Quests/Notifications in it twice is now officially resolved.

  • Property svn:eol-style set to native
File size: 8.3 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"
[1996]39#include "core/CoreIncludes.h"
[5693]40#include "core/GUIManager.h"
[5745]41#include "core/ConsoleCommand.h"
[5755]42#include "core/LuaState.h"
[5929]43#include "core/ScopedSingletonManager.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    {
[5745]76        for(std::map<PlayerInfo*, QuestGUI*>::iterator it = this->questGUIs_.begin(); it != this->questGUIs_.end(); it++)
77        {
[6417]78            it->second->destroy();
[5745]79        }
80        this->questGUIs_.clear();
[1996]81    }
82
83    /**
84    @brief
[5745]85        Retreive all Quests.
86    @return
87        Returns a map with all Quests indexed by their id's.
88    */
89    std::map<std::string, Quest*> & QuestManager::getQuests(void)
90    {
91        return this->questMap_;
92    }
93
94    /**
95    @brief
[2261]96        Registers a Quest with the QuestManager to make it globally accessable.
97        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]98    @param quest
[2261]99        The Quest that is to be registered.
[1996]100    @return
101        Returns true if successful, false if not.
102    */
[2911]103    bool QuestManager::registerQuest(Quest* quest)
[1996]104    {
[2261]105        if(quest == NULL) //!< Doh! Just as if there were actual quests behind NULL-pointers.
[2068]106        {
107            COUT(2) << "Registration of Quest in QuestManager failed, because inserted Quest-pointer was NULL." << std::endl;
108            return false;
[2093]109        }
[2092]110
[2261]111        std::pair<std::map<std::string, Quest*>::iterator,bool> result;
[2911]112        result = this->questMap_.insert( std::pair<std::string,Quest*>(quest->getId(),quest) ); //!< Inserting the Quest.
[2092]113
[2261]114        if(result.second) //!< If inserting was a success.
[2068]115        {
[6940]116            quest->setRegistered();
[6945]117            COUT(4) << "Quest with questId {" << quest->getId() << "} successfully inserted." << std::endl;
[2068]118            return true;
[2093]119        }
120        else
121        {
122           COUT(2) << "Quest with the same id was already present." << std::endl;
123           return false;
124        }
[1996]125    }
[2092]126
[1996]127    /**
128    @brief
[6940]129        Unregisters a Quest in the QuestManager.
130    */
131    bool QuestManager::unregisterQuest(Quest* quest)
132    {
133        return this->questMap_.erase(quest->getId()) == 1;
134    }
135
136    /**
137    @brief
[1996]138        Registers a QuestHint with the QuestManager to make it globally accessable.
[2261]139        Uses it's id to make sure to be able to be identify and retrieve it later.
[1996]140    @param hint
141        The QuestHint to be registered.
142    @return
143        Returns true if successful, false if not.
144    */
[2911]145    bool QuestManager::registerHint(QuestHint* hint)
[1996]146    {
[2261]147        if(hint == NULL) //!< Still not liking NULL-pointers.
[2068]148        {
149            COUT(2) << "Registration of QuestHint in QuestManager failed, because inserted QuestHint-pointer was NULL." << std::endl;
150            return false;
151        }
[2092]152
[2261]153        std::pair<std::map<std::string, QuestHint*>::iterator,bool> result;
[2911]154        result = this->hintMap_.insert ( std::pair<std::string,QuestHint*>(hint->getId(),hint) ); //!< Inserting the QuestHSint.
[2092]155
[2261]156        if(result.second) //!< If inserting was a success.
[2068]157        {
[6940]158            hint->setRegistered();
[6945]159            COUT(4) << "QuestHint with hintId {" << hint->getId() << "} successfully inserted." << std::endl;
[2068]160            return true;
[2093]161        }
162        else
163        {
164           COUT(2) << "QuestHint with the same id was already present." << std::endl;
165           return false;
166        }
[1996]167    }
[2092]168
[1996]169    /**
170    @brief
[6940]171        Unregisters a QuestHint in the QuestManager.
172    */
173    bool QuestManager::unregisterHint(QuestHint* hint)
174    {
175        return this->hintMap_.erase(hint->getId()) == 1;
176    }
177
178    /**
179    @brief
[2261]180        Finds a Quest with the given id.
[1996]181    @param questId
[2261]182        The id of the Quest sought for.
[1996]183    @return
[2261]184        Returns a pointer to the Quest with the input id.
185        Returns NULL if there is no Quest with the given questId.
[2068]186    @throws
187        Throws an exception if the given questId is invalid.
[1996]188    */
[2911]189    Quest* QuestManager::findQuest(const std::string & questId)
[1996]190    {
[6940]191        if(questId.compare(BLANKSTRING) == 1) //!< Check vor validity of the given id.
[2093]192        {
[2068]193            ThrowException(Argument, "Invalid questId.");
[2093]194        }
[2092]195
[1996]196        Quest* quest;
[2911]197        std::map<std::string, Quest*>::iterator it = this->questMap_.find(questId);
198        if (it != this->questMap_.end()) //!< If the Quest is registered.
[2093]199        {
200            quest = it->second;
201        }
202        else
203        {
204           quest = NULL;
205           COUT(2) << "The quest with id {" << questId << "} is nowhere to be found." << std::endl;
206        }
[2092]207
[2093]208        return quest;
[1996]209
210    }
[2092]211
[1996]212    /**
213    @brief
[2261]214        Finds a QuestHint with the given id.
[1996]215    @param hintId
[2261]216        The id of the QuestHint sought for.
[1996]217    @return
[2261]218        Returns a pointer to the QuestHint with the input id.
219        Returns NULL if there is no QuestHint with the given hintId.
[2068]220    @throws
221        Throws an exception if the given hintId is invalid.
[1996]222    */
[2911]223    QuestHint* QuestManager::findHint(const std::string & hintId)
[1996]224    {
[6940]225        if(hintId.compare(BLANKSTRING) == 1) //!< Check vor validity of the given id.
[2093]226        {
[2068]227            ThrowException(Argument, "Invalid hintId.");
[2093]228        }
[2092]229
[1996]230        QuestHint* hint;
[2911]231        std::map<std::string, QuestHint*>::iterator it = this->hintMap_.find(hintId);
232        if (it != this->hintMap_.end()) //!< If the QuestHint is registered.
[2093]233        {
234            hint = it->second;
235        }
236        else
237        {
238           hint = NULL;
239           COUT(2) << "The hint with id {" << hintId << "} is nowhere to be found." << std::endl;
240        }
[2092]241
[2093]242        return hint;
[1996]243
244    }
245
[2993]246    /**
247    @brief
[5745]248        Retreive the main window for the GUI.
249        This is for the use in the lua script tu start the QuestGUI.
250    @param guiName
251        The name of the GUI.
[2993]252    @return
[5745]253        Returns a CEGUI Window.
[2993]254    */
[5745]255    CEGUI::Window* QuestManager::getQuestGUI(const std::string & guiName)
[2963]256    {
[6536]257        PlayerInfo* player = this->retrievePlayer(guiName);
[5693]258
[5745]259        if(this->questGUIs_.find(player) == this->questGUIs_.end()) //!< Create a new GUI, if there is none, yet.
260            this->questGUIs_[player] = new QuestGUI(player);
[5693]261
[5745]262        return this->questGUIs_[player]->getGUI();
[2963]263    }
264
[2993]265    /**
266    @brief
[5745]267        Retrieve the player for a certain GUI.
268    @param guiName
269        The name of the GUI the player is retrieved for.
[2993]270    @return
[5745]271        Returns the player.
272    @todo
273        This very well might be outdated. So: Check if still needed, and update if necessary.
[2993]274    */
[6536]275    PlayerInfo* QuestManager::retrievePlayer(const std::string & guiName)
[2963]276    {
[5745]277        PlayerInfo* player = GUIManager::getInstance().getPlayer(guiName);
278        if(player == NULL)
[2963]279        {
[5745]280            COUT(1) << "Error: GUIOverlay with name '" << guiName << "' has no player." << std::endl;
[2993]281            return NULL;
282        }
[5693]283
[5745]284        return player;
[2963]285    }
286
[1996]287}
Note: See TracBrowser for help on using the repository browser.